Greedy Algorithms
Greedy Algorithms
Greedy Algorithms
Greedy Method
The greedy method suggests that one can device an algorithm which works in stages considering one
input at a time.At each stage a decision is made regarding whether or not a particular input is an
optimal solution. That means an input is considered and if that input is feasible, will include it in
solution and from all feasible solutions we will get an optimal solution.
For a problem, there are many solutions in which some solution satisfies the condition
(constraint), such solutions are called feasible solution. A solution which is already feasible and give
best result then that solution is called as optimal solution.
Here S4 &S5 are feasible because to cover the distance by 12 hrs requires either train or flight. If we
need to cover the journey in minimum cost , problem demands the result should be minimum. This
problem is minimization problem.
General method:
Algorithm greedy(a,n)
{
for i=1to n do
{
x=select(a)
if feasible(x) then
solution=solution+x
}
}
Example Problem: Change making
A child buys candy values 52 cents . He pays $1 to the cashier. The cashier wishes to return change
48 cents using fewest number of coins. Assume that an unlimited supply of quarters(25 cents],
dimes[10 cents],nickels[5 cents], pennies[ 1 cent] is available.
Solution:
The cashier construct the change in stages. In each stage a coin is selected using greedy
criteria(select largest coin, fewest coins). To assure feasibility ( the change given exactly equals the
desired amount of the solution, the selected coin should not cause the total amount of change given
to exceed the final desired amount.
Stage 1: Choose 1 quarter, then 23 cents remaining.
Stage 2: Choose 1 dime which is feasible. Now 13 cents remaining
Stage 3: Again choose 1 more dime. Now 3 cents remaining.
Stage 4: Select 1 penny, balance 1 cent.
Stage 5: Again select 1 penny
Stage 6: Again select 1 more penny to get the requires change.
Maximum
Subject to
and
Solutions that satisfy 2&3 is feasible and the feasible solution that satisfy 1is optimal. This is a
maximization problem.
Consider n objects where n=3,and a knapsack with capacity m=20.
Item(i) profit (p) weight (w)
Case 1. Let’s choose item with largest profit. Ie, item 1 with profit 25 and weight 18. Now the
remaining knapsack capacity is 2.Next item with largest profit is item 2with profit 24 and weight 15.
Knapsack cannot hold full of it , so a fraction of it is added (2/15). Now item 3 cannot be added.
Case 2: Lets choose the item with smallest weight.
In the above 2 solutions the maximum earned is solution 2. So the optimal solution is solution 2
since it maximize the profit.
Algorithm:
for i=1 to n do
if (w[i]>m) break;
else
w[i]=1
m=m-w[i]
if(i<=n)
x[i]=m/w[i]
Jobs J1 J2 J3 J4 J5
Profit 20 15 10 5 1
Deadline 2 2 1 3 3
We want set of jobs that should be completed within deadline such that profit earned should be
maximum. This is a maximization problem.
To do this first sort the job according to their profit in decreasing order. Then find the maximum
deadline value and execute the jobs within deadline.
Algorithm:
4.Optimal BST
Another method below :
5.Minimum Cost Spanning Tree
A spanning tree is a subset of an undirected Graph that has all the vertices
connected by minimum number of edges.
If all the vertices are connected in a graph, then there exists at least one spanning
tree. In a graph, there may exist more than one spanning tree.
Properties
• A spanning tree does not have any cycle.
• Any vertex can be reached from any other vertex.
Minimum Spanning Tree
A Minimum Spanning Tree (MST) is a subset of edges of a connected weighted
undirected graph that connects all the vertices together with the minimum possible
total edge weight. To derive an MST, Prim’s algorithm or Kruskal’s algorithm can be
used.
Example:
Prim's Algorithm
It is a greedy algorithm. It starts with an empty spanning tree. The idea is to maintain
two sets of vertices:
At every step, it considers all the edges and picks the minimum weight edge. After
picking the edge, it moves the other endpoint of edge to set containing MST.
Algorithm
1. Step 1: Select a starting vertex
2. Step 2: Repeat Steps 3 and 4 until there are fringe vertices
3. Step 3: Select an edge 'e' connecting the tree vertex and fringe vertex that has
minimum weight
4. Step 4: Add the selected edge and the vertex to the minimum spanning tree T
5. [END OF LOOP]
6. Step 5: EXIT
Step 2 - Now, we have to choose and add the shortest edge from vertex B. There are
two edges from vertex B that are B to C with weight 10 and edge B to D with weight
4. Among the edges, the edge BD has the minimum weight. So, add it to the MST.
Step 3 - Now, again, choose the edge with the minimum weight among all the other
edges. In this case, the edges DE and CD are such edges. Add them to MST and explore
the adjacent of C, i.e., E and A. So, select the edge DE and add it to the MST.
Step 4 - Now, select the edge CD, and add it to the MST.
Step 5 - Now, choose the edge CA. Here, we cannot select the edge CE as it would
create a cycle to the graph. So, choose the edge CA and add it to the MST.
So, the graph produced in step 5 is the minimum spanning tree of the given graph.
The cost of the MST is given below -
Kruskals Algorithm:
Kruskal's Algorithm is used to find the minimum spanning tree for a connected weighted
graph. The main target of the algorithm is to find the subset of edges by using which we can
traverse every vertex of the graph.
Example of Kruskal's algorithm
Now, let's see the working of Kruskal's algorithm using an example. It will be easier to
understand Kruskal's algorithm using an example.
The weight of the edges of the above graph is given in the below table -
Edge AB AC AD AE BC CD DE
Weight 1 7 10 5 3 4 2
Now, sort the edges given above in the ascending order of their weights.
Edge AB DE BC CD AE AC AD
Weight 1 2 3 4 5 7 10
Step 3 - Add the edge BC with weight 3 to the MST, as it is not creating any cycle or
loop.
Step 4 - Now, pick the edge CD with weight 4 to the MST, as it is not forming the
cycle.
Step 5 - After that, pick the edge AE with weight 5. Including this edge will create the
cycle, so discard it.
Step 6 - Pick the edge AC with weight 7. Including this edge will create the cycle, so
discard it.
Step 7 - Pick the edge AD with weight 10. Including this edge will also create the
cycle, so discard it.
So, the final minimum spanning tree obtained from the given weighted graph by using
Kruskal's algorithm is -
The cost of the MST is = AB + DE + BC + CD = 1 + 2 + 3 + 4 = 10.
Algorithm
1. Step 1: Create a forest F in such a way that every vertex of the graph is a separ
ate tree.
2. Step 2: Create a set E that contains all the edges of the graph.
3. Step 3: Repeat Steps 4 and 5 while E is NOT EMPTY and F is not spanning
4. Step 4: Remove an edge from E with minimum weight
5. Step 5: IF the edge obtained in Step 4 connects two different trees, then add it
to the forest F
6. (for combining two trees into one tree).
7. ELSE
8. Discard the edge
9. Step 6: END
Complexity of Kruskal's algorithm
Now, let's see the time complexity of Kruskal's algorithm.
o Time Complexity
The time complexity of Kruskal's algorithm is O(E logE) or O(V logV), where E is the no.
of edges, and V is the no. of vertices.
6.Activity Selection Problem
7.0-1 Knapsack problem
8.Travelling Salesman Problem