Upcoming SlideShare
×

# Spsp fw

300 views
225 views

Published on

Published in: Technology, Education
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
300
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
17
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Spsp fw

1. 1. Single pair shortest path problem MD Tanvir Anwoar & Shajedur Rahman
2. 2. Introduction: In graph theory, the shortest path problem is the problem of finding a path between two vertices (or nodes) in a graph such that the sum of the weights of its constituent edges is minimized. This is analogous to the problem of finding the shortest path between two intersections on a road map: the graph's vertices correspond to intersections and the edges correspond to road segments, each weighted by the length of its road segment.
3. 3. Application: * The traffic and length of the highways are path weights. * Vehicle routing problem solving * Solving network design problem * In video games, these algorithms are frequently used to find the shortest path between two points on a map Purpose of the problem solving: * Improve Quality of the service, Reducing time and cost
4. 4. Problem solving methods : Dijkstra Algorithm : a. All edge weights should be non-negative b. Each iteration of Dijkstra takes O(n^2) for array-based or O(m log n) for heap-based c. Total complexity is either O(n^3) or O(mn log n) d. This is a case where just repeatedly using a solution to a simpler problem works out fine.
5. 5. Problem solving methods : Bellman-Ford Algorithm: a. If some edge weights are negative b. It has complexity O(nm) for a single source c. Total sources solution is O(n^2 m), which is O(n4) for dense graphs
6. 6. Problem solving methods : Floyd-Warshall Algorithm : a. If some edge weights are negative b. Dynamic programming solution to compute all sources shortest paths c. Works with negative weights (or without) – we assume no negative cycles d. Complexity O(n^3)
7. 7. Floyd-Warshall Algorithm 1. Graph should be directed 2. It may contain negative edges but no negative cycle 3. Find the shortest path between all the pairs of nodes A B
8. 8. Floyd-Warshall Algorithm 1. Graph should be directed 2. It may contain negative edges but no negative cycle 3. Find the shortest path between all the pairs of nodes -2 A B -1
9. 9. Floyd-Warshall Algorithm Example : 1 2 4 3
10. 10. Floyd-Warshall Algorithm Example : 1 4 D 1 1,4 = 2 3
11. 11. Floyd-Warshall Algorithm Example : 1 4 D 1 1,4 2 3 = 1 ,2,4
12. 12. Floyd-Warshall Algorithm Example : 1 4 D 1 1,4 2 3 = 1 ,2,4 ; 1,4
13. 13. Floyd-Warshall Algorithm matrix formation : D (k) { min (D if k>= 1 (k-1) (I,j) = (I,j)) , D (k-1) (I,k ) + D (k-1) (k,j) }
14. 14. Floyd-Warshall Algorithm matrix formation : 3 1 2 -4 7 4 8 5 3 1 2 6 4 -5 0 X X 2 X 3 0 4 X X D(0) 8 X X 1 0 X -5 1 X 0 -4 7 -4 7 0
15. 15. Floyd-Warshall Algorithm matrix formation : 3 1 2 -4 7 4 8 5 3 1 0 X X 2 X 3 0 4 X X D(0) 8 X X 11 0 X -5 1 X 0 2 6 D (0) 4 -5 (2,4) =1 -4 7 -4 7 0
16. 16. Floyd-Warshall Algorithm matrix formation : 3 1 2 -4 7 4 8 5 3 1 2 6 0 X X 2 X 3 0 4 5 X D(1) 8 X X 1 0 X -5 0 X 6 -4 7 X -2 -2 0 Adjacency Matrix 4 -5 D (1) (4,5) = -2
17. 17. Floyd-Warshall Algorithm matrix formation : 3 1 2 -4 7 4 8 5 3 1 2 6 0 3 7 2 8 1 0 4 -1 5 D(5) -3 2 -4 1 -4 0 5 -5 0 1 6 -4 -1 3 -2 0 Adjacency Matrix 4 -5 D (5) (2,3) = -4
18. 18. Floyd-Warshall Algorithm Pseudocode: 1. For i=1 to |V| do 2. For j=1 to |V| do 3. S[i,j,0] = w(i,j) 4. For k=1 to |V| do 5. For i=1 to |V| do 6. For j=1 to |V| do 7. S[i,j,k] = min { 8. S[i,j,k-1], 9. S[i,k,k-1]+S[k,j,k-1] } 10. Return S[:,:,n] # return 2d array with n = |V|
19. 19. Floyd-Warshall Algorithm