Presentation is loading. Please wait.

Presentation is loading. Please wait.

Algorithm Course Dr. Aref Rashad February 20131 Algorithms Course..... Dr. Aref Rashad Part: 6 Shortest Path Algorithms.

Similar presentations


Presentation on theme: "Algorithm Course Dr. Aref Rashad February 20131 Algorithms Course..... Dr. Aref Rashad Part: 6 Shortest Path Algorithms."— Presentation transcript:

1 Algorithm Course Dr. Aref Rashad February 20131 Algorithms Course..... Dr. Aref Rashad Part: 6 Shortest Path Algorithms

2 The Shortest Path Problem  Given a directed, weighted graph G = ( V, E )  What is the shortest path from the start vertex to some end vertex?  Minimize the sum of the edge weights s 3 t 2 6 7 4 5 24 18 2 9 14 15 15 5 30 20 44 16 11 6 19 6

3 Bellman–Ford algorithm computes single-source shortest paths in a weighted digraph (where some of the edge weights may be negative). Dijkstra's algorithm accomplishes the same problem with a lower running time, but requires edge weights to be non- negative. Thus, Bellman–Ford is usually used only when there are negative edge weights. Bellman–Ford runs in O(V·E) time, where V and E are the number of vertices and edges respectively. The Shortest Path Problem

4 In a graph in which edges have costs.. Find the shortest path from a source to a destination Surprisingly.. While finding the shortest path from a source to one destination, we can find the shortest paths to all over destinations as well! Dijkstra's Shortest Path Algorithm

5 Problem: From a given source vertex s V, find the shortest- path weights d(s, v)for all v V. IDEA: Greedy. 1.Maintain a set S of vertices whose shortest-path distances from s are known. 2.At each step add to S the vertex v V –S whose distance estimate from s is minimal. 3.Update the distance estimates of vertices adjacent to v. Dijkstra Algorithm

6 Dijkstra's Shortest Path Algorithm  For a graph, G = ( V, E )  Dijkstra’s algorithm keeps two sets of vertices: S Vertices whose shortest paths have already been determined V-S Remainder  Also d Best estimates of shortest path to each vertex p Predecessors for each vertex

7 Dijkstra's Shortest Path Algorithm Find shortest path from s to t. s 3 t 2 6 7 4 5 24 18 2 9 14 15 5 30 20 44 16 11 6 19 6

8 Dijkstra's Shortest Path Algorithm s 3 t 2 6 7 4 5 24 18 2 9 14 15 5 30 20 44 16 11 6 19 6        0 distance label S = { } PQ = { s, 2, 3, 4, 5, 6, 7, t }

9 s 3 t 2 6 7 4 5 24 18 2 9 14 15 5 30 20 44 16 11 6 19 6        0 distance label S = { } PQ = { s, 2, 3, 4, 5, 6, 7, t } delmin Dijkstra's Shortest Path Algorithm

10 s 3 t 2 6 7 4 5 24 18 2 9 14 15 5 30 20 44 16 11 6 19 6 15 9    14  0 distance label S = { s } PQ = { 2, 3, 4, 5, 6, 7, t } decrease key  X   X X Dijkstra's Shortest Path Algorithm

11 s 3 t 2 6 7 4 5 24 18 2 9 14 15 5 30 20 44 16 11 6 19 6 15 9    14  0 distance label S = { s } PQ = { 2, 3, 4, 5, 6, 7, t }  X   X X delmin Dijkstra's Shortest Path Algorithm

12 9 2 s 3 t 6 7 4 5 24 18 2 14 15 5 30 20 44 16 11 6 19 6 15 9    14  0 S = { s, 2 } PQ = { 3, 4, 5, 6, 7, t }  X   X X Dijkstra's Shortest Path Algorithm

13 s 3 t 2 6 7 4 5 24 18 2 9 14 15 5 30 20 44 16 11 6 19 6 15 9    14  0 S = { s, 2 } PQ = { 3, 4, 5, 6, 7, t }  X   X X decrease key X 33 Dijkstra's Shortest Path Algorithm

14 s 3 t 2 6 7 4 5 24 18 2 9 14 15 5 30 20 44 16 11 6 19 6 15 9    14  0 S = { s, 2 } PQ = { 3, 4, 5, 6, 7, t }  X   X X X 33 delmin Dijkstra's Shortest Path Algorithm

15 s 3 t 2 6 7 4 5 24 18 2 9 14 15 5 30 20 44 16 11 6 19 6 15 9    14  0 S = { s, 2, 6 } PQ = { 3, 4, 5, 7, t }  X   X X X 33 44 X X 32 Dijkstra's Shortest Path Algorithm

16 s 3 t 2 6 7 4 5 24 18 2 9 14 15 5 30 20 44 16 11 6 19 6 15 9   14  0 S = { s, 2, 6 } PQ = { 3, 4, 5, 7, t }  X   X X 44 X delmin  X 33 X 32 Dijkstra's Shortest Path Algorithm

17 s 3 t 2 6 7 4 5 18 2 9 14 15 5 30 20 44 16 11 6 19 6 15 9   14  0 S = { s, 2, 6, 7 } PQ = { 3, 4, 5, t }  X   X X 44 X 35 X 59 X 24  X 33 X 32 Dijkstra's Shortest Path Algorithm

18 s 3 t 2 6 7 4 5 24 18 2 9 14 15 5 30 20 44 16 11 6 19 6 15 9   14  0 S = { s, 2, 6, 7 } PQ = { 3, 4, 5, t }  X   X X 44 X 35 X 59 X delmin  X 33 X 32 Dijkstra's Shortest Path Algorithm

19 s 3 t 2 6 7 4 5 24 18 2 9 14 15 5 30 20 44 16 11 6 19 6 15 9   14  0 S = { s, 2, 3, 6, 7 } PQ = { 4, 5, t }  X   X X 44 X 35 X 59 XX 51 X 34  X 33 X 32 Dijkstra's Shortest Path Algorithm

20 s 3 t 2 6 7 4 5 18 2 9 14 15 5 30 20 44 16 11 6 19 6 15 9   14  0 S = { s, 2, 3, 6, 7 } PQ = { 4, 5, t }  X   X X 44 X 35 X 59 XX 51 X 34 delmin  X 33 X 32 24 Dijkstra's Shortest Path Algorithm

21 s 3 t 2 6 7 4 5 18 2 9 14 15 5 30 20 44 16 11 6 19 6 15 9   14  0 S = { s, 2, 3, 5, 6, 7 } PQ = { 4, t }  X   X X 44 X 35 X 59 XX 51 X 34 24 X 50 X 45  X 33 X 32 Dijkstra's Shortest Path Algorithm

22 s 3 t 2 6 7 4 5 18 2 9 14 15 5 30 20 44 16 11 6 19 6 15 9   14  0 S = { s, 2, 3, 5, 6, 7 } PQ = { 4, t }  X   X X 44 X 35 X 59 XX 51 X 34 24 X 50 X 45 delmin  X 33 X 32 Dijkstra's Shortest Path Algorithm

23 s 3 t 2 6 7 4 5 18 2 9 14 15 5 30 20 44 16 11 6 19 6 15 9   14  0 S = { s, 2, 3, 4, 5, 6, 7 } PQ = { t }  X   X X 44 X 35 X 59 XX 51 X 34 24 X 50 X 45  X 33 X 32 Dijkstra's Shortest Path Algorithm

24 s 3 t 2 6 7 4 5 18 2 9 14 15 5 30 20 44 16 11 6 19 6 15 9   14  0 S = { s, 2, 3, 4, 5, 6, 7 } PQ = { t }  X   X X 44 X 35 X 59 XX 51 X 34 X 50 X 45 delmin  X 33 X 32 24 Dijkstra's Shortest Path Algorithm

25 s 3 t 2 6 7 4 5 24 18 2 9 14 15 5 30 20 44 16 11 6 19 6 15 9   14  0 S = { s, 2, 3, 4, 5, 6, 7, t } PQ = { }  X   X X 44 X 35 X 59 XX 51 X 34 X 50 X 45  X 33 X 32 Dijkstra's Shortest Path Algorithm

26 s 3 t 2 6 7 4 5 24 18 2 9 14 15 5 30 20 44 16 11 6 19 6 15 9   14  0 S = { s, 2, 3, 4, 5, 6, 7, t } PQ = { }  X   X X 44 X 35 X 59 XX 51 X 34 X 50 X 45  X 33 X 32 Dijkstra's Shortest Path Algorithm

27 Running time is O(V 2 ) using linear array for priority queue. O((V + E) lg V) using binary heap. Time= O(V)·T(EXTRACT-MIN)+ O(E)·T(DECREASE-KEY)

28 Given a directed graph G = (V, E), with edge weights Cvw, find shortest path from node s to node t. Dijkstra. Can fail if negative edge-costs exist. u t sv 2 1 3 -6 Allow for negative weights The Shortest Paths Problem

29 BELLMAN-FORD(G, w, s) 1 INITIALIZE-SINGLE-SOURCE(G, s) 2 for i ← 1 to |V[G]| - 1 3 do for each edge (u, v) E[G] 4 do RELAX(u, v, w) 5 for each edge (u, v) E[G] 6 do if d[v] > d[u] + w(u, v) 7 then return FALSE 8 return TRUE Bellman-Ford Algorithm Time Complexity is O(VE). Can have negative- weight edges. Will “detect” reachable negative-weight cycles.

30 Bellman-Ford Algorithm Example 0    z uv x y 6 5 –3 9 7 7 8 –2 –4 2

31 Example 0  7  6 z uv x y 6 5 –3 9 7 7 8 –2 –4 2 Bellman-Ford Algorithm

32 Example 0 2 7 46 z uv x y 6 5 –3 9 7 7 8 –2 –4 2 Bellman-Ford Algorithm

33 0 2 7 42 z uv x y 6 5 –3 9 7 7 8 –2 –4 2 ExampleBellman-Ford Algorithm

34 0 -2 7 42 z uv x y 6 5 –3 9 7 7 8 –2 –4 2 ExampleBellman-Ford Algorithm

35 Another Example

36 Shortest Path Using Dynamic Programming

37 Shortest Paths: Dynamic Programming  Def. OPT(i, v) = length of shortest v-t path P using at most i edges.  Case 1: P uses at most i-1 edges.  OPT(i, v) = OPT(i-1, v)  Case 2: P uses exactly i edges.  if (v, w) is first edge, then OPT uses (v, w), and then selects best w-t path using at most i-1 edges

38 5 6 7 3 11 3 14 2 7 Start End a c d 7 5 6 g f e b Shortest Paths: Dynamic Programming

39 b = min(6+g, 5+e, 7+f) End 7 5 6 g f e b Shortest Paths: Dynamic Programming

40 g = min(6+d, 14) e = min(3+c, 7+d, 7+g) f = 2+c 6 7 3 2 7 c d g f e 14 a Shortest Paths: Dynamic Programming

41 c = min(5, 11+d) d = 3 5 11 3 Start a c d Shortest Paths: Dynamic Programming

42 b = min(6+g, 5+e, 7+f) g = min(6+d, 14) e = min(3+c, 7+d, 7+g) f = 2+c c = min(5, 11+d) d = 3 via “a to d” Shortest Paths: Dynamic Programming

43 b = min(6+g, 5+e, 7+f) g = min(6+d, 14) e = min(3+c, 7+d, 7+g) f = 2+c c = min(5, 11+d) d = 3 via “a to d” Shortest Paths: Dynamic Programming

44 b = min(6+g, 5+e, 7+f) g = min(6+3, 14) e = min(3+c, 7+3, 7+g) f = 2+c c = min(5, 11+3) d = 3 via “a to d” Shortest Paths: Dynamic Programming

45 b = min(6+g, 5+e, 7+f) g = min(9, 14) e = min(3+c, 10, 7+g) f = 2+c c = min(5, 14) d = 3 via “a to d” Shortest Paths: Dynamic Programming

46 b = min(6+g, 5+e, 7+f) g = min(9, 14) e = min(3+c, 10, 7+g) f = 2+c c = min(5, 14) d = 3 via “a to d” Shortest Paths: Dynamic Programming

47 b = min(6+g, 5+e, 7+f) g = 9 via “a to d to g” e = min(3+c, 10, 7+g) f = 2+c c = 5 via “a to c” d = 3 via “a to d” Shortest Paths: Dynamic Programming 5 11 3 a d

48 b = min(6+g, 5+e, 7+f) g = 9 via “a to d to g” e = min(3+c, 10, 7+g) f = 2+c c = 5 via “a to c” d = 3 via “a to d” Shortest Paths: Dynamic Programming 6 g 14 a d

49 b = min(6+9, 5+e, 7+f) g = 9 via “a to d to g” e = min(3+5, 10, 7+9) f = 2+5 c = 5 via “a to c” d = 3 via “a to d” Shortest Paths: Dynamic Programming

50 b = min(15, 5+e, 7+f) g = 9 via “a to d to g” e = min(8, 10, 16) f = 7 via “a to c to f” c = 5 via “a to c” d = 3 via “a to d” Shortest Paths: Dynamic Programming

51 b = min(15, 5+e, 7+f) g = 9 via “a to d to g” e = min(8, 10, 16) f = 7 via “a to c to f” c = 5 via “a to c” d = 3 via “a to d” Shortest Paths: Dynamic Programming

52 b = min(15, 5+e, 7+f) g = 9 via “a to d to g” e = 8 via “a to c to e” f = 7 via “a to c to f” c = 5 via “a to c” d = 3 via “a to d” Shortest Paths: Dynamic Programming

53 b = min(15, 5+e, 7+f) g = 9 via “a to d to g” e = 8 via “a to c to e” f = 7 via “a to c to f” c = 5 via “a to c” d = 3 via “a to d” Shortest Paths: Dynamic Programming

54 b = min(15, 5+8, 7+7) g = 9 via “a to d to g” e = 8 via “a to c to e” f = 7 via “a to c to f” c = 5 via “a to c” d = 3 via “a to d” Shortest Paths: Dynamic Programming

55 b = min(15, 13, 14) g = 9 via “a to d to g” e = 8 via “a to c to e” f = 7 via “a to c to f” c = 5 via “a to c” d = 3 via “a to d” Shortest Paths: Dynamic Programming

56 b = min(15, 13, 14) g = 9 via “a to d to g” e = 8 via “a to c to e” f = 7 via “a to c to f” c = 5 via “a to c” d = 3 via “a to d” Shortest Paths: Dynamic Programming

57 b = 13 via “a to c to e to b” g = 9 via “a to d to g” e = 8 via “a to c to e” f = 7 via “a to c to f” c = 5 via “a to c” d = 3 via “a to d” Shortest Paths: Dynamic Programming

58 6 7 11 3 14 2 7 Start End a c d 7 6 g f e b 5 3 5 Shortest Path = 13 Analysis.  (VE) time,  (V 2 ) space Shortest Paths: Dynamic Programming


Download ppt "Algorithm Course Dr. Aref Rashad February 20131 Algorithms Course..... Dr. Aref Rashad Part: 6 Shortest Path Algorithms."

Similar presentations


Ads by Google