0% found this document useful (0 votes)
74 views23 pages

Minimum Spanning Tree

The document discusses algorithms for finding minimum spanning trees in graphs. It describes Prim's algorithm, which maintains a priority queue of vertices not yet included in the spanning tree, and adds edges connecting vertices based on minimum weight. It also describes Kruskal's algorithm, a greedy approach that sorts edges by weight and adds them to the spanning tree if they connect different components without creating cycles. Both algorithms run in O(E log V) time, where E is the number of edges and V is the number of vertices. Examples are provided to illustrate Prim's algorithm.

Uploaded by

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

Minimum Spanning Tree

The document discusses algorithms for finding minimum spanning trees in graphs. It describes Prim's algorithm, which maintains a priority queue of vertices not yet included in the spanning tree, and adds edges connecting vertices based on minimum weight. It also describes Kruskal's algorithm, a greedy approach that sorts edges by weight and adds them to the spanning tree if they connect different components without creating cycles. Both algorithms run in O(E log V) time, where E is the number of edges and V is the number of vertices. Examples are provided to illustrate Prim's algorithm.

Uploaded by

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

Data Structures

Lecture 28: Minimum Spanning Tree

Lovely Professional University, Punjab

Outlines

Minimum Spanning Tree Prims Algorithm Kruskals Algorithm

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Minimum Spanning Tree


Input: A connected, undirected graph G = (V, E) with weight function w : E R. For simplicity, we assume that all edge weights are distinct.

Output: A spanning tree T, a tree that connects all vertices, of minimum weight:

w(T )

(u ,v )T

w(u, v)

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Example of MST
6 5 14 8 7 10 12 9 15

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Prims Algorithm IDEA: Maintain V A as a priority queue Q. Key each vertex in Q with the weight of the leastweight edge connecting it to a vertex in A.
QV key[v] for all v V key[s] 0 for some arbitrary s V while Q do u EXTRACT-MIN(Q) for each v Adj[u] do if v Q and w(u, v) < key[v] then key[v] w(u, v) p[v] u

DECREASE-KEY

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Example of Prims algorithm


A VA 14 6

5 8

12 7 15 0 10 9

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Example of Prims algorithm


A VA 14 6

5 8

12 7 15 0 10 9

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Example of Prims algorithm


A VA 14 6

5 8

12 7 7 0 10 9 15 15

10

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Example of Prims algorithm


A VA 14 6

5 8

12 7 7 0 10 9 15 15

10

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Example of Prims algorithm


A VA 5 14 8 6

12
5

12 7 7 0 10 9 15 9 15

10

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Example of Prims algorithm


A VA 5 14 8 6

12
5

12 7 7 0 10 9 15 9 15

10

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Example of Prims algorithm


A VA 5 14 14 8 6

6
5

12 7 7 0 10 9 15 9 15

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Example of Prims algorithm


A VA 5 14 14 8 6

6
5

12 7 7 0 10 9 15 9 15

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Example of Prims algorithm


A VA 5 14 14 8 6

6
5

12 7 7 0 10 9 15 9 15

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Example of Prims algorithm


A VA 5 14 3 8 6

6
5

12 7 7 0 10 9 15 9 15

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Example of Prims algorithm


A VA 5 14 3 8 6

6
5

12 7 7 0 10 9 15 9 15

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Example of Prims algorithm


A VA 5 14 3 8 6

6
5

12 7 7 0 10 9 15 9 15

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Example of Prims algorithm


A VA 5 14 3 8 6

6
5

12 7 7 0 10 9 15 9 15

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Kruskals Algorithm
It is a greedy algorithm. In Kruskals algorithm, the set A is a forest.

The safe edge is added to A is always a least-weight edge in the graph that connects two distinct Components.

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Kruskals Algorithm
The operation FIND-SET(u) returns a representative element from the set that contains u. Thus we can determine whether two vertices u and v belong to the same tree by testing whether

FIND-SET(u) = FIND-SET(v) The combining of trees is accomplished by the


UNION procedure

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Kruskals Algorithm
1. A

2. for each vertex v V[G] 3.do MAKE-SET(v) 4.short the edges of E into non-decreasing order by weight. 5.for each edge (u, v) E, taken in non-decreasing order by weight. 6.do if FIND-SET(u) != FIND-SET(v) 7. then, A A U {(u, v)} 8. UNION(u, v) 9.return A
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Complexity
Prims Algorithm: O(E logV) Kruskals Algorithm: O(E logV)

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

You might also like