Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 WEEK 9-10 Graphs II Unweighted Shortest Paths Dijkstra’s Algorithm Graphs with negative costs Acyclic Graphs Izmir University of Economics.

Similar presentations


Presentation on theme: "1 WEEK 9-10 Graphs II Unweighted Shortest Paths Dijkstra’s Algorithm Graphs with negative costs Acyclic Graphs Izmir University of Economics."— Presentation transcript:

1 1 WEEK 9-10 Graphs II Unweighted Shortest Paths Dijkstra’s Algorithm Graphs with negative costs Acyclic Graphs Izmir University of Economics

2 2 Shortest-Path Algorithms Izmir University of Economics The input is a weighted graph: associated with each edge (v i, v j ) is a cost c i,j. The cost of a path v 1 v 2...v N is ∑c i,i+1 for i in [1..N-1]  weighted path length The unweighted path length is merely the number of edges on the path, namely, N-1.

3 3 Shortest-Path Algorithms Izmir University of Economics Single-Source Shortest-Path Problem: Given as input a weighted graph G=(V, E), and a distinguished vertex, s, find the shortest weighted path from s to every other vertex in G.

4 Negative Cost Cycles Izmir University of Economics4 In the graph to the left, the shortest path from v 1 to v 6 has a cost of 6 and the path itself is v 1 v 4 v 7 v 6. The shortest unweighted path has 2 edges. In the graph to the right, we have a negative cost. The path from v 5 to v 4 has cost 1, but a shorter path exists by following the loop v 5 v 4 v 2 v 5 v 4 which has cost -5. This path is still not the shortest, because we could stay in the loop arbitrarily long.

5 Shortest Path Lenght: Problems We will examine 4 algorithms to solve four versions of the problem 1.Unweighted shortest path  O(|E|+|V|) 2.Weighted shortest path without negative edges  O(|E|log|V|) using queues 3.Weighted shortest path with negative edges  O(|E|. |V|) 4.Weighted shortest path of acyclic graphs  linear time Izmir University of Economics5

6 Unweighted Shortest Paths Using some vertex, s, which is an input parameter, find the shortest path from s to all other vertices in an unweighted graph. Assume s=v 3. Izmir University of Economics6

7 Unweighted Shortest Paths Algorithm: find vertices that are at distance 1, 2,... N-1 by processing vertices in layers (breadth-first search) Izmir University of Economics7

8 Unweighted Shortest Paths Izmir University of Economics8

9 Unweighted Shortest Paths-Implementation For each vertex v 1.The distance from s in the entry  d v 2.The bookkeeping variable (path) which will allow us to print the actual path  p v 3.A boolean variable to check is the vertex is processed or not  known 9 class Vertex { List adj; bool known; int dist; Vertex path; }

10 Unweighted Shortest Paths Complexity O(|V| 2 ) Izmir University of Economics10

11 Unweighted Shortest Paths - Improvement 11

12 Unweighted Shortest Paths - Improvement At any point in time there are only two types of unknown vertices that have d v ≠∞. Some have d v = currDist and the rest have d v = currDist +1. We can make use of a queue data structure. O(|E|+|V|) 12

13 Weighted Shortest Path Dijkstra’s Algorithm With weighted shortest path,distance d v is tentative. It turns out to be the shortest path length from s to v using only known vertices as intermediates. Greedy algorithm: proceeds in stages doing the best at each stage. Dijkstra’s algorithm selects a vertex v with smallest d v among all unknown vertices and declares it known. Remainder of the stage consists of updating the values d w for all edges (v, w). Izmir University of Economics13

14 Dijkstra’s Algorithm - Example Izmir University of Economics14 ► ► ►

15 Dijkstra’s Algorithm - Example A proof by contradiction will show that this algorithm always works as long as no edge has a negative cost. Izmir University of Economics15 ► ► ► ►

16 Dijkstra’s Algorithm Example – Stages I Izmir University of Economics16

17 Dijkstra’s Algorithm Example – Stages II Izmir University of Economics17

18 Dijkstra’s Algorithm - Pseudocode If the vertices are sequentially scanned to find minimum d v, each phase will take O(|V|) to find the minimum, thus O(|V| 2 ) over the course of the algorithm. The time for updates is constant and at most one update per edge for a total of O(|E|). Therefore the total time spent is O(|V| 2 +|E|). If the graph is dense, OPTIMAL. 18

19 Dijkstra’s Algorithm-What if the graph is sparse? If the graph is sparse |E|=θ(|V|), algorithm is too slow. The distances of vertices need to be kept in a priority queue. Selection of vertex with minimum distance via deleteMin, and updates via decreaseKey operation. Hence; O(|E|log|V|+|V|log|V|) find operations are not supported, so you need to be able to maintain locations of d i in the heap and update them as they change. Alternative: insert w and d w with every update. Izmir University of Economics19

20 Graphs with negative edge costs Dijkstra’s algorithm does not work with negative edge costs. Once a vertex u is known, it is possible that from some other unknown vertex v, there is a path back to u that is very negative. Algorithm: A combination of weighted and unweighted algorithms. Forget about the concept of known vertices. Izmir University of Economics20

21 Graphs with negative edge costs - I O(|E|*|V|) Each vertex can dequeue at most O(|V|) times. (Why? Algorithm computes shortest paths with at most 0, 1,..., |V|-1 edges in this order). Hence, the result! If negative cost cycles, then each vertex should be checked to have been dequeued at most |V| times. Izmir University of Economics21

22 Acyclic Graphs If the graph is known to be acyclic, the order in which vertices are declared known, can be set to be the topological order. Running time = O(|V|+|E|) This selection rule works because when a vertex is selected, its distance can no longer be lowered, since by topological ordering rule it has no incoming edges emanating from unknown nodes. Izmir University of Economics22

23 Acyclic Graphs - Example Izmir University of Economics23


Download ppt "1 WEEK 9-10 Graphs II Unweighted Shortest Paths Dijkstra’s Algorithm Graphs with negative costs Acyclic Graphs Izmir University of Economics."

Similar presentations


Ads by Google