380 likes | 390 Views
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.
E N D
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 BAD! I J
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
Application of DAGs: Representing Partial Orders reserve flight check in airport call taxi pack bags take flight locate gate taxi to airport
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
Topological Sort reserve flight take flight call taxi check in airport locate gate pack bags taxi to airport
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:
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 ?
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 ?
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
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
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
DFS on Trees a e b c d h i j f g k l
DFS on Graphs a e b c d h i j f g k l Problem with cycles: may run into an infinite loop
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 ??
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)
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
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); • }
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
Stack: Last-In-First-Out LIFO Queue: First-In-First-Out FIFO Stack v.s. Queue
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!
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?
The Trouble with Negative Weighted Cycles 2 A B 10 -5 1 E 2 C D
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.”
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
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; • } • } • }
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
Dijkstra’s Algorithm:Correctness Proof x startNode Known
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
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
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
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
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
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
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
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
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
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|)