CSE 101- Winter ‘15 Discussion Section January 26th 2015 Shortest Paths • Breadth-first search finds the shortest path in graphs whose edges have unit length. • How do we adapt breadth-first search to find the shortest path in a more general graph G = (V,E) whose edge lengths le are positive integers. • We could break G’s long edges into unit-length pieces by introducing “dummy nodes”. Breadth-first search on this new graph should give the shortest path between the nodes in the original graph. • For a more detailed explanation of the above procedure, please refer to slides 9-13 of lecture 6. (Lecture 6 link) Dijsktra’s Algorithm • The usage of the concept of alarm clock in the breadth-first search performed on the graph with dummy nodes results in Dijsktra’s algorithm. (refer to slide 12 of lecture 6 – link ) • We explain the algorithm briefly through the below example : • In the graph below, we are finding the shortest paths to all the nodes from the source node A. 2 B D 4 4 A 3 1 1 3 2 C 5 E Dijsktra’s Algorithm • The usage of the concept of alarm clock in the breadth-first search performed on the graph with dummy nodes results in Dijsktra’s algorithm. (refer to slide 12 of lecture 6 – link ) • We explain the algorithm briefly through the below example : • In the graph below, we are finding the shortest paths to all the nodes from the source node A. We maintain the nodes not in the known 2 B region in a priority queue with their current distance from node A as the key. D 4 4 A 3 1 3 Initially only node A is part of the known region Distance from node A A 0 B 4 C 2 D ∞ E ∞ 1 2 C Nodes 5 E Update the distances of the neighbors of A Dijsktra’s Algorithm • The usage of the concept of alarm clock in the breadth-first search performed on the graph with dummy nodes results in Dijsktra’s algorithm. (refer to slide 12 of lecture 6 – link ) • We explain the algorithm briefly through the below example : • In the graph below, we are finding the shortest paths to all the nodes from the source node A. 2 B D 4 4 A 3 1 Distance from node A A 0 B 3 C 2 D 6 E 7 1 3 2 C Nodes 5 Delete-min – Returns C as the node with the shortest distance from the known region. C is added to the known region and deleted from the priority queue. E Update the distances of the neighbors of C Dijsktra’s Algorithm • The usage of the concept of alarm clock in the breadth-first search performed on the graph with dummy nodes results in Dijsktra’s algorithm. (refer to slide 12 of lecture 6 – link ) • We explain the algorithm briefly through the below example : • In the graph below, we are finding the shortest paths to all the nodes from the source node A. 2 B D 4 4 A 3 1 Distance from node A A 0 B 3 C 2 D 5 E 6 1 3 2 C Nodes 5 Delete-min – Returns B as the node with the shortest distance from the known region. B is added to the known region and deleted from the priority queue. E Update the distances of the neighbors of B Dijsktra’s Algorithm • The usage of the concept of alarm clock in the breadth-first search performed on the graph with dummy nodes results in Dijsktra’s algorithm. (refer to slide 12 of lecture 6 – link ) • We explain the algorithm briefly through the below example : • In the graph below, we are finding the shortest paths to all the nodes from the source node A. Since D has no 2 B outgoing edges, we have nothing to update D 4 4 A 3 1 Distance from node A A 0 B 3 C 2 D 5 E 6 1 3 2 C Nodes 5 Delete-min – Returns D as the node with the shortest distance from the known region. D is added to the known region and deleted from the priority queue. E Dijsktra’s Algorithm • The usage of the concept of alarm clock in the breadth-first search performed on the graph with dummy nodes results in Dijsktra’s algorithm. (refer to slide 12 of lecture 6 – link ) • We explain the algorithm briefly through the below example : • In the graph below, we are finding the shortest paths to all the nodes from the source node A. 2 B D 4 4 A 3 1 3 We have the shortest path from A to all the other nodes Distance from node A A 0 B 3 C 2 D 5 E 6 1 2 C Nodes 5 E Dijkstra’s Algorithm: Pseudo-Code procedure dijkstra(G, l, s) for all u ∈ V: dist(u) = ∞ prev(u) = nil dist(s) = 0 H = makequeue(V) (using dist-values as keys) While H is not empty: u = deletemin(H) for all edges (u, v) ∈ E: if(dist(v)) > dist(u) + l(u,v) : dist(v) = dist(u) + l(u,v) prev(v) = u decreasekey(H,v) We use a binary heap data structure for the priority queue. makequeue() takes O (|V| log |V|) Delete operation in a binary heap takes O(log|V|) time. Since we do a total of |V| deletions, total time taken = O (|V| log |V|) decreaseKey operation takes O( log |V|) time. Since this is done |E| times in total, total time taken = O(|E| log|V|) Hence, total time taken = O ((|V| + |E|) log |V|) Remember : If we use an array to implement the priority queue. 1) Time spent in makeQueue() = O(V). 2) Delete Operation takes O(|V|) time. Time taken for |V| deletions = O(|V|2) time. 3) Time taken for decreaseKey operation = O(|1|). We do this operation |E] times. Total time taken = O(|E|). |E| = O(|V|2). Hence, total time taken for this step = O(|V|2). 4) Hence, total time taken = O (|V|2). Exercise 1 • Give an algorithm that takes as input a directed graph with positive edge lengths, and returns the length of the shortest cycle in the graph (if the graph is acyclic, it should say so). Your algorithm should take time at most O(|V|3 ). Exercise 1 • Give an algorithm that takes as input a directed graph with positive edge lengths, and returns the length of the shortest cycle in the graph (if the graph is acyclic, it should say so). Your algorithm should take time at most O(|V|3 ). • Solution : – If there is a cycle in the graph then we know there is a path from a vertex U to a vertex V and a path from V to U. – Finding the shortest cycle involves finding two vertices U and V such that above property is satisfied and the sum of the distances of the shortest path from U to V and the shortest path from V to U is the shortest amongst all the cycles in the graph. – We run Dijkstra’s once from each vertex in the graph. Implementing Dijkstra’s algorithm using an array as the priority queue takes O(|V|2) time. Since we run Dijkstra’s with each of the vertices as the source vertex, time taken for this step is O(|V|2) * |V| = O(|V|3). If there are multiple pairs of vertices U and V which satisfy the above property, we an use any one of them since we just need the length of the shortest cycle. – We then check every combination of vertices (U,V) in order to find the shortest cycle. Since we have O(|V|2) combinations, time taken for this check = O(|V|2). Shortest Path (negative edges) • Dijkstra’s algorithm works in part because the shortest path from the starting node S to any node V must pass exclusively through nodes that are closer than V. • This no longer holds when the edge lengths can be negative. • In the figure below, shortest path from S to A passes through B, a node that is further away. A 3 -2 S 4 B Properties of Dijkstra’s algorithm • Consider the following update operation performed in Dijkstra’s: procedure update((u, v) ∈ E) dist(v) = min{dist(v), dist(u) + l(u, v)} • This update operation has the following properties : – It gives the correct distance to v in the particular case where u is the second-last node in the shortest path to v, and dist(u) is correctly set. – It will never make dist(v) too small, and in this sense it is safe. For instance, a slew of extraneous update operations can’t hurt. A safe update is one which will not increase the current distance of any vertex from the source vertex. • If the shortest path from the source vertex S to a vertex V is as below: S U1 U2 Uk V • If the sequence of updates performed includes (S,U1),(S,U2),….,(Uk,V) in that order (though not necessarily consecutively), then the distance to V is computed correctly. Bellman-Ford Algorithm • If the shortest path from the source vertex S to a vertex V is as below: S U1 U2 Uk V • Dijkstra’s algorithm performs a sequence of safe updates. . • Although Dijkstra’s performs safe updates, it does not ensure the sequence of updates happen in the order expected (order expected is explained in the previous slide). • This problem can be solved by updating all the edges |V| - 1 times. This should ensure we get the shortest path even when the graph has negative edges. • Why do we need to do this |V| -1 times? – The shortest path from the source vertex S to a vertex V has at most V-1 edges. • This results in a O(|V||E| ) procedure called the Bellman-Ford algorithm. Bellman-Ford Algorithm: Pseudo-Code procedure update((u, v) ∈ E) dist(v) = min{dist(v), dist(u) + l(u, v)} procedure shortest-paths(G, l, s) for all u ∈ V: dist(u) = ∞ prev(u) = nil dist(s) = 0 Repeat 𝑉 − 1 times: for all e ∈ E: update(e) (Ref: Algorithms, Fig 4.8) Comparion of Bellman-Ford and Dijkstra’s Dijkstra’s Bellman-Ford Time Complexity = O ( |V| + |E| ) log |V| Time Complexity = O( |V| |E| ) Solves the single-source shortest path problem for a graph with non-negative edge weights. Solves the single-source shortest path problem for a graph with negative edge weights. Cannot identify negative cycles. Can identify negative cycles.