Hi
I'm just learning java and one of the projects I'm working on is doing the Dijkstra Algorithm. My problem is not the Dijkstra algorithm -- it does work! My problem is how I have to input the data:
public class TestShortestPath
{
public static void main(String[] args)
{
dijkstra theGraph = new dijkstra();
theGraph.addVertex('O'); // 0 (start)
theGraph.addVertex('A'); // 1
theGraph.addVertex('B'); // 2
theGraph.addVertex('C'); // 3
theGraph.addVertex('D'); // 4
theGraph.addVertex('E'); // 5
theGraph.addVertex('T'); // 6
theGraph.addedge(0, 1, 2); // O to A is 2
theGraph.addedge(0, 2, 5); // O to B is 5
theGraph.addedge(0, 3, 4); // O to C is 4
theGraph.addedge(1, 4, 7); // A to D is 7
theGraph.addedge(1, 2, 2); // A to B is 2
theGraph.addedge(1, 0, 2); // A to O is 2
theGraph.addedge(2, 1, 2); // B to A is 2
theGraph.addedge(2, 0, 3); // B to O is 3
theGraph.addedge(2, 3, 1); // B to C is 1
theGraph.addedge(2, 5, 3); // B to E is 3
theGraph.addedge(2, 4, 4); // B to D is 4
theGraph.addedge(3, 5, 4); // C to E is 4
theGraph.addedge(3, 2, 1); // C to B is 1
theGraph.addedge(3, 0, 4); // C to O is 4
theGraph.addedge(4, 1, 7); // D to A is 7
theGraph.addedge(4, 2, 4); // D to B is 4
theGraph.addedge(4, 6, 5); // D to T is 5
theGraph.addedge(4, 5, 1); // D to E is 1
theGraph.addedge(5, 6, 7); // E to T is 7
theGraph.addedge(5, 4, 1); // E to D is 1
theGraph.addedge(5, 2, 3); // E to B is 3
theGraph.addedge(5, 3, 4); // E to C is 4
theGraph.addedge(6, 5, 7); // T to E is 7
theGraph.addedge(6, 4, 5); // T to D is 5
System.out.println("Shortest Paths from Node O : ");
theGraph.path();
System.out.println();
} //end main()
}//end class TestShortestPath
//-------------------------------------------
The above is dealt with this:
/*
* Procedure addVertex. Takes a characther as input and
* adds it to the metrix array.
*/
public void addVertex(char m)
{
vertexList[vertCount++] = new Vertex(m);
}
/*
* Procedure addedge. Double matrix with the starting
* and ending edges. Int x is the distance from each other.
*/
public void addedge(int start, int end, int x)
{
adjMatrix[start][end] = x;
}
What is what I need? A for-loop that will convert the above into an elegant for-loop procedure in Java. So the user can put whatever he or she wants.
I would greatly appreciate your help.