0

I have a text file "NYRoadNetwork.txt" which contains the following information of a weighted graph:

The first row represents the number of nodes in the graph, i.e. 30

The second row represents the number of edges connecting any two nodes in the graph, i.e. 17

The remains are the weight of edges connecting any two nodes, eg. "0 1 2" in third row means the weight of edge connecting node 0 and 1 is 2.

30   
17   
0 1 2   
2 3 0    
4 5 1   
6 7 3   
8 9 4   
8 10 3  
0 11 2  
1 12 1  
13 14 3  
15 16 4   
17 18 2   
19 20 3   
19 21 3   
22 23 6    
24 25 1           
26 27 1            
28 29 1    

Now, my question is, instead of input each node and edges one by one, how can I write a java code to generate the complete graph after reading the data from the text file?

FYI, this is my part of original java code which I want to modify.

    // mark all the vertices
    Vertex 0 = new Vertex("0");
    Vertex 1 = new Vertex("1");
    Vertex 2 = new Vertex("2");
    Vertex 3 = new Vertex("3");
    Vertex 4 = new Vertex("4"); ......

    // set the edges and weight
    0.adjacencies = new Edge[]{ new Edge(1, 2) };
    0.adjacencies = new Edge[]{ new Edge(11, 2) };
    1.adjacencies = new Edge[]{ new Edge(12, 1) };
    2.adjacencies = new Edge[]{ new Edge(3, 0) };
    4.adjacencies = new Edge[]{ new Edge(5, 1) };
    6.adjacencies = new Edge[]{ new Edge(7, 3) }; .......
larry
  • 1
  • `Vertex 0 = new Vertex("0");` -- this is not Java. But regardless, your question is off topic. You have written none of the code that solves the problem, and we are not going to write it for you. That's not the way StackOverflow works. Please visit the [help] and read [ask] for more information, and especially read [Why is “Can someone help me?” not an actual question?](http://meta.stackoverflow.com/q/284236/18157) – Jim Garrison Mar 09 '17 at 06:38

1 Answers1

0

You can use BufferedReader to read a line from a file.

After that use String.split() method to split a line into an array of string.

I suggest you, to go through Java APIs before asking this type of questions.

Community
  • 1
  • 1
Avinash L
  • 167
  • 2
  • 15