Download presentation

Presentation is loading. Please wait.

Published byHamza Faul Modified over 3 years ago

1
Design and Analysis of Algorithms Single-source shortest paths, all-pairs shortest paths Haidong Xue Summer 2012, at GSU

2
Single-source shortest paths

3
1 23 4 5 10 5 32 1 2 4 9 7 6 Given this weighted, directed graph, what is the weight of path: ? 10+2=12

4
Single-source shortest paths 1 23 4 5 10 5 32 1 2 4 9 7 6 Given this weighted, directed graph, what is the weight of path: ? 10+2 + 3 + 2 = 17

5
Single-source shortest paths 1 23 4 5 10 5 32 1 2 4 9 7 6 Given this weighted, directed graph, what is the weight of path: ?

6
Single-source shortest paths 1 23 4 5 10 5 32 1 2 4 9 7 -6 Given this weighted, directed graph, what is the weight of path: and ?? 4-6 = -2 and -6+4-6+4 = -4 Negative cycleThere is no shortest path from 1 to 5

7
Single-source shortest paths Shortest path of a pair of vertices : a path from u to v, with minimum path weight Applications: – Your GPS navigator – If weights are time, it produces the fastest route – If weights are gas cost, it produces the lowest cost route – If weights are distance, it produces the shortest route

8
Single-source shortest paths Single-source shortest path problem: given a weighted, directed graph G=(V, E) with source vertex s, find all the shortest (least weight) paths from s to all vertices in V.

9
Single-source shortest paths Two classic algorithms to solve single-source shortest path problem – Bellman-Ford algorithm A dynamic programming algorithm Works when some weights are negative – Dijkstra’s algorithm A greedy algorithm Faster than Bellman-Ford Works when weights are all non-negative

10
Bellman-Ford algorithm Observation: If there is a negative cycle, there is no solution – Add this cycle again can always produces a less weight path If there is no negative cycle, a shortest path has at most |V|-1 edges Idea: Solve it using dynamic programming For all the paths have at most 0 edge, find all the shortest paths For all the paths have at most 1 edge, find all the shortest paths … For all the paths have at most |V|-1 edge, find all the shortest paths

11
Bellman-Ford algorithm //Initialize 0-edge shortest paths //bottom-up construct 0-to-(|V|-1)-edges shortest paths //test negative cycle

12
Bellman-Ford algorithm e.g. 1 2 3 10 1 What is the 0-edge shortest path from 1 to 1? <> with path weight 0 What is the 0-edge shortest path from 1 to 2? What is the 0-edge shortest path from 1 to 3? 0 20

13
Bellman-Ford algorithm e.g. 1 2 3 10 1 What is the at most 1-edge shortest path from 1 to 1? <> with path weight 0 What is the at most 1-edge shortest path from 1 to 2? with path weight 10 What is the at most 1-edge shortest path from 1 to 3? with path weight 20 0 20 In Bellman-Ford, they are calculated by scan all edges once

14
Bellman-Ford algorithm e.g. 1 2 3 10 1 What is the at most 2-edges shortest path from 1 to 1? <> with path weight 0 What is the at most 2-edges shortest path from 1 to 2? with path weight 10 What is the at most 2-edges shortest path from 1 to 3? with path weight 11 0 20 In Bellman-Ford, they are calculated by scan all edges once

15
Bellman-Ford algorithm 1 23 4 5 6 7 8 5 9 7 -3 2 -4 -2 All 0 edge shortest paths 0

16
Bellman-Ford algorithm 1 23 4 5 6 7 8 5 9 7 -3 2 -4 -2 Calculate all at most 1 edge shortest paths 0 /0

17
Bellman-Ford algorithm 1 23 4 5 6 7 8 5 9 7 -3 2 -4 -2 Calculate all at most 2 edges shortest paths 0 6 7 /11 /7 /2 /0 /4

18
Bellman-Ford algorithm 1 23 4 5 6 7 8 5 9 7 -3 2 -4 -2 Calculate all at most 3 edges shortest paths 0 6 4 7 2 /4 /7 /2 /0

19
Bellman-Ford algorithm 1 23 4 5 6 7 8 5 9 7 -3 2 -4 -2 Calculate all at most 4 edges shortest paths 0 2 4 7 2 /4 /7 /-2 /0

20
Bellman-Ford algorithm 1 23 4 5 6 7 8 5 9 7 -3 2 -4 -2 Final result: 0 2 4 7 -2 What is the shortest path from 1 to 5? What is weight of this path? 1, 4, 3, 2, 5 -2 What is the shortest path from 1 to 2, 3, and 4?

21
Dijkstra’s Algorithm

22
1 23 4 5 10 5 32 1 2 4 9 7 6 The shortest path of the vertex with smallest distance is determined 0

23
Dijkstra’s Algorithm 1 23 4 5 10 5 32 1 2 4 9 7 6 Choose a vertex whose shortest path is now determined 0 10

24
Dijkstra’s Algorithm 1 23 4 5 10 5 32 1 2 4 9 7 6 Choose a vertex whose shortest path is now determined 0 8 14

25
Dijkstra’s Algorithm 1 23 4 5 10 5 32 1 2 4 9 7 6 Choose a vertex whose shortest path is now determined 0 8 13

26
Dijkstra’s Algorithm 1 23 4 5 10 5 32 1 2 4 9 7 6 Choose a vertex whose shortest path is now determined 0 8 9

27
Dijkstra’s Algorithm 1 23 4 5 10 5 32 1 2 4 9 7 6 Final result: 0 8 9 What is the shortest path from 1 to 5? What is weight of this path? 1, 4, 5 7 What is the shortest path from 1 to 2, 3, and 4?

28
All-pairs shortest paths

31
What are the weights of shortest paths with no intermediate vertices, D(0)? 1234 1 2 3 4 0 4 3 2 60 12 3 4 3 2 4 9 6

32
All-pairs shortest paths 12 3 4 3 9 4 2 6 D(0) 1234 1 2 3 4 0 04 3 2 0 9 60 What are the weights of shortest paths with intermediate vertex 1? 1234 1 2 3 4 0 04 3 0 9 60 D(1)

33
All-pairs shortest paths 4 D(1) What are the weights of shortest paths with intermediate vertices 1 and 2? 1234 1 2 3 4 0 04 3 0 6 60 D(2) 1234 1 2 3 4 0 04 3 0 9 60 12 3 4 3 9 4 2 6

34
All-pairs shortest paths D(2) What are the weights of shortest paths with intermediate vertices 1, 2 and 3? 1234 1 2 3 4 0 04 3 0 6 60 D(3) 1234 1 2 3 4 0 04 3 0 6 60 12 3 4 3 9 4 2 6

35
All-pairs shortest paths D(3) What are the weights of shortest paths with intermediate vertices 1, 2, 3 and 4? 1234 1 2 3 4 0 04 3 0 6 60 D(4) 1234 1 2 3 4 0 04 3 0 6 60 12 3 4 3 9 4 2 6

36
All-pairs shortest paths D(0) 1234 1 2 3 4 D(1) 1234 1 2 3 4 D(2) 1234 1 2 3 4 D(3) 1234 1 2 3 4 Add predecessor information to reconstruct a shortest path 0/n 4/2 3/3 0/n 9/3 6/4 0/n If updated the predecessor i-j in D(k) is the same as the predecessor k-j in D(k-1) 0/n 4/2 3/3 0/n 9/3 6/4 0/n 4/2 3/3 0/n 6/26/2 6/4 0/n 4/2 3/3 0/n 6/2 6/4 0/n

37
All-pairs shortest paths D(4) 12 3 4 3 9 4 2 6 What is the shortest path from 3 to 4? What is weight of this path? 3, 2, 4 6 1234 1 2 3 4 0/n 4/2 3/3 0/n 6/2 6/4 0/n

Similar presentations

Presentation is loading. Please wait....

OK

Introduction to Algorithms Jiafen Liu Sept. 2013.

Introduction to Algorithms Jiafen Liu Sept. 2013.

© 2018 SlidePlayer.com Inc.

All rights reserved.

To ensure the functioning of the site, we use **cookies**. We share information about your activities on the site with our partners and Google partners: social networks and companies engaged in advertising and web analytics. For more information, see the Privacy Policy and Google Privacy & Terms.
Your consent to our cookies if you continue to use this website.

Ads by Google

Ppt on rag picking Ppt on total internal reflection evanescence Ppt on coalition government in israel Ppt on main bodies of uno Ppt on limitation act in pakistan Free ppt on brain machine interface software Ppt on solar system in hindi Ppt on obesity management strategies Ppt on resources and development of class 10 Ppt on sectors of indian economy download