Java Reference
In-Depth Information
1
/**
2
* Initializes the vertex output info prior to running
3
* any shortest path algorithm.
4
*/
5
private void clearAll( )
6
{
7
for( Vertex v : vertexMap.values( ) )
8
v.reset( );
9
}
figure 14.11
Private routine for
initializing the output
members for use by
the shortest-path
algorithms
1
/**
2
* Recursive routine to print shortest path to dest
3
* after running shortest path algorithm. The path
4
* is known to exist.
5
*/
6
private void printPath( Vertex dest )
7
{
8
if( dest.prev != null )
9
{
10
printPath( dest.prev );
11
System.out.print( " to " );
12
}
13
System.out.print( dest.name );
14
}
figure 14.12
A recursive routine for
printing the shortest
path
1
/**
2
* Driver routine to handle unreachables and print total cost.
3
* It calls recursive routine to print shortest path to
4
* destNode after a shortest path algorithm has run.
5
*/
6
public void printPath( String destName )
7
{
8
Vertex w = vertexMap.get( destName );
9
if( w == null )
10
throw new NoSuchElementException( );
11
else if( w.dist == INFINITY )
12
System.out.println( destName + " is unreachable" );
13
else
14
{
15
System.out.print( "(Cost is: " + w.dist + ") " );
16
printPath( w );
17
System.out.println( );
18
}
19
}
figure 14.13
A routine for printing
the shortest path by
consulting the graph
table (see Figure
14.5)
Search WWH ::
Custom Search