Greedy Method
Greedy Method
Greedy Method
Unit II
Presented By – Amrapali Babar
What is Greedy Algorithm?
knapsack
Minimum - Cost Spanning Tree
- A minimum spanning tree (MST) is defined as a spanning
tree that has the minimum weight among all the possible
spanning trees.
- A spanning tree is defined as a tree-like sub graph of a
connected, undirected graph that includes all the vertices of the
graph.
Properties of Spanning Tree
● The number of vertices (V) in the graph and the spanning tree is
the same.
● There is a fixed number of edges in the spanning tree which is
equal to one less than the total number of vertices ( E = V-1 ).
● The spanning tree should not be disconnected, as in there
should only be a single source of component, not more than
that.
● The spanning tree should be acyclic, which means there would
not be any cycle in the tree.
● The total cost (or weight) of the spanning tree is defined as the
sum of the edge weights of all the edges of the spanning tree.
● There can be many possible spanning trees for a graph.
Spanning Tree
Given (connected) graph G(V,E),
a spanning tree T(V’,E’):
- Is a sub graph of G;
- Spans the graph (V’ = V)
- Forms a tree (no cycle);
So, E’ has |V| -1 edges
Algorithms to find Minimum Spanning Tree:
There are several algorithms to find the minimum spanning tree from a given
graph, some of them are listed below:
Step 2 - Add the edge DE with weight 2 to the MST as it is not creating
the cycle.
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 –
Time Complexity for Kruskal’s Algorithm
• 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.
2) Prim’s Minimum Spanning Tree Algorithm
This is also a greedy algorithm, the workflow is as follows :
● It starts by selecting an arbitrary vertex and then adding it to the MST.
● Then, it repeatedly checks for the minimum edge weight that connects
one vertex of MST to another vertex that is not yet in the MST.
● This process is continued until all the vertices are included in the MST.
Prim’s Algorithm :
• Step 1: Select a starting vertex
• Step 2: Repeat Steps 3 and 4 until there are fringe vertices
• Step 3: Select an edge 'e' connecting the tree vertex and fringe
vertex that has minimum weight
• Step 4: Add the selected edge and the vertex to the minimum
spanning tree T
• [END OF LOOP]
• Step 5: EXIT
Example of prim's algorithm :
Given a weighted graph,
Step 1 - First, we have to choose a vertex from the above graph. Let's
choose B.
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 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.
Continued..
Tape –
0 1 2 4 5 6 7 8 9 10
0 1 2 4 5 6 7 8 9 10
5 10 3
Time taken to retrieve a program is proportional to
sum of the lengths of the programs based on ordering = d( i )
L1 = 5, L2 = 10 ,L3 = 3
Ordering ( i ) =1 ,2 ,3 Time Complexity – O (n log n) MRT(Mean
Ordering d( i ) Retrieval Time)
1 2 3 5 5 + 10 5 + 10 + 3 38
1 3 2 5 5+3 5 + 3 + 10 31
2 1 3 10 10 + 5 10 + 5 + 3 43
2 3 1 10 10 + 3 10 + 3 + 5 41
3 1 2 3 3+5 3 + 5 + 10 29
3 2 1 3 3 + 10 3 + 10 + 5 34
For Optimal Storage on Tape
Single source shortest paths :
• Single source shortest path problem determines the shortest paths in
a graph from a specific source node to all other nodes.
• It is foundational in various applications, impacting efficiency in
navigation, telecommunications and network analysis.
• Used extensively in GPS navigation systems for optimal route finding.
• Critical in telecommunications for determining efficient data packet
routing in networks.
• Plays a role in urban planning, helping design transportation and
utility networks.
Shortest Path
• Input: t x
6
• Directed graph G = (V, E) 3 9
3
• Weight function w : E → R 4
s 2 1
0
2 7
• Weight of path p = v0, v1, . . . , vk 5 3
k
w( p ) w( vi 1 , vi ) 5
6
11
i 1 y z
∞ otherwise
// if the temporary distance is less than the given distance of the path to
the Node, updating the resultant distance with the minimum value
if temporary_distance < distance[N]
distance[N] := temporary_distance
previous[N] := Q
• Directed Graph
• Weighted Graph
• Visited Property
• Path Property
• Relaxation d[u] + c[u , v]<d[v]
• d[v]= d[u] + c[u , v]
2 4 ∞, 6
1 2 3
u v
Example -
• Given directed graph, starting vertex A
Thank You…