Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 326: Data Structures Graphs Ben Lerner Summer 2007.

Similar presentations


Presentation on theme: "CSE 326: Data Structures Graphs Ben Lerner Summer 2007."— Presentation transcript:

1 CSE 326: Data Structures Graphs Ben Lerner Summer 2007

2 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 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 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 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 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 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 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 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 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 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 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 13 Applications –Network routing –Driving directions –Cheap flight tickets –Critical paths in project management (see textbook) –…

14 14 SSSP: Unweighted Version Ideas?

15 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 16 v3v3 v6v6 v1v1 v2v2 v4v4 v5v5 v0v0 s VDistpath v0 v1 v2 v3 v4 v5 v6

17 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 350 375 40 25 35 15 25 15,356 35 285 75 70 365 350 Can we calculate shortest distance to all nodes from Allen Center?

18 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 e-mail; so, his staff had to print out messages and put them in his box. E.W. Dijkstra (1930-2002) 1972 Turning Award Winner, Programming Languages, semaphores, and …

19 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 20 Dijkstra’s Algorithm: Idea At each step: 1)Pick closest unknown vertex 2)Add it to known vertices 3)Update distances

21 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 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 23 Dijkstra’s Algorithm in action A B D C FH E G 0       2 2 3 1 10 2 3 1 11 7 1 9 2 4 VertexVisited?CostFound by A0 B?? C D E F G H

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

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

26 26 Dijkstra’s Algorithm in action A B D C FH E G 0 2 4  4 1 12  2 2 3 1 10 2 3 1 11 7 1 9 2 4 VertexVisited?CostFound by AY0 BY2A CY1A D<=4A E<=12C F<=4B G?? H

27 27 Dijkstra’s Algorithm in action A B D C FH E G 0 2 4  4 1 12  2 2 3 1 10 2 3 1 11 7 1 9 2 4 VertexVisited?CostFound by AY0 BY2A CY1A DY4A E<=12C F<=4B G?? H

28 28 Dijkstra’s Algorithm in action A B D C FH E G 0 2 47 4 1 12  2 2 3 1 10 2 3 1 11 7 1 9 2 4 VertexVisited?CostFound by AY0 BY2A CY1A DY4A E<=12C FY4B G?? H<=7F

29 29 Dijkstra’s Algorithm in action A B D C FH E G 0 2 47 4 1 12 8 2 2 3 1 10 2 3 1 11 7 1 9 2 4 VertexVisited?CostFound by AY0 BY2A CY1A DY4A E<=12C FY4B G<=8H HY7F

30 30 Dijkstra’s Algorithm in action A B D C FH E G 0 2 47 4 1 11 8 2 2 3 1 10 2 3 1 11 7 1 9 2 4 VertexVisited?CostFound by AY0 BY2A CY1A DY4A E<=11G FY4B GY8H HY7F

31 31 Dijkstra’s Algorithm in action A B D C FH E G 0 2 47 4 1 11 8 2 2 3 1 10 2 3 1 11 7 1 9 2 4 VertexVisited?CostFound by AY0 BY2A CY1A DY4A EY11G FY4B GY8H HY7F

32 32 Your turn v3v3 v6v6 v1v1 v2v2 v4v4 v5v5 v0v0 s 1 2 2 2 1 11 53 5 6 10 VVisited?CostFound by v0 v1 v2 v3 v4 v5 v6

33 33 Answer v3v3 v6v6 v1v1 v2v2 v4v4 v5v5 v0v0 s 1 2 2 2 1 11 53 5 6 10 VVisited?CostFound by v0Y0 V1Y6V3 V2Y2V0 V3Y1V0 V4Y2V3 V5Y4V2 V6Y6V3

34 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 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 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 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 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 39 The Trouble with Negative Weight Cycles AB CD E 2 10 1 -5 2 What’s the shortest path from A to E? Problem?

40 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 :


Download ppt "CSE 326: Data Structures Graphs Ben Lerner Summer 2007."

Similar presentations


Ads by Google