Daa Module 5
Daa Module 5
Daa Module 5
1. The divide-and-conquer paradigm involves three steps at each level of the recursion:
• Divide the problem into a number of sub problems.
• Conquer the sub problems by solving them recursively. If the sub problem sizes are small
enough, however, just solve the sub problems in a straightforward manner.
• Combine the solutions to the sub problems into the solution for the original problem.
2. They call themselves recursively one or more times to deal with closely related sub
problems.
3. D&C does more work on the sub-problems and hence has more time consumption.
Dynamic Programming
3. DP solves the sub problems only once and then stores it in the table.
Greedy Algorithm
A greedy algorithm is an algorithmic strategy that makes the best optimal choice at each
small stage with the goal of this eventually leading to a globally optimum solution. This
means that the algorithm picks the best solution at the moment without regard for
consequences. It picks the best immediate output, but does not consider the big picture, hence
it is considered greedy.
Components of Greedy Algorithm
Greedy algorithms have the following five components −
A candidate set − A solution is created from this set.
A selection function − Used to choose the best candidate to be added to the solution.
A feasibility function − Used to determine whether a candidate can be used to
contribute to the solution.
An objective function − Used to assign a value to a solution or a partial solution.
A solution function − Used to indicate whether a complete solution has been
reached.
General method:
-Given n inputs choose a subset that satisfies some constraints.
– A subset that satisfies the constraints is called a feasible solution.
– A feasible solution that maximises or minimises a given (objective) function is said to be
optimal.
Often it is easy to find a feasible solution but difficult to find the optimal solution.
The greedy method suggests that one can devise an algorithm that works in stage. At each
stage a decision is made whether a particular input is in the optimal solution. This is called
subset paradigm.
Control Abstraction for Greedy algorithm Algorithm
Greedy(A : set; n : integer){
MakeEmpty(solution);
f or(i = 2;i <= n;i + +){
x = Select(A);
if F easible(solution, x) then
solution = Union(solution, {x})
}
return solution
}
The function Greedy describes the essential way that a greedy algorithm will look, once a
particular problem is chosen and the functions
Select, F easible and Union are properly implemented. The function Select selects an input
from A whose value is assign to x. F easible is a Booleanvalued function that determines if x
can be included into the solution vector. The function Union combines x with the solution,
and update the objective function.
subject to constraint,
It is clear that an optimal solution must fill the knapsack exactly, otherwise we could add a
fraction of one of the remaining items and increase the overall profit.
Thus, an optimal solution can be obtained by
In this context, first we need to sort those items according to the value of pi/wi, so
that pi+1/wi+1 ≤ pi/wi . Here, x is an array to store the fraction of items.
Greedy-fractional-knapsack (w, v, W)
FOR i =1 to n
do x[i] =0
weight = 0
while weight < W
do i = best remaining item
IF weight + w[i] ≤ W
then x[i] = 1
weight = weight + w[i]
else
x[i] = (W - weight) / w[i]
weight = W
return x
Analysis
If we keep the items in heap with largest vi/wi at the root. Then
creating the heap takes O(n) time
while-loop now takes O(log n) time (since heap property must be restored after the
removal of root)
Example
ITEM WEIGHT VALUE
i1 6 6
i2 10 2
i3 3 1
i4 5 8
i5 1 3
i6 3 5
ITEM WEIGHT VALUE DENSITY
i1 6 6 1.000
i2 10 2 0.200
i3 3 1 0.333
i5 1 3 3.000
i6 3 5 1.667
i4 5 8 1.600
i1 6 6 1.000
i3 3 1 0.333
i2 10 2 0.200
i4 5 8 1.600
i5 1 3 3.000
i6 3 5 1.667
i5 1 3 1.000 3.000
i6 3 5 4.000 8.000
i4 5 8 9.000 16.000
i1 6 6 15.000 22.000
So, total weight in the knapsack = 16 and total value inside it = 22.333336
MINIMUM SPANNING TREE (MST)
In a weighted graph, a minimum spanning tree is a spanning tree that has minimum weight
than all other spanning trees of the same graph. In real-world situations, this weight can be
measured as distance, congestion, traffic load or any arbitrary value denoted to the edges.
Prim’s Algorithm
Kruskal's Algorithm
Prim's algorithm
Prim's algorithm is a greedy algorithm that finds a minimum spanning tree for a connected
weighted undirected graph.
It finds a subset of the edges that forms a tree that includes every vertex, where the total
weight of all the edges in the tree is minimized.
We start from one vertex and keep adding edges with the lowest weight until we reach our
goal.
Kruskal's Algorithm
Kruskal's algorithm to find the minimum cost spanning tree uses the greedy approach. This
algorithm treats the graph as a forest and every node it has as an individual tree. A tree
connects to another only and only if, it has the least cost among all available options and
does not violate MST properties.
1. Sort all the edges in non-decreasing order of their weight.
2. Pick the smallest edge. Check if it forms a cycle with the spanning tree formed so far.
If cycle is not formed, include this edge. Else, discard it.
3. Repeat step#2 until there are (V-1) edges in the spanning tree.