Single-Source Shortest Path

Directed graph G= (V,E)

Path from u to v

Before: length of path = number of edges

Cost C: E -> R+

Length(or cost) of path: sum of costs of edges on path

1--2: 1,3,2 25
1--3: 1,3 15
1--4: 1,4 20
1--5: 1,3,2,5 40

Shortest path from a single source node to all other nodes.

Figure 4.15 (page 242)


P1: shortest path from v0 to v.

If d2 < d1, then P1 is not a shortest path from v0 to v.

Lemma: If v0, v1, v2..., vk, P is a shortest path from v0 to v, then v0, v1,..., vi is a shortest path from v0 to vi for all 1 < i < k (shortest total is shortest all along way)

Also, dist (v0,v1) < dist (v0,v2) < .... < dist (v0,v)

Scan graph for nodes within a given radius of v0

Thus first find shortest path for each intermittent stop possible. Keep increasing radius.

S = set of nodes for which shortest paths have been computed.

The next node to be included must be the one with the shortest path from v0 among all nodes not in S.

Let this node be v.

All intermediate nodes on the shortest path from v0 to v must belong to S.

S -- set of nodes "closest" to v0 for which shortest paths have already been computed.

v <- next node to be included in S.

Greedy: v is that node in V-S closest to v0

Dist (u)<-- cost of shortest path from v0 to u passing only through nodes in S. (for all u V)

Once v is included in S, dist's will have to be corrected. So each time we put a vertex v in, we have to check all vertices v is connected to, and increment Dist(u)

Dijkstra's Shortest Path algorithm:

Code in book on page 245 has constant for initial v.

Also see another form and this code traced

Time: O(n2)

Dikstra Quotes: "If you don't know what your program is supposed to do, you'd better not start writing it."

"The question of whether a computer can think is no more interesting than the question of whether a submarine can swim."

Negative costs

Negative cycles

Defn. of path does not exclude cycles

shortest path to v is negative !

If we restrict ourselves to paths with no cycles:

Negative cycles in shortest paths

- Most negative edge won't work

Single source shortest paths: nonnegative edge costs

All pairs shortest paths: no negative cycles.