Download - Lecture 12:
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.
Weighted Graphs
Traveling Salesperson Problem (TSP)
A B
C
D
EF
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 -
A
B
C
D
E
F
G
H
A B C D E F G H
public static int 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;}
Finding the Closest Available Next City
ith
ro
w
jth column
M =
public static void 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);}
Greedy TSP
3 1 0 6
1 1 1 1 0 0 1 0
C B A G
itour
used
tour
A B C D E F G HL
Single Source Shortest Path
Given a weighted graph G find the minimum weight path from a specified vertex v0 to every other vertex.
2
11
11 3
4
5
3
6
v1
v0
v5
v4
v3
v2
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.
Vs
v1 v2 v3 v4 v5
node minimum
list path
v1 v2 v3 v4 v5
5 1 4 - 6
v1 v2 v3 v4 v5
5 1 4 - 6
{2} 3 4 2 6
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
{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
{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
{2413} 4
2
11
11 3
4
5
3
6
v1
v0
v5
v4
v3
v2
Dijkstra's Algorithm for SSSP
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.