Top Banner
Data Structures Data Structures Lecture 28: Minimum Spanning Tree
23

Minimum spanning tree

Jan 25, 2015

Download

Education

Tanmay Baranwal

a spanning tree of that graph is a subgraph that is a tree and connects all the vertices together. A single graph can have many different spanning trees.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Minimum spanning tree

Data StructuresData StructuresLecture 28: Minimum Spanning Tree

Page 2: Minimum spanning tree

Outlines

Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm

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

Page 3: Minimum spanning tree

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:

Tvu

vuwTw),(

),()(

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

Page 4: Minimum spanning tree

Example of MST

6 125

14

3

8

10

15

9

7

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

Page 5: Minimum spanning tree

Prim’s AlgorithmIDEA: Maintain V – A as a priority queue Q. Key each vertex in Q with the weight of the least-weight edge connecting it to a vertex in A.Q Vkey[v] for all v V key[s] 0 for some arbitrary s Vwhile 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) ⊳ DECREASE-KEY

[v] u

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

Page 6: Minimum spanning tree

Example of Prim’s algorithm

A V – A

00

6 125

14

3

8

10

15

9

7

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

Page 7: Minimum spanning tree

Example of Prim’s algorithm

A V – A

00

6 125

14

3

8

10

15

9

7

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

Page 8: Minimum spanning tree

Example of Prim’s algorithm

A V – A

00

6 125

14

3

8

10

15

9

7

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

Page 9: Minimum spanning tree

Example of Prim’s algorithm

A V – A

77

00

6 125

14

3

8

10

15

9

7

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

Page 10: Minimum spanning tree

Example of Prim’s algorithm

A V – A

77

00

6 125

14

3

8

10

15

9

7

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

Page 11: Minimum spanning tree

Example of Prim’s algorithm

A V – A

55 77

00

6 125

14

3

8

10

15

9

7

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

Page 12: Minimum spanning tree

Example of Prim’s algorithm

A V – A

55 77

00

6 125

14

3

8

10

15

9

7

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

Page 13: Minimum spanning tree

Example of Prim’s algorithm

A V – A

66

55 77

00

6 125

14

3

8

10

15

9

7

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

Page 14: Minimum spanning tree

Example of Prim’s algorithm

A V – A

66

55 77

00

88

6 125

14

3

8

10

15

9

7

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

Page 15: Minimum spanning tree

Example of Prim’s algorithm

A V – A

66

55 77

00

88

6 125

14

3

8

10

15

9

7

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

Page 16: Minimum spanning tree

Example of Prim’s algorithm

A V – A

66

55 77

33 00

88

6 125

14

3

8

10

15

9

7

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

Page 17: Minimum spanning tree

Example of Prim’s algorithm

A V – A

66

55 77

33 00

88

99

6 125

14

3

8

10

15

9

7

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

Page 18: Minimum spanning tree

Example of Prim’s algorithm

A V – A

66

55 77

33 00

88

99

1515

6 125

14

3

8

10

15

9

7

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

Page 19: Minimum spanning tree

Kruskal’s Algorithm

It is a greedy algorithm.

In Kruskal’s 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)

Page 20: Minimum spanning tree

Kruskal’s 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)

Page 21: Minimum spanning tree

Kruskal’s 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 orderby 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)

Page 22: Minimum spanning tree

Complexity

Prim’s Algorithm: O(E logV)

Kruskal’s Algorithm: O(E logV)

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

Page 23: Minimum spanning tree

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