minimum spanning tree - skku · 2019-12-07 · 2. minimum spanning tree (mst)-the spanning tree of...

81
Minimum Spanning Tree SWE2016-44

Upload: others

Post on 04-Jul-2020

14 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Minimum Spanning Tree

SWE2016-44

Page 2: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Definitions

2

1. Spanning Tree

- Given a connected and undirected graph, a spanning tree of that graph is a subgraph that is a tree connects all the vertices together.

Page 3: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Definitions

3

1. Spanning Tree

- Given a connected and undirected graph, a spanning tree of that graph is a subgraph that is a tree connects all the vertices together.

- Properties• The spanning tree does not have any cycle

• Spanning tree has n-1 edges, where n is the number of vertices

• From a complete graph, by removing maximum e - n + 1 edges, we can construct a spanning tree.

Page 4: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Definitions

4

2. Minimum Spanning Tree (MST)

- The spanning tree of the graph whose sum of weights of edges is minimum.

• A graph may have more than 1 minimum spanning tree.

Page 5: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Definitions

5

2. Minimum Spanning Tree (MST)

- The spanning tree of the graph whose sum of weights of edges is minimum.

• A graph may have more than 1 minimum spanning tree.

- Two most important MST• Kruskal’s Algorithm

• Prim’s Algorithm

※ Both are greedy algorithms.

Page 6: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Kruskal’s Algorithm

6

Page 7: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Kruskal’s Algorithm

7

1. Sort all the edges in non-decreasing order of their weight.

2. Pick the smallest edge. Check if it forms a cycle with the spanning tree formed so far. If cycle is not formed, include this edge. Else, discard it.

3. Repeat step#2 until there are (V-1) edges in the spanning tree.

Page 8: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Examples

8

1. Sort all the edges in non-decreasing order of their weight.

3. Repeat step#2 until there are (V-1) edges in the spanning tree.

2. Pick the smallest edge. Check if it forms a cycle with the spanning tree formed so far. If cycle is not formed, include this edge. Else, discard it.

V7

V0

V1

V6

V2

V5

V3

V4V8

4

8 7

1 2

8

11

2

67

144

10

9

|V|=9

Page 9: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Examples

9

1. Sort all the edges in non-decreasing order of their weight.

3. Repeat step#2 until there are (V-1) edges in the spanning tree.

2. Pick the smallest edge. Check if it forms a cycle with the spanning tree formed so far. If cycle is not formed, include this edge. Else, discard it.

V7

V0

V1

V6

V2

V5

V3

V4V8

4

8 7

1 2

8

11

2

67

144

10

9

|V|=9

v7,v6 v8,v2 v6,v5 v0,v1 v2,v5 v8,v6 v2,v3 v7,v8 v0,v7 v1,v2 v3,v4 v5,v4 v1,v7 v3,v5

1 2 2 4 4 6 7 7 8 8 9 10 11 14

Page 10: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Examples

10

1. Sort all the edges in non-decreasing order of their weight.

3. Repeat step#2 until there are (V-1) edges in the spanning tree.

2. Pick the smallest edge. Check if it forms a cycle with the spanning tree formed so far. If cycle is not formed, include this edge. Else, discard it.

V7

V0

V1

V6

V2

V5

V3

V4V8

4

8 7

1 2

8

11

2

67

144

10

9

|V|=9

v7,v6 v8,v2 v6,v5 v0,v1 v2,v5 v8,v6 v2,v3 v7,v8 v0,v7 v1,v2 v3,v4 v5,v4 v1,v7 v3,v5

1 2 2 4 4 6 7 7 8 8 9 10 11 14

Page 11: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Examples

11

1. Sort all the edges in non-decreasing order of their weight.

3. Repeat step#2 until there are (V-1) edges in the spanning tree.

2. Pick the smallest edge. Check if it forms a cycle with the spanning tree formed so far. If cycle is not formed, include this edge. Else, discard it.

V7

V0

V1

V6

V2

V5

V3

V4V8

4

8 7

1 2

8

11

2

67

144

10

9

|V|=9

v7,v6 v8,v2 v6,v5 v0,v1 v2,v5 v8,v6 v2,v3 v7,v8 v0,v7 v1,v2 v3,v4 v5,v4 v1,v7 v3,v5

1 2 2 4 4 6 7 7 8 8 9 10 11 14

# of Edges in MST = 1

Page 12: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Examples

12

1. Sort all the edges in non-decreasing order of their weight.

3. Repeat step#2 until there are (V-1) edges in the spanning tree.

2. Pick the smallest edge. Check if it forms a cycle with the spanning tree formed so far. If cycle is not formed, include this edge. Else, discard it.

V7

V0

V1

V6

V2

V5

V3

V4V8

4

8 7

1 2

8

11

2

67

144

10

9

|V|=9

v7,v6 v8,v2 v6,v5 v0,v1 v2,v5 v8,v6 v2,v3 v7,v8 v0,v7 v1,v2 v3,v4 v5,v4 v1,v7 v3,v5

1 2 2 4 4 6 7 7 8 8 9 10 11 14

# of Edges in MST = 1

Page 13: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Examples

13

1. Sort all the edges in non-decreasing order of their weight.

3. Repeat step#2 until there are (V-1) edges in the spanning tree.

2. Pick the smallest edge. Check if it forms a cycle with the spanning tree formed so far. If cycle is not formed, include this edge. Else, discard it.

V7

V0

V1

V6

V2

V5

V3

V4V8

4

8 7

1 2

8

11

2

67

144

10

9

|V|=9

v7,v6 v8,v2 v6,v5 v0,v1 v2,v5 v8,v6 v2,v3 v7,v8 v0,v7 v1,v2 v3,v4 v5,v4 v1,v7 v3,v5

1 2 2 4 4 6 7 7 8 8 9 10 11 14

# of Edges in MST = 2

Page 14: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Examples

14

1. Sort all the edges in non-decreasing order of their weight.

3. Repeat step#2 until there are (V-1) edges in the spanning tree.

2. Pick the smallest edge. Check if it forms a cycle with the spanning tree formed so far. If cycle is not formed, include this edge. Else, discard it.

V7

V0

V1

V6

V2

V5

V3

V4V8

4

8 7

1 2

8

11

2

67

144

10

9

|V|=9

v7,v6 v8,v2 v6,v5 v0,v1 v2,v5 v8,v6 v2,v3 v7,v8 v0,v7 v1,v2 v3,v4 v5,v4 v1,v7 v3,v5

1 2 2 4 4 6 7 7 8 8 9 10 11 14

# of Edges in MST = 3

Page 15: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Examples

15

1. Sort all the edges in non-decreasing order of their weight.

3. Repeat step#2 until there are (V-1) edges in the spanning tree.

2. Pick the smallest edge. Check if it forms a cycle with the spanning tree formed so far. If cycle is not formed, include this edge. Else, discard it.

V7

V0

V1

V6

V2

V5

V3

V4V8

4

8 7

1 2

8

11

2

67

144

10

9

|V|=9

v7,v6 v8,v2 v6,v5 v0,v1 v2,v5 v8,v6 v2,v3 v7,v8 v0,v7 v1,v2 v3,v4 v5,v4 v1,v7 v3,v5

1 2 2 4 4 6 7 7 8 8 9 10 11 14

# of Edges in MST = 4

Page 16: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Examples

16

1. Sort all the edges in non-decreasing order of their weight.

3. Repeat step#2 until there are (V-1) edges in the spanning tree.

2. Pick the smallest edge. Check if it forms a cycle with the spanning tree formed so far. If cycle is not formed, include this edge. Else, discard it.

V7

V0

V1

V6

V2

V5

V3

V4V8

4

8 7

1 2

8

11

2

67

144

10

9

|V|=9

v7,v6 v8,v2 v6,v5 v0,v1 v2,v5 v8,v6 v2,v3 v7,v8 v0,v7 v1,v2 v3,v4 v5,v4 v1,v7 v3,v5

1 2 2 4 4 6 7 7 8 8 9 10 11 14

# of Edges in MST = 5

Page 17: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Examples

17

1. Sort all the edges in non-decreasing order of their weight.

3. Repeat step#2 until there are (V-1) edges in the spanning tree.

2. Pick the smallest edge. Check if it forms a cycle with the spanning tree formed so far. If cycle is not formed, include this edge. Else, discard it.

V7

V0

V1

V6

V2

V5

V3

V4V8

4

8 7

1 2

8

11

2

67

144

10

9

|V|=9

v7,v6 v8,v2 v6,v5 v0,v1 v2,v5 v8,v6 v2,v3 v7,v8 v0,v7 v1,v2 v3,v4 v5,v4 v1,v7 v3,v5

1 2 2 4 4 6 7 7 8 8 9 10 11 14

# of Edges in MST = 5

Page 18: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Examples

18

1. Sort all the edges in non-decreasing order of their weight.

3. Repeat step#2 until there are (V-1) edges in the spanning tree.

2. Pick the smallest edge. Check if it forms a cycle with the spanning tree formed so far. If cycle is not formed, include this edge. Else, discard it.

V7

V0

V1

V6

V2

V5

V3

V4V8

4

8 7

1 2

8

11

2

67

144

10

9

|V|=9

v7,v6 v8,v2 v6,v5 v0,v1 v2,v5 v8,v6 v2,v3 v7,v8 v0,v7 v1,v2 v3,v4 v5,v4 v1,v7 v3,v5

1 2 2 4 4 6 7 7 8 8 9 10 11 14

# of Edges in MST = 6

Page 19: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Examples

19

1. Sort all the edges in non-decreasing order of their weight.

3. Repeat step#2 until there are (V-1) edges in the spanning tree.

2. Pick the smallest edge. Check if it forms a cycle with the spanning tree formed so far. If cycle is not formed, include this edge. Else, discard it.

V7

V0

V1

V6

V2

V5

V3

V4V8

4

8 7

1 2

8

11

2

67

144

10

9

|V|=9

v7,v6 v8,v2 v6,v5 v0,v1 v2,v5 v8,v6 v2,v3 v7,v8 v0,v7 v1,v2 v3,v4 v5,v4 v1,v7 v3,v5

1 2 2 4 4 6 7 7 8 8 9 10 11 14

# of Edges in MST = 6

Page 20: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Examples

20

1. Sort all the edges in non-decreasing order of their weight.

3. Repeat step#2 until there are (V-1) edges in the spanning tree.

2. Pick the smallest edge. Check if it forms a cycle with the spanning tree formed so far. If cycle is not formed, include this edge. Else, discard it.

V7

V0

V1

V6

V2

V5

V3

V4V8

4

8 7

1 2

8

11

2

67

144

10

9

|V|=9

v7,v6 v8,v2 v6,v5 v0,v1 v2,v5 v8,v6 v2,v3 v7,v8 v0,v7 v1,v2 v3,v4 v5,v4 v1,v7 v3,v5

1 2 2 4 4 6 7 7 8 8 9 10 11 14

# of Edges in MST = 7

Page 21: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Examples

21

1. Sort all the edges in non-decreasing order of their weight.

3. Repeat step#2 until there are (V-1) edges in the spanning tree.

2. Pick the smallest edge. Check if it forms a cycle with the spanning tree formed so far. If cycle is not formed, include this edge. Else, discard it.

V7

V0

V1

V6

V2

V5

V3

V4V8

4

8 7

1 2

8

11

2

67

144

10

9

|V|=9

v7,v6 v8,v2 v6,v5 v0,v1 v2,v5 v8,v6 v2,v3 v7,v8 v0,v7 v1,v2 v3,v4 v5,v4 v1,v7 v3,v5

1 2 2 4 4 6 7 7 8 8 9 10 11 14

# of Edges in MST = 7

Page 22: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Examples

22

1. Sort all the edges in non-decreasing order of their weight.

3. Repeat step#2 until there are (V-1) edges in the spanning tree.

2. Pick the smallest edge. Check if it forms a cycle with the spanning tree formed so far. If cycle is not formed, include this edge. Else, discard it.

V7

V0

V1

V6

V2

V5

V3

V4V8

4

8 7

1 2

8

11

2

67

144

10

9

|V|=9

v7,v6 v8,v2 v6,v5 v0,v1 v2,v5 v8,v6 v2,v3 v7,v8 v0,v7 v1,v2 v3,v4 v5,v4 v1,v7 v3,v5

1 2 2 4 4 6 7 7 8 8 9 10 11 14

# of Edges in MST = 8

Page 23: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Examples

23

1. Sort all the edges in non-decreasing order of their weight.

3. Repeat step#2 until there are (V-1) edges in the spanning tree.

2. Pick the smallest edge. Check if it forms a cycle with the spanning tree formed so far. If cycle is not formed, include this edge. Else, discard it.

V7

V0

V1

V6

V2

V5

V3

V4V8

4

8 7

1 2

8

11

2

67

144

10

9

|V|=9

v7,v6 v8,v2 v6,v5 v0,v1 v2,v5 v8,v6 v2,v3 v7,v8 v0,v7 v1,v2 v3,v4 v5,v4 v1,v7 v3,v5

1 2 2 4 4 6 7 7 8 8 9 10 11 14

Terminate!!!

Page 24: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Examples

24

1. Sort all the edges in non-decreasing order of their weight.

3. Repeat step#2 until there are (V-1) edges in the spanning tree.

2. Pick the smallest edge. Check if it forms a cycle with the spanning tree formed so far. If cycle is not formed, include this edge. Else, discard it.

V7

V0

V1

V6

V2

V5

V3

V4V8

4

7

1 2

8

2

4

9

v7,v6 v8,v2 v6,v5 v0,v1 v2,v5 v8,v6 v2,v3 v7,v8 v0,v7 v1,v2 v3,v4 v5,v4 v1,v7 v3,v5

1 2 2 4 4 6 7 7 8 8 9 10 11 14

Now we have our MST!

Page 25: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Examples

25

1. Sort all the edges in non-decreasing order of their weight.

3. Repeat step#2 until there are (V-1) edges in the spanning tree.

2. Pick the smallest edge. Check if it forms a cycle with the spanning tree formed so far. If cycle is not formed, include this edge. Else, discard it.

V7

V0

V1

V6

V2

V5

V3

V4V8

4

7

1 2

8

2

4

9

v7,v6 v8,v2 v6,v5 v0,v1 v2,v5 v8,v6 v2,v3 v7,v8 v0,v7 v1,v2 v3,v4 v5,v4 v1,v7 v3,v5

1 2 2 4 4 6 7 7 8 8 9 10 11 14

Now we have our MST!

Weights of MST = 37

Page 26: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Pseudocode

26

O(1)

O(E log E)

O(E log V)

Overall Complexity = O(E Log E + E Log V)

→ V-1 ≤ E ≤ (V2-V)/2 → O(Log E) ≈ O(Log V)

→ Therefore, time complexity is O(E Log E) or O(E Log V)

Page 27: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Pseudocode

27

MAKE-SET: Create a node whose parent is itself, a root

→ MAKE-SET(v): O(1)

FIND-SET(i): Follow the pointer from i to the root of its tree

→ FIND-SET(u): O(h), where h is the height of the tree.

V1 V2 V3 V4

V1 V2 V3

FIND-SET(V3) = V1 V4

Page 28: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Pseudocode

28

UNION(x,y): If rank of x > rank of y, make y point to x. If rank of x = rank of y, make y point to x and increase rank of x.

→ UNION(x,y): O(1)

V1 V2 V3

V4

V5 V6

RANK 2

RANK 1

V1 V2

V5 V6

RANK 1

RANK 1

→ RANK 2

Page 29: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Implementation (1)

29

Page 30: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Implementation (2)

30

Page 31: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Kruskal’s algorithm Proof Correctness

31

Tree (T): Determined using

Kruskal’s algorithm

e1 e2

Hypothetical MST: T’

e’ e’’

Assume T is not a MST and T’ is a MST.

Then, weight(T) > weight(T’)

Page 32: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Kruskal’s algorithm Proof Correctness

32

Tree (T): Determined using

Kruskal’s algorithm

e1 e2

Hypothetical MST: T’

e’ e’’

e1

If weight(e1) > weight(e’), we have to select e’ instead of e1

for Kruskal’s algorithm.

However, since we selected e1, weight(e1) <= weight(e’).

Page 33: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Kruskal’s algorithm Proof Correctness

33

Tree (T): Determined using

Kruskal’s algorithm

e1 e2

Hypothetical MST: T’

e’ e’’

e1

weight(e1) <= weight(e’)

weight(T’) – weight(e’) + weight(e1) <= weight(T’)

→ We should select e1 instead of e’ for T’.

Page 34: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Kruskal’s algorithm Proof Correctness

34

Tree (T): Determined using

Kruskal’s algorithm

e1 e2

Hypothetical MST: T’

e’’

e1

Page 35: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Kruskal’s algorithm Proof Correctness

35

Tree (T): Determined using

Kruskal’s algorithm

e1 e2

Hypothetical MST: T’

e’’

e1

weight(e2) <= weight(e’’)

With every edge change and the corresponding Reduction

in the edge difference, weight(T’) only reduces further and

eventually as T’ becomes the same as T, both have the same

Total weight. weight(T’) >= weight(T).

e2

Page 36: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Kruskal’s algorithm Proof Correctness

36

Tree (T): Determined using

Kruskal’s algorithm

e1 e2

Hypothetical MST: T’

e’’

e1

weight(e2) <= weight(e’’)

With every edge change and the corresponding Reduction

in the edge difference, weight(T’) only reduces further and

eventually as T’ becomes the same as T, both have the same

Total weight. weight(T’) >= weight(T).

Page 37: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Kruskal’s algorithm Proof Correctness

37

Tree (T): Determined using

Kruskal’s algorithm

e1 e2

Hypothetical MST: T’

e1 e2

T’=T

Page 38: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Prim’s Algorithm

38

Page 39: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Prim’s Algorithm (1)

39

1. Create a set mstSet that keeps track of vertices already included in MST.

2. Assign a key value to all vertices in the input graph. Initialize all key values as INFINITE. Assign key value as 0 for the first vertex so that it is picked first.

Page 40: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Prim’s Algorithm (2)

40

3. While mstSet doesn’t include all vertices

a. Pick a vertex u which is not there in mstSet and has minimum key value.

b. Include u to mstSet.

c. Update key value of all adjacent vertices of u.

To update the key values, iterate through all adjacent vertices. For every adjacent vertex v, if weight of edge u-v is less than the previous key value of v, update the key value as weight of u-v

Page 41: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Examples

41

1. Create a set mstSet that keeps track of vertices already included in MST.

3. While mstSet doesn’t include

all vertices

a. Pick a vertex u which is not

there in mstSet and has

minimum key value.

b. Include u to mstSet.

c. Update key value of all

adjacent vertices of u.

2. Assign a key value to all

vertices in the input graph.

Initialize all key values as

INFINITE. Assign key value as

0 for the first vertex so that it

is picked first.

V7

V0

V1

V6

V2

V5

V3

V4V8

4

8 7

1 2

8

11

2

67

144

10

9

Page 42: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Examples

42

1. Create a set mstSet that keeps track of vertices already included in MST.

3. While mstSet doesn’t include

all vertices

a. Pick a vertex u which is not

there in mstSet and has

minimum key value.

b. Include u to mstSet.

c. Update key value of all

adjacent vertices of u.

2. Assign a key value to all

vertices in the input graph.

Initialize all key values as

INFINITE. Assign key value as

0 for the first vertex so that it

is picked first.

V7

V0

V1

V6

V2

V5

V3

V4V8

4

8 7

1 2

8

11

2

67

144

10

9

mstSet={}

Page 43: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Examples

43

1. Create a set mstSet that keeps track of vertices already included in MST.

3. While mstSet doesn’t include

all vertices

a. Pick a vertex u which is not

there in mstSet and has

minimum key value.

b. Include u to mstSet.

c. Update key value of all

adjacent vertices of u.

2. Assign a key value to all

vertices in the input graph.

Initialize all key values as

INFINITE. Assign key value as

0 for the first vertex so that it

is picked first.

V7

V0

V1

V6

V2

V5

V3

V4V8

4

8 7

1 2

8

11

2

67

144

10

9

Ver. v0 v1 v2 v3 v4 v5 v6 v7 v8

Key 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞

mstSet={}

Page 44: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Examples

44

1. Create a set mstSet that keeps track of vertices already included in MST.

3. While mstSet doesn’t include

all vertices

a. Pick a vertex u which is not

there in mstSet and has

minimum key value.

b. Include u to mstSet.

c. Update key value of all

adjacent vertices of u.

2. Assign a key value to all

vertices in the input graph.

Initialize all key values as

INFINITE. Assign key value as

0 for the first vertex so that it

is picked first.

V7

V0

V1

V6

V2

V5

V3

V4V8

4

8 7

1 2

8

11

2

67

144

10

9

Ver. v0 v1 v2 v3 v4 v5 v6 v7 v8

Key 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞

mstSet={}

Page 45: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Examples

45

1. Create a set mstSet that keeps track of vertices already included in MST.

3. While mstSet doesn’t include

all vertices

a. Pick a vertex u which is not

there in mstSet and has

minimum key value.

b. Include u to mstSet.

c. Update key value of all

adjacent vertices of u.

2. Assign a key value to all

vertices in the input graph.

Initialize all key values as

INFINITE. Assign key value as

0 for the first vertex so that it

is picked first.

V7

V0

V1

V6

V2

V5

V3

V4V8

4

8 7

1 2

8

11

2

67

144

10

9

mstSet={v0}

Ver. v0 v1 v2 v3 v4 v5 v6 v7 v8

Key 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞

u

u

If w(u,v)<v.key, v.key=w(u,v)

Page 46: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Examples

46

1. Create a set mstSet that keeps track of vertices already included in MST.

3. While mstSet doesn’t include

all vertices

a. Pick a vertex u which is not

there in mstSet and has

minimum key value.

b. Include u to mstSet.

c. Update key value of all

adjacent vertices of u.

2. Assign a key value to all

vertices in the input graph.

Initialize all key values as

INFINITE. Assign key value as

0 for the first vertex so that it

is picked first.

V7

V0

V1

V6

V2

V5

V3

V4V8

4

8 7

1 2

8

11

2

67

144

10

9

mstSet={v0}

Ver. v0 v1 v2 v3 v4 v5 v6 v7 v8

Key 0 4 ∞ ∞ ∞ ∞ ∞ 8 ∞

Page 47: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Examples

47

1. Create a set mstSet that keeps track of vertices already included in MST.

3. While mstSet doesn’t include

all vertices

a. Pick a vertex u which is not

there in mstSet and has

minimum key value.

b. Include u to mstSet.

c. Update key value of all

adjacent vertices of u.

2. Assign a key value to all

vertices in the input graph.

Initialize all key values as

INFINITE. Assign key value as

0 for the first vertex so that it

is picked first.

V7

V0

V1

V6

V2

V5

V3

V4V8

4

8 7

1 2

8

11

2

67

144

10

9

mstSet={v0, v1}

Ver. v0 v1 v2 v3 v4 v5 v6 v7 v8

Key 0 4 ∞ ∞ ∞ ∞ ∞ 8 ∞

u

u

Page 48: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Examples

48

1. Create a set mstSet that keeps track of vertices already included in MST.

3. While mstSet doesn’t include

all vertices

a. Pick a vertex u which is not

there in mstSet and has

minimum key value.

b. Include u to mstSet.

c. Update key value of all

adjacent vertices of u.

2. Assign a key value to all

vertices in the input graph.

Initialize all key values as

INFINITE. Assign key value as

0 for the first vertex so that it

is picked first.

V7

V0

V1

V6

V2

V5

V3

V4V8

4

8 7

1 2

8

11

2

67

144

10

9

mstSet={v0, v1}

Ver. v0 v1 v2 v3 v4 v5 v6 v7 v8

Key 0 4 8 ∞ ∞ ∞ ∞ 8 ∞

Page 49: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Examples

49

1. Create a set mstSet that keeps track of vertices already included in MST.

3. While mstSet doesn’t include

all vertices

a. Pick a vertex u which is not

there in mstSet and has

minimum key value.

b. Include u to mstSet.

c. Update key value of all

adjacent vertices of u.

2. Assign a key value to all

vertices in the input graph.

Initialize all key values as

INFINITE. Assign key value as

0 for the first vertex so that it

is picked first.

V7

V0

V1

V6

V2

V5

V3

V4V8

4

8 7

1 2

8

11

2

67

144

10

9

mstSet={v0,v1,v7}

Ver. v0 v1 v2 v3 v4 v5 v6 v7 v8

Key 0 4 8 ∞ ∞ ∞ ∞ 8 ∞

u

u

Page 50: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Examples

50

1. Create a set mstSet that keeps track of vertices already included in MST.

3. While mstSet doesn’t include

all vertices

a. Pick a vertex u which is not

there in mstSet and has

minimum key value.

b. Include u to mstSet.

c. Update key value of all

adjacent vertices of u.

2. Assign a key value to all

vertices in the input graph.

Initialize all key values as

INFINITE. Assign key value as

0 for the first vertex so that it

is picked first.

V7

V0

V1

V6

V2

V5

V3

V4V8

4

8 7

1 2

8

11

2

67

144

10

9

mstSet={v0,v1,v7}

Ver. v0 v1 v2 v3 v4 v5 v6 v7 v8

Key 0 4 8 ∞ ∞ ∞ 1 8 7

Page 51: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Examples

51

1. Create a set mstSet that keeps track of vertices already included in MST.

3. While mstSet doesn’t include

all vertices

a. Pick a vertex u which is not

there in mstSet and has

minimum key value.

b. Include u to mstSet.

c. Update key value of all

adjacent vertices of u.

2. Assign a key value to all

vertices in the input graph.

Initialize all key values as

INFINITE. Assign key value as

0 for the first vertex so that it

is picked first.

V7

V0

V1

V6

V2

V5

V3

V4V8

4

8 7

1 2

8

11

2

67

144

10

9

mstSet={v0,v1,v7,v6}

Ver. v0 v1 v2 v3 v4 v5 v6 v7 v8

Key 0 4 8 ∞ ∞ ∞ 1 8 7

u u

Page 52: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Examples

52

1. Create a set mstSet that keeps track of vertices already included in MST.

3. While mstSet doesn’t include

all vertices

a. Pick a vertex u which is not

there in mstSet and has

minimum key value.

b. Include u to mstSet.

c. Update key value of all

adjacent vertices of u.

2. Assign a key value to all

vertices in the input graph.

Initialize all key values as

INFINITE. Assign key value as

0 for the first vertex so that it

is picked first.

V7

V0

V1

V6

V2

V5

V3

V4V8

4

8 7

1 2

8

11

2

67

144

10

9

mstSet={v0,v1,v7,v6}

Ver. v0 v1 v2 v3 v4 v5 v6 v7 v8

Key 0 4 8 ∞ ∞ 2 1 8 6

Page 53: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Examples

53

1. Create a set mstSet that keeps track of vertices already included in MST.

3. While mstSet doesn’t include

all vertices

a. Pick a vertex u which is not

there in mstSet and has

minimum key value.

b. Include u to mstSet.

c. Update key value of all

adjacent vertices of u.

2. Assign a key value to all

vertices in the input graph.

Initialize all key values as

INFINITE. Assign key value as

0 for the first vertex so that it

is picked first.

V7

V0

V1

V6

V2

V5

V3

V4V8

4

8 7

1 2

8

11

2

67

144

10

9

mstSet={v0,v1,v7,v6,v5}

Ver. v0 v1 v2 v3 v4 v5 v6 v7 v8

Key 0 4 8 ∞ ∞ 2 1 8 6

u

u

Page 54: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Examples

54

1. Create a set mstSet that keeps track of vertices already included in MST.

3. While mstSet doesn’t include

all vertices

a. Pick a vertex u which is not

there in mstSet and has

minimum key value.

b. Include u to mstSet.

c. Update key value of all

adjacent vertices of u.

2. Assign a key value to all

vertices in the input graph.

Initialize all key values as

INFINITE. Assign key value as

0 for the first vertex so that it

is picked first.

V7

V0

V1

V6

V2

V5

V3

V4V8

4

8 7

1 2

8

11

2

67

144

10

9

mstSet={v0,v1,v7,v6,v5}

Ver. v0 v1 v2 v3 v4 v5 v6 v7 v8

Key 0 4 4 14 10 2 1 8 6

Page 55: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Examples

55

1. Create a set mstSet that keeps track of vertices already included in MST.

3. While mstSet doesn’t include

all vertices

a. Pick a vertex u which is not

there in mstSet and has

minimum key value.

b. Include u to mstSet.

c. Update key value of all

adjacent vertices of u.

2. Assign a key value to all

vertices in the input graph.

Initialize all key values as

INFINITE. Assign key value as

0 for the first vertex so that it

is picked first.

V7

V0

V1

V6

V2

V5

V3

V4V8

4

8 7

1 2

8

11

2

67

144

10

9

mstSet={v0,v1,v7,v6,v5,v2}

Ver. v0 v1 v2 v3 v4 v5 v6 v7 v8

Key 0 4 4 14 10 2 1 8 6

u

u

Page 56: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Examples

56

1. Create a set mstSet that keeps track of vertices already included in MST.

3. While mstSet doesn’t include

all vertices

a. Pick a vertex u which is not

there in mstSet and has

minimum key value.

b. Include u to mstSet.

c. Update key value of all

adjacent vertices of u.

2. Assign a key value to all

vertices in the input graph.

Initialize all key values as

INFINITE. Assign key value as

0 for the first vertex so that it

is picked first.

V7

V0

V1

V6

V2

V5

V3

V4V8

4

8 7

1 2

8

11

2

67

144

10

9

mstSet={v0,v1,v7,v6,v5,v2}

Ver. v0 v1 v2 v3 v4 v5 v6 v7 v8

Key 0 4 4 7 10 2 1 8 2

Page 57: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Examples

57

1. Create a set mstSet that keeps track of vertices already included in MST.

3. While mstSet doesn’t include

all vertices

a. Pick a vertex u which is not

there in mstSet and has

minimum key value.

b. Include u to mstSet.

c. Update key value of all

adjacent vertices of u.

2. Assign a key value to all

vertices in the input graph.

Initialize all key values as

INFINITE. Assign key value as

0 for the first vertex so that it

is picked first.

V7

V0

V1

V6

V2

V5

V3

V4V8

4

8 7

1 2

8

11

2

67

144

10

9

mstSet={v0,v1,v7,v6,v5,v2,v8}

Ver. v0 v1 v2 v3 v4 v5 v6 v7 v8

Key 0 4 4 7 10 2 1 8 2

u

u

Page 58: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Examples

58

1. Create a set mstSet that keeps track of vertices already included in MST.

3. While mstSet doesn’t include

all vertices

a. Pick a vertex u which is not

there in mstSet and has

minimum key value.

b. Include u to mstSet.

c. Update key value of all

adjacent vertices of u.

2. Assign a key value to all

vertices in the input graph.

Initialize all key values as

INFINITE. Assign key value as

0 for the first vertex so that it

is picked first.

V7

V0

V1

V6

V2

V5

V3

V4V8

4

8 7

1 2

8

11

2

67

144

10

9

Ver. v0 v1 v2 v3 v4 v5 v6 v7 v8

Key 0 4 4 7 10 2 1 8 2

mstSet={v0,v1,v7,v6,v5,v2,v8}

Page 59: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Examples

59

1. Create a set mstSet that keeps track of vertices already included in MST.

3. While mstSet doesn’t include

all vertices

a. Pick a vertex u which is not

there in mstSet and has

minimum key value.

b. Include u to mstSet.

c. Update key value of all

adjacent vertices of u.

2. Assign a key value to all

vertices in the input graph.

Initialize all key values as

INFINITE. Assign key value as

0 for the first vertex so that it

is picked first.

V7

V0

V1

V6

V2

V5

V3

V4V8

4

8 7

1 2

8

11

2

67

144

10

9

Ver. v0 v1 v2 v3 v4 v5 v6 v7 v8

Key 0 4 4 7 10 2 1 8 2

mstSet={v0,v1,v7,v6,v5,v2,v8,v3}

u

u

Page 60: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Examples

60

1. Create a set mstSet that keeps track of vertices already included in MST.

3. While mstSet doesn’t include

all vertices

a. Pick a vertex u which is not

there in mstSet and has

minimum key value.

b. Include u to mstSet.

c. Update key value of all

adjacent vertices of u.

2. Assign a key value to all

vertices in the input graph.

Initialize all key values as

INFINITE. Assign key value as

0 for the first vertex so that it

is picked first.

V7

V0

V1

V6

V2

V5

V3

V4V8

4

8 7

1 2

8

11

2

67

144

10

9

Ver. v0 v1 v2 v3 v4 v5 v6 v7 v8

Key 0 4 4 7 9 2 1 8 2

mstSet={v0,v1,v7,v6,v5,v2,v8,v3}

Page 61: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Examples

61

1. Create a set mstSet that keeps track of vertices already included in MST.

3. While mstSet doesn’t include

all vertices

a. Pick a vertex u which is not

there in mstSet and has

minimum key value.

b. Include u to mstSet.

c. Update key value of all

adjacent vertices of u.

2. Assign a key value to all

vertices in the input graph.

Initialize all key values as

INFINITE. Assign key value as

0 for the first vertex so that it

is picked first.

V7

V0

V1

V6

V2

V5

V3

V4V8

4

8 7

1 2

8

11

2

67

144

10

9

Ver. v0 v1 v2 v3 v4 v5 v6 v7 v8

Key 0 4 4 7 9 2 1 8 2

mstSet={v0,v1,v7,v6,v5,v2,v8,v3,v4}

u

u

Page 62: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Examples

62

1. Create a set mstSet that keeps track of vertices already included in MST.

3. While mstSet doesn’t include

all vertices

a. Pick a vertex u which is not

there in mstSet and has

minimum key value.

b. Include u to mstSet.

c. Update key value of all

adjacent vertices of u.

2. Assign a key value to all

vertices in the input graph.

Initialize all key values as

INFINITE. Assign key value as

0 for the first vertex so that it

is picked first.

V7

V0

V1

V6

V2

V5

V3

V4V8

4

8 7

1 2

8

11

2

67

144

10

9

Ver. v0 v1 v2 v3 v4 v5 v6 v7 v8

Key 0 4 4 7 9 2 1 8 2

mstSet={v0,v1,v7,v6,v5,v2,v8,v3,v4}

Terminate!!

Page 63: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Examples

63

Time Complexity

• Time Complexity of Prim’s Algorithm is O(V2) if adjacency matrix is used.

• If adjacency list is used, then the time complexity of Prim’s algorithm can be reduced to O(E Log V) with the help of binary heap and O(E + V Log V) with the help of Fibonacci heap.

V7

V0

V1

V6

V2

V5

V3

V4V8

4

7

1 2

8

2

4

9

Ver. v0 v1 v2 v3 v4 v5 v6 v7 v8

Key 0 4 4 7 9 2 1 8 2

u

Weights of MST = 37

Page 64: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Pseudocode

64

Page 65: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Pseudocode (when we use Binary Heap)

65

O(log V)

O(log V)

→ O(E log V + V log V) = O(E log V)

Page 66: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Pseudocode (when we use Fibonacci Heap)

66

O(log V)

O(1)

→ O(V log V + E)

Page 67: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Pseudocode (when we use Fibonacci Heap)

67

Fibonacci Heap

Page 68: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Pseudocode (when we use Fibonacci Heap)

68

INSERT(Q, n): O(1)

Page 69: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Pseudocode (when we use Fibonacci Heap)

69

EXTRACT-MIN(Q): O(log n)

1. Delete min; meld its children into root list; update min

Page 70: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Pseudocode (when we use Fibonacci Heap)

70

2. Consolidate trees so that no two roots have same rank

(# of children).

Page 71: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Pseudocode (when we use Fibonacci Heap)

71

2. Consolidate trees so that no two roots have same rank

(# of children).

Page 72: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Pseudocode (when we use Fibonacci Heap)

72

DECREASE-KEY(Q, v, w(u,v)): O(1)

1. Decrease key of x

Page 73: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Pseudocode (when we use Fibonacci Heap)

73

2. Cut tree rooted at x, meld into root list, and unmark.

[heap order violated]

Page 74: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Pseudocode (when we use Fibonacci Heap)

74

3. If parent p of x is unmarked (hasn't yet lost a child), mark it;

[heap order violated]

Page 75: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Pseudocode (when we use Fibonacci Heap)

75

3. Otherwise, cut p, meld into root list, and unmark

(and do so recursively for all ancestors that lose a second child).

[heap order violated]

Page 76: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Pseudocode (when we use Fibonacci Heap)

76

[heap order violated]

3. Otherwise, cut p, meld into root list, and unmark

(and do so recursively for all ancestors that lose a second child).

Page 77: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Implementation (1)

77

Page 78: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Implementation (2)

78

Page 79: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Kruskal’s Algorithm vs Prim’s Algorithm

79

Page 80: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Kruskal’s Algorithm vs Prim’s Algorithm

80

Kruskal’s Algorithm Prim’s Algorithm

Start to build MST from minimum

edge in the graph

Start to build MST from any vertices

Disjoint Set Adjacency Matrix, Binary Heap,

Fibonacci Heap

Run faster in sparse graphs Run faster in dense graphs

O(E Log V) O(E Log V) with Binary Heap,

O(E + V Log V) with Fibonacci Heap

Traverse the edge only once. Based

on cycle, it will either reject it or

accept it

Traverse the one node several time

in order to get minimum distance

Greedy Algorithm Greedy Algorithm

Page 81: Minimum Spanning Tree - SKKU · 2019-12-07 · 2. Minimum Spanning Tree (MST)-The spanning tree of the graph whose sum of weights of edges is minimum. •A graph may have more than

Reference

• Charles Leiserson and Piotr Indyk, “Introduction to Algorithms”, September 29, 2004

• https://www.geeksforgeeks.org

• https://www.cs.princeton.edu/~wayne/teaching/fibonacci-heap.pdf