I have here a piece of code. This is a snippet from my Dijkstra implementation. Here, we have 4 vertices instantiated and each vertex has specified edges and its distance from the vertex(node). This code is straightforward and beneficial if the data will directly be inputted in the main class.
Vertex v0 = new Vertex("Redvile");
Vertex v1 = new Vertex("Blueville");
Vertex v2 = new Vertex("Greenville");
Vertex v3 = new Vertex("Orangeville");
Vertex v4 = new Vertex("Purpleville");
v0.adjacencies = new Edge[]{ new Edge(v1, 5),
new Edge(v2, 10),
new Edge(v3, 8) };
v1.adjacencies = new Edge[]{ new Edge(v0, 5),
new Edge(v2, 3),
new Edge(v4, 7) };
v2.adjacencies = new Edge[]{ new Edge(v0, 10),
new Edge(v1, 3) };
v3.adjacencies = new Edge[]{ new Edge(v0, 8),
new Edge(v4, 2) };
v4.adjacencies = new Edge[]{ new Edge(v1, 7),
new Edge(v3, 2) };
However, my data is from a file. I have to read the file and create instances of the vertex together with the edges (adjacencies) on each vertex. I have a problem implementing this function. To wit, I need a code that translates this piece of code above to something wherein the data is from a file.
Here's my sample data:
a0 - a1,1.6
a1 - a0,1.6 * a2,0.85 * a3,0.5
a2 - a1,0.85 * a34,1.2 * a39,0.65
a3 - a1,0.5 * a4,0.2 * a5,0.1
a4 - a3,0.2 * a5,0.2
a5 - a4,0.1 * a6,1 * a36,0.65
a6 - a5,1 * a7,1.5 * a14,0.45
a7 - a6,1.5 * a8,0.18 * a11,1.2
a8 - a7,0.18 * a9,1
a9 - a8,1
a10 - a13,1.9
The a0-a10 are the vertices and the a1,1.6 are others represent the adjacencies or edges connected to the vertex in (vertex,distance) format.
So far, I am able to read this file and put each line on an ArrayList. My problem now is basically how to instantiate each of the vertex and adding the adjacencies.
This is how I read the file:
private void readFile(String fileName) {
try{
FileReader file = new FileReader(fileName);
Scanner sc = new Scanner(file);
int i = 0;
while (sc.hasNextLine()) {
input.add(sc.nextLine());
i++;
}
setNodes();
System.out.println();
file.close();
} catch(Exception e){
System.out.println(e);
}
}
The input is the arrayList containing all the content of the file per line. On the other hand, nodes will have the list of all the vertices.
public void setNodes() {
System.out.println();
for(int i=0; i<input.size(); i++) {
line = this.input.get(i);
nodes.add(line.substring(0,line.indexOf("-")).trim());
}
}
Also,
PS I also a problem with data type. My arrayList is string and the vertices are of type Vertex, which is a defined class in my code.
Should this description be inadequate, feel free to comment. Thank you and cheers!