1 / 38

CSE 326: Data Structures: Graphs

CSE 326: Data Structures: Graphs. Lecture 19: Wednesday, Feb 26, 2003. Trees as Graphs. Every tree is a graph with some restrictions: the tree is directed there are no cycles (directed or undirected) there is a directed path from the root to every node. A. B. C. D. E. F. G. H.

atomas
Download Presentation

CSE 326: Data Structures: Graphs

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 326: Data Structures: Graphs Lecture 19: Wednesday, Feb 26, 2003

  2. Trees as Graphs • Every tree is a graph with some restrictions: • the tree is directed • there are no cycles (directed or undirected) • there is a directed path from the root to every node A B C D E F G H BAD! I J

  3. Directed Acyclic Graphs (DAGs) DAGs are directed graphs with no cycles. main() mult() if program call graph is a DAG, then all procedure calls can be in-lined add() read() access() Trees  DAGs  Graphs

  4. Application of DAGs: Representing Partial Orders reserve flight check in airport call taxi pack bags take flight locate gate taxi to airport

  5. Topological Sort Given a 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. reserve flight check in airport call taxi take flight taxi to airport locate gate pack bags

  6. Topological Sort reserve flight take flight call taxi check in airport locate gate pack bags taxi to airport

  7. Label each vertex’s in-degree (# of inbound edges) While there are vertices remaining Pick a vertex with in-degree of zero and output it Reduce the in-degree of all vertices adjacent to it Remove it from the list of vertices Topo-Sort Take One runtime:

  8. label each vertex’s in-degree Initialize a queue to contain all in-degree zero vertices While there are vertices remaining in the queue Remove a vertex v with in-degree of zero and output it Reduce the in-degree of all vertices adjacent to v Put any of these with new in-degree zero on the queue Topo-Sort Take Two runtime: Can we use a stack instead of a queue ?

  9. Topo-Sort • So we sort again in linear time: O(|V|+|E|) • Can we apply this to normal sorting ?If we sort an array A[1], A[2], ..., A[n] using topo-sort, what is the running time ?

  10. Recall: Tree Traversals a e b c d h i j f g k l a b f g k c d h i l j e

  11. Depth-First Search • Pre/Post/In – order traversals are examples of depth-first search • Nodes are visited deeply on the left-most branches before any nodes are visited on the right-most branches • Visiting the right branches deeply before the left would still be depth-first! Crucial idea is “go deep first!” • Difference in pre/post/in-order is how some computation (e.g. printing) is done at current node relative to the recursive calls • In DFS the nodes “being worked on” are kept on a stack

  12. void DFS(Node startNode) { Stack s = new Stack; s.push(startNode); while (!s.empty()) { x = s.pop(); /* process x */ for y in x.children() do s.push(y); } DFS

  13. DFS on Trees a e b c d h i j f g k l

  14. DFS on Graphs a e b c d h i j f g k l Problem with cycles: may run into an infinite loop

  15. DFS • void DFS(Node startNode) { • for v in Nodes do • v.visited = false; • Stack s = new Stack; • s.push(startNode); • while (!s.empty()) { • x = s.pop(); • x.visited = true; • /* process x */ • for y in x.children() do • if (!y.visited) s.push(y); • } Running time: Can avoid infinite loopswithout the “visited” field.How ??

  16. a e b c d h i j f g k l Breadth-First Search • Traversing tree level by level fromtop to bottom (alphabetic order)

  17. Breadth-First Search BFS characteristics • Nodes being worked on maintained in a FIFO Queue, not a stack • Iterative style procedures often easier to design than recursive procedures

  18. Breadth-First Search • void DFS(Node startNode) { • for v in Nodes do • v.visited = false; • Stack s = new Stack; • s.push(startNode); • while (!s.empty()) { • x = s.pop(); • x.visited = true; • /* process x */ • for y in x.children() do • if (!y.visited) s.push(y); • } • void BFS(Node startNode) { • for v in Nodes do • v.visited = false; • Queue s = new Queue; • s.enqueue(startNode); • while (!s.empty()) { • x = s.dequeue(); • x.visited = true; • /* process x */ • for y in x.children() do • if (!y.visited) s.enqueue(y); • }

  19. a e b c d h i j f g k l QUEUE a b c d e c d e f g d e f g e f g h i j f g h i j g h i j h i j k i j k j k l k l l

  20. Stack: Last-In-First-Out LIFO Queue: First-In-First-Out FIFO Stack v.s. Queue

  21. Graph Traversals DFS and BFS • Either can be used to determine connectivity: • Is there a path between two given vertices? • Is the graph (weakly) connected? • Important difference: Breadth-first search always finds a shortest path from the start vertex to any other (for unweighted graphs) • Depth first search may not!

  22. Single Source, Shortest Path for Weighted Graphs Given a graph G = (V, E) with edge costs c(e), and a vertex s  V, find the shortest (lowest cost) path from s to every vertex in V • Graph may be directed or undirected • Graph may or may not contain cycles • Weights may be all positive or not • What is the problem if graph contains cycles whose total cost is negative?

  23. The Trouble with Negative Weighted Cycles 2 A B 10 -5 1 E 2 C D

  24. Edsger Wybe Dijkstra (1930-2002) • Invented concepts of structured programming, synchronization, weakest precondition, and "semaphores" for controlling computer processes. The Oxford English Dictionary cites his use of the words "vector" and "stack" in a computing context. • Believed programming should be taught without computers • 1972 Turing Award • “In their capacity as a tool, computers will be but a ripple on the surface of our culture. In their capacity as intellectual challenge, they are without precedent in the cultural history of mankind.”

  25. Dijkstra’s Algorithm for Single Source Shortest Path • Classic algorithm for solving shortest path in weighted graphs (with onlypositive edge weights) • Similar to breadth-first search, but uses a priority queue instead of a FIFO queue: • Always select (expand) the vertex that has a lowest-cost path to the start vertex • a kind of “greedy” algorithm • Correctly handles the case where the lowest-cost (shortest) path to a vertex is not the one with fewest edges

  26. void BFS(Node startNode) { • Queue s = new Queue; • for v in Nodes do • v.visited = false; • startNode.dist = 0; • s.enqueue(startNode); • while (!s.empty()) { • x = s.dequeue(); • for y in x.children() do • if (x.dist+1<y.dist) { • y.dist = x.dist+1; • s.enqueue(y); • } • } • } • void shortestPath(Node startNode) { • Heap s = new Heap; • for v in Nodes do • v.dist = ; • s.insert(v); • startNode.dist = 0; • s.decreaseKey(startNode); • startNode.previous = null; • while (!s.empty()) { • x = s.deleteMin(); • for y in x.children() do • if (x.dist+c(x,y) < y.dist) { • y.dist = x.dist+c(x,y); s.enqueue(y); • y.previous = x; • } • } • }

  27. Dijkstra’s Algorithm:Correctness Proof Let Known be the set of nodes that were extracted from the heap (through deleteMin) • For every node x, x.dist = the cost of the shortest path from startNode to x going only through nodes in Known • In particular, if x in Known then x.dist = the shortest path cost • Once a node x is in Known, it will never be reinserted into the heap

  28. Dijkstra’s Algorithm:Correctness Proof x startNode Known

  29. 2 2 3 B A F H 1 1 2 1 4 10 9 4 G C 8 2 D 1 E 7 Dijkstra’s Algorithm in Action

  30. Dijkstra’s Algorithm in Action  9  2 2 3 B A F H 1  1 2 1 4 10 9 0 4 G C  8 2  D 1 E 7 8 next

  31. Dijkstra’s Algorithm in Action  9  2 2 next 3 B A F H 1  1 2 1 4 10 9 0 4 G C 9 8 2 15 D 1 E 7 8

  32. Dijkstra’s Algorithm in Action 11 9  2 2 3 B A F H 1  1 2 1 4 10 9 0 4 G C 9 8 2 13 D 1 E 7 8 next

  33. next Dijkstra’s Algorithm in Action 11 9 11 2 2 3 B A F H 1  1 2 1 4 10 9 0 4 G C 9 8 2 13 D 1 E 7 8

  34. Dijkstra’s Algorithm in Action next 11 9 11 2 2 3 B A F H 1  1 2 1 4 10 9 0 4 G C 9 8 2 13 D 1 E 7 8

  35. Dijkstra’s Algorithm in Action 11 9 11 2 2 3 B A F H 1 14 1 2 1 4 10 9 0 4 G C 9 8 2 13 D 1 E 7 8 next

  36. Dijkstra’s Algorithm in Action 11 9 11 2 2 3 B A F H 1 14 1 2 1 4 10 9 0 4 G C 9 8 2 13 D 1 E 7 8 next

  37. Dijkstra’s Algorithm in Action 11 9 11 2 2 3 B A F H 1 14 1 2 1 4 10 9 0 4 G C 9 8 2 13 D 1 E 7 8 Done

  38. Data Structures for Dijkstra’s Algorithm |V| times: Select the unknown node with the lowest cost findMin/deleteMin O(log |V|) |E| times: y’s cost = min(y’s old cost, …) decreaseKey O(log |V|) runtime: O(|E| log |V|)

More Related