Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

IN JAVA how do I do I grab this information from the text file and put it into m

ID: 3804742 • Letter: I

Question

IN JAVA how do I do I grab this information from the text file and put it into my Edge[] ?

I have a program ( https://github.com/jeremiahlukus/implementingDijkstra ) that creates a directed weighted graph and it implements Dijkstra's algorithm. I do this is by hard coding the vertex edge and weight like this
[code]

The output is

"Number of nodes = 8
Number of edges = 14
The shortest distance from node 0 to node 0 is 0
The shortest distance from node 0 to node 1 is 3
The shortest distance from node 0 to node 2 is 1
The shortest distance from node 0 to node 3 is 4
The shortest distance from node 0 to node 4 is 2
The shortest distance from node 0 to node 5 is 4
The shortest distance from node 0 to node 6 is 8
The shortest distance from node 0 to node 7 is 4"

Which is correct.

Now I am trying to do this reading from a text file ( https://github.com/jeremiahlukus/implementingDijkstra/blob/master/test.txt ). When I run my code I am getting edge to be 12 (should be 14) also if hard code the correct value I get an array index out of bounds. Also, when I read from the file I am getting "The shortest distance from node 0 to node 1 is 5" and it should be "The shortest distance from node 0 to node 1 is 3"

TO GET ALL OF THESE LINES TO PRINT, you need to go to the Graph class, then printResult() method then add a "+" in front of output.


I am not sure what I am doing wrong.

Please help this is my last question. Thank you for your time! I will thumbs up!

Explanation / Answer

There are two corrections required:

test.txt has missing entries for required nodes. I have updated the test.txt

n=8 m=14
0
2 1
3 4
4 2
1 3
1
3 2
4 3
5 1
2
4 1
3
5 4
4
5 2
6 7
7 2
5
6 4
6
7 5

Graph.java file concatenation of string missing in method printResult():

for(int i = 0; i < this.nodes.length; i++){
output += (" The shortest distance from node 0 to node " + i + " is " + nodes[i].getDistanceFromSource());
}
System.out.println(output);
}