Chapter 18: Graph Applications (continued)

Single-Source Shortest Path

Text page 721

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
from--tonodes in pathlength
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.

See also Figure 18.9

Rationale/Premises

Technique - Greedy Method:

Dijkstra's shortest path

Dijkstra's shortest path algorithm: famous

walk-through

Dijkstra's Shortest Path Algorithm: (obviously pseudocode - I will let you implement it in Java readme)

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

Graph:

Next To Do Certain Pathlength Pathpred
    Of:     Is:     Of:     Is:    
Try first yourself, and then see this chart
Filled in

O(n2)

One more visual example


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?

Implementation cannot be totally function independent (what do I mean by that?) Discuss

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)