CSE 373 Data Structures and Algorithms

Slides:



Advertisements
Similar presentations
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 13 Minumum spanning trees Motivation Properties of minimum spanning trees Kruskal’s.
Advertisements

Graph Algorithms: Minimum Spanning Tree We are given a weighted, undirected graph G = (V, E), with weight function w:
Directed Graph Algorithms CSE 373 Data Structures Lecture 14.
CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1.
CSE 326: Data Structures Spanning Trees Ben Lerner Summer 2007.
1 Minimum Spanning Trees Longin Jan Latecki Temple University based on slides by David Matuszek, UPenn, Rose Hoberman, CMU, Bing Liu, U. of Illinois, Boting.
Minimum Spanning Trees
1 Minimum Spanning Trees Longin Jan Latecki Temple University based on slides by David Matuszek, UPenn, Rose Hoberman, CMU, Bing Liu, U. of Illinois, Boting.
Minimum Spanning Trees What is a MST (Minimum Spanning Tree) and how to find it with Prim’s algorithm and Kruskal’s algorithm.
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms: Minimum.
Theory of Computing Lecture 10 MAS 714 Hartmut Klauck.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Graph Dr. Bernard Chen Ph.D. University of Central Arkansas.
Fundamentals, Terminology, Traversal, Algorithms Graph Algorithms Telerik Algo Academy
2IL05 Data Structures Fall 2007 Lecture 13: Minimum Spanning Trees.
Spring 2015 Lecture 11: Minimum Spanning Trees
Spanning Trees CSIT 402 Data Structures II 1. 2 Two Algorithms Prim: (build tree incrementally) – Pick lower cost edge connected to known (incomplete)
1 Minimum Spanning Trees (some material adapted from slides by Peter Lee, Ananda Guna, Bettina Speckmann)
Topological Sort: Definition
Minimum Spanning Trees CSE 373 Data Structures Lecture 21.
CSC2100B Tutorial 10 Graph Jianye Hao.
Graph Revisited Fei Chen CSCI2100B Data Structures Tutorial 12 1.
CSE 421 Algorithms Richard Anderson Autumn 2015 Lecture 5.
WEEK 12 Graphs IV Minimum Spanning Tree Algorithms.
Lecture 12 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
CSE 373: Data Structures and Algorithms Lecture 21: Graphs V 1.
Graph Search Applications, Minimum Spanning Tree
Topological Sorting.
Instructor: Lilian de Greef Quarter: Summer 2017
COMP108 Algorithmic Foundations Greedy methods
Introduction to Algorithms
May 12th – Minimum Spanning Trees
Fundamentals, Terminology, Traversal, Algorithms
Minimum Spanning Trees
Lecture 12 Algorithm Analysis
CISC 235: Topic 10 Graph Algorithms.
Graph Algorithm.
Minimum-Cost Spanning Tree
Minimum Spanning Tree.
Minimum Spanning Tree.
CSE373: Data Structures & Algorithms Lecture 12: Minimum Spanning Trees Catie Baker Spring 2015.
CSE373: Data Structures & Algorithms Lecture 20: Minimum Spanning Trees Linda Shapiro Spring 2016.
Topological Sort CSE 373 Data Structures Lecture 19.
Data Structures – LECTURE 13 Minumum spanning trees
Graphs.
Minimum Spanning Trees
Minimum Spanning Tree Section 7.3: Examples {1,2,3,4}
Lecture 12 Algorithm Analysis
Directed Graph Algorithms
Directed Graph Algorithms
Richard Anderson Autumn 2016 Lecture 5
CSE332: Data Abstractions Lecture 18: Minimum Spanning Trees
CSCI2100 Data Structures Tutorial
Minimum Spanning Trees
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Minimum Spanning Tree.
Minimum Spanning Trees
CSE 373: Data Structures and Algorithms
CSE 417: Algorithms and Computational Complexity
CSE 373: Data Structures and Algorithms
Lecture 12 Algorithm Analysis
Richard Anderson Winter 2019 Lecture 6
CSE 373: Data Structures and Algorithms
Richard Anderson Winter 2019 Lecture 5
Topological Sorting Minimum Spanning Trees Shortest Path
Minimum-Cost Spanning Tree
Richard Anderson Autumn 2015 Lecture 6
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Presentation transcript:

CSE 373 Data Structures and Algorithms Lecture 23: Graphs V Sumber dari : https://courses.cs.washington.edu/courses/cse373/12sp/lectures/05-21-graphs5/23-graphs5.ppt.

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

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

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

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

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

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

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

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

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

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

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

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

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

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 A B C D E F E

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

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: 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 := [Vertices with in-degree 0] while notEmpty(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

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

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

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

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

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

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

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

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

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

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

Topological Sort Runtime? Initialize D // Mapping of vertex to its in-degree Queue Q := [Vertices with in-degree 0] while notEmpty(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

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

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

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 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. Output: T subset of E such that (V, T) is a connected graph (V, T) has no cycles

Spanning Tree Psuedocode pick random vertex v. T := {} spanningTree(v, T) return T. spanningTree(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. spanningTree(vi, T)

Example of Depth First Search 2 1 3 7 4 6 5

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

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

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

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

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

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

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

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

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

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

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

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

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

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}

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

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

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 If enew has a lower cost than eold, we have progressed!

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

Two Different Approaches Prim’s Algorithm Looks familiar! Kruskals’ Algorithm Completely different! One node, grow greedily Forest of MSTs, Union them together. I wonder how to union…

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 Pick one node as the root, Incrementally add edges that connect a “new” vertex to the tree. Pick the edge (u,v) where u is in the tree, v is not AND where the edge weight is the smallest of all edges (where u is in the tree and v is not). known

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

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

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

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

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

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

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

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

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

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

Example of Kruskal 1 1 6 5 4 7 2 3 {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 3 3 4

Example of Kruskal 2 1 2 3 1 3 3 3 7 3 4 1 6 4 2 2 5 {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 3 3 4

Example of Kruskal 2 1 2 3 1 3 3 3 7 3 4 1 6 4 2 2 5 {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 3 3 4

Example of Kruskal 3 1 2 3 1 3 3 3 7 3 4 1 6 4 2 2 5 {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 3 3 4

Example of Kruskal 4 1 2 3 1 3 3 3 7 3 4 1 6 4 2 2 5 {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 3 3 4

Example of Kruskal 5 1 2 3 1 3 3 3 7 3 4 1 6 4 2 2 5 {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 3 3 4

Example of Kruskal 6 1 2 3 1 3 3 3 7 3 4 1 6 4 2 2 5 {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 3 3 4

Example of Kruskal 7 1 2 3 1 3 3 3 7 3 4 1 6 4 2 2 5 {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 3 3 4

Example of Kruskal 7 1 2 3 1 3 3 3 7 3 4 1 6 4 2 2 5 {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 3 3 4

Example of Kruskal 8,9 1 2 3 1 3 3 3 7 3 4 1 6 4 2 2 5 {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 3 3 4

Kruskal's Algorithm Implementation sort edges in increasing order of length (e1, e2, e3, ..., 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?