210 likes | 309 Views
Lecture 12:. Shortest-Path Problems. Floyd's Algorithm. Another popular graph optimization problem is All-Pairs Shortest Path. In this problem, you are to compute the minimal path from every node to every other node in a directed weighted graph.
E N D
Lecture 12: Shortest-Path Problems
Floyd's Algorithm Another popular graph optimization problem is All-Pairs Shortest Path. In this problem, you are to compute the minimal path from every node to every other node in a directed weighted graph. The array shown below is a representation of the directed weighted graph. Each row and column represents a particular node in the graph, while each entry in the body of the array is the weight of an arc connected the corresponding nodes.
A B C D E F G H A B C D E F G H - 5 7 6 4 10 8 9 8 - 14 9 3 4 6 2 7 9 - 11 10 9 5 7 16 6 8 - 5 7 7 9 1 3 2 5 - 8 6 7 12 8 5 3 2 - 10 13 9 5 7 9 6 3 - 4 3 9 6 8 5 7 9 - Traveling Salesperson Problem (TSP) A B H C G D E F
Finding the Closest Available Next City publicstaticint minAvail(int row) { int minval = int.MaxValue; int imin = -1; for (int j = 0; j < n; j++) { if (row != j && !used[j] && M[row, j] < minval) { minval = M[row, j]; imin = j; } } return imin; } jth column M = ith row
L A B C D E F G H itour 3 1 0 6 used 1 1 1 1 0 0 1 0 tour C B A G Greedy TSP publicstaticvoid doGreedyTSP(int p) { int k = 0; do { itour[k] = p; tour[k] = L[p]; used[itour[k]] = true; p = minAvail(p); k += 1; } while (k < n); }
2 v3 v2 v1 v0 v5 v4 5 1 3 1 1 4 1 6 3 The single source shortest path problem is as follows. We are given a directed graph with nonnegative edge weights G = (V,E) and a distinguished source vertex, . The problem is to determine the distance from the source vertex to every vertex in the graph. Single Source Shortest Path Given a weighted graph G find the minimum weight path from a specified vertex v0 to every other vertex.
2 v3 v4 v5 v0 v1 v2 v1 v2 v3 v4 v5 node minimum list path v1 v2 v3 v4 v5 5 1 4 - 6 {2} 3 42 6 {24} 3 3 5 {241} 35 {2413} 4 5 1 3 1 1 4 1 6 3 Dijkstra's Algorithm for SSSP v1 v2 v3 v4 v5 5 1 4 - 6 {2} 3 4 2 6 {24} 3 3 5 {241} 3 5 {2413} 4 v1 v2 v3 v4 v5 5 1 4 - 6 {2} 3 4 2 6 {24} 3 3 5 {241} 3 5 v1 v2 v3 v4 v5 5 1 4 - 6 {2} 3 4 2 6 {24} 3 3 5 v1 v2 v3 v4 v5 5 1 4 - 6 {2} 3 4 2 6 v1 v2 v3 v4 v5 5 1 4 - 6
Eulers Formula Let G be a connected planar simple graph with e edges and v vertices. Let r be the number of regions in a planar representation of G. Then r = e - v + 2.