Graph - import java.util.*; i import java.io.*; class...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
import java.util.*; import java.io.*; i class Vertex { String name; // Vertex name LinkedList adj; // Adjacent vertices int dist; // Cost Vertex path; // Previous vertex on shortest path public Vertex( String nm ) { name = nm; adj = new LinkedList( ); reset( ); } public void reset( ) { dist = Graph.INFINITY; path = null; } } class Graph { public static final int INFINITY = Integer.MAX_VALUE; private HashMap vertexMap = new HashMap( ); // Maps vertices to internal Vertex V public void addEdge( String sourceName, String destName ) { Vertex v = getVertex( sourceName ); Vertex w = getVertex( destName ); v.adj.add( w ); } public void printPath( String destName ) throws NoSuchElementException { Vertex w = (Vertex) vertexMap.get( destName ); if( w == null ) throw new NoSuchElementException( "Destination vertex not found" ); else if( w.dist == INFINITY ) System.out.println( destName + " is unreachable" ); else { printPath( w ); System.out.println( ); } } // If vertexName is not present, add it to vertexMap. // In either case, return the Vertex.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 3

Graph - import java.util.*; i import java.io.*; class...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online