public class DirectedWeightedExampleSlide18
{
public static void main(String[] args)
{
int currentVertex, userChoice;
Scanner input = new Scanner(System.in);
// create graph using your WeightedGraph based on author's Graph
WeightedGraph myGraph = new WeightedGraph(4);
// add labels
myGraph.setLabel(0,"Spot zero");
myGraph.setLabel(1,"Spot one");
myGraph.setLabel(2,"Spot two");
myGraph.setLabel(3,"Spot three");
// Add each edge (this directed Graph has 5 edges,
// so we add 5 edges)
myGraph.addEdge(0,2,9);
myGraph.addEdge(1,0,7);
myGraph.addEdge(2,3,12);
myGraph.addEdge(3,0,15);
myGraph.addEdge(3,1,6);
// let's pretend we are on v2
currentVertex = 2;
// do - this is a good place for the top of the loop
// display the current vertex
System.out.println(" You are currently at vertex " + currentVertex +
"-" + myGraph.getLabel(currentVertex));
// who are our neighbors?
System.out.println("neighbors of " + currentVertex + " are: ");
System.out.printf("%3s %-10s %4s ","#","Neighbor","Cost");
System.out.printf("%3s %-10s %4s ","===","==========","====");
if (myGraph.neighbors(currentVertex).length == 0)
System.out.println("No Neighbors");
else
{
for (int neighbor : myGraph.neighbors(currentVertex))
{
System.out.printf("%3d %-10s %4d ",
neighbor,
myGraph.getLabel(neighbor),
myGraph.getWeight(currentVertex,neighbor));
}
System.out.println();
}
// suppose I was interacting with user,
// I could ask for their choice
/* 1: if their choice is -1 then say goodbye
2: else if the user enters a value too large or too small report an out of range error
3: else if their choice is a valid neighbor then change the current vertex to that choice
4: else then report the vertex they entered is unreachable
hint for 3:
else if myGraph.isEdge(currentVertex,choice)
currentVertex=choice;
*/
// while...this is a good place for the bottom of the loop
System.out.println(" you need to modify this file: \tDirectedWeightedExampleSlide18.java"+
" press enter to continue...");
input.nextLine();
}
}