Download presentation

Presentation is loading. Please wait.

Published byPercival Harrison Modified about 1 year ago

1
CSE 101- Winter ‘15 Discussion Section January 26th 2015

2
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 l e 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)(Lecture 6 link)

3
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 )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. B A C D E 4 2 3 1 2 4 3 1 5

4
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 )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. A B C D E 4 2 3 1 2 4 3 1 5 Initially only node A is part of the known region NodesDistance from node A A0 B4 C2 D∞ E∞ Update the distances of the neighbors of A We maintain the nodes not in the known region in a priority queue with their current distance from node A as the key.

5
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 )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. A B C D E 4 2 3 1 2 4 3 1 5 NodesDistance from node A A0 B3 C2 D6 E7 Update the distances of the neighbors of C 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.

6
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 )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. A B C D E 4 2 3 1 2 4 3 1 5 NodesDistance from node A A0 B3 C2 D5 E6 Update the distances of the neighbors of B 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.

7
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 )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. A B C D E 4 2 3 1 2 4 3 1 5 NodesDistance from node A A0 B3 C2 D5 E6 Since D has no outgoing edges, we have nothing to update 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.

8
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 )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. A B C D E 4 2 3 1 2 4 3 1 5 We have the shortest path from A to all the other nodes NodesDistance from node A A0 B3 C2 D5 E6

9
Dijkstra’s Algorithm: Pseudo-Code 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 ).

10
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 ).

11
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 ).

12
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. S A B 3 4 -2

13
Properties of Dijkstra’s algorithm U1U1 SU2U2 UkUk V

14
Bellman-Ford Algorithm If the shortest path from the source vertex S to a vertex V is as below: 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. U1U1 SU2U2 UkUk V

15
Bellman-Ford Algorithm: Pseudo-Code

16
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.

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google