CSE 326: Data Structures Graphs Ben Lerner Summer 2007.

Slides:



Advertisements
Similar presentations
§3 Shortest Path Algorithms Given a digraph G = ( V, E ), and a cost function c( e ) for e  E( G ). The length of a path P from source to destination.
Advertisements

CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
§2 Topological Sort 〖 Example 〗 Courses needed for a computer science degree at a hypothetical university How shall we convert this list into a graph?
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
The Shortest Path Problem. Shortest-Path Algorithms Find the “shortest” path from point A to point B “Shortest” in time, distance, cost, … Numerous.
1 Shortest Path Algorithms Given a graph G = (V, E) and a distinguished vertex s, find the shortest weighted path from s to every other vertex in G. weighted.
CSE332: Data Abstractions Lecture 17: Shortest Paths Dan Grossman Spring 2010.
CSE332: Data Abstractions Lecture 16: Topological Sort / Graph Traversals Tyler Robison Summer
CSE332: Data Abstractions Lecture 16: Topological Sort / Graph Traversals Dan Grossman Spring 2010.
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
CSE 326 Graphs David Kaplan Dept of Computer Science & Engineering Autumn 2001.
Directed Graph Algorithms CSE 373 Data Structures Lecture 14.
CSE 326: Data Structures Graphs Ben Lerner Summer 2007.
CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1.
CSE 780 Algorithms Advanced Algorithms Graph Algorithms Representations BFS.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
Data Structures Graphs. Graph… ADT? Not quite an ADT… operations not clear A formalism for representing relationships between objects Graph G = (V,E)
More Graph Algorithms Weiss ch Exercise: MST idea from yesterday Alternative minimum spanning tree algorithm idea Idea: Look at smallest edge not.
Dijkstra’s Algorithm Slide Courtesy: Uwash, UT 1.
Graphs CS 400/600 – Data Structures. Graphs2 Graphs  Used to represent all kinds of problems Networks and routing State diagrams Flow and capacity.
CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.
CS 146: Data Structures and Algorithms July 21 Class Meeting
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms Shortest-Path.
1 WEEK 9-10 Graphs II Unweighted Shortest Paths Dijkstra’s Algorithm Graphs with negative costs Acyclic Graphs Izmir University of Economics.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
1 Chapter 9 Graph Algorithms Real-life graph problems Algorithms for some graph problems Choice of data structures for graph problems.
Graph Algorithms Terminology Topological sort Shortest-path algorithms
CSE373: Data Structures & Algorithms Lecture 17: Shortest Paths Nicki Dell Spring 2014.
CSE 326: Data Structures Lecture #20 Graphs I.5 Alon Halevy Spring Quarter 2001.
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
CSE332: Data Abstractions Lecture 17: Shortest Paths Dan Grossman Spring 2012.
CSE373: Data Structures & Algorithms Lecture 17: Dijkstra’s Algorithm Lauren Milne Summer 2015.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 10 Prepared by İnanç TAHRALI.
CSE373: Data Structures & Algorithms Lecture 19: Dijkstra’s algorithm and Spanning Trees Catie Baker Spring 2015.
CSE373: Data Structures & Algorithms Lecture 14: Topological Sort / Graph Traversals Dan Grossman Fall 2013.
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
1 CSE 332: Graphs Richard Anderson Spring Announcements This week and next week – Graph Algorithms Reading, Monday and Wednesday, Weiss
CSE 326: Data Structures Lecture #23 Dijkstra and Kruskal (sittin’ in a graph) Steve Wolfman Winter Quarter 2000.
CSE 373: Data Structures and Algorithms Lecture 21: Graphs V 1.
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Graph Algorithms CS 202 – Fundamental Structures of Computer Science.
Instructor: Lilian de Greef Quarter: Summer 2017
CSE 373: Data Structures & Algorithms Graph Traversals: Dijkstra’s
Cse 373 May 8th – Dijkstras.
Minimum Spanning Trees and Shortest Paths
Cinda Heeren / Geoffrey Tien
CSE373: Data Structures & Algorithms Lecture 17: Shortest Paths
Graphs.
Paul Beame in lieu of Richard Anderson
CSE 373 Data Structures and Algorithms
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
CSE373: Data Structures & Algorithms Lecture 18: Dijkstra’s Algorithm
CSE 373: Data Structures and Algorithms
Shortest Path Algorithms
Section 7: Dijkstra’s & Midterm Postmortem
Based on slides of Dan Suciu
Directed Graph Algorithms
Directed Graph Algorithms
Slide Courtesy: Uwash, UT
CSE 373: Data Structures and Algorithms
CSE332: Data Abstractions Lecture 17: Shortest Paths
CSE 373 Data Structures and Algorithms
Slide Courtesy: Uwash, UT
Fundamental Structures of Computer Science II
Graphs G = (V, E) V are the vertices; E are the edges.
Richard Anderson Spring 2016
Directed Graphs (Part II)
Presentation transcript:

CSE 326: Data Structures Graphs Ben Lerner Summer 2007

2 Application: Topological Sort Given a directed graph, G = (V,E), output all the vertices in V such that no vertex is output before any other vertex with an edge to it. CSE 142CSE 143 CSE 321 CSE 341 CSE 378 CSE 326 CSE 370 CSE 403 CSE 421 CSE 467 CSE 451 CSE 322 Is the output unique? This is a partial ordering, for sorting we had a total ordering Minimize and DO a topo sort

3 Topological Sort: Take One 1.Label each vertex with its in-degree (# of inbound edges) 2.While there are vertices remaining: a.Choose a vertex v of in-degree zero ; output v b.Reduce the in-degree of all vertices adjacent to v c.Remove v from the list of vertices Runtime:

4 void Graph::topsort(){ Vertex v, w; labelEachVertexWithItsIn-degree(); for (int counter=0; counter < NUM_VERTICES; counter++){ v = findNewVertexOfDegreeZero(); v.topologicalNum = counter; for each w adjacent to v w.indegree--; } Time? What’s the bottleneck? Time?

5 Topological Sort: Take Two 1.Label each vertex with its in-degree 2.Initialize a queue Q to contain all in-degree zero vertices 3.While Q not empty a.v = Q.dequeue; output v b.Reduce the in-degree of all vertices adjacent to v c.If new in-degree of any such vertex u is zero Q.enqueue(u) Runtime: O(|V| + |E|) Note: could use a stack, list, set, box, … instead of a queue

6 void Graph::topsort(){ Queue q(NUM_VERTICES); int counter = 0; Vertex v, w; labelEachVertexWithItsIn-degree(); q.makeEmpty(); for each vertex v if (v.indegree == 0) q.enqueue(v); while (!q.isEmpty()){ v = q.dequeue(); v.topologicalNum = ++counter; for each w adjacent to v if (--w.indegree == 0) q.enqueue(w); } intialize the queue get a vertex with indegree 0 insert new eligible vertices Runtime:

7 Graph Connectivity Undirected graphs are connected if there is a path between any two vertices Directed graphs are strongly connected if there is a path from any one vertex to any other Directed graphs are weakly connected if there is a path between any two vertices, ignoring direction A complete graph has an edge between every pair of vertices

8 Graph Traversals Breadth-first search (and depth-first search) work for arbitrary (directed or undirected) graphs - not just mazes! –Must mark visited vertices so you do not go into an infinite loop! Either can be used to determine connectivity: –Is there a path between two given vertices? –Is the graph (weakly) connected? Which one: –Uses a queue? –Uses a stack? –Always finds the shortest path (for unweighted graphs)?

9 The Shortest Path Problem Given a graph G, edge costs c i,j, and vertices s and t in G, find the shortest path from s to t. For a path p = v 0 v 1 v 2 … v k –unweighted length of path p = k (a.k.a. length) –weighted length of path p =  i=0..k-1 c i,i+1 (a.k.a cost) Path length equals path cost when ?

10 Single Source Shortest Paths (SSSP) Given a graph G, edge costs c i,j, and vertex s, find the shortest paths from s to all vertices in G. –Is this harder or easier than the previous problem?

11 All Pairs Shortest Paths (APSP) Given a graph G and edge costs c i,j, find the shortest paths between all pairs of vertices in G. –Is this harder or easier than SSSP? –Could we use SSSP as a subroutine to solve this?

12 Variations of SSSP –Weighted vs. unweighted –Directed vs undirected –Cyclic vs. acyclic –Positive weights only vs. negative weights allowed –Shortest path vs. longest path –…

13 Applications –Network routing –Driving directions –Cheap flight tickets –Critical paths in project management (see textbook) –…

14 SSSP: Unweighted Version Ideas?

15 void Graph::unweighted (Vertex s){ Queue q(NUM_VERTICES); Vertex v, w; q.enqueue(s); s.dist = 0; while (!q.isEmpty()){ v = q.dequeue(); for each w adjacent to v if (w.dist == INFINITY){ w.dist = v.dist + 1; w.path = v; q.enqueue(w); } each edge examined at most once – if adjacency lists are used each vertex enqueued at most once total running time: O( )

16 v3v3 v6v6 v1v1 v2v2 v4v4 v5v5 v0v0 s VDistpath v0 v1 v2 v3 v4 v5 v6

17 Weighted SSSP: The Quest For Food Vending Machine in EE1 ALLEN HUB Delfino’s Ben & Jerry’s Neelam’s Cedars Coke Closet Home Schultzy’s Parent’s Home Café Allegro 10 The Ave U Village , Can we calculate shortest distance to all nodes from Allen Center?

18 Dijkstra, Edsger Wybe Legendary figure in computer science; was a professor at University of Texas. Supported teaching introductory computer courses without computers (pencil and paper programming) Supposedly wouldn’t (until very late in life) read his ; so, his staff had to print out messages and put them in his box. E.W. Dijkstra ( ) 1972 Turning Award Winner, Programming Languages, semaphores, and …

19 Dijkstra’s Algorithm: Idea Adapt BFS to handle weighted graphs Two kinds of vertices: –Finished or known vertices Shortest distance has been computed –Unknown vertices Have tentative distance

20 Dijkstra’s Algorithm: Idea At each step: 1)Pick closest unknown vertex 2)Add it to known vertices 3)Update distances

21 Dijkstra’s Algorithm: Pseudocode Initialize the cost of each node to  Initialize the cost of the source to 0 While there are unknown nodes left in the graph Select an unknown node b with the lowest cost Mark b as known For each node a adjacent to b a’s cost = min(a’s old cost, b’s cost + cost of (b, a)) a’s prev path node = b

22 Important Features Once a vertex is made known, the cost of the shortest path to that node is known While a vertex is still not known, another shorter path to it might still be found The shortest path itself can found by following the backward pointers stored in node.path

23 Dijkstra’s Algorithm in action A B D C FH E G 0       VertexVisited?CostFound by A0 B?? C D E F G H

24 Dijkstra’s Algorithm in action A B D C FH E G 0 2  4 1   VertexVisited?CostFound by AY0 B<=2A C<=1A D<=4A E?? F G H

25 Dijkstra’s Algorithm in action A B D C FH E G 0 2   VertexVisited?CostFound by AY0 B<=2A CY1A D<=4A E<=12C F?? G H

26 Dijkstra’s Algorithm in action A B D C FH E G   VertexVisited?CostFound by AY0 BY2A CY1A D<=4A E<=12C F<=4B G?? H

27 Dijkstra’s Algorithm in action A B D C FH E G   VertexVisited?CostFound by AY0 BY2A CY1A DY4A E<=12C F<=4B G?? H

28 Dijkstra’s Algorithm in action A B D C FH E G  VertexVisited?CostFound by AY0 BY2A CY1A DY4A E<=12C FY4B G?? H<=7F

29 Dijkstra’s Algorithm in action A B D C FH E G VertexVisited?CostFound by AY0 BY2A CY1A DY4A E<=12C FY4B G<=8H HY7F

30 Dijkstra’s Algorithm in action A B D C FH E G VertexVisited?CostFound by AY0 BY2A CY1A DY4A E<=11G FY4B GY8H HY7F

31 Dijkstra’s Algorithm in action A B D C FH E G VertexVisited?CostFound by AY0 BY2A CY1A DY4A EY11G FY4B GY8H HY7F

32 Your turn v3v3 v6v6 v1v1 v2v2 v4v4 v5v5 v0v0 s VVisited?CostFound by v0 v1 v2 v3 v4 v5 v6

33 Answer v3v3 v6v6 v1v1 v2v2 v4v4 v5v5 v0v0 s VVisited?CostFound by v0Y0 V1Y6V3 V2Y2V0 V3Y1V0 V4Y2V3 V5Y4V2 V6Y6V3

34 Dijkstra’s Alg: Implementation Initialize the cost of each node to  Initialize the cost of the source to 0 While there are unknown nodes left in the graph Select the unknown node b with the lowest cost Mark b as known For each node a adjacent to b a’s cost = min(a’s old cost, b’s cost + cost of (b, a)) a’s prev path node = b (if we updated a’s cost) What data structures should we use? Running time?

35 void Graph::dijkstra(Vertex s){ Vertex v,w; Initialize s.dist = 0 and set dist of all other vertices to infinity while (there exist unknown vertices, find the one b with the smallest distance) b.known = true; for each a adjacent to b if (!a.known) if (b.dist + weight(b,a) < a.dist){ a.dist = (b.dist + weight(b,a)); a.path = b; } Sounds like deleteMin on a heap… Sounds like adjacency lists Sounds like decreaseKey Running time: O(|E| log |V|) – there are |E| edges to examine, and each one causes a heap operation of time O(log |V|)

36 Dijkstra’s Algorithm: Summary Classic algorithm for solving SSSP in weighted graphs without negative weights A greedy algorithm (irrevocably makes decisions without considering future consequences) Intuition for correctness: –shortest path from source vertex to itself is 0 –cost of going to adjacent nodes is at most edge weights –cheapest of these must be shortest path to that node –update paths for new node and continue picking cheapest path

37 The Known Cloud V Next shortest path from inside the known cloud W Better path to V? No! Correctness: The Cloud Proof How does Dijkstra’s decide which vertex to add to the Known set next? If path to V is shortest, path to W must be at least as long (or else we would have picked W as the next vertex) So the path through W to V cannot be any shorter! Source

38 Correctness: Inside the Cloud Prove by induction on # of nodes in the cloud: Initial cloud is just the source with shortest path 0 Assume: Everything inside the cloud has the correct shortest path Inductive step: Only when we prove the shortest path to some node v (which is not in the cloud) is correct, we add it to the cloud When does Dijkstra’s algorithm not work?

39 The Trouble with Negative Weight Cycles AB CD E What’s the shortest path from A to E? Problem?

40 Dijkstra’s vs BFS At each step: 1)Pick closest unknown vertex 2)Add it to finished vertices 3)Update distances Dijkstra’s Algorithm At each step: 1)Pick vertex from queue 2)Add it to visited vertices 3)Update queue with neighbors Breadth-first Search Some Similarities :