minimum spanning tree - george mason universitykosecka/cs483-001/primkruskal.pdf · 2016-09-30 ·...

52
Minimum Spanning Tree Problem: given a connected, undirected, weighted graph: 14 10 3 6 4 5 2 9 15 8

Upload: others

Post on 10-Jul-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Minimum Spanning Tree - George Mason Universitykosecka/cs483-001/PrimKruskal.pdf · 2016-09-30 · Minimum Spanning Tree • Problem: given a connected, undirected, weighted graph,

Minimum Spanning Tree•  Problem: given a connected, undirected, weighted

graph:

14 10

3

6 4 5

2

9

15

8

Page 2: Minimum Spanning Tree - George Mason Universitykosecka/cs483-001/PrimKruskal.pdf · 2016-09-30 · Minimum Spanning Tree • Problem: given a connected, undirected, weighted graph,

Minimum Spanning Tree•  Problem: given a connected, undirected, weighted

graph, find a spanning tree using edges that minimize the total weight

14 10

3

6 4 5

2

9

15

8

Page 3: Minimum Spanning Tree - George Mason Universitykosecka/cs483-001/PrimKruskal.pdf · 2016-09-30 · Minimum Spanning Tree • Problem: given a connected, undirected, weighted graph,

Minimum Spanning Tree•  Which edges form the minimum spanning tree (MST)

of the below graph?

H B C

G E D

F

A

14 10

3

6 4 5

2

9

15

8

Page 4: Minimum Spanning Tree - George Mason Universitykosecka/cs483-001/PrimKruskal.pdf · 2016-09-30 · Minimum Spanning Tree • Problem: given a connected, undirected, weighted graph,

Minimum Spanning Tree•  Answer:

H B C

G E D

F

A

14 10

3

6 4 5

2

9

15

8

Page 5: Minimum Spanning Tree - George Mason Universitykosecka/cs483-001/PrimKruskal.pdf · 2016-09-30 · Minimum Spanning Tree • Problem: given a connected, undirected, weighted graph,

Minimum Spanning Tree•  MSTs satisfy the optimal substructure property: an optimal

tree is composed of optimal subtrees•  Let T be an MST of G with an edge (u,v) in the middle

Removing (u,v) partitions T into two trees T1 and T2

•  Claim: T1 is an MST of G1 = (V1,E1), and T2 is an MST of G2 = (V2,E2) (Do V1 and V2 share vertices? Why?)

•  Proof: w(T) = w(u,v) + w(T1) + w(T2)���(There can’t be a better tree than T1 or T2, or T would be suboptimal)

Page 6: Minimum Spanning Tree - George Mason Universitykosecka/cs483-001/PrimKruskal.pdf · 2016-09-30 · Minimum Spanning Tree • Problem: given a connected, undirected, weighted graph,

Minimum Spanning Tree•  Thm:

Let T be MST of G, and let A ⊆ T be subtree of TLet (u,v) be min-weight edge connecting A to V-AThen (u,v) ∈ T

Page 7: Minimum Spanning Tree - George Mason Universitykosecka/cs483-001/PrimKruskal.pdf · 2016-09-30 · Minimum Spanning Tree • Problem: given a connected, undirected, weighted graph,

Minimum Spanning Tree•  Thm:

Let T be MST of G, and let A ⊆ T be subtree of TLet (u,v) be min-weight edge connecting A to V-AThen (u,v) ∈ T

•  Proof: in book (see Thm 23.1)

Page 8: Minimum Spanning Tree - George Mason Universitykosecka/cs483-001/PrimKruskal.pdf · 2016-09-30 · Minimum Spanning Tree • Problem: given a connected, undirected, weighted graph,

Prim’s AlgorithmMST-Prim(G, w, r) Q = V[G]; for each u ∈ Q key[u] = ∞; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v ∈ Adj[u] if (v ∈ Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

Page 9: Minimum Spanning Tree - George Mason Universitykosecka/cs483-001/PrimKruskal.pdf · 2016-09-30 · Minimum Spanning Tree • Problem: given a connected, undirected, weighted graph,

Prim’s AlgorithmMST-Prim(G, w, r) Q = V[G]; for each u ∈ Q key[u] = ∞; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v ∈ Adj[u] if (v ∈ Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

14 10

3

6 4 5

2

9

15

8

Run on example graph

Page 10: Minimum Spanning Tree - George Mason Universitykosecka/cs483-001/PrimKruskal.pdf · 2016-09-30 · Minimum Spanning Tree • Problem: given a connected, undirected, weighted graph,

Prim’s AlgorithmMST-Prim(G, w, r) Q = V[G]; for each u ∈ Q key[u] = ∞; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v ∈ Adj[u] if (v ∈ Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

Pick a start vertex r

∞ ∞ ∞

∞ ∞ ∞

14 10

3

6 4 5

2

9

15

8 r ∞

Page 11: Minimum Spanning Tree - George Mason Universitykosecka/cs483-001/PrimKruskal.pdf · 2016-09-30 · Minimum Spanning Tree • Problem: given a connected, undirected, weighted graph,

Prim’s AlgorithmMST-Prim(G, w, r) Q = V[G]; for each u ∈ Q key[u] = ∞; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v ∈ Adj[u] if (v ∈ Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

∞ ∞ ∞

0 ∞ ∞ ∞

14 10

3

6 4 5

2

9

15

8

Pick a start vertex r

r

Page 12: Minimum Spanning Tree - George Mason Universitykosecka/cs483-001/PrimKruskal.pdf · 2016-09-30 · Minimum Spanning Tree • Problem: given a connected, undirected, weighted graph,

Prim’s Algorithm

MST-Prim(G, w, r) Q = V[G]; for each u ∈ Q key[u] = ∞; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v ∈ Adj[u] if (v ∈ Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

Red vertices have been removed from Q

∞ ∞ ∞

0 ∞ ∞

14 10

3

6 4 5

2

9

15

8 u

Page 13: Minimum Spanning Tree - George Mason Universitykosecka/cs483-001/PrimKruskal.pdf · 2016-09-30 · Minimum Spanning Tree • Problem: given a connected, undirected, weighted graph,

Prim’s Algorithm

MST-Prim(G, w, r) Q = V[G]; for each u ∈ Q key[u] = ∞; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v ∈ Adj[u] if (v ∈ Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

Red arrows indicate parent pointers

3

∞ ∞ ∞

0 ∞ ∞

14 10

3

6 4 5

2

9

15

8 u

Page 14: Minimum Spanning Tree - George Mason Universitykosecka/cs483-001/PrimKruskal.pdf · 2016-09-30 · Minimum Spanning Tree • Problem: given a connected, undirected, weighted graph,

Prim’s Algorithm

MST-Prim(G, w, r) Q = V[G]; for each u ∈ Q key[u] = ∞; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v ∈ Adj[u] if (v ∈ Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

3

14 ∞ ∞

0 ∞ ∞

14 10

3

6 4 5

2

9

15

8 u

Page 15: Minimum Spanning Tree - George Mason Universitykosecka/cs483-001/PrimKruskal.pdf · 2016-09-30 · Minimum Spanning Tree • Problem: given a connected, undirected, weighted graph,

Prim’s Algorithm

MST-Prim(G, w, r) Q = V[G]; for each u ∈ Q key[u] = ∞; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v ∈ Adj[u] if (v ∈ Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

u 3

10 ∞ ∞

0 ∞ ∞

14 10

3

6 4 5

2

9

15

8

Page 16: Minimum Spanning Tree - George Mason Universitykosecka/cs483-001/PrimKruskal.pdf · 2016-09-30 · Minimum Spanning Tree • Problem: given a connected, undirected, weighted graph,

Prim’s AlgorithmMST-Prim(G, w, r) Q = V[G]; for each u ∈ Q key[u] = ∞; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v ∈ Adj[u] if (v ∈ Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

3

10 ∞ ∞

0 8 ∞

14 10

3

6 4 5

2

9

15

8

Page 17: Minimum Spanning Tree - George Mason Universitykosecka/cs483-001/PrimKruskal.pdf · 2016-09-30 · Minimum Spanning Tree • Problem: given a connected, undirected, weighted graph,

Prim’s AlgorithmMST-Prim(G, w, r) Q = V[G]; for each u ∈ Q key[u] = ∞; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v ∈ Adj[u] if (v ∈ Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 6

3

10 ∞ ∞

0 8 ∞

14 10

3

6 4 5

2

9

15

8

Page 18: Minimum Spanning Tree - George Mason Universitykosecka/cs483-001/PrimKruskal.pdf · 2016-09-30 · Minimum Spanning Tree • Problem: given a connected, undirected, weighted graph,

Prim’s AlgorithmMST-Prim(G, w, r) Q = V[G]; for each u ∈ Q key[u] = ∞; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v ∈ Adj[u] if (v ∈ Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

3

10 ∞ ∞

0 8 ∞

14 10

3

6 4 5

2

9

15

8 u

Page 19: Minimum Spanning Tree - George Mason Universitykosecka/cs483-001/PrimKruskal.pdf · 2016-09-30 · Minimum Spanning Tree • Problem: given a connected, undirected, weighted graph,

Prim’s AlgorithmMST-Prim(G, w, r) Q = V[G]; for each u ∈ Q key[u] = ∞; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v ∈ Adj[u] if (v ∈ Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

3

10 2 ∞

0 8 ∞

14 10

3

6 4 5

2

9

15

8

Page 20: Minimum Spanning Tree - George Mason Universitykosecka/cs483-001/PrimKruskal.pdf · 2016-09-30 · Minimum Spanning Tree • Problem: given a connected, undirected, weighted graph,

Prim’s AlgorithmMST-Prim(G, w, r) Q = V[G]; for each u ∈ Q key[u] = ∞; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v ∈ Adj[u] if (v ∈ Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

3

10 2 ∞

0 8 15

14 10

3

6 4 5

2

9

15

8

Page 21: Minimum Spanning Tree - George Mason Universitykosecka/cs483-001/PrimKruskal.pdf · 2016-09-30 · Minimum Spanning Tree • Problem: given a connected, undirected, weighted graph,

Prim’s AlgorithmMST-Prim(G, w, r) Q = V[G]; for each u ∈ Q key[u] = ∞; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v ∈ Adj[u] if (v ∈ Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

3

10 2 9

0 8 15

14 10

3

6 4 5

2

9

15

8

Page 22: Minimum Spanning Tree - George Mason Universitykosecka/cs483-001/PrimKruskal.pdf · 2016-09-30 · Minimum Spanning Tree • Problem: given a connected, undirected, weighted graph,

Prim’s AlgorithmMST-Prim(G, w, r) Q = V[G]; for each u ∈ Q key[u] = ∞; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v ∈ Adj[u] if (v ∈ Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

3

10 2 9

0 8 15

4

14 10

3

6 4 5

2

9

15

8

Page 23: Minimum Spanning Tree - George Mason Universitykosecka/cs483-001/PrimKruskal.pdf · 2016-09-30 · Minimum Spanning Tree • Problem: given a connected, undirected, weighted graph,

Prim’s Algorithm

MST-Prim(G, w, r) Q = V[G]; for each u ∈ Q key[u] = ∞; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v ∈ Adj[u] if (v ∈ Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

3

5 2 9

0 8 15

4

14 10

3

6 4 5

2

9

15

8

Page 24: Minimum Spanning Tree - George Mason Universitykosecka/cs483-001/PrimKruskal.pdf · 2016-09-30 · Minimum Spanning Tree • Problem: given a connected, undirected, weighted graph,

Prim’s Algorithm

MST-Prim(G, w, r) Q = V[G]; for each u ∈ Q key[u] = ∞; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v ∈ Adj[u] if (v ∈ Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

3

5 2 9

0 8 15

4

14 10

3

6 4 5

2

9

15

8

Page 25: Minimum Spanning Tree - George Mason Universitykosecka/cs483-001/PrimKruskal.pdf · 2016-09-30 · Minimum Spanning Tree • Problem: given a connected, undirected, weighted graph,

Prim’s AlgorithmMST-Prim(G, w, r) Q = V[G]; for each u ∈ Q key[u] = ∞; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v ∈ Adj[u] if (v ∈ Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

3

5 2 9

0 8 15

4

14 10

3

6 4 5

2

9

15

8

Page 26: Minimum Spanning Tree - George Mason Universitykosecka/cs483-001/PrimKruskal.pdf · 2016-09-30 · Minimum Spanning Tree • Problem: given a connected, undirected, weighted graph,

Prim’s AlgorithmMST-Prim(G, w, r) Q = V[G]; for each u ∈ Q key[u] = ∞; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v ∈ Adj[u] if (v ∈ Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

3

5 2 9

0 8 15

4

14 10

3

6 4 5

2

9

15

8

Page 27: Minimum Spanning Tree - George Mason Universitykosecka/cs483-001/PrimKruskal.pdf · 2016-09-30 · Minimum Spanning Tree • Problem: given a connected, undirected, weighted graph,

Review: Prim’s AlgorithmMST-Prim(G, w, r) Q = V[G]; for each u ∈ Q key[u] = ∞; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v ∈ Adj[u] if (v ∈ Q and w(u,v) < key[v]) p[v] = u; DecreaseKey(v, w(u,v));

Page 28: Minimum Spanning Tree - George Mason Universitykosecka/cs483-001/PrimKruskal.pdf · 2016-09-30 · Minimum Spanning Tree • Problem: given a connected, undirected, weighted graph,

Review: Prim’s AlgorithmMST-Prim(G, w, r) Q = V[G]; for each u ∈ Q key[u] = ∞; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v ∈ Adj[u] if (v ∈ Q and w(u,v) < key[v]) p[v] = u; DecreaseKey(v, w(u,v));

How often is ExtractMin() called? How often is DecreaseKey() called?

ExtractMin total number of calls O(V log V) DecreaseKey total number of calls O(E log V)

Page 29: Minimum Spanning Tree - George Mason Universitykosecka/cs483-001/PrimKruskal.pdf · 2016-09-30 · Minimum Spanning Tree • Problem: given a connected, undirected, weighted graph,

Review: Prim’s AlgorithmMST-Prim(G, w, r) Q = V[G]; for each u ∈ Q key[u] = ∞; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v ∈ Adj[u] if (v ∈ Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

What will be the running time? A: Depends on queue binary heap: O(E lg V) Fibonacci heap: O(V lg V + E)

ExtractMin total number of calls O(V log V) DecreaseKey total number of calls O(E log V) Total number of calls O(V logV +E logV) = O(E log V)Think why we can combine things in the expression above

Page 30: Minimum Spanning Tree - George Mason Universitykosecka/cs483-001/PrimKruskal.pdf · 2016-09-30 · Minimum Spanning Tree • Problem: given a connected, undirected, weighted graph,

Kruskal’s AlgorithmKruskal() { T = ∅; for each v ∈ V MakeSet(v); sort E by increasing edge weight w for each (u,v) ∈ E (in sorted order) if FindSet(u) ≠ FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); }

2 19

9

1

5

13

17 25

14 8

21

Run the algorithm:

Page 31: Minimum Spanning Tree - George Mason Universitykosecka/cs483-001/PrimKruskal.pdf · 2016-09-30 · Minimum Spanning Tree • Problem: given a connected, undirected, weighted graph,

Kruskal’s AlgorithmKruskal() { T = ∅; for each v ∈ V MakeSet(v); sort E by increasing edge weight w for each (u,v) ∈ E (in sorted order) if FindSet(u) ≠ FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); }

2 19

9

1

5

13

17 25

14 8

21

Run the algorithm:

Page 32: Minimum Spanning Tree - George Mason Universitykosecka/cs483-001/PrimKruskal.pdf · 2016-09-30 · Minimum Spanning Tree • Problem: given a connected, undirected, weighted graph,

Kruskal’s AlgorithmKruskal() { T = ∅; for each v ∈ V MakeSet(v); sort E by increasing edge weight w for each (u,v) ∈ E (in sorted order) if FindSet(u) ≠ FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); }

2 19

9

1

5

13

17 25

14 8

21

Run the algorithm:

Page 33: Minimum Spanning Tree - George Mason Universitykosecka/cs483-001/PrimKruskal.pdf · 2016-09-30 · Minimum Spanning Tree • Problem: given a connected, undirected, weighted graph,

Kruskal’s AlgorithmKruskal() { T = ∅; for each v ∈ V MakeSet(v); sort E by increasing edge weight w for each (u,v) ∈ E (in sorted order) if FindSet(u) ≠ FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); }

2 19

9

1?

5

13

17 25

14 8

21

Run the algorithm:

Page 34: Minimum Spanning Tree - George Mason Universitykosecka/cs483-001/PrimKruskal.pdf · 2016-09-30 · Minimum Spanning Tree • Problem: given a connected, undirected, weighted graph,

Kruskal’s AlgorithmKruskal() { T = ∅; for each v ∈ V MakeSet(v); sort E by increasing edge weight w for each (u,v) ∈ E (in sorted order) if FindSet(u) ≠ FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); }

2 19

9

1

5

13

17 25

14 8

21

Run the algorithm:

Page 35: Minimum Spanning Tree - George Mason Universitykosecka/cs483-001/PrimKruskal.pdf · 2016-09-30 · Minimum Spanning Tree • Problem: given a connected, undirected, weighted graph,

Kruskal’s AlgorithmKruskal() { T = ∅; for each v ∈ V MakeSet(v); sort E by increasing edge weight w for each (u,v) ∈ E (in sorted order) if FindSet(u) ≠ FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); }

2? 19

9

1

5

13

17 25

14 8

21

Run the algorithm:

Page 36: Minimum Spanning Tree - George Mason Universitykosecka/cs483-001/PrimKruskal.pdf · 2016-09-30 · Minimum Spanning Tree • Problem: given a connected, undirected, weighted graph,

Kruskal’s AlgorithmKruskal() { T = ∅; for each v ∈ V MakeSet(v); sort E by increasing edge weight w for each (u,v) ∈ E (in sorted order) if FindSet(u) ≠ FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); }

2 19

9

1

5

13

17 25

14 8

21

Run the algorithm:

Page 37: Minimum Spanning Tree - George Mason Universitykosecka/cs483-001/PrimKruskal.pdf · 2016-09-30 · Minimum Spanning Tree • Problem: given a connected, undirected, weighted graph,

Kruskal’s AlgorithmKruskal() { T = ∅; for each v ∈ V MakeSet(v); sort E by increasing edge weight w for each (u,v) ∈ E (in sorted order) if FindSet(u) ≠ FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); }

2 19

9

1

5?

13

17 25

14 8

21

Run the algorithm:

Page 38: Minimum Spanning Tree - George Mason Universitykosecka/cs483-001/PrimKruskal.pdf · 2016-09-30 · Minimum Spanning Tree • Problem: given a connected, undirected, weighted graph,

Kruskal’s AlgorithmKruskal() { T = ∅; for each v ∈ V MakeSet(v); sort E by increasing edge weight w for each (u,v) ∈ E (in sorted order) if FindSet(u) ≠ FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); }

2 19

9

1

5

13

17 25

14 8

21

Run the algorithm:

Page 39: Minimum Spanning Tree - George Mason Universitykosecka/cs483-001/PrimKruskal.pdf · 2016-09-30 · Minimum Spanning Tree • Problem: given a connected, undirected, weighted graph,

Kruskal’s AlgorithmKruskal() { T = ∅; for each v ∈ V MakeSet(v); sort E by increasing edge weight w for each (u,v) ∈ E (in sorted order) if FindSet(u) ≠ FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); }

2 19

9

1

5

13

17 25

14 8?

21

Run the algorithm:

Page 40: Minimum Spanning Tree - George Mason Universitykosecka/cs483-001/PrimKruskal.pdf · 2016-09-30 · Minimum Spanning Tree • Problem: given a connected, undirected, weighted graph,

Kruskal’s AlgorithmKruskal() { T = ∅; for each v ∈ V MakeSet(v); sort E by increasing edge weight w for each (u,v) ∈ E (in sorted order) if FindSet(u) ≠ FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); }

2 19

9

1

5

13

17 25

14 8

21

Run the algorithm:

Page 41: Minimum Spanning Tree - George Mason Universitykosecka/cs483-001/PrimKruskal.pdf · 2016-09-30 · Minimum Spanning Tree • Problem: given a connected, undirected, weighted graph,

Kruskal’s AlgorithmKruskal() { T = ∅; for each v ∈ V MakeSet(v); sort E by increasing edge weight w for each (u,v) ∈ E (in sorted order) if FindSet(u) ≠ FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); }

2 19

9?

1

5

13

17 25

14 8

21

Run the algorithm:

Page 42: Minimum Spanning Tree - George Mason Universitykosecka/cs483-001/PrimKruskal.pdf · 2016-09-30 · Minimum Spanning Tree • Problem: given a connected, undirected, weighted graph,

Kruskal’s AlgorithmKruskal() { T = ∅; for each v ∈ V MakeSet(v); sort E by increasing edge weight w for each (u,v) ∈ E (in sorted order) if FindSet(u) ≠ FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); }

2 19

9

1

5

13

17 25

14 8

21

Run the algorithm:

Page 43: Minimum Spanning Tree - George Mason Universitykosecka/cs483-001/PrimKruskal.pdf · 2016-09-30 · Minimum Spanning Tree • Problem: given a connected, undirected, weighted graph,

Kruskal’s AlgorithmKruskal() { T = ∅; for each v ∈ V MakeSet(v); sort E by increasing edge weight w for each (u,v) ∈ E (in sorted order) if FindSet(u) ≠ FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); }

2 19

9

1

5

13?

17 25

14 8

21

Run the algorithm:

Page 44: Minimum Spanning Tree - George Mason Universitykosecka/cs483-001/PrimKruskal.pdf · 2016-09-30 · Minimum Spanning Tree • Problem: given a connected, undirected, weighted graph,

Kruskal’s AlgorithmKruskal() { T = ∅; for each v ∈ V MakeSet(v); sort E by increasing edge weight w for each (u,v) ∈ E (in sorted order) if FindSet(u) ≠ FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); }

2 19

9

1

5

13

17 25

14 8

21

Run the algorithm:

Page 45: Minimum Spanning Tree - George Mason Universitykosecka/cs483-001/PrimKruskal.pdf · 2016-09-30 · Minimum Spanning Tree • Problem: given a connected, undirected, weighted graph,

Kruskal’s AlgorithmKruskal() { T = ∅; for each v ∈ V MakeSet(v); sort E by increasing edge weight w for each (u,v) ∈ E (in sorted order) if FindSet(u) ≠ FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); }

2 19

9

1

5

13

17 25

14? 8

21

Run the algorithm:

Page 46: Minimum Spanning Tree - George Mason Universitykosecka/cs483-001/PrimKruskal.pdf · 2016-09-30 · Minimum Spanning Tree • Problem: given a connected, undirected, weighted graph,

Kruskal’s AlgorithmKruskal() { T = ∅; for each v ∈ V MakeSet(v); sort E by increasing edge weight w for each (u,v) ∈ E (in sorted order) if FindSet(u) ≠ FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); }

2 19

9

1

5

13

17 25

14 8

21

Run the algorithm:

Page 47: Minimum Spanning Tree - George Mason Universitykosecka/cs483-001/PrimKruskal.pdf · 2016-09-30 · Minimum Spanning Tree • Problem: given a connected, undirected, weighted graph,

Kruskal’s AlgorithmKruskal() { T = ∅; for each v ∈ V MakeSet(v); sort E by increasing edge weight w for each (u,v) ∈ E (in sorted order) if FindSet(u) ≠ FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); }

2 19

9

1

5

13

17? 25

14 8

21

Run the algorithm:

Page 48: Minimum Spanning Tree - George Mason Universitykosecka/cs483-001/PrimKruskal.pdf · 2016-09-30 · Minimum Spanning Tree • Problem: given a connected, undirected, weighted graph,

Kruskal’s AlgorithmKruskal() { T = ∅; for each v ∈ V MakeSet(v); sort E by increasing edge weight w for each (u,v) ∈ E (in sorted order) if FindSet(u) ≠ FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); }

2 19?

9

1

5

13

17 25

14 8

21

Run the algorithm:

Page 49: Minimum Spanning Tree - George Mason Universitykosecka/cs483-001/PrimKruskal.pdf · 2016-09-30 · Minimum Spanning Tree • Problem: given a connected, undirected, weighted graph,

Kruskal’s Algorithm

Kruskal() { T = ∅; for each v ∈ V MakeSet(v); sort E by increasing edge weight w for each (u,v) ∈ E (in sorted order) if FindSet(u) ≠ FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); }

2 19

9

1

5

13

17 25

14 8

21?

Run the algorithm:

Page 50: Minimum Spanning Tree - George Mason Universitykosecka/cs483-001/PrimKruskal.pdf · 2016-09-30 · Minimum Spanning Tree • Problem: given a connected, undirected, weighted graph,

Kruskal’s AlgorithmKruskal() { T = ∅; for each v ∈ V MakeSet(v); sort E by increasing edge weight w for each (u,v) ∈ E (in sorted order) if FindSet(u) ≠ FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); }

2 19

9

1

5

13

17 25?

14 8

21

Run the algorithm:

Page 51: Minimum Spanning Tree - George Mason Universitykosecka/cs483-001/PrimKruskal.pdf · 2016-09-30 · Minimum Spanning Tree • Problem: given a connected, undirected, weighted graph,

Kruskal’s AlgorithmKruskal() { T = ∅; for each v ∈ V MakeSet(v); sort E by increasing edge weight w for each (u,v) ∈ E (in sorted order) if FindSet(u) ≠ FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); }

2 19

9

1

5

13

17 25

14 8

21

Run the algorithm:

Page 52: Minimum Spanning Tree - George Mason Universitykosecka/cs483-001/PrimKruskal.pdf · 2016-09-30 · Minimum Spanning Tree • Problem: given a connected, undirected, weighted graph,

Kruskal’s Algorithm

Kruskal() { T = ∅; for each v ∈ V MakeSet(v); sort E by increasing edge weight w for each (u,v) ∈ E (in sorted order) if FindSet(u) ≠ FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); }

2 19

9

1

5

13

17 25

14 8

21

Run the algorithm: