Presentation is loading. Please wait.

Presentation is loading. Please wait.

Shortest Paths.

Similar presentations


Presentation on theme: "Shortest Paths."— Presentation transcript:

1 Shortest Paths

2 The problem A D 2 5 1 F 10 B 2 6 E C 3

3 Breadth-first search A D 1 1 1 F 1 B 1 1 E C 1

4 Breadth-first search Queue: A A D 1 1 1 F 1 B 1 1 E C 1

5 Breadth-first search Queue: B, C A D 1 1 1 F 1 B 1 1 1 E C 1 1

6 Breadth-first search Queue: C, D 2 A D 1 1 1 F 1 B 1 1 1 E C 1 1

7 Breadth-first search Queue: D, E 2 A D 1 1 1 F 1 B 1 1 1 E C 1 2 1

8 Breadth-first search Queue: E, F 2 A 1 3 D 1 1 F 1 B 1 1 1 E C 1 2 1

9 Breadth-first search Queue: F 2 A 1 3 D 1 1 F 1 B 1 1 1 E C 1 2 1

10 Breadth-first search Queue: 2 A 1 3 D 1 1 F 1 B 1 1 1 E C 1 2 1

11 Breadth-first search Time: O(n+e)

12 Dijkstra's algorithm A D 2 5 1 F 10 B 2 6 E C 3

13 Dijkstra's algorithm Priority queue: A, B, C, D, E, F oo A D 2 5 1 F
A D 2 5 1 F oo 10 B 2 oo 6 E C 3 oo oo

14 Dijkstra's algorithm Priority queue: C, B, D, E, F oo A D 2 5 1 F oo
A D 2 5 1 F oo 10 B 2 5 6 E C 3 oo 2

15 Dijkstra's algorithm Priority queue: B, E, D, F oo A D 2 5 1 F oo 10 B
A D 2 5 1 F oo 10 B 2 5 6 E C 3 5 2

16 Dijkstra's algorithm Priority queue: E, D, F 6 A D 2 5 1 F oo 10 B 2 5
A D 2 5 1 F oo 10 B 2 5 6 E C 3 5 2

17 Dijkstra's algorithm Priority queue: D, F 6 A D 2 5 1 F 11 10 B 2 5 6
A D 2 5 1 F 11 10 B 2 5 6 E C 3 5 2

18 Dijkstra's algorithm Priority queue: F 6 A D 2 5 1 F 8 10 B 2 5 6 E C
A D 2 5 1 F 8 10 B 2 5 6 E C 3 5 2

19 Dijkstra's algorithm Priority queue: 6 A D 2 5 1 F 8 10 B 2 5 6 E C 3
A D 2 5 1 F 8 10 B 2 5 6 E C 3 5 2

20 Dijkstra's algorithm Time: O((n + e)log n)

21 Floyd-Warshall Algorithm
Start: d[i][j] = weight of edge from i to j or infinity if no edge End: d[i][j] = shortest distance from i to j or infinity if no edge for j = 1 to n for i = 1 to n for k = 1 to n d[i][k] = min(d[i][k], d[i][j] + d[j][k]) NB: Loop order is very important! Time: O(n^3) Space: O(n^2)


Download ppt "Shortest Paths."

Similar presentations


Ads by Google