1 2/23/2016 ITCS 6114 Topological Sort Minimum Spanning Trees.

38
1 06/19/2 ITCS 6114 Topological Sort Minimum Spanning Trees

description

3 2/23/2016 DFS and DAGs ● Argue that a directed graph G is acyclic iff a DFS of G yields no back edges: ■ Forward: if G is acyclic, will be no back edges ○ Trivial: a back edge implies a cycle ■ Backward: if no back edges, G is acyclic ○ Argue contrapositive: G has a cycle   a back edge  Let v be the vertex on the cycle first discovered, and u be the predecessor of v on the cycle  When v discovered, whole cycle is white  Must visit everything reachable from v before returning from DFS-Visit()  So path from u  v is yellow  yellow, thus (u, v) is a back edge

Transcript of 1 2/23/2016 ITCS 6114 Topological Sort Minimum Spanning Trees.

Page 1: 1 2/23/2016 ITCS 6114 Topological Sort Minimum Spanning Trees.

1 05/05/23

ITCS 6114

Topological SortMinimum Spanning Trees

Page 2: 1 2/23/2016 ITCS 6114 Topological Sort Minimum Spanning Trees.

2 05/05/23

Directed Acyclic Graphs

● A directed acyclic graph or DAG is a directed graph with no directed cycles:

Page 3: 1 2/23/2016 ITCS 6114 Topological Sort Minimum Spanning Trees.

3 05/05/23

DFS and DAGs

● Argue that a directed graph G is acyclic iff a DFS of G yields no back edges:■ Forward: if G is acyclic, will be no back edges

○ Trivial: a back edge implies a cycle■ Backward: if no back edges, G is acyclic

○ Argue contrapositive: G has a cycle a back edge Let v be the vertex on the cycle first discovered, and u be the predecessor

of v on the cycle When v discovered, whole cycle is white Must visit everything reachable from v before returning from DFS-Visit() So path from uv is yellowyellow, thus (u, v) is a back edge

Page 4: 1 2/23/2016 ITCS 6114 Topological Sort Minimum Spanning Trees.

4 05/05/23

Topological Sort

● Topological sort of a DAG:■ Linear ordering of all vertices in graph G such that

vertex u comes before vertex v if edge (u, v) G● Real-world example: getting dressed

Page 5: 1 2/23/2016 ITCS 6114 Topological Sort Minimum Spanning Trees.

5 05/05/23

Getting Dressed

Underwear Socks

ShoesPants

Belt

Shirt

Watch

Tie

Jacket

Page 6: 1 2/23/2016 ITCS 6114 Topological Sort Minimum Spanning Trees.

6 05/05/23

Getting Dressed

Underwear Socks

ShoesPants

Belt

Shirt

Watch

Tie

Jacket

Socks Underwear Pants Shoes Watch Shirt Belt Tie Jacket

Page 7: 1 2/23/2016 ITCS 6114 Topological Sort Minimum Spanning Trees.

Topological Sort Algorithm

Topological-Sort(){

Run DFSWhen a vertex is finished, output itVertices are output in reverse topological order

}

● Time: O(V+E)● Correctness: Want to prove that

(u,v) G uf > vf

Page 8: 1 2/23/2016 ITCS 6114 Topological Sort Minimum Spanning Trees.

8 05/05/23

Correctness of Topological Sort

● Claim: (u,v) G uf > vf■ When (u,v) is explored, u is yellow

○ v = yellow (u,v) is back edge. Contradiction (Why?)○ v = white v becomes descendent of u vf < uf

(since must finish v before backtracking and finishing u)

○ v = black v already finished vf < uf

Page 9: 1 2/23/2016 ITCS 6114 Topological Sort Minimum Spanning Trees.

9 05/05/23

Minimum Spanning Tree

● Problem: given a connected, undirected, weighted graph:

1410

3

6 45

2

9

15

8

Page 10: 1 2/23/2016 ITCS 6114 Topological Sort Minimum Spanning Trees.

10 05/05/23

Minimum Spanning Tree

● Problem: given a connected, undirected, weighted graph, find a spanning tree using edges that minimize the total weight

1410

3

6 45

2

9

15

8

Page 11: 1 2/23/2016 ITCS 6114 Topological Sort Minimum Spanning Trees.

11 05/05/23

Minimum Spanning Tree

● Which edges form the minimum spanning tree (MST) of the below graph?

H B C

G E D

F

A

1410

3

6 45

2

9

15

8

Page 12: 1 2/23/2016 ITCS 6114 Topological Sort Minimum Spanning Trees.

12 05/05/23

Minimum Spanning Tree

● Answer:

H B C

G E D

F

A

1410

3

6 45

2

9

15

8

Page 13: 1 2/23/2016 ITCS 6114 Topological Sort Minimum Spanning Trees.

13 05/05/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);

Page 14: 1 2/23/2016 ITCS 6114 Topological Sort Minimum Spanning Trees.

14 05/05/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);

14 10

3

6 45

2

9

15

8

Run on example graph

Page 15: 1 2/23/2016 ITCS 6114 Topological Sort Minimum Spanning Trees.

15 05/05/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);

14 10

3

6 45

2

9

15

8

Run on example graph

Page 16: 1 2/23/2016 ITCS 6114 Topological Sort Minimum Spanning Trees.

16 05/05/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);

0

14 10

3

6 45

2

9

15

8

Pick a start vertex r

r

Page 17: 1 2/23/2016 ITCS 6114 Topological Sort Minimum Spanning Trees.

17 05/05/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);

0

14 10

3

6 45

2

9

15

8

Red vertices have been removed from Q

u

Page 18: 1 2/23/2016 ITCS 6114 Topological Sort Minimum Spanning Trees.

18 05/05/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);

0

3

14 10

3

6 45

2

9

15

8

Red arrows indicate parent pointers

u

Page 19: 1 2/23/2016 ITCS 6114 Topological Sort Minimum Spanning Trees.

19 05/05/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);

14

0

3

14 10

3

6 45

2

9

15

8

u

Page 20: 1 2/23/2016 ITCS 6114 Topological Sort Minimum Spanning Trees.

20 05/05/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);

14

0

3

14 10

3

6 45

2

9

15

8u

Page 21: 1 2/23/2016 ITCS 6114 Topological Sort Minimum Spanning Trees.

21 05/05/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);

14

0 8

3

14 10

3

6 45

2

9

15

8u

Page 22: 1 2/23/2016 ITCS 6114 Topological Sort Minimum Spanning Trees.

22 05/05/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

0 8

3

14 10

3

6 45

2

9

15

8u

Page 23: 1 2/23/2016 ITCS 6114 Topological Sort Minimum Spanning Trees.

23 05/05/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

0 8

3

14 10

3

6 45

2

9

15

8u

Page 24: 1 2/23/2016 ITCS 6114 Topological Sort Minimum Spanning Trees.

24 05/05/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

3

14 10

3

6 45

2

9

15

8u

Page 25: 1 2/23/2016 ITCS 6114 Topological Sort Minimum Spanning Trees.

25 05/05/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

14 10

3

6 45

2

9

15

8u

Page 26: 1 2/23/2016 ITCS 6114 Topological Sort Minimum Spanning Trees.

26 05/05/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

14 10

3

6 45

2

9

15

8

u

Page 27: 1 2/23/2016 ITCS 6114 Topological Sort Minimum Spanning Trees.

27 05/05/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 9

0 8 15

3

14 10

3

6 45

2

9

15

8

u

Page 28: 1 2/23/2016 ITCS 6114 Topological Sort Minimum Spanning Trees.

28 05/05/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 9

0 8 15

3

4

14 10

3

6 45

2

9

15

8

u

Page 29: 1 2/23/2016 ITCS 6114 Topological Sort Minimum Spanning Trees.

29 05/05/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);

5 2 9

0 8 15

3

4

14 10

3

6 45

2

9

15

8

u

Page 30: 1 2/23/2016 ITCS 6114 Topological Sort Minimum Spanning Trees.

e 30 05/05/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);

5 2 9

0 8 15

3

4

14 10

3

6 45

2

9

15

8

u

Page 31: 1 2/23/2016 ITCS 6114 Topological Sort Minimum Spanning Trees.

31 05/05/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);

5 2 9

0 8 15

3

4

14 10

3

6 45

2

9

15

8

u

Page 32: 1 2/23/2016 ITCS 6114 Topological Sort Minimum Spanning Trees.

32 05/05/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);

5 2 9

0 8 15

3

4

14 10

3

6 45

2

9

15

8

u

Page 33: 1 2/23/2016 ITCS 6114 Topological Sort Minimum Spanning Trees.

33 05/05/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);

5 2 9

0 8 15

3

4

14 10

3

6 45

2

9

15

8

u

Page 34: 1 2/23/2016 ITCS 6114 Topological Sort Minimum Spanning Trees.

34 05/05/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 35: 1 2/23/2016 ITCS 6114 Topological Sort Minimum Spanning Trees.

35 05/05/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 36: 1 2/23/2016 ITCS 6114 Topological Sort Minimum Spanning Trees.

ke 36 05/05/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 37: 1 2/23/2016 ITCS 6114 Topological Sort Minimum Spanning Trees.

37 05/05/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 38: 1 2/23/2016 ITCS 6114 Topological Sort Minimum Spanning Trees.

38 05/05/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?