Minimum spanning tree

23
Data Structures Data Structures Lecture 28: Minimum Spanning Tree

description

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.

Transcript of Minimum spanning tree

Data StructuresData StructuresLecture 28: Minimum Spanning Tree

Outlines

Minimum Spanning Tree Prim’s Algorithm Kruskal’s 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:

Tvu

vuwTw),(

),()(

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

Example of MST

6 125

14

3

8

10

15

9

7

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

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

Complexity

Prim’s Algorithm: O(E logV)

Kruskal’s Algorithm: O(E logV)

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

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