CSE 373 Data Structures and Algorithms Lecture 23

  • Slides: 76
Download presentation
CSE 373 Data Structures and Algorithms Lecture 23: Graphs V Sumber dari : https:

CSE 373 Data Structures and Algorithms Lecture 23: Graphs V Sumber dari : https: //courses. cs. washington. edu/courses/cse 373/12 sp/lectures/0521 -graphs 5/23 -graphs 5. ppt.

Topological Sort 142 312 143 Problem: Find an order in which all these courses

Topological Sort 142 312 143 Problem: Find an order in which all these courses can be taken. 311 332 351 Example: 142 143 331 351 311 332 312 421 401 In order to take a course, you must take all of its prerequisites first 2 421 401

Topological Sort In G = (V, E), find total ordering of vertices such that

Topological Sort In G = (V, E), find total ordering of vertices such that for any edge (v, w), v precedes w in the ordering B C A F D 3 E

Topological Sort: Good Example Any total ordering in which all the arrows go to

Topological Sort: Good Example Any total ordering in which all the arrows go to the right is a valid solution B C A F D E A B F C D Note that F can go anywhere in this list because it is not connected. Also the solution is not unique. 4 E

Topological Sort: Bad Example Any ordering in which an arrow goes to the left

Topological Sort: Bad Example Any ordering in which an arrow goes to the left is not a valid solution B C A F D E A B F C E NO! 5 D

Only acyclic graphs can be topo sorted A directed graph with a cycle cannot

Only acyclic graphs can be topo sorted A directed graph with a cycle cannot be topologically sorted. B C A F D 6 E

Topological Sort Algorithm: Step 1: Identify vertices that have no incoming edges The “in-degree”

Topological Sort Algorithm: Step 1: Identify vertices that have no incoming edges The “in-degree” of these vertices is zero B A C F D 7 E

Topological Sort Algorithm: Step 1 a Step 1: Identify vertices that have no incoming

Topological Sort Algorithm: Step 1 a Step 1: Identify vertices that have no incoming edges If no such vertices, graph has cycle(s) Topological sort not possible – Halt. B A D 8 C Example of a cyclic graph

Topological Sort Algorithm: Step 1 b Step 1: Identify vertices that have no incoming

Topological Sort Algorithm: Step 1 b Step 1: Identify vertices that have no incoming edges Select one such vertex Select B A C F D 9 E

Topological Sort Algorithm: Step 2: Delete this vertex of in-degree 0 and all its

Topological Sort Algorithm: Step 2: Delete this vertex of in-degree 0 and all its outgoing edges from the graph. Place it in the output. B A C F D 10 E A

Topological Sort Algorithm: Repeat Step 1 and Step 2 until graph is empty Select

Topological Sort Algorithm: Repeat Step 1 and Step 2 until graph is empty Select B C F D 11 E A

B Select B. Copy to sorted list. Delete B and its edges. B C

B Select B. Copy to sorted list. Delete B and its edges. B C F D 12 E A B

C Select C. Copy to sorted list. Delete C and its edges. C F

C Select C. Copy to sorted list. Delete C and its edges. C F D 13 E A B C

D Select D. Copy to sorted list. Delete D and its edges. F D

D Select D. Copy to sorted list. Delete D and its edges. F D 14 E A B C D

E, F Select E. Copy to sorted list. Delete E and its edges. Select

E, F Select E. Copy to sorted list. Delete E and its edges. Select F. Copy to sorted list. Delete F and its edges. F E 15 A B C D E F

Done B A C F D E A B C D 16 E F

Done B A C F D E A B C D 16 E F

Topological Sort Algorithm Store each vertex’s In-Degree in an hash table D Initialize queue

Topological Sort Algorithm Store each vertex’s In-Degree in an hash table D Initialize queue with all “in-degree=0” vertices While there are vertices remaining in the queue: 1. 2. 3. a) b) c) 4. 17 Dequeue and output a vertex Reduce In-Degree of all vertices adjacent to it by 1 Enqueue any of these vertices whose In-Degree became zero If all vertices are output then success, otherwise there is a cycle.

Pseudocode Initialize D // Mapping of vertex to its in-degree Queue Q : =

Pseudocode Initialize D // Mapping of vertex to its in-degree Queue Q : = [Vertices with in-degree 0] while not. Empty(Q) do x : = Dequeue(Q) Output(x) y : = A[x]; // y gets a linked list of adjacent vertices while y null do D[y. value] : = D[y. value] – 1; if D[y. value] = 0 then Enqueue(Q, y. value); y : = y. next; endwhile 18

Topological Sort with Queue (before): Queue (after): 1, 6 1 1 2 0 1

Topological Sort with Queue (before): Queue (after): 1, 6 1 1 2 0 1 2 2 4 Answer: 19 0 3 6 5

Topological Sort with Queue (before): 1, 6 Queue (after): 6, 2 0 1 1

Topological Sort with Queue (before): 1, 6 Queue (after): 6, 2 0 1 1 2 4 Answer: 1 20 0 3 6 5

Topological Sort with Queue (before): 6, 2 Queue (after): 2 0 1 1 1

Topological Sort with Queue (before): 6, 2 Queue (after): 2 0 1 1 1 2 4 Answer: 1, 6 21 0 3 6 5

Topological Sort with Queue (before): 2 Queue (after): 3 0 2 0 1 1

Topological Sort with Queue (before): 2 Queue (after): 3 0 2 0 1 1 4 0 2 Answer: 1, 6, 2 22 0 3 6 5

Topological Sort with Queue (before): 3 Queue (after): 4 0 2 0 1 0

Topological Sort with Queue (before): 3 Queue (after): 4 0 2 0 1 0 4 0 0 3 1 6 5 Answer: 1, 6, 2, 3 23

Topological Sort with Queue (before): 4 Queue (after): 5 0 2 0 1 0

Topological Sort with Queue (before): 4 Queue (after): 5 0 2 0 1 0 4 0 0 3 0 6 5 Answer: 1, 6, 2, 3, 4 24

Topological Sort with Queue (before): 5 Queue (after): 0 2 0 1 0 4

Topological Sort with Queue (before): 5 Queue (after): 0 2 0 1 0 4 0 0 3 0 6 5 Answer: 1, 6, 2, 3, 4, 5 25

Topological Sort Fails (cycle) Queue (before): Queue (after): 1 1 2 2 0 1

Topological Sort Fails (cycle) Queue (before): Queue (after): 1 1 2 2 0 1 3 2 1 4 Answer: 26 5

Topological Sort Fails (cycle) Queue (before): 1 Queue (after): 2 0 2 2 0

Topological Sort Fails (cycle) Queue (before): 1 Queue (after): 2 0 2 2 0 1 3 1 1 4 Answer: 1 27 5

Topological Sort Fails (cycle) Queue (before): 2 Queue (after): 0 2 0 1 1

Topological Sort Fails (cycle) Queue (before): 2 Queue (after): 0 2 0 1 1 3 1 1 4 Answer: 1, 2 28 5

Topological Sort Runtime? Initialize D // Mapping of vertex to its in-degree Queue Q

Topological Sort Runtime? Initialize D // Mapping of vertex to its in-degree Queue Q : = [Vertices with in-degree 0] while not. Empty(Q) do x : = Dequeue(Q) Output(x) y : = A[x]; // y gets a linked list of adjacent vertices while y null do D[y. value] : = D[y. value] – 1; if D[y. value] = 0 then Enqueue(Q, y. value); y : = y. next; endwhile 29

Topological Sort Analysis Initialize In-Degree map: O(|V| + |E|) Initialize Queue with In-Degree 0

Topological Sort Analysis Initialize In-Degree map: O(|V| + |E|) Initialize Queue with In-Degree 0 vertices: O(|V|) Dequeue and output vertex: Reduce In-Degree of all vertices adjacent to a vertex and Enqueue any In-Degree 0 vertices: |V| vertices, each takes only O(1) to dequeue and output: O(|V|) O(|E|) Runtime = O(|V| + |E|) 30 Linear!

Minimum Spanning Tree tree: a connected, directed acyclic graph spanning tree: a subgraph of

Minimum Spanning Tree tree: a connected, directed acyclic graph spanning tree: a subgraph of a graph, which meets the constraints to be a tree (connected, acyclic) and connects every vertex of the original graph minimum spanning tree: a spanning tree with weight less than or equal to any other spanning tree for the given graph 31

Minimum Spanning Tree: Applications Consider a cable TV company laying cable to a new

Minimum Spanning Tree: Applications Consider a cable TV company laying cable to a new neighborhood Can only bury the cable only along certain paths Some of paths may be more expensive (i. e. longer, harder to install) A spanning tree for that graph would be a subset of those paths that has no cycles but still connects to every house. Similar situations 32 Installing electrical wiring in a house Installing computer networks between cities Building roads between neighborhoods

Spanning Tree Problem Input: An undirected graph G = (V, E). G is connected.

Spanning Tree Problem Input: An undirected graph G = (V, E). G is connected. Output: T subset of E such that 33 (V, T) is a connected graph (V, T) has no cycles

Spanning Tree Psuedocode spanning. Tree(): pick random vertex v. T : = {} spanning.

Spanning Tree Psuedocode spanning. Tree(): pick random vertex v. T : = {} spanning. Tree(v, T) return T. spanning. Tree(v, T): mark v as visited. for each neighbor vi of v where there is an edge from v: if vi is not visited add edge (v, vi) to T. spanning. Tree(vi, T) return T. 34

Example of Depth First Search ST(1) 2 1 3 7 4 6 5 35

Example of Depth First Search ST(1) 2 1 3 7 4 6 5 35

Example Step 2 ST(1) ST(2) 2 1 3 7 4 6 5 {1, 2}

Example Step 2 ST(1) ST(2) 2 1 3 7 4 6 5 {1, 2} 36

Example Step 3 2 1 3 7 4 6 5 {1, 2} {2, 7}

Example Step 3 2 1 3 7 4 6 5 {1, 2} {2, 7} 37 ST(1) ST(2) ST(7)

Example Step 4 2 1 3 7 4 6 5 {1, 2} {2, 7}

Example Step 4 2 1 3 7 4 6 5 {1, 2} {2, 7} {7, 5} 38 ST(1) ST(2) ST(7) ST(5)

Example Step 5 2 1 3 7 4 6 5 {1, 2} {2, 7}

Example Step 5 2 1 3 7 4 6 5 {1, 2} {2, 7} {7, 5} {5, 4} 39 ST(1) ST(2) ST(7) ST(5) ST(4)

Example Step 6 2 1 3 7 4 6 5 {1, 2} {2, 7}

Example Step 6 2 1 3 7 4 6 5 {1, 2} {2, 7} {7, 5} {5, 4} {4, 3} 40 ST(1) ST(2) ST(7) ST(5) ST(4) ST(3)

Example Step 7 2 1 3 7 4 6 5 {1, 2} {2, 7}

Example Step 7 2 1 3 7 4 6 5 {1, 2} {2, 7} {7, 5} {5, 4} {4, 3} 41 ST(1) ST(2) ST(7) ST(5) ST(4) ST(3)

Example Step 8 2 1 3 7 4 6 5 {1, 2} {2, 7}

Example Step 8 2 1 3 7 4 6 5 {1, 2} {2, 7} {7, 5} {5, 4} {4, 3} 42 ST(1) ST(2) ST(7) ST(5) ST(4)

Example Step 9 2 1 3 7 4 6 5 {1, 2} {2, 7}

Example Step 9 2 1 3 7 4 6 5 {1, 2} {2, 7} {7, 5} {5, 4} {4, 3} 43 ST(1) ST(2) ST(7) ST(5) ST(4)

Example Step 10 2 1 3 7 4 6 5 {1, 2} {2, 7}

Example Step 10 2 1 3 7 4 6 5 {1, 2} {2, 7} {7, 5} {5, 4} {4, 3} 44 ST(1) ST(2) ST(7) ST(5)

Example Step 11 2 1 3 7 4 6 5 {1, 2} {2, 7}

Example Step 11 2 1 3 7 4 6 5 {1, 2} {2, 7} {7, 5} {5, 4} {4, 3} {5, 6} 45 ST(1) ST(2) ST(7) ST(5) ST(6)

Example Step 12 2 1 3 7 4 6 5 {1, 2} {2, 7}

Example Step 12 2 1 3 7 4 6 5 {1, 2} {2, 7} {7, 5} {5, 4} {4, 3} {5, 6} 46 ST(1) ST(2) ST(7) ST(5) ST(6)

Example Step 13 2 1 3 7 4 6 5 {1, 2} {2, 7}

Example Step 13 2 1 3 7 4 6 5 {1, 2} {2, 7} {7, 5} {5, 4} {4, 3} {5, 6} 47 ST(1) ST(2) ST(7) ST(5)

Example Step 14 2 1 3 7 4 6 5 {1, 2} {2, 7}

Example Step 14 2 1 3 7 4 6 5 {1, 2} {2, 7} {7, 5} {5, 4} {4, 3} {5, 6} 48 ST(1) ST(2) ST(7)

Example Step 15 ST(1) ST(2) 2 1 3 7 4 6 5 {1, 2}

Example Step 15 ST(1) ST(2) 2 1 3 7 4 6 5 {1, 2} {2, 7} {7, 5} {5, 4} {4, 3} {5, 6} 49

Example Step 16 ST(1) 2 1 3 7 4 6 5 {1, 2} {2,

Example Step 16 ST(1) 2 1 3 7 4 6 5 {1, 2} {2, 7} {7, 5} {5, 4} {4, 3} {5, 6} 50

Minimum Spanning Tree Problem Input: Undirected Graph G = (V, E) and a cost

Minimum Spanning Tree Problem Input: Undirected Graph G = (V, E) and a cost function C from E to non-negative real numbers. C(e) is the cost of edge e. Output: A spanning tree T with minimum total cost. That is: T that minimizes 51

Observations About Spanning Trees For any spanning tree T, inserting an edge enew not

Observations About Spanning Trees For any spanning tree T, inserting an edge enew not in T creates a cycle But removing any edge eold from the cycle gives back a spanning tree 52 If enew has a lower cost than eold, we have progressed!

Find the MST 1 A B 7 2 11 12 9 C 10 53

Find the MST 1 A B 7 2 11 12 9 C 10 53 H 5 3 D 6 F 13 4 G 4 E

Two Different Approaches 54 Prim’s Algorithm Kruskals’ Algorithm Looks familiar! Completely different!

Two Different Approaches 54 Prim’s Algorithm Kruskals’ Algorithm Looks familiar! Completely different!

Prim’s Algorithm Idea: Grow a tree by adding an edge from the “known” vertices

Prim’s Algorithm Idea: Grow a tree by adding an edge from the “known” vertices to the “unknown” vertices. Pick the edge with the smallest weight. G v known 55

Prim’s Algorithm Starting from empty T, choose a vertex at random and initialize V

Prim’s Algorithm Starting from empty T, choose a vertex at random and initialize V = {A}, T ={} A 10 5 1 8 B 1 C 3 D 6 1 4 F 56 2 E 6 G

Prim’s Algorithm Choose vertex u not in V such that edge weight from u

Prim’s Algorithm Choose vertex u not in V such that edge weight from u to a vertex in V is minimal (greedy!) A V = {A, C} 10 5 1 T = { (A, C) } 8 B 1 C 3 D 6 1 4 F 57 2 E 6 G

Prim’s Algorithm Repeat until all vertices have been chosen V = {A, C, D}

Prim’s Algorithm Repeat until all vertices have been chosen V = {A, C, D} A 10 T= { (A, C), (C, D)} 5 1 8 B 1 C 3 D 6 1 4 F 58 2 E 6 G

Prim’s Algorithm V = {A, C, D, E} T = { (A, C), (C,

Prim’s Algorithm V = {A, C, D, E} T = { (A, C), (C, D), (D, E) } A 10 5 1 8 B 1 C 3 D 6 1 4 F 59 2 E 6 G

Prim’s Algorithm V = {A, C, D, E, B} T = { (A, C),

Prim’s Algorithm V = {A, C, D, E, B} T = { (A, C), (C, D), (D, E), (E, B) } A 10 5 1 8 B 1 C 3 D 6 1 4 F 60 2 E 6 G

Prim’s Algorithm V = {A, C, D, E, B, F} T = { (A,

Prim’s Algorithm V = {A, C, D, E, B, F} T = { (A, C), (C, D), (D, E), (E, B), (B, F) }A 10 5 1 8 B 1 C 3 D 6 1 4 F 61 2 E 6 G

Prim’s Algorithm V = {A, C, D, E, B, F, G} T = {

Prim’s Algorithm V = {A, C, D, E, B, F, G} T = { (A, C), (C, D), (D, E), A 10 (E, B), (B, F), (E, G) } 1 8 B 1 5 C 3 D 6 1 4 F 62 2 E 6 G

Prim’s Algorithm Final Cost: 1 + 3 + 4 + 1 + 6 =

Prim’s Algorithm Final Cost: 1 + 3 + 4 + 1 + 6 = 16 A 10 5 1 8 B 1 C 3 D 6 1 4 F 63 2 E 6 G

Prim’s Algorithm Analysis How is it different from Djikstra's algorithm? If the step that

Prim’s Algorithm Analysis How is it different from Djikstra's algorithm? If the step that removes unknown vertex with minimum distance is done with binary heap, the running time is: O(|E|log |V|) 64

Kruskal’s MST Algorithm Idea: Grow a forest out of edges that do not create

Kruskal’s MST Algorithm Idea: Grow a forest out of edges that do not create a cycle. Pick an edge with the smallest weight. G=(V, E) v 65

Example of Kruskal 1 1 3 2 1 3 3 4 7 3 3

Example of Kruskal 1 1 3 2 1 3 3 4 7 3 3 0 1 6 2 5 4 2 {7, 4} {2, 1} {7, 5} {5, 6} {5, 4} {1, 6} {2, 7} {2, 3} {3, 4} {1, 5} 0 1 1 2 2 3 3 4 66

Example of Kruskal 2 1 3 3 4 7 3 3 0 1 6

Example of Kruskal 2 1 3 3 4 7 3 3 0 1 6 2 5 4 2 {7, 4} {2, 1} {7, 5} {5, 6} {5, 4} {1, 6} {2, 7} {2, 3} {3, 4} {1, 5} 0 1 1 2 2 3 3 4 67

Example of Kruskal 2 1 3 3 4 7 3 3 0 1 6

Example of Kruskal 2 1 3 3 4 7 3 3 0 1 6 2 5 4 2 {7, 4} {2, 1} {7, 5} {5, 6} {5, 4} {1, 6} {2, 7} {2, 3} {3, 4} {1, 5} 0 1 1 2 2 3 3 4 68

Example of Kruskal 3 1 3 2 1 3 3 4 7 3 3

Example of Kruskal 3 1 3 2 1 3 3 4 7 3 3 0 1 6 2 5 4 2 {7, 4} {2, 1} {7, 5} {5, 6} {5, 4} {1, 6} {2, 7} {2, 3} {3, 4} {1, 5} 0 1 1 2 2 3 3 4 69

Example of Kruskal 4 1 3 2 1 3 3 4 7 3 3

Example of Kruskal 4 1 3 2 1 3 3 4 7 3 3 0 1 6 2 5 4 2 {7, 4} {2, 1} {7, 5} {5, 6} {5, 4} {1, 6} {2, 7} {2, 3} {3, 4} {1, 5} 0 1 1 2 2 3 3 4 70

Example of Kruskal 5 1 3 2 1 3 3 4 7 3 3

Example of Kruskal 5 1 3 2 1 3 3 4 7 3 3 0 1 6 2 5 4 2 {7, 4} {2, 1} {7, 5} {5, 6} {5, 4} {1, 6} {2, 7} {2, 3} {3, 4} {1, 5} 0 1 1 2 2 3 3 4 71

Example of Kruskal 6 1 3 2 1 3 3 4 7 3 3

Example of Kruskal 6 1 3 2 1 3 3 4 7 3 3 0 1 6 2 5 4 2 {7, 4} {2, 1} {7, 5} {5, 6} {5, 4} {1, 6} {2, 7} {2, 3} {3, 4} {1, 5} 0 1 1 2 2 3 3 4 72

Example of Kruskal 7 1 3 2 1 3 3 4 7 3 3

Example of Kruskal 7 1 3 2 1 3 3 4 7 3 3 0 1 6 2 5 4 2 {7, 4} {2, 1} {7, 5} {5, 6} {5, 4} {1, 6} {2, 7} {2, 3} {3, 4} {1, 5} 0 1 1 2 2 3 3 4 73

Example of Kruskal 7 1 3 2 1 3 3 4 7 3 3

Example of Kruskal 7 1 3 2 1 3 3 4 7 3 3 0 1 6 2 5 4 2 {7, 4} {2, 1} {7, 5} {5, 6} {5, 4} {1, 6} {2, 7} {2, 3} {3, 4} {1, 5} 0 1 1 2 2 3 3 4 74

Example of Kruskal 8, 9 1 3 2 1 3 3 4 7 3

Example of Kruskal 8, 9 1 3 2 1 3 3 4 7 3 3 0 1 6 2 5 4 2 {7, 4} {2, 1} {7, 5} {5, 6} {5, 4} {1, 6} {2, 7} {2, 3} {3, 4} {1, 5} 0 1 1 2 2 3 3 4 75

Kruskal's Algorithm Implementation Kruskals(): sort edges in increasing order of length (e 1, e

Kruskal's Algorithm Implementation Kruskals(): sort edges in increasing order of length (e 1, e 2, e 3, . . . , em). T : = {}. for i = 1 to m if ei does not add a cycle: add ei to T. return T. How can we determine that adding ei to T won't add a cycle? 76