#9 Greedy Algorithms Tech Move
#9 Greedy Algorithms Tech Move
The greedy method is one of the strategies like Divide and conquer used
F or example, suppose we want to find the longest path in the graph below
e
M
ov
h
Our problem is to find the largest path. And, the optimal solution at the
c
Finally the weight of an only child of 3 is 1. This gives us our final result
e
20 + 3 + 1 = 24.
T
However, it is not the optimal solution. There is another path that carries
more weight (20 + 2 + 10 = 32).
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.
Prim’s Algorithm
P rim’s Algorithm is a famous greedy algorithm.
It is used for finding the Minimum Spanning Tree (MST) of a given graph.
To apply Prim’s algorithm, the given graph must be weighted, connected
and undirected.
e
The vertex connecting to the edge having least weight is usually
selected.
Step-02:
o
ind all the edges that connect the tree to new vertices.
v
F
Find the least weight edge among those edges and include it in the
existing tree.
If including that edge creates a cycle, then reject that edge and look for
the next least weight edge.
M
h
Step-03:
c
Keep repeating step-02 until all the vertices are included and Minimum
Spanning Tree (MST) is obtained.
C
e
PRACTICE PROBLEMS BASED ON PRIM’S ALGORITHM
T
onstruct the minimum spanning tree (MST) for the given graph using
Prim’s Algorithm-
S tep:2
S tep:3
S tep:4
S tep:5
S tep:6
e
S
ince all the vertices have been included in the MST, so we stop.
M
ov
h
=S
5 + 22 + 12 + 16 + 14
c
= 10 + 2
e
= 99 units
T
Time Complexities
Worst case time complexity of Prim’s Algorithm is-
O(
O(E + VlogV) using Fibonacci heap
If adjacency list is used to represent the graph, then using breadth first search,
= O( E + V) x O(logV)
= O(( E + V)logV)
= O( ElogV)
given graph-
Solution:
=1+4+2+ 6 + 3 + 10
=2 6 units
Kruskal’s Algorithm
K ruskal’s Algorithm is a famous greedy algorithm.
It is used for finding the Minimum Spanning Tree (MST) of a given graph.
To apply Kruskal’s Algorithm, the given graph must be weighted,
connected and undirected.
e
ov
S ort all the edges from low weight to high weight.
S tep-02:
Take the edge with the lowest weight and use it to connect the vertices
M
of graph.
h
If adding an edge creates a cycle, then reject that edge and go for the
next least weight edge.
c
S tep-03:
e
Keep adding edges until all the vertices are connected and a Minimum
T
Spanning Tree (MST) is obtained.
Kruskal’s Algorithm-
Step:2
Step:3
Step:4
e
M
ov
Step:5
ch
Te
Step:6
Step:7
S ince all the vertices have been connected / included in the MST, so we stop.
= 10 + 2 5 + 22 + 12 + 16 + 14
= 99 units
Time Complexities
Worst case time complexity of Kruskal’s Algorithm
= O(ElogV) or O(ElogE)
Dijkstra’s Algorithm
Dijkstra's algorithm allows us to find the shortest path between any two
vertices of a graph.
Dijkstra algorithm is a single-source shortest path algorithm.
Here, single-source means that only one source is given, and we have to
find the shortest path from the source to all the nodes.
e
ov
Dijkstra's Algorithm works on the basis that any subpath B -> D of the
shortest path A -> D between vertices A and D is also the shortest path
between vertices B and D.
M
ch
Te
PRACTICE PROBLEMS BASED ON DIJKSTRA’S ALGORITHM
It is easier to start with an example and then think about the algorithm.
C hoose a starting vertex and assign infinity path values to all other devices
If the path length of the adjacent vertex is lesser than new path length, don't
update it
e
After each iteration, we pick the unvisited vertex with the least path length.
So we choose 5 before 7
M
ov
c h
Te
Notice how the rightmost vertex has its path length updated twice
Time Complexities
Time Complexity: O(E Log V)
9
F E
2 6
14 11
C
D
9
10
A 15
7
o r e
S u c D estination
A B C D E F
0 ∞ ∞ ∞ ∞ ∞
A->B 7 9 ∞ ∞ 14
e
A,B,C 7 9 22 ∞ 14
A,B,C,F 7 9 20 ∞ 11
ov
A,B,C,F,D 7 9 20 20 11
A,B,C,F,D,E 7 9 20 20 11
M
h
A,B,C,F,D,E 7 9 20 20 11
c
F nali solution:
Te
9
F E
11
C
D
9
A
7
B A B C
-8
10
0 ∞ ∞
A C AC 10 5
5 ACB 10 5
10
10
B B
10 -8 10 -8
A C A A C
5
5 5
2
Every edge relax time: (V-1)+1
e
M
ov
ch
Te
Time Complexities
Best Case ComplexityO(E)
Space Complexities
S pace Complexity: O(V)
ch
e
To find the shortest path of the above graph, the first step is note down all
T
the edges which are given below:
(A, B), (A, C), (A, D), (B, E), (C, E), (D, C), (D, F), (E, F), (C, B)
The source vertex as 'A'; therefore, the distance value at vertex A is 0 and
the distance value at all the other vertices as infinity.
S ince the graph has six vertices so it will have five iterations.
First iteration
Consider the edge (A, B). Denote vertex 'A' as 'u' and vertex 'B' as 'v'. Now
use the relaxing formula:
d(u) = 0
d(v) = ∞
c(u , v) = 6
d(v) = 0 + 6 = 6
Consider the edge (A, C). Denote vertex 'A' as 'u' and vertex 'C' as 'v'. Now
use the relaxing formula:
d(u) = 0
d(v) = ∞
c(u , v) = 4
d(v) = 0 + 4 = 4
Second iteration
In the second iteration, we again check all the edges. The first edge is (A, B).
Since (0 + 6) is greater than 1 so there would be no updation in the vertex B.
The next edge is (A, C). Since (0 + 4) is greater than 3 so there would be no
updation in the vertex C.
The next edge is (B, E). Since (1 - 1) equals to 0 which is less than 5 so
update:
= 1 - 1 = 0
The next edge is (C, E). Since (3 + 3) equals to 6 which is greater than 5 so
there would be no updation in the vertex E.
e
The next edge is (E, F). Since (5 + 3) equals to 8 which is greater than 4 so
there would be no updation in the vertex F.
ov
The next edge is (C, B). Since (3 - 2) equals to 1` so there would be no
updation in the vertex B.
M
ch
Third iteration
Te
We will perform the same steps as we did in the previous iterations. We will
observe that there will be no updation in the distance of vertices.
A: 0
B: 1
C: 3
D: 5
E: 0
F: 3
The bellman ford algorithm does not produce a correct answer if the sum of
the edges of a cycle is negative.
Knapsack Problem
A knapsack (kind of shoulder bag) with limited weight capacity.
Few items each having some weight and value.
The problem states
The value or profit obtained by putting the items into the knapsack is
maximum
And the weight limit of the knapsack does not exceed.
Ex amples:
Item Weight Value
e
ov
1 18 2 5
2 15 24
C apacity 20Kg
3 10 1 5
M
Greedy About value:
Greedy About Weight:
h
25+2/15*24 = 28.2 15+10/15*24 = 31
c
Greedy About Both Weight and Value:
e
Item Weight Value V/W
T
1 18 2 5 1.3
2 15 24 1.6
3 10 1 5 1.5
Time Complexity
The main time taking step is the sorting of all items in decreasing order of
their value / weight ratio
If the items are already arranged in the required order, then while loop
takes O(n) time
The average time complexity of Quick Sort is O(nlogn)
Therefore, total time taken including the sort is O(nlogn).
KNAPSACK PROBLEM
For the given set of items and knapsack capacity = 60 kg, find the optimal
solution for the fractional knapsack problem making use of greedy approach.
1 5 3 0
2 10 40
3 1 5 4 5
4 22 77
5 2 5 90
OR
Find the optimal solution for the fractional knapsack problem making use of
greedy approach. Consider-
n = 5
w = 60 kg
(w1, w2, w3, w4, w5) = (5, 10, 15, 22, 25)
(b1, b2, b3, b4, b5) = (30, 40, 45, 77, 90)
Solution-
e
S tep-02: Sort all the items in decreasing order of their value / weight ratio-
I1 I2 I5 I4 I3
o
(6) (4) (3.6) (3.5) (3)
S tep-03: Start filling the knapsack by putting the items into it one by one.
v
M
c h
Now
K
Te
napsack weight left to be filled is 20 kg but item-4 has a weight of 22 kg
Since in fractional knapsack problem, even the fraction of any item can be
taken
So, knapsack will contain the following items-
= 160 + (20/22) x 77
= 160 + 70
= 230 units