Directed graph G= (V,E)
Path from u to v
Length(or cost) of path: sum of costs of edges on path
Before: length of path = number of edges
Cost C: E -> R+
| from--to | nodes in path | length |
| 1--2: | 1,3,2 | 25 |
| 1--3: | 1,3 | 15 |
| 1--4: | 1,4 | 20 |
| 1--5: | 1,3,2,5 | 40 |
See also Figure 18.9
Rationale/Premises
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 vk, then v0,
v1,..., vi is a shortest path from v0 to vi for all 1 < i <
k
Also, dist (v0,v1) < dist (v0,v2) < .... < dist
(v0,v)
Technique - Greedy Method:
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.
Dijkstra's Shortest Path Algorithm: (obviously pseudocode - I will let you implement it in Java readme)
Graph:
P1: proposed shortest path from v0 to v. Path P1 includes the subpath from v0 to u of distance d1
(shortest total is shortest all along way) Dynamic programming
Scan graph for nodes within a given radius of v0
Dijkstra's shortest path
Dijkstra's shortest path algorithm: famous
walk-through
method ShortestPath (Vertex S,T);
Begin
For each vertex Vi in V (in G) // initialize
Begin
Pathlength (Vi) = infinity;
Pathpred (Vi) = Undefined;
Certain (Vi) = False;
End
Pathlength (S)=0;
Nextodo={S};
While NotCertain (T) do
Begin
Pick vertex Vi in Nextodo with shortest pathlength;
Certain (Vi)= true;
for each neighbor Wj of Vi
if Pathlength (Wj) > (Pathlength (Vi) + Dist (Vi,Wj)) then
begin
Pathlength (Wj) = Pathlength (V)+ Dist (Vi,Wj);
Pathpred (Wj) = Vi;
end;
Nextodo = Nextodo - Vi + {neighbors of Vi which are not Certain}
End;
End;
Trace example-- Dijkstra's Algorithm gradually expands set of 'Certain'
Vertices
| Next To Do | Certain | Pathlength | Pathpred |
| Of: Is: | Of: Is: |
O(n2)
Dijkstra 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."
Additional tidbits a long time ago and more
quotes
Fine, let's talk about object implementation.
Alternative Code in book on page 725.
We have Graphs, Vertices, Edges, Paths.
Should they be their own objects?
What information goes in these?
By the way: BF and DF numbering
0(n+ e)
Java applet for Dijksta's Shortest Path See demos on left.
(A (6,10) graph means 6 vertices and 10 edges)
Implementation cannot be totally function independent (what do I mean
by that?) Discuss
Should I just represent an edge as a triplet? (x,y,c) or should it be an object with two vertices and a cost
Should they be temporary variables inside code? or should they be their own class with instances that hold information for
later use?