Prims and Kruskals Algorithm Minimal Spanning Tree Harikrishnan

  • Slides: 41
Download presentation
Prim’s and Kruskal’s Algorithm Minimal Spanning Tree Harikrishnan G R Department of CSE MESCE

Prim’s and Kruskal’s Algorithm Minimal Spanning Tree Harikrishnan G R Department of CSE MESCE Kuttipuram

Minimum Spanning Trees • A Minimum Spanning Tree (MST) is a subgraph of an

Minimum Spanning Trees • A Minimum Spanning Tree (MST) is a subgraph of an undirected graph such that the subgraph spans (includes) all nodes, is connected, is acyclic, and has minimum total edge weight Department of CSE MESCE Kuttipuram

Constructing Minimum Spanning Trees • Any traversal of a connected, undirected graph visits all

Constructing Minimum Spanning Trees • Any traversal of a connected, undirected graph visits all the vertices in that graph. The set of edges which are traversed during a traversal forms a spanning tree. • For example, Fig: (b) shows the spanning tree obtained from a breadth-first traversal starting at vertex b. • Similarly, Fig: (c) shows the spanning tree obtained from a depth-first traversal starting at vertex c. Department of CSE MESCE Kuttipuram • (a) Graph G • (b) Breadth-first spanning tree of G rooted at b • (c) Depth-first spanning tree of G rooted at c

What is a Minimum-Cost Spanning Tree • For an edge-weighted , connected, undirected graph,

What is a Minimum-Cost Spanning Tree • For an edge-weighted , connected, undirected graph, G, the total cost of G is the sum of the weights on all its edges. • A minimum-cost spanning tree for G is a minimum spanning tree of G that has the least total cost. • Example: The graph • Has 16 spanning trees. Some are: • The graph has two minimum-cost spanning trees, each with a cost of 6: Department of CSE MESCE Kuttipuram

Algorithm Characteristics • Both Prim’s and Kruskal’s Algorithms work with undirected graphs • Both

Algorithm Characteristics • Both Prim’s and Kruskal’s Algorithms work with undirected graphs • Both work with weighted and unweighted graphs but are more interesting when edges are weighted • Both are greedy algorithms that produce optimal solutions Department of CSE MESCE Kuttipuram

Prim’s Algorithm • Similar to Dijkstra’s Algorithm except that dv records edge weights, not

Prim’s Algorithm • Similar to Dijkstra’s Algorithm except that dv records edge weights, not path lengths • Prim’s algorithm finds a minimum cost spanning tree by selecting edges from the graph one-by-one as follows: • It starts with a tree, T, consisting of the starting vertex, x. • Then, it adds the shortest edge emanating from x that connects T to the rest of the graph. • It then moves to the added vertex and repeats the process. Department of CSE MESCE Kuttipuram

Department of CSE MESCE Kuttipuram

Department of CSE MESCE Kuttipuram

Walk-Through 2 F 10 A 3 7 4 H 18 B 9 C 3

Walk-Through 2 F 10 A 3 7 4 H 18 B 9 C 3 4 8 Initialize array 25 2 3 D 10 G 7 E K dv pv A F B F C F D F E F F F G F H F Department of CSE MESCE Kuttipuram

2 F 10 A 3 7 4 H 3 G 25 7 E dv

2 F 10 A 3 7 4 H 3 G 25 7 E dv pv T 0 A D 10 2 K B 18 B 9 C 3 4 8 Start with any node, say D C D E F G H Department of CSE MESCE Kuttipuram

Update distances of adjacent, unselected nodes 2 F 10 A 3 7 4 H

Update distances of adjacent, unselected nodes 2 F 10 A 3 7 4 H 3 G 25 7 E pv 3 D 0 E 25 D F 18 D G 2 D A D 10 2 dv B 18 B 9 K 3 4 8 C C D T H Department of CSE MESCE Kuttipuram

Select node with minimum distance 2 F 10 A 3 7 4 H 3

Select node with minimum distance 2 F 10 A 3 7 4 H 3 G 25 7 E pv 3 D 0 E 25 D F 18 D 2 D A D 10 2 dv B 18 B 9 K 3 4 8 C C D G T T H Department of CSE MESCE Kuttipuram

Update distances of adjacent, unselected nodes 2 F 10 A 3 7 4 H

Update distances of adjacent, unselected nodes 2 F 10 A 3 7 4 H 3 G 25 7 E pv 3 D 0 E 7 G F 18 D 2 D 3 G A D 10 2 dv B 18 B 9 K 3 4 8 C C D G T T H Department of CSE MESCE Kuttipuram

Select node with minimum distance 2 F 10 A 3 7 4 H 3

Select node with minimum distance 2 F 10 A 3 7 4 H 3 D 10 25 2 G 7 E dv pv C T 3 D D T 0 E 7 G F 18 D 2 D 3 G B 18 B 9 K A 3 4 8 C G T H Department of CSE MESCE Kuttipuram

Update distances of adjacent, unselected nodes 2 F 10 A 3 7 4 H

Update distances of adjacent, unselected nodes 2 F 10 A 3 7 4 H 25 2 3 D 10 G 7 E dv pv 4 C A B 18 B 9 K 3 4 8 C C T 3 D D T 0 E 7 G F 3 C 2 D 3 G G T H Department of CSE MESCE Kuttipuram

Select node with minimum distance 2 F 10 A 3 7 4 H 25

Select node with minimum distance 2 F 10 A 3 7 4 H 25 2 3 D 10 G 7 E dv pv 4 C A B 18 B 9 K 3 4 8 C C T 3 D D T 0 7 G E F T 3 C G T 2 D 3 G H Department of CSE MESCE Kuttipuram

Update distances of adjacent, unselected nodes 2 F 10 A 3 7 4 H

Update distances of adjacent, unselected nodes 2 F 10 A 3 7 4 H 18 B 9 D 10 25 2 3 K 3 4 8 C G 7 E dv pv A 10 F B 4 C C T 3 D D T 0 2 F E F T 3 C G T 2 D 3 G H Department of CSE MESCE Kuttipuram

Select node with minimum distance 2 F 10 A 3 7 4 H 18

Select node with minimum distance 2 F 10 A 3 7 4 H 18 B 9 D 10 25 2 3 K 3 4 8 C G 7 E dv pv A 10 F B 4 C C T 3 D D T 0 E T 2 F F T 3 C G T 2 D 3 G H Department of CSE MESCE Kuttipuram

Update distances of adjacent, unselected nodes 2 F 10 A 3 7 4 H

Update distances of adjacent, unselected nodes 2 F 10 A 3 7 4 H 18 B 9 D 10 25 2 3 K 3 4 8 C G 7 E dv pv A 10 F B 4 C C T 3 D D T 0 E T 2 F F T 3 C G T 2 D 3 G H Table entries unchanged Department of CSE MESCE Kuttipuram

Select node with minimum distance 2 F 10 A 3 7 4 H 18

Select node with minimum distance 2 F 10 A 3 7 4 H 18 B 9 D 10 25 2 3 K 3 4 8 C G 7 E dv pv A 10 F B 4 C C T 3 D D T 0 E T 2 F F T 3 C G T 2 D H T 3 G Department of CSE MESCE Kuttipuram

Update distances of adjacent, unselected nodes 2 F 10 A 3 7 4 H

Update distances of adjacent, unselected nodes 2 F 10 A 3 7 4 H 18 B 9 D 10 25 2 3 K 3 4 8 C G 7 E dv pv A 4 H B 4 C C T 3 D D T 0 E T 2 F F T 3 C G T 2 D H T 3 G Department of CSE MESCE Kuttipuram

Select node with minimum distance 2 F 10 A 3 7 4 H 3

Select node with minimum distance 2 F 10 A 3 7 4 H 3 D 10 25 2 G 7 E K dv pv T 4 H 4 C B 18 B 9 A 3 4 8 C C T 3 D D T 0 E T 2 F F T 3 C G T 2 D H T 3 G Department of CSE MESCE Kuttipuram

Update distances of adjacent, unselected nodes 2 F 10 A 3 7 4 H

Update distances of adjacent, unselected nodes 2 F 10 A 3 7 4 H 3 D 10 25 2 G 7 E K dv pv T 4 H 4 C B 18 B 9 A 3 4 8 C C T 3 D D T 0 E T 2 F F T 3 C G T 2 D H T 3 G Table entries unchanged Department of CSE MESCE Kuttipuram

Select node with minimum distance 2 F 10 A 3 7 4 18 B

Select node with minimum distance 2 F 10 A 3 7 4 18 B 9 H 3 4 8 C 25 2 3 D 10 G 7 E K dv pv A T 4 H B T 4 C C T 3 D D T 0 E T 2 F F T 3 C G T 2 D H T 3 G Department of CSE MESCE Kuttipuram

Cost of Minimum Spanning Tree = dv = 21 2 3 F A C

Cost of Minimum Spanning Tree = dv = 21 2 3 F A C 3 4 4 B H D 2 3 G E K dv pv A T 4 H B T 4 C C T 3 D D T 0 E T 2 F F T 3 C G T 2 D H T 3 G Done Department of CSE MESCE Kuttipuram

Kruskal’s Algorithm • Kruskal’s algorithm also finds the minimum cost spanning tree of a

Kruskal’s Algorithm • Kruskal’s algorithm also finds the minimum cost spanning tree of a graph by adding edges one-by-one. • Work with edges, rather than nodes • Two steps: – Sort edges by increasing edge weight – Select the first |V| – 1 edges that do not generate a cycle Department of CSE MESCE Kuttipuram

Department of CSE MESCE Kuttipuram

Department of CSE MESCE Kuttipuram

Walk-Through F 10 A 4 3 6 B 4 H C 4 8 5

Walk-Through F 10 A 4 3 6 B 4 H C 4 8 5 Consider an undirected, weight graph 3 D 4 1 2 3 G 3 E Department of CSE MESCE Kuttipuram

F 10 A 4 3 6 B 4 H C 4 8 5 Sort

F 10 A 4 3 6 B 4 H C 4 8 5 Sort the edges by increasing edge weight 3 D 4 1 2 3 G 3 E edge dv (D, E) 1 (B, E) 4 (D, G) 2 (B, F) 4 (E, G) 3 (B, H) 4 (C, D) 3 (A, H) 5 (G, H) 3 (D, F) 6 (C, F) 3 (A, B) 8 (B, C) 4 (A, F) 10 Department of CSE MESCE Kuttipuram

Select first |V|– 1 edges which do not generate a cycle F 10 A

Select first |V|– 1 edges which do not generate a cycle F 10 A 3 4 6 B 4 H 3 4 8 5 C D 4 1 2 3 G 3 E edge dv (B, E) 4 2 (B, F) 4 (E, G) 3 (B, H) 4 (C, D) 3 (A, H) 5 (G, H) 3 (D, F) 6 (C, F) 3 (A, B) 8 (B, C) 4 (A, F) 10 edge dv (D, E) 1 (D, G) Department of CSE MESCE Kuttipuram

Select first |V|– 1 edges which do not generate a cycle F 10 A

Select first |V|– 1 edges which do not generate a cycle F 10 A 3 4 6 B 4 H 3 4 8 5 C D 4 1 2 3 G 3 E edge dv (B, E) 4 (B, F) 4 3 (B, H) 4 (C, D) 3 (A, H) 5 (G, H) 3 (D, F) 6 (C, F) 3 (A, B) 8 (B, C) 4 (A, F) 10 edge dv (D, E) 1 (D, G) 2 (E, G) Department of CSE MESCE Kuttipuram

Select first |V|– 1 edges which do not generate a cycle F 10 A

Select first |V|– 1 edges which do not generate a cycle F 10 A 3 4 6 B 4 H 3 4 8 5 C D 4 1 2 3 G 3 E edge dv (B, E) 4 2 (B, F) 4 (E, G) 3 (B, H) 4 (C, D) 3 (A, H) 5 (G, H) 3 (D, F) 6 (C, F) 3 (A, B) 8 (B, C) 4 (A, F) 10 edge dv (D, E) 1 (D, G) Accepting edge (E, G) would create a cycle Department of CSE MESCE Kuttipuram

Select first |V|– 1 edges which do not generate a cycle F 10 A

Select first |V|– 1 edges which do not generate a cycle F 10 A 3 4 6 B 4 H 3 4 8 5 C D 4 1 2 3 G 3 E edge dv (B, E) 4 2 (B, F) 4 (E, G) 3 (B, H) 4 (C, D) 3 (A, H) 5 (G, H) 3 (D, F) 6 (C, F) 3 (A, B) 8 (B, C) 4 (A, F) 10 edge dv (D, E) 1 (D, G) Department of CSE MESCE Kuttipuram

Select first |V|– 1 edges which do not generate a cycle F 10 A

Select first |V|– 1 edges which do not generate a cycle F 10 A 3 4 6 B 4 H 3 4 8 5 C D 4 1 2 3 G 3 E edge dv (B, E) 4 2 (B, F) 4 (E, G) 3 (B, H) 4 (C, D) 3 (A, H) 5 (G, H) 3 (D, F) 6 (C, F) 3 (A, B) 8 (B, C) 4 (A, F) 10 edge dv (D, E) 1 (D, G) Department of CSE MESCE Kuttipuram

Select first |V|– 1 edges which do not generate a cycle F 10 A

Select first |V|– 1 edges which do not generate a cycle F 10 A 3 4 6 B 4 H 3 4 8 5 C D 4 1 2 3 G 3 E edge dv (B, E) 4 2 (B, F) 4 (E, G) 3 (B, H) 4 (C, D) 3 (A, H) 5 (G, H) 3 (D, F) 6 (C, F) 3 (A, B) 8 (B, C) 4 (A, F) 10 edge dv (D, E) 1 (D, G) Department of CSE MESCE Kuttipuram

Select first |V|– 1 edges which do not generate a cycle F 10 A

Select first |V|– 1 edges which do not generate a cycle F 10 A 3 4 6 B 4 H 3 4 8 5 C D 4 1 2 3 G 3 E edge dv (B, E) 4 2 (B, F) 4 (E, G) 3 (B, H) 4 (C, D) 3 (A, H) 5 (G, H) 3 (D, F) 6 (C, F) 3 (A, B) 8 (B, C) 4 (A, F) 10 edge dv (D, E) 1 (D, G) Department of CSE MESCE Kuttipuram

Select first |V|– 1 edges which do not generate a cycle F 10 A

Select first |V|– 1 edges which do not generate a cycle F 10 A 3 4 6 B 4 H 3 4 8 5 C D 4 1 2 3 G 3 E edge dv (B, E) 4 2 (B, F) 4 (E, G) 3 (B, H) 4 (C, D) 3 (A, H) 5 (G, H) 3 (D, F) 6 (C, F) 3 (A, B) 8 (B, C) 4 (A, F) 10 edge dv (D, E) 1 (D, G) Department of CSE MESCE Kuttipuram

Select first |V|– 1 edges which do not generate a cycle F 10 A

Select first |V|– 1 edges which do not generate a cycle F 10 A 3 4 6 B 4 H 3 4 8 5 C D 4 1 2 3 G 3 E edge dv (B, E) 4 2 (B, F) 4 (E, G) 3 (B, H) 4 (C, D) 3 (A, H) 5 (G, H) 3 (D, F) 6 (C, F) 3 (A, B) 8 (B, C) 4 (A, F) 10 edge dv (D, E) 1 (D, G) Department of CSE MESCE Kuttipuram

Select first |V|– 1 edges which do not generate a cycle F 10 A

Select first |V|– 1 edges which do not generate a cycle F 10 A 3 4 6 B 4 H 3 4 8 5 C D 4 1 2 3 G 3 E edge dv (B, E) 4 2 (B, F) 4 (E, G) 3 (B, H) 4 (C, D) 3 (A, H) 5 (G, H) 3 (D, F) 6 (C, F) 3 (A, B) 8 (B, C) 4 (A, F) 10 edge dv (D, E) 1 (D, G) Department of CSE MESCE Kuttipuram

Select first |V|– 1 edges which do not generate a cycle F 10 A

Select first |V|– 1 edges which do not generate a cycle F 10 A 3 4 6 B 4 H 3 4 8 5 C D 4 1 2 3 G 3 E edge dv (B, E) 4 2 (B, F) 4 (E, G) 3 (B, H) 4 (C, D) 3 (A, H) 5 (G, H) 3 (D, F) 6 (C, F) 3 (A, B) 8 (B, C) 4 (A, F) 10 edge dv (D, E) 1 (D, G) Department of CSE MESCE Kuttipuram

Select first |V|– 1 edges which do not generate a cycle 3 F A

Select first |V|– 1 edges which do not generate a cycle 3 F A C 3 4 5 B H D 1 2 3 G E edge dv (B, E) 4 2 (B, F) 4 (E, G) 3 (B, H) 4 (C, D) 3 (A, H) 5 (G, H) 3 (D, F) 6 (C, F) 3 (A, B) 8 (B, C) 4 (A, F) 10 edge dv (D, E) 1 (D, G) Done Total Cost = Department of CSE MESCE Kuttipuram dv = 21 } not considered

Kruskal’s algorithm 1. 2. 3. Prim’s algorithm Select the shortest edge in a network

Kruskal’s algorithm 1. 2. 3. Prim’s algorithm Select the shortest edge in a network 1. Select any vertex 2. Select the shortest edge connected to that vertex 3. Select the shortest edge connected to any vertex already connected 4. Repeat step 3 until all vertices have been connected Select the next shortest edge which does not create a cycle Repeat step 2 until all vertices have been connected Department of CSE MESCE Kuttipuram