0% found this document useful (0 votes)
17 views71 pages

18-Minimum Cost Spanning Tree - Kruskal's Algorithm

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views71 pages

18-Minimum Cost Spanning Tree - Kruskal's Algorithm

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 71

Shortest-path

algorithms -
Minimum cost Spanning Tree (MST) Algorithms,
Dijkstra’s Algorithm

Mrs. K Jyothsna Devi


SCOPE
Graphs
• A graph consists of set of vertices V = {v1, v2, ….. vn} and set of edges that connect the vertices E ={e1, e2, …. em}.
Undirected Graph:
• Edges in the graph do not point any particular direction i.e., can traverse bidirectional.
Directed Graph:
• All the Edges in the graph point to a particular direction i.e., can traverse only Unidirectional.
Connected Graph
• Path exists from one vertex to any other vertex
Disconnected Graph
• One of the vertex does not have path to any other vertex in the graph

edge

Directed graph Undirected graph Connected graph Disconnected graph


Spanning tree

Tree is constructed with vertices V and minimum number of edges 𝐸 ′ , i.e., 𝐺 ′ (V, El).
Graph G(V, E) – Have cycle.

Like graph structure, Tree contains vertices 𝑉 = 𝑉 and edges 𝐸 ′ 𝑉 = −


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

𝑆𝑝𝑎𝑛𝑛𝑖𝑛𝑔 𝑆𝑝𝑎𝑛𝑛𝑖𝑛𝑔 𝑆𝑝𝑎𝑛𝑛𝑖𝑛𝑔


𝑇𝑟𝑒𝑒 3 𝑇𝑟𝑒𝑒 4 𝑇𝑟𝑒𝑒 5
1 Cost = 12 4 5 1 3 = 13 4 Cost = 14 4
Cost 1
5 5
3
1 5 1 5
4 4 5
4
2 2
3 2 2
3 2 3

A minimum spanning tree is a least-cost (minimum) subset of the edges of a graph


that connects all the nodes
Complete Graph – Possible Spanning trees
• If the graph G(V,E) is complete graph i.e., each pair of vertices is connected with unique edge.
• Each node is connected to every node in the graph, and that are making cycle.
• In this case, Number of possible Spanning Trees = 𝑉𝑉−2

• Example

• No of possible Spanning Tress = 𝑉 𝑉−2 = 𝟒𝟒−𝟐 = 𝟏𝟔


• Let consider Graph with 4 vertices. All vertices connected to all the vertices.

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

6 • The result is a least-cost (3+3+2+2+5=15) spanning tree


6 – New edge should not form a cycle, even if edge with minimum
5 cost.
1 4 5 – To break the cycle, you must remove the edge with the another
3 2 greatest cost edge.
– Graph may have many spanning tree and also many
3 3 3 2 3
minimum spanning tree i.e. same cost.
2 4
4
Growing a MST(Generic Algorithm)
GENERIC_MST(G,w)
1 A:={}
2 while A does not form a spanning tree do
3 find an edge (u,v) that is safe for
4 A
5 A:=A∪{(u,v)}
return A
• Set A is always a subset of some minimum spanning tree. This property is called the
invariant Property.
• An edge (u, v) is a safe edge for A, if adding the edge to A does not destroy the invariant.
• A safe edge is just the CORRECT edge to choose to add to T.
Application of MST: an example
• In the design of electronic circuitry, it is often necessary to make a set of pins electrically equivalent
by wiring them together.
• To interconnect n pins, we can use n-1 wires, each connecting two pins.

• We want to minimize the total length of the wires.

• Minimum Spanning Trees can be used to model this problem.


Find the minimum cost spanning tree for disconnected graph
• Build a spanning tree from Disconnected graph that contains 9 Vertices and 11 Edges.

8
b c d
4 9
2
a 11 i 14 e
7 6
8 10
h g f
1

• Not possible to build a minimum spanning tree from Disconnected


graph
Find the minimum cost spanning tree for connected graph

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

• A spanning tree is the subgraph of an undirected connected graph.


• In Kruskal's algorithm,

– The set A is a forest.

– 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 set A forms a single tree.

– 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.

1. Create a Forest with all vertices in the graph.

2. Every vertex assumed as a separate tree.

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.

6. If Repeat step 2 until all vertices have been connected


• Kruskal’s algorithm is a greedy algorithm, because at each step it adds to the forest an edge of least
possible weight.
Kruskal's algorithm – Build minimum cost spanning tree Example
• The edges are considered by the algorithm in sorted order by
weight.

8 7 Set of Edges with weights =


c d {a,b} -4
b 9
4 {a,h} -8
{b,c} -8
2
a 11 i 4 14 e {b,h} - 11
{i,c} -2
7 6
8 10 {i,g} -6
h g f {i,h} -7
1 {c,d} - 7,
{c,f} -4
{d,f} - 14,
2 {d,e} - 9,
{e,f} -10
No. of disjoint Sets (V) = {a}, {b}, {c}, {d}, {e}, {f}, {g}, {h}, {g,f} -2
{i} {h,g} -1
= 9 separate trees in the forest
Kruskal's algorithm – Build minimum cost spanning tree
Example
Step 1. Sort the edges in increasing • The edge weight under consideration at each step
order is
Sorted Set of Edges =
shown with a red color value.
{h,g} - 1
{i,c} -2
{g,f} -2 Step 2. Select the shortest edge
{a,b} - 4 {h, g} - 1
{c,f} -4
{i,g} -6 8 7
{i,h} -7 b c d 9
4
{c,d} - 7,
2
{a,h} - 8 a 11 i 14 e
4
{b,c} - 8 7 6
8 10
{d,e} - 9,
h g f
{e,f} -10 1 2
{b,h} - 11
{d,f} - 14
Kruskal's algorithm – Build minimum cost spanning tree Example
8
b 7 c 9
4 d
{i, c} - 2
2 a 11 i 14 e
4
7 6
8 10
h g f
1 2

{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

List the edges in


order of size:
B 5
C
ED 2
3 AB 3
4
8 6 AE 4
CD 4
8 BC 5
A F D
7 EF 5
CF 6
5 AF 7
4 2 BF 8
CF 8
E
Kruskal's algorithm – Build minimum cost spanning tree – Example 2

Select the shortest


B 5 edge in the network
C
3 ED 2
4
8 6
8
7 F D
A
5
4 2

E
Kruskal's algorithm – Build minimum cost spanning tree – Example 2

Select the next


shortest edge
B 5 which does not
C
create a cycle
3 • ED 2
4
8 6 • AB 3

8
A F D
7
5
4 2

E
Kruskal's algorithm – Build minimum cost spanning tree – Example 2

Select the next shortest


edge which does not create
B 5 a cycle
C
• ED 2
3 • AB 3
4
8 6 • CD 4 (or AE
4)
8
A F D
7
5
4 2

E
Kruskal's algorithm – Build minimum cost spanning tree – Example 2

Select the next


shortest edge
B 5 which does not
C
create a cycle
3 • ED 2
4
8 6 • AB 3
• CD 4
8
• AE 4
A F D
7
5
4 2

E
Kruskal's algorithm – Build minimum cost spanning tree – Example 2

Select the next shortest edge


which does not create a
B 5 cycle
• ED 2
C
• AB 3
3 • CD 4
4
8 6 • AE 4
• BC 5 – forms a cycle
8 • EF 5
A F D
7
5
4 2

E
Kruskal's algorithm – Build minimum cost spanning tree – Example 2

All vertices have


been
B 5 connected.
C
The solution
3 is
4
8 6 • ED 2
• AB 3
8
• CD 4
A F D
7 • AE 4
• EF 5
5
4 2
• Total weight of tree:
E 18
Prim’s algorithm - Build minimum cost spanning tree
Property:
Edge in the set A always form a Single Tree

1. Select any vertex randomly.

2. Select the safe (min weight) edge that is connected to that vertex,
add it to the MST.

3. Select the next safe (min weight)edge connected to any


vertex already connected.

4. Add it to the minimum cost spanning tree

5. Repeat step 3 until all vertices have been connected


Prim's algorithm
PRIM(G,w,r)
A={}
S:={u} //u is an arbitrary vertex in V
Q=V[G] // priority queue using binary min-
heap
while Q is not empty do { - |V| times
extract (min w) edge (u, v), u S and v  Q , v S - O(V log
V)
for each v
parent (v) = u
add (u, v) to A,

}}
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.

• Grow the minimum spanning tree from the root vertex u.

• key[v] is the minimum weight of any edge connecting v to a vertex u in the


tree.
• Algorithm terminates when Q is empty, the minimum spanning tree A for G

is A={(v, parent[v]) : v ∈V - {u}}

• Running time complexity - O(E logV) with binary min heap

– E edges and V vertices


Prim's algorithm – Build a Minimum cost spanning tree

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

The Optimal Solution (shortest path) =


4+8+1+2+2+4+7+9 = 37
Prim's algorithm
class MST {
private static final int V = 5; // Number of vertices // construct and print MST represented using adjacency matrix
// find the vertex with minimum key value, from the set of void primMST(int graph[][]) {
//vertices not yet added in MST int parent[] = new int[V]; // store constructed MST
int minKey(int key[], Boolean mstSet[]) { // Key values used to pick min weight edge
// Initialize min value int key[] = new int[V];
int min = Integer.MAX_VALUE, min_index = -1; // set of vertices included in MST
for (int v = 0; v < V; v++) Boolean mstSet[] = new Boolean[V];
if (mstSet[v] == false && key[v] < min) { // Initialize all keys as INFINITE
min = key[v]; for (int i = 0; i < V; i++) {
min_index = v; key[i] = Integer.MAX_VALUE;
} mstSet[i] = false;
return min_index; }
} // Always include first 1st vertex in
// print the constructed MST stored in parent[] MST.
void printMST(int parent[], int graph[][]) { key[0] = 0; // Make key 0 i.e., this vertex
is picked as first vertex
System.out.println("Edge \tWeight");
parent[0] = -1; // First node is
for (int i = 1; i < V; i++)
always root of MST
System.out.println(parent[i] + " - " + i + "\t"+
// The MST will have V vertices
graph[i][parent[i]]); for (int count = 0; count < V - 1;
} count++) {
// Pick the min key vertex from the V not
Prim's algorithm
// Update key value and parent index of the adjacent public static void main(String[] args) {
//vertices of the picked vertex. MST t = new MST();
// Consider only those vertices which are not yet included in MST int graph[][] = new int[][] { { 0, 2, 0, 6, 0 },
for (int v = 0; v < V; v++) { 2, 0, 3, 8, 5 },
// graph[u][v] is non zero only for adjacent vertices of m { 0, 3, 0, 0, 7 },
// mstSet[v] is false for vertices not yet included in MST { 6, 8, 0, 0, 9 },
// Update the key only if graph[u][v] is smaller { 0, 5, 7, 9, 0 } };
// than key[v] // Print the shortest path using MST
if (graph[u][v] != 0 && mstSet[v] == false && t.primMST(graph);
graph[u][v] < key[v]) { }
parent[v] = u; }
key[v] = graph[u][v];
}
}
// print the constructed MST
printMST(parent, graph);
}
Prim'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
Prim'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
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

All vertices have


been
B 5 connected.
C
The solution
3 is
4
8 6 AB 3
AE 4
8
A ED 2
7 F D
DC 4
EF 5
5
4 2
Total weight of tree:
E 18
Industry
Applications
• Cable Network
• Computer Network Cable
• Electrical Cable (Grids, Buildings, Factories etc)
• Telephone
• Television Cables
• Pipelines (Mechanical)
• Geometry
• Generation of mazes
• Cluster Analysis
• Handwriting recognition
• Image segmentation
• Study of Molecular bonds in Chemistry
Single-Source Shortest Path Problem
A
• Finding shortest paths from a source vertex v to all other vertices in 4 1

the weighted graph. C 2


D
• The cost of path is the total of the weights (cost) of its constituent 5 8
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}.

while List is not empty:


u := remove vertex with minimum distance from
List . mark u as known.
for each unknown neighbor v of u:
dist := u's distance + edge (u, v)'s weight.

if dist is smaller than v's distance:


v's distance := dist.
v's previous := u
Dijkstra Example: Initialization

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

No. of outgoing edges = 2 i.e., B, and D. 0 2


Distance(B) from A = 2 A 2
B
1
Distance(D) from A = 4 1 3
0

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
∞ ∞

Pick vertex in List with minimum distance, i.e., D

59
Update the neighbors list for ‘D’

No. of outgoing edges from D = 4 i.e., C, E, F, and G. 0 2


2
Calculate the distance A B
• Distance(C) = 1 + 2 = 3 4 1 3 10

• 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

• Pick vertex in List with minimum distance (B) and update


neighbors
• No. of neighbors to B = 2 i.e., D, and E 0 2
2
• Distance(D) is not updated since D is already known. A B
• Distance(E) = 2 + 10 = 12 4 1 3 10

• Distance(E) is not updated since it is larger than 2 2


previously computed, i.e., 12 > 3. 3 C D E 3
5 8 1 4 6

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

• Distance(F) is updated since it is smaller 2 2

than previously computed, i.e., 6 < 8. 3 C D E 3


5 8 1 4 6

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|

=> O(|V| * |V|)


= O(|V|2)
= O(n2)
Dijkstra's algorithm
// Implement Using Priority Queue // Distance to the source is 0
public class GFG { dist[src] = 0;
private int while (settled.size() != V) {
dist[]; // Terminating condition check when the priority queue is empty, return

private Set<Integer> settled; private if (pq.isEmpty())


PriorityQueue<Node> pq; private return;
// Removing the minimum distance node from the priority queue
int V; // Number of vertices
int u = pq.remove().node;
List<List<Node> > adj;
// Adding the node whose distance is finalized
public GFG(int V) {
if (settled.contains(u))
this.V = V;
// Continue keyword skips execution for following check
dist = new int[V];
continue;
settled = new HashSet<Integer>();
// Don’t call e_Neighbors(u) if u is already present in the settled set.
pq = new PriorityQueue<Node>(V,
settled.add(u);
new Node());
e_Neighbours(u);
}
}
public void
}
dijkstra(List<List<Node> > adj, int
src) {
this.adj = adj;
for (int i = 0; i < V; i++)
Dijkstra's algorithm
// Method 2 process all the neighbours of the passed node public static void main(String arg[]) {
private void e_Neighbours(int u) { int V = 5;
int edgeDistance = -1; int source = 0;
int newDistance = -1; // Adjacency list of the connected edges by declaring
// All the neighbors of v List
for (int i = 0; i < adj.get(u).size(); i++) { class object Declaring object of type List<Node>
Node v = adj.get(u).get(i); List<List<Node>
for (int i = 0; i < V;> i++)
adj= {new //ArrayList<List<Node>
Initialize list for every node
>(); List<Node> item = new
// If current node hasn't already been processed
if (!settled.contains(v.node)) { ArrayList<Node>();
edgeDistance = v.cost; adj.add(item);
newDistance = dist[u] + edgeDistance; }
// If new distance is cheaper in cost adj.get(0).add(new Node(1, 9));
if (newDistance < dist[v.node]) adj.get(0).add(new Node(2, 6));
dist[v.node] = newDistance; adj.get(0).add(new Node(3, 5));
// Add the current node to the queue adj.get(0).add(new Node(4, 3));
pq.add(new Node(v.node, dist[v.node])); adj.get(2).add(new Node(1, 2));
} adj.get(2).add(new Node(3, 4));
} // Calculating the single source shortest
} path GFG dpq = new GFG(V);
Dijkstra's algorithm
// Print the shortest path to all the nodes from the source @Override public int compare(Node node1,
System.out.println("The shorted path from Node
node :"); for (int i = 0; i < dpq.dist.length; i++) node2) {
System.out.println(source + " to " + i + " is "+ if (node1.cost <
pq.dist[i]); node2.cost) return -1;
} if (node1.cost >
} node2.cost)
// class implements Comparator interface return 1;
class Node implements Comparator<Node> { return 0;
public int node; }
public int cost; }
public Node() {}
public Node(int node, int cost)
{ this.node = node;
this.cost = cost;
}
Disadvantages

• It will not suitable for negative weight edges in the


graph
0
3
A B

5 -10
C 2 D

• It is solved by Bellman Ford single source shortest path


algorithm
•References

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

You might also like