shortest path graph theory basics anil kishore. introduction weighted graph – edges have weights...
Post on 04-Jan-2016
230 Views
Preview:
TRANSCRIPT
Shortest Path
Graph Theory Basics
Anil Kishore
Introduction
• Weighted Graph – Edges have weights• Shortest Path problem is finding a path
between two vertices such that the sum of the weights of edges along the path is minimized
A
B C
D
E
F
G
20
4
6
25
8
3
2
Variations
• Single-Source Shortest Path - find shortest paths from a source vertex S to all other vertices
• Single-Destination Shortest Path - find shortest paths from all vertices in the directed graph to a single destination vertex D
• All-Pairs Shortest Path - find shortest paths between every pair of vertices u, v in the graph.
Applications
• Road Network– Cities in a country can be considered as vertices and the
roads connecting them are edges– Edge weight is length of the road ( distance to be
travelled )– One-way roads ( Directed Edges )– Find shortest path from city A to city B
• … many other
Algorithms
• Dijkstra's algorithm : single-source shortest path problem
• Bellman–Ford algorithm : single-source shortest path problem if edge weights may be negative.
• Floyd–Warshall algorithm : all pairs shortest paths.
Dijkstra’s AlgorithmDijkstra( S ) set dist[u] = INF for all vertices u
set dist[S] = 0 and insert S in a data structure q while( q is not empty )
u = remove the vertex with minimum dist in q
for v in nbrs(u)newDist = dist[u] + weight(u,v)if( newDist < dist[v] )
dist[v] = newDist;end-if
end-forend-while
end-Diijkstra
Dijkstra’s Algorithm example
A
B C
D
E
F
G
20
4
6
25
8
3
2
Dijkstra’s Algorithm example
A
B C
D
E
F
G
20
4
6
25
8
3
2
0
Dijkstra’s Algorithm example
A
B C
D
E
F
G
20
4
6
25
8
3
2
0
20
4
6
Dijkstra’s Algorithm example
A
B C
D
E
F
G
20
4
6
25
8
3
2
0
20
4
6
Dijkstra’s Algorithm example
A
B C
D
E
F
G
20
4
6
25
8
3
2
0
20
4
69
12
Dijkstra’s Algorithm example
A
B C
D
E
F
G
20
4
6
25
8
3
2
0
20
4
69
12
Dijkstra’s Algorithm example
A
B C
D
E
F
G
20
4
6
25
8
3
2
0
20
4
68
12
Dijkstra’s Algorithm example
A
B C
D
E
F
G
20
4
6
25
8
3
2
0
20
4
68
12
Dijkstra’s Algorithm example
A
B C
D
E
F
G
20
4
6
25
8
3
2
0
20
4
68
12
Dijkstra’s Algorithm example
A
B C
D
E
F
G
20
4
6
25
8
3
2
0
20
4
68
12
Dijkstra’s Algorithm example
A
B C
D
E
F
G
20
4
6
25
8
3
2
0
20
4
68
12
15
Dijkstra’s Algorithm example
A
B C
D
E
F
G
20
4
6
25
8
3
2
0
20
4
68
12
15
Dijkstra’s Algorithm example
A
B C
D
E
F
G
20
4
6
25
8
3
2
0
17
4
68
12
15
Dijkstra’s Algorithm example
A
B C
D
E
F
G
20
4
6
25
8
3
2
0
17
4
68
12
15
Dijkstra’s Algorithm example
A
B C
D
E
F
G
4
6
2
8
3
2
0
17
4
68
12
15
Shortest Path Tree
Dijkstra’s Algorithm Analysis
• Running time depends mainly on the associated data structure q
• q– Array : O(n2)– Binary Heap : O( (n+m) logn )– Fibonacci Heap : O( m + n logn )
• What if the edge weights are negative ?
Bellman-Ford Algorithm
Bellman Ford( S )set dist[u] = INF for all udist[S] = 0;Repeat (n-1) times
for all edges (u,v) in the graphif(dist[v] > dist[u] + weight(u,v) )
dist[v] = dist[u] + graph[u][v];end-if
end-forend-repeat
end-Bellman-Ford // Complexity : O( n m )
Bellman-Ford Algorithm example
A
B C
D
E
F
G
20
4
6
25
8
3
2
All-Pairs Shortest Path
• Repeat Single-Source Shortest Path n times
• Matrix Exponentiation – Recursive Squaring
• Dynamic Programming – Floyd-Warshall
Floyd-Warshall AlgorithmFloyd-Warshall
set dist[u][v] = INF for all u,vfor each edge (u,v) in the graph
dist[u][v] = weight(u,v)end-for// Each stage has shortest paths using intermediate vertices (1..k−1)for k: 1 to n for u : 1 to n
for v : 1 to ndist[u][v] = minimum( dist[u][v], dist[u][k] + dist[k][v] )
end-for-v end-for-uend-for-k
end-floyd-warshall // Complexity : O( n3 )
References
• http://en.wikipedia.org/wiki/Shortest_path_problem
• Introduction to AlgorithmsThomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein
top related