80
David Luebke 1 06/18/22 CS 332: Algorithms Single-Source Shortest Path

lecture 20

  • Upload
    sajinsc

  • View
    618

  • Download
    0

Embed Size (px)

Citation preview

Page 1: lecture 20

David Luebke 1 04/13/23

CS 332: Algorithms

Single-Source Shortest Path

Page 2: lecture 20

David Luebke 2 04/13/23

Review: 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);

Page 3: lecture 20

David Luebke 3 04/13/23

Review: 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);

1410

3

6 45

2

9

15

8

Run on example graph

Page 4: lecture 20

David Luebke 4 04/13/23

Review: 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);

1410

3

6 45

2

9

15

8

Run on example graph

Page 5: lecture 20

David Luebke 5 04/13/23

Review: 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);

0

1410

3

6 45

2

9

15

8

Pick a start vertex r

r

Page 6: lecture 20

David Luebke 6 04/13/23

Review: 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);

0

1410

3

6 45

2

9

15

8

Red vertices have been removed from Q

u

Page 7: lecture 20

David Luebke 7 04/13/23

Review: 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);

0

3

1410

3

6 45

2

9

15

8

Red arrows indicate parent pointers

u

Page 8: lecture 20

David Luebke 8 04/13/23

Review: 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);

14

0

3

1410

3

6 45

2

9

15

8

u

Page 9: lecture 20

David Luebke 9 04/13/23

Review: 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);

14

0

3

1410

3

6 45

2

9

15

8u

Page 10: lecture 20

David Luebke 10 04/13/23

Review: 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);

14

0 8

3

1410

3

6 45

2

9

15

8u

Page 11: lecture 20

David Luebke 11 04/13/23

Review: 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);

10

0 8

3

1410

3

6 45

2

9

15

8u

Page 12: lecture 20

David Luebke 12 04/13/23

Review: 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);

10

0 8

3

1410

3

6 45

2

9

15

8u

Page 13: lecture 20

David Luebke 13 04/13/23

Review: 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);

10 2

0 8

3

1410

3

6 45

2

9

15

8u

Page 14: lecture 20

David Luebke 14 04/13/23

Review: 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);

10 2

0 8 15

3

1410

3

6 45

2

9

15

8u

Page 15: lecture 20

David Luebke 15 04/13/23

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);

10 2

0 8 15

3

1410

3

6 45

2

9

15

8

u

Page 16: lecture 20

David Luebke 16 04/13/23

Review: 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);

10 2 9

0 8 15

3

1410

3

6 45

2

9

15

8

u

Page 17: lecture 20

David Luebke 17 04/13/23

Review: 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);

10 2 9

0 8 15

3

4

1410

3

6 45

2

9

15

8

u

Page 18: lecture 20

David Luebke 18 04/13/23

Review: 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);

5 2 9

0 8 15

3

4

1410

3

6 45

2

9

15

8

u

Page 19: lecture 20

David Luebke 19 04/13/23

Review: 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);

5 2 9

0 8 15

3

4

1410

3

6 45

2

9

15

8

u

Page 20: lecture 20

David Luebke 20 04/13/23

Review: 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);

5 2 9

0 8 15

3

4

1410

3

6 45

2

9

15

8

u

Page 21: lecture 20

David Luebke 21 04/13/23

Review: 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);

5 2 9

0 8 15

3

4

1410

3

6 45

2

9

15

8

u

Page 22: lecture 20

David Luebke 22 04/13/23

Review: 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);

5 2 9

0 8 15

3

4

1410

3

6 45

2

9

15

8

u

Page 23: lecture 20

David Luebke 23 04/13/23

Review: 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);

What is the hidden cost in this code?

Page 24: lecture 20

David Luebke 24 04/13/23

Review: 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;

DecreaseKey(v, w(u,v));

Page 25: lecture 20

David Luebke 25 04/13/23

Review: 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;

DecreaseKey(v, w(u,v));

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

Page 26: lecture 20

David Luebke 26 04/13/23

Review: 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);

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

Page 27: lecture 20

David Luebke 27 04/13/23

Single-Source Shortest Path

● Problem: given a weighted directed graph G, find the minimum-weight path from a given source vertex s to another vertex v■ “Shortest-path” = minimum weight ■ Weight of path is sum of edges■ E.g., a road map: what is the shortest path from

Chapel Hill to Charlottesville?

Page 28: lecture 20

David Luebke 28 04/13/23

Shortest Path Properties

● Again, we have optimal substructure: the shortest path consists of shortest subpaths:

■ Proof: suppose some subpath is not a shortest path○ There must then exist a shorter subpath ○ Could substitute the shorter subpath for a shorter path○ But then overall path is not shortest path. Contradiction

Page 29: lecture 20

David Luebke 29 04/13/23

Shortest Path Properties

● Define (u,v) to be the weight of the shortest path from u to v

● Shortest paths satisfy the triangle inequality: (u,v) (u,x) + (x,v)

● “Proof”: x

u v

This path is no longer than any other path

Page 30: lecture 20

David Luebke 30 04/13/23

Shortest Path Properties

● In graphs with negative weight cycles, some shortest paths will not exist (Why?):

< 0

Page 31: lecture 20

David Luebke 31 04/13/23

Relaxation

● A key technique in shortest path algorithms is relaxation■ Idea: for all v, maintain upper bound d[v] on (s,v)Relax(u,v,w) {

if (d[v] > d[u]+w) then d[v]=d[u]+w;

}

952

752

Relax

652

652

Relax

Page 32: lecture 20

David Luebke 32 04/13/23

Bellman-Ford Algorithm

BellmanFord()

for each v V d[v] = ; d[s] = 0;

for i=1 to |V|-1

for each edge (u,v) E Relax(u,v, w(u,v));

for each edge (u,v) E if (d[v] > d[u] + w(u,v))

return “no solution”;

Relax(u,v,w): if (d[v] > d[u]+w) then d[v]=d[u]+w

Initialize d[], whichwill converge to shortest-path value

Relaxation: Make |V|-1 passes, relaxing each edge

Test for solution Under what conditiondo we get a solution?

Page 33: lecture 20

David Luebke 33 04/13/23

Bellman-Ford Algorithm

BellmanFord()

for each v V d[v] = ; d[s] = 0;

for i=1 to |V|-1

for each edge (u,v) E Relax(u,v, w(u,v));

for each edge (u,v) E if (d[v] > d[u] + w(u,v))

return “no solution”;

Relax(u,v,w): if (d[v] > d[u]+w) then d[v]=d[u]+w

What will be the running time?

Page 34: lecture 20

David Luebke 34 04/13/23

Bellman-Ford Algorithm

BellmanFord()

for each v V d[v] = ; d[s] = 0;

for i=1 to |V|-1

for each edge (u,v) E Relax(u,v, w(u,v));

for each edge (u,v) E if (d[v] > d[u] + w(u,v))

return “no solution”;

Relax(u,v,w): if (d[v] > d[u]+w) then d[v]=d[u]+w

What will be the running time?

A: O(VE)

Page 35: lecture 20

David Luebke 35 04/13/23

Bellman-Ford Algorithm

BellmanFord()

for each v V d[v] = ; d[s] = 0;

for i=1 to |V|-1

for each edge (u,v) E Relax(u,v, w(u,v));

for each edge (u,v) E if (d[v] > d[u] + w(u,v))

return “no solution”;

Relax(u,v,w): if (d[v] > d[u]+w) then d[v]=d[u]+w

B

E

DC

A

-1 2

2

1-3

5

3

4

Ex: work on board

s

Page 36: lecture 20

David Luebke 36 04/13/23

Bellman-Ford

● Note that order in which edges are processed affects how quickly it converges

● Correctness: show d[v] = (s,v) after |V|-1 passes■ Lemma: d[v] (s,v) always

○ Initially true○ Let v be first vertex for which d[v] < (s,v)○ Let u be the vertex that caused d[v] to change:

d[v] = d[u] + w(u,v)○ Then d[v] < (s,v)

(s,v) (s,u) + w(u,v) (Why?) (s,u) + w(u,v) d[u] + w(u,v) (Why?)

○ So d[v] < d[u] + w(u,v). Contradiction.

Page 37: lecture 20

David Luebke 37 04/13/23

Bellman-Ford

● Prove: after |V|-1 passes, all d values correct■ Consider shortest path from s to v:

s v1 v2 v3 v4 v○ Initially, d[s] = 0 is correct, and doesn’t change (Why?)

○ After 1 pass through edges, d[v1] is correct (Why?) and doesn’t change

○ After 2 passes, d[v2] is correct and doesn’t change

○ …○ Terminates in |V| - 1 passes: (Why?) ○ What if it doesn’t?

Page 38: lecture 20

David Luebke 38 04/13/23

DAG Shortest Paths

● Problem: finding shortest paths in DAG■ Bellman-Ford takes O(VE) time. ■ How can we do better?■ Idea: use topological sort

○ If were lucky and processes vertices on each shortest path from left to right, would be done in one pass

○ Every path in a dag is subsequence of topologically sorted vertex order, so processing verts in that order, we will do each path in forward order (will never relax edges out of vert before doing all edges into vert).

○ Thus: just one pass. What will be the running time?

Page 39: lecture 20

David Luebke 39 04/13/23

Dijkstra’s Algorithm

● If no negative edge weights, we can beat BF● Similar to breadth-first search

■ Grow a tree gradually, advancing from vertices taken from a queue

● Also similar to Prim’s algorithm for MST■ Use a priority queue keyed on d[v]

Page 40: lecture 20

David Luebke 40 04/13/23

Dijkstra’s Algorithm

Dijkstra(G)

for each v V d[v] = ; d[s] = 0; S = ; Q = V; while (Q ) u = ExtractMin(Q);

S = S U {u}; for each v u->Adj[] if (d[v] > d[u]+w(u,v))

d[v] = d[u]+w(u,v);RelaxationStepNote: this

is really a call to Q->DecreaseKey()

B

C

DA

10

4 3

2

15

Ex: run the algorithm

Page 41: lecture 20

David Luebke 41 04/13/23

Dijkstra’s Algorithm

Dijkstra(G)

for each v V d[v] = ; d[s] = 0; S = ; Q = V; while (Q ) u = ExtractMin(Q);

S = S U {u}; for each v u->Adj[] if (d[v] > d[u]+w(u,v))

d[v] = d[u]+w(u,v);

How many times is ExtractMin() called?

How many times is DecraseKey() called?

What will be the total running time?

Page 42: lecture 20

David Luebke 42 04/13/23

Dijkstra’s Algorithm

Dijkstra(G)

for each v V d[v] = ; d[s] = 0; S = ; Q = V; while (Q ) u = ExtractMin(Q);

S = S U {u}; for each v u->Adj[] if (d[v] > d[u]+w(u,v))

d[v] = d[u]+w(u,v);

How many times is ExtractMin() called?

How many times is DecraseKey() called?

A: O(E lg V) using binary heap for QCan acheive O(V lg V + E) with Fibonacci heaps

Page 43: lecture 20

David Luebke 43 04/13/23

Dijkstra’s Algorithm

Dijkstra(G)

for each v V d[v] = ; d[s] = 0; S = ; Q = V; while (Q ) u = ExtractMin(Q);

S = S U{u}; for each v u->Adj[] if (d[v] > d[u]+w(u,v))

d[v] = d[u]+w(u,v);Correctness: we must show that when u is removed from Q, it has already converged

Page 44: lecture 20

David Luebke 44 04/13/23

Correctness Of Dijkstra's Algorithm

● Note that d[v] (s,v) v ● Let u be first vertex picked s.t. shorter path than d[u] d[u] > (s,u)● Let y be first vertex V-S on actual shortest path from su d[y] = (s,y)

■ Because d[x] is set correctly for y's predecessor x S on the shortest path, and■ When we put x into S, we relaxed (x,y), giving d[y] the correct value

s

xy

up2

p2

Page 45: lecture 20

David Luebke 45 04/13/23

Correctness Of Dijkstra's Algorithm

● Note that d[v] (s,v) v ● Let u be first vertex picked s.t. shorter path than d[u] d[u] > (s,u)● Let y be first vertex V-S on actual shortest path from su d[y] = (s,y)● d[u] > (s,u)

= (s,y) + (y,u) (Why?)= d[y] + (y,u) d[y] But if d[u] > d[y], wouldn't have chosen u. Contradiction.

s

xy

up2

p2

Page 46: lecture 20

David Luebke 46 04/13/23

Disjoint-Set Union Problem

● Want a data structure to support disjoint sets ■ Collection of disjoint sets S = {Si}, Si ∩ Sj =

● Need to support following operations:■ MakeSet(x): S = S U {{x}}

■ Union(Si, Sj): S = S - {Si, Sj} U {Si U Sj}

■ FindSet(X): return Si S such that x Si

● Before discussing implementation details, we look at example application: MSTs

Page 47: lecture 20

David Luebke 47 04/13/23

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));

}

Page 48: lecture 20

David Luebke 48 04/13/23

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

1725

148

21

Run the algorithm:

Page 49: lecture 20

David Luebke 49 04/13/23

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

1725

148

21

Run the algorithm:

Page 50: lecture 20

David Luebke 50 04/13/23

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

1725

148

21

Run the algorithm:

Page 51: lecture 20

David Luebke 51 04/13/23

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

1725

148

21

Run the algorithm:

Page 52: lecture 20

David Luebke 52 04/13/23

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

1725

148

21

Run the algorithm:

Page 53: lecture 20

David Luebke 53 04/13/23

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

1725

148

21

Run the algorithm:

Page 54: lecture 20

David Luebke 54 04/13/23

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

1725

148

21

Run the algorithm:

Page 55: lecture 20

David Luebke 55 04/13/23

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

1725

148

21

Run the algorithm:

Page 56: lecture 20

David Luebke 56 04/13/23

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

1725

148

21

Run the algorithm:

Page 57: lecture 20

David Luebke 57 04/13/23

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

1725

148?

21

Run the algorithm:

Page 58: lecture 20

David Luebke 58 04/13/23

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

1725

148

21

Run the algorithm:

Page 59: lecture 20

David Luebke 59 04/13/23

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

1725

148

21

Run the algorithm:

Page 60: lecture 20

David Luebke 60 04/13/23

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

1725

148

21

Run the algorithm:

Page 61: lecture 20

David Luebke 61 04/13/23

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?

1725

148

21

Run the algorithm:

Page 62: lecture 20

David Luebke 62 04/13/23

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

1725

148

21

Run the algorithm:

Page 63: lecture 20

David Luebke 63 04/13/23

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

1725

14?8

21

Run the algorithm:

Page 64: lecture 20

David Luebke 64 04/13/23

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

1725

148

21

Run the algorithm:

Page 65: lecture 20

David Luebke 65 04/13/23

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

148

21

Run the algorithm:

Page 66: lecture 20

David Luebke 66 04/13/23

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

1725

148

21

Run the algorithm:

Page 67: lecture 20

David Luebke 67 04/13/23

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

1725

148

21?

Run the algorithm:

Page 68: lecture 20

David Luebke 68 04/13/23

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

1725?

148

21

Run the algorithm:

Page 69: lecture 20

David Luebke 69 04/13/23

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

1725

148

21

Run the algorithm:

Page 70: lecture 20

David Luebke 70 04/13/23

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

1725

148

21

Run the algorithm:

Page 71: lecture 20

David Luebke 71 04/13/23

Correctness Of Kruskal’s Algorithm

● Sketch of a proof that this algorithm produces an MST for T:■ Assume algorithm is wrong: result is not an MST■ Then algorithm adds a wrong edge at some point■ If it adds a wrong edge, there must be a lower weight

edge (cut and paste argument)■ But algorithm chooses lowest weight edge at each step.

Contradiction

● Again, important to be comfortable with cut and paste arguments

Page 72: lecture 20

David Luebke 72 04/13/23

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));

}

What will affect the running time?

Page 73: lecture 20

David Luebke 73 04/13/23

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));

}

What will affect the running time? 1 Sort

O(V) MakeSet() callsO(E) FindSet() callsO(V) Union() calls

(Exactly how many Union()s?)

Page 74: lecture 20

David Luebke 74 04/13/23

Kruskal’s Algorithm: Running Time

● To summarize: ■ Sort edges: O(E lg E) ■ O(V) MakeSet()’s■ O(E) FindSet()’s■ O(V) Union()’s

● Upshot: ■ Best disjoint-set union algorithm makes above 3

operations take O(E(E,V)), almost constant■ Overall thus O(E lg E), almost linear w/o sorting

Page 75: lecture 20

David Luebke 75 04/13/23

Disjoint Set Union

● So how do we implement disjoint-set union?■ Naïve implementation: use a linked list to

represent each set:

○ MakeSet(): ??? time○ FindSet(): ??? time○ Union(A,B): “copy” elements of A into B: ??? time

Page 76: lecture 20

David Luebke 76 04/13/23

Disjoint Set Union

● So how do we implement disjoint-set union?■ Naïve implementation: use a linked list to represent each

set:

○ MakeSet(): O(1) time○ FindSet(): O(1) time○ Union(A,B): “copy” elements of A into B: O(A) time

■ How long can a single Union() take?■ How long will n Union()’s take?

Page 77: lecture 20

David Luebke 77 04/13/23

Disjoint Set Union: Analysis

● Worst-case analysis: O(n2) time for n Union’sUnion(S1, S2) “copy” 1 element

Union(S2, S3) “copy” 2 elements

Union(Sn-1, Sn) “copy” n-1 elements

O(n2)

● Improvement: always copy smaller into larger■ Why will this make things better?■ What is the worst-case time of Union()?

● But now n Union’s take only O(n lg n) time!

Page 78: lecture 20

David Luebke 78 04/13/23

Amortized Analysis of Disjoint Sets

● Amortized analysis computes average times without using probability

● With our new Union(), any individual element is copied at most lg n times when forming the complete set from 1-element sets■ Worst case: Each time copied, element in smaller set

1st time resulting set size 2

2nd time 4

(lg n)th time n

Page 79: lecture 20

David Luebke 79 04/13/23

Amortized Analysis of Disjoint Sets

● Since we have n elements each copied at most lg n times, n Union()’s takes O(n lg n) time

● We say that each Union() takes O(lg n) amortized time■ Financial term: imagine paying $(lg n) per Union■ At first we are overpaying; initial Union $O(1)■ But we accumulate enough $ in bank to pay for later

expensive O(n) operation. ■ Important: amount in bank never goes negative

Page 80: lecture 20

David Luebke 80 04/13/23

The End