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

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]

Edge[] edges = { new Edge(0, 2, 1), new Edge(0, 3, 4), new Edge(0, 4, 2), new Edge(0, 1, 3), new Edge(1, 3, 2), new Edge(1, 4, 3), new Edge(1, 5, 1), new Edge(2, 4, 1), new Edge(3, 5, 4), new Edge(4, 5, 2), new Edge(4, 6, 7), new Edge(4, 7, 2), new Edge(5, 6, 4), new Edge(6, 7, 5), }; [/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!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!