18-Minimum Cost Spanning Tree - Kruskal's Algorithm
18-Minimum Cost Spanning Tree - Kruskal's Algorithm
algorithms -
Minimum cost Spanning Tree (MST) Algorithms,
Dijkstra’s Algorithm
edge
1
•
• It does not have a cycle.in the structure.
•
Let’s consider the graph G(V,E) with 5 vertices and 5 edges. Find the possible Spanning trees 𝐺 ′ (V, El).
This structure is Spanning tree.
•
• In cyclic graph i.e., V= E, then Number of Spanning Tree = No. of vertices V.
𝑆𝑝𝑎𝑛𝑛𝑖𝑛𝑔
𝑇𝑟𝑒𝑒 1
1 3 4
5 1 Cost3 = 10 4
𝑉 =5 and
1 5 5
4 1
𝐸 ′ = 𝑉 − 1=
4
2 2 2
3 2 3
4
𝑆𝑝𝑎𝑛𝑛𝑖𝑛𝑔
𝑇𝑟𝑒𝑒 2
1 3 4 Spanning tree
5
1 Cost = 11 4
3 5
𝑉 =5 &
1 5
4
𝐸 ′ = 𝑉 − 1=
2 2 1 5
3
2 2
4 3
• Example
1 4
2 3
Properties of Spanning tree
• A connected graph G can have more than one spanning tree.
• All possible spanning trees of graph G, have the same number of edges and vertices.
• The spanning tree does not have any cycle (loops).
• Removing one edge from the spanning tree will make the graph disconnected, i.e. the spanning tree
is minimally connected.
• Adding one edge to the spanning tree will create a circuit or loop, i.e. the spanning tree is maximally acyclic.
• In a complete graph, remove maximum e - V + 1 edges for a spanning tree.
𝑆𝑝𝑎𝑛𝑛𝑖𝑛𝑔
𝑇𝑟𝑒𝑒 1
1 3 4
5 1 Cost3 = 10 4
1 5 5
4 1
4
2 2 2
3 2 3
𝑉 =5 and 𝐸 ′ = 𝑉 − 1=
4
Minimum spanning tree (minimum-weight spanning tree)
• A minimum spanning tree is a least-cost subset of the edges of a graph that connects all the nodes
Algorithm:
– Start by picking any node and adding it to the tree
– Repeatedly: Pick any least-cost edge from a node in the graph to a node not in the tree
– Add the edge (if not form cycle) and new node to the tree
– Stop when all nodes have been added to the tree
8
b c d
4 9
2
a 11 i 14 e
7 6
8 10
h g f
1
8 7
b c d
4 9
2
8 7 a 11 i 4 14 e
b c d 7 6
4 9 10
8
2 h g f
a 11 i 14 e 1 2
4
7 6
8 10
h g f 8 7
1 2 b c d
9
4
• 2
Note that the tree is not unique: a 11 i 14 e
•
4
Replacing (b,c) with (a,h) yields another 7 6
spanning tree with the same minimum weight. 8 10
h g f
1 2
MST Algorithms - Kruskal and Prim
– The safe edge added to A is always a least-weight edge in the graph that connects two distinct
components.
• In Prim's algorithm,
– The safe edge added to A is always a least-weight edge connecting the tree to a vertex not in the
tree.
Kruskal's algorithm (G,w)
For each vertex v ∈
A:={} - O(1)
𝑉 𝐺
MAKE_SET(v)
Sort the edges in an increasing order by - O(E log E) //using min heap
w
while E is not empty { // Find-Set -
}
O(E) take an safe edge (u, v) that is shortest (least weight) in E and delete it
from E
if u and v are in different (tree) components then - O(V) O((V+E)
} add safe edge (u, v) to A
-
Total running time is O(E log V)
Disjoint-Set
• Keep a collection of sets S1, S2, .., Sk,
– Each Si is a set, e.g., S1={v1, v2, vn}.
• Three operations
– Make-Set(x) - Each set contains the vertices in a tree of the current forest.
– Union(x, y) –unites the sets that contain x and y, say, Sx and Sy, into a new set that is the
union of the two sets.
– Find-Set(x) - returns a pointer to the representative of the set containing x.
– Each operation takes O(log n) time.
• Running time complexity- O(E log V)
– E edges and V vertices
Kruskal’s algorithm
• A spanning tree is the subgraph of an undirected connected graph.
3. Sort all the edges E from low weight to high weight (i.e., edge cost).
4. Select the safe (min weight) edge and add in a forest i.e., spanning tree.
5. Select the next shortest edge (min weight) which does not create a cycle, and add it into the forest.
{h, g} -
1
8 7
b c d 9
4
2
{g, f} - a 11 i 14 e
4
2 7 6
8 10
h g f
1 2
Kruskal's algorithm – Build minimum cost spanning tree Example
8 7
b c
4 9
d
2 {a, b} - 4
a 11 i 4 14 e
7 6
8 10
h g f
1 2
8 7
b c d
4 9
2
a 11 i 14 e {c, f} - 4
4
7 6
8 10
h g f
1 2
Kruskal's algorithm – Build minimum cost spanning tree Example
8 7
b c
4 9 {i, g} - 6
d
2 Rejected since
a 11 i 4 14 e cycle forms
7 6
8 10
h g f
1 2
8 7
b c d
4 9
{c, d} - 7
2
a 11 i 4 14 e
7 6
8 10
h g f
1 2
Kruskal's algorithm – Build minimum cost spanning tree Example
8 7
b c d
4 9 {i,h} -7
2 Rejected since
a 11 i 4 14 e
cycle forms
7 6
8 10
h g f
1 2
8 7
b c d
4 9
2
a 11 i 4 14 e {a,h} - 8
7 6
8 10
h g f
1 2
Kruskal's algorithm – Build minimum cost spanning tree Example
8 7
b c
4 9
d
2 {b,c} - 8
a 11 i 4 14 e
7 6
8 10
h g f
1 2
8 7
b c d
4 9
2
a 11 i 4 14 e {d,e} - 9
7 6
8 10
h g f
1
2
The Optimal Solution (shortest path) = 4+8+1+2+2+4+7+9 =
Kruskal's algorithm
public class Graph { // find set of an element i (uses path compression technique)
class Edge implements Comparable<Edge> { int find(subset subsets[], int i) {
int src, dest, weight; // find root and make root as parent of i (path compression)
// sorting edges based on their weight if (subsets[i].parent != i)
public int compareTo(Edge compareEdge) subsets[i].parent = find(subsets, subsets[i].parent);
{ return subsets[i].parent;
return this.weight - }
compareEdge.weight; //union of two sets of x and y (uses union by rank)
} void Union(subset subsets[], int x, int y){
}; class subset { //subset for union-find int xroot = find(subsets, x);
int parent, rank; int yroot = find(subsets, y);
}; // Attach smaller rank tree under
int V, E; // V-> no. of vertices & E->no.of edges root of high rank tree (Union by
Rank)
Edge edge[]; // collection of all edges
if (subsets[xroot].rank <
// Creates a graph with V and E edges subsets[yroot].rank)
Graph(int v, int e) { subsets[xroot].parent =
V = v; yroot;
E = e; else if (subsets[xroot].rank > subsets[yroot].rank)
edge = new Edge[E]; subsets[yroot].parent = xroot;
for (int i = 0; i < e; ++i) // If ranks are same, then make one as root and
edge[i] = increment its rank by one
new else {
Edge(); subsets[yroot].parent = xroot;
} subsets[xroot].rank++;
Kruskal's algorithm
// construct MST i = 0; // Index used to pick next edge
void KruskalMST() { // Number of edges to be taken is equal to V-1
Edge result[] = new Edge[V]; // store the resultant MST while (e < V - 1) {
int e = 0; // index // Pick the smallest edge. And increment
int i = 0; // // the index for next iteration
index for (i = 0; i < V; + Edge next_edge = edge[i++];
+i) int x = find(subsets, next_edge.src); int
result[i] = new Edge(); y = find(subsets, next_edge.dest);
// Sort all the edges in non-decreasing using weight. // If this edge doesn't cause cycle,
Arrays.sort(edge); include it in result and
// Allocate memory for creating V subsets // increment the index of result for next
subset subsets[] = new subset[V]; edge
for (i = 0; i < V; ++i) if (x != y) {
subsets[i] = new subset(); result[e++] = next_edge;
// Create V subsets with single Union(subsets, x, y);
elements }
for (int v = 0; v < V; ++v) { // Else discard the next_edge
subsets[v].parent = v; }
subsets[v].rank = 0;
}
Kruskal's algorithm
System.out.println("Following are the edges in MST"); // display the MST
int minimumCost = 0;
for (i = 0; i < e; ++i) {
System.out.println(result[i].src + " -- "+ result[i].dest+ " == " + result[i].weight);
minimumCost += result[i].weight;
}
System.out.println("Minimum Cost Spanning Tree "+ minimumCost);
}
public static void main(String[] args) {
int V = 4;
int E = 5;
Graph graph = new Graph(V, E);
graph.edge[0].src = 0; // add edge 0-1
graph.edge[0].dest = 1;
graph.edge[0].weight = 10;
graph.edge[1].src = 0; // add edge 0-2
graph.edge[1].dest = 2;
graph.edge[1].weight = 6;
---
---
---
graph.KruskalMST();
}
Kruskal's algorithm – Build minimum cost spanning tree – Example 2
• A cable company wants to connect six cities to their network. What is the minimum length of cable
needed?
Six cities are Bhimavaram, Guntur, Gudivada, Vijayawada, Amaravati, Machilipatnam.
• Distance between Vijayawada, and Guntur = 6
• Distance between Vijayawada, and Gudivada = 7
• Distance between Vijayawada, and Amaravati = 8
• Distance between Vijayawada, and Machilipatnam = 5
• Distance between Amaravati, and Machilipatnam = 2
• Distance between Gudivada, and Machilipatnam = 4
• Distance between Bhimavaram, and Guntur = 5
• Distance between Bhimavaram, and Vijayawada = 8
• Distance between Bhimavaram, and Gudivada = 3
• Distance between Amaravati, and Guntur = 4
Kruskal's algorithm – Build minimum cost spanning tree – Example 2
• Initially sketch the given scenario as a network, then find the minimum connector for the
network
Bhimavara 5 Guntu
m r
3
6 4
8
8
Gudivad Vijayawad Amarava
a 7 a ti
5
4 2
Machilipatna
m
Kruskal's algorithm – Build minimum cost spanning tree – Example 2
E
Kruskal's algorithm – Build minimum cost spanning tree – Example 2
8
A F D
7
5
4 2
E
Kruskal's algorithm – Build minimum cost spanning tree – Example 2
E
Kruskal's algorithm – Build minimum cost spanning tree – Example 2
E
Kruskal's algorithm – Build minimum cost spanning tree – Example 2
E
Kruskal's algorithm – Build minimum cost spanning tree – Example 2
2. Select the safe (min weight) edge that is connected to that vertex,
add it to the MST.
}}
add v to S - O(E) - O(E log
V)
delete v from - O(log V)
} Q
So, O(V log V + E log V) = O(E log
V)
Prim's algorithm
• Q is a priority queue, holding all vertices that are not in the tree now.
8 7
the root b c d
4 9
vertex
2
a 11 i 4 14 e
7 6
8 10
h g f
1 2
8 7
b c d
4 9
2
a 11 i 4 14 e
7 6
8 10
h g f
1 2
Prim's algorithm – Build a Minimum cost spanning tree
8 7
b c
4 9
d
2
a 11 i 4 14 e
7 6
8 10
h g f
1 2
8 7
b c d
4 9
2
a 11 i 4 14 e
7 6
8 10
h g f
1 2
Prim's algorithm – Build a Minimum cost spanning tree
8 7
b c
4 9
d
2
a 11 i 4 14 e
7 6
8 10
h g f
1 2
8 7
b c d
4 9
2
a 11 i 4 14 e
7 6
8 10
h g f
1 2
Prim's algorithm – Build a Minimum cost spanning tree
8 7
b c d
4 9
2
a 11 i 4 14 e
7 6
8 10
h g f
1 2
8 7
b c d
4 9
2
a 11 i 4 14 e
7 6
8 10
h g f
1 2
Prim's algorithm – Build a Minimum cost spanning tree
8 7
b c d
4 9
2
a 11 i 4 14 e
7 6
8 10
h g f
1 2
• Initially sketch the given scenario as a network, then find the minimum connector for the
network
Bhimavara 5 Guntu
m r
3
6 4
8
8
Gudivad Vijayawad Amarava
a 7 a ti
5
4 2
Machilipatna
m
Prim's algorithm – Build minimum cost spanning tree – Example 2
Select any
vertex
B 5 A
C
3 Select the
4
8 6 shortest edge
connected to
8 that vertex
A F D AB
7
3
5
4 2
E
Prim's algorithm – Build minimum cost spanning tree – Example 2
Select the
shortest edge
B 5 connected to
C
any vertex
3 already
4
8 6 connected.
AE
4
8
A F D
7
5
4 2
E
Prim's algorithm – Build minimum cost spanning tree – Example 2
Select the
shortest edge
B 5 connected to
C
any vertex
3 already
4
8 6 connected.
ED
2
8
A F D
7
5
4 2
E
Prim's algorithm – Build minimum cost spanning tree – Example 2
Select the
shortest edge
•B •5 connected to
•C
any vertex
•3 already
•4
•8 •6 connected.
DC
4
•8
•A •F •D
•7
•5
•4 •2
•E
Prim's algorithm – Build minimum cost spanning tree – Example 2
Select the
shortest edge
•B •5 connected to
•C
any vertex
•3 already
•4
•8 •6 connected.
EF
5
•8
•A •F •D
•7
•5
•4 •2
•E
Prim's algorithm – Build minimum cost spanning tree – Example 2
edges.
•
F 1
G
BFS finds the shortest path on unweighted graphs i.e., each edge can
be considered to have unit weight.
• Dijkstra’s Algorithm finds the shortest path on weighted digraph with
non-negative weight (cost).
Applications
• Maps (Map Quest, Google Maps)
• Routing Systems
Dijkstra's algorithm A 2 B
(Single-Source shortest path problem in graph
theory) 4 1 3 10
• Let conbsider the Weighted graph G = {E,V} and
Source vertex u ∈ V, such that all edge weights are non-negative C 2 D 2 E
•
• Compute Lengths of shortest paths (or the shortest paths themselves) 5 8 ∞ 4 6
from a given source vertex u ∈ V to all other vertices. F 1 G
Working Principle
• Computes distance for each directly connected vertex v from the start vertex u, that is, the weight of a shortest
path between v and u.
• Keeps track of the set of vertices for which the distance has been computed, C
• Every vertex has a label distance D associated with it.
• For any vertex v, D[v] stores an approximation of the distance between u and v.
• The algorithm will update a D[v] value when it finds a shorter path from u to v.
• When a vertex v is added to the C, its label D[v] is equal to the actual (final) distance between the starting vertex
u and vertex v.
Dijkstra pseudocode
Dijkstra(V):
for each vertex v: // Initialization
v's distance := infinity.
v's previous := none.
u's distance := 0.
List := {all vertices}.
Distance (all
vertices except
source) = ∞
Distance 0 ∞
(source) = 0 A 2
B
4 1 3 10
2 2
∞ C D E ∞
5 8 ∞ 4 6
1
F G
∞ ∞
Pick vertex in List with minimum distance.
57
Update neighbors' distance
1 2 2
∞ C D E ∞
5 8 1 4 6
1
F G
∞ ∞
Select the vertex with minimum distance
0 2
2
A B
1 3 10
∞ 4
C 2
D 2
E ∞
5 8 1 4 6
1
F G
∞ ∞
59
Update the neighbors list for ‘D’
• Distance(E) = 1 + 2 = 3
2 2
• Distance(F) = 1 + 8 = 9 3 C D E 3
• Distance(G) = 1 + 4 =
5 8 1 4 6
5 1
F G
9 5
60
Select the vertex with minimum distance and Update neighbors
1
F G
9 Not updating
5
Select the vertex List with minimum distance and Update the neighbors
• Pick vertex List with minimum distance (E) and update
neighbors
0 2
No. of outgoing edges from E = 1 i.e., 2
A B
G. Calculate the distance
4 1 3 10
• Distance(G) = 3+ 6 = 9
2 2
• Distance(G) is not updated since it is larger than 3 C D E 3
previously computed, i.e., 9 > 5.
5 8 1 4 6
1
F G
9 5
Not updating
Select the vertex List with minimum distance and Update the neighbors
• Pick vertex List with minimum distance (C), and update the
neighbors
No. of outgoing edges from C = 1 i.e., F.
0 2
Calculate the distance 2
A B
• Distance(F) = 3+ 5 = 8
4 1 3 10
• Distance(G) is updated since it is smaller than previously
computed, i.e., 8 < 9. 2 2
3 C D E 3
5 8 1 4 6
1
F G
8 5
63
Select the vertex List with minimum distance and Update the neighbors
• Pick vertex List with minimum distance (G)
and update the neighbors.
No. of outgoing edges from G = 1 i.e., F.
0 2
Calculate the distance 2
A B
• Distance(F) = 5+ 1 = 8 4 1 3 10
1
F G
6
5
Select the vertex List with minimum distance and Update the neighbors
• Pick vertex not in S with lowest cost than current cost of (F). All vertices are
connected.
• So, stop the algorithms.
0 2
2
A B
4 1 3 10
2 2
3 C D E 3
5 8 1 4 6
1
F G
6 5
65
Time complexity
• Worst Case
Every vertex is connected with all other vertices., i.e. Complete Graph.
Then n = |V|
5 -10
C 2 D
1. Goodrich, Michael T & Roberto Tamassia, and “Data Structures & Algorithms in Java”, WILEY
publications 2015.
2. Sartaj Sahni, “Data Structures, Algorithms and Applications in Java”, Second Edition, Universities
Press.
3. Mark Allen Weiss, “Data Structures & Algorithm Analysis in Java”, Third Edition,
Pearson
Publishing.
4. https://www.javatpoint.com/what-are-connected-graphs-in-data-structure
5. www.geeksforgeeks.org