Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
133 views
Welcome To Ask or Share your Answers For Others

1 Answer

This should be straight forward

a0 -    a1,1.6
a1 -    a0,1.6      * a2,0.85   * a3,0.5

I will show you the process not the code

Create vertex_list list 
For each line read
  split on '-' left side is your current_vertex, right side is your 'adjacency list'
   check if your vertex is present in vertex_list if not create it and add it
   adj list =  a0,1.6      * a2,0.85   * a3,0.5
   split your adj list   on '*' 
   for each entry ex 'a0,1.6'
      split on ','  now you know the edge_vertex=split[0] = a0 and weight = split[1] = 1.6
      check if you already have edge_vertex in your vertex list if not add it
      create and add edge(edge_vertex, weight)  to current vertex
    end
 end

At the end of you process you should be able to display all your vertices from your vertex_list

As for giving a vertex a name you could do this in few different ways, easy way would be to give a vertex a name in the config file. This would required you to do two passes over you list, first one to generate each vertex with a name and second just like the pseudocode above except there is no need to create new vertexes as they have been created in first pass, you would simply add the edge vertex to the adj. list.

a0,Name A -    a1,1.6
a1,Name B -    a0,1.6      * a2,0.85   * a3,0.5
a2,Name C -    a1,0.85     * a34,1.2   * a39,0.65
a3,Name D -    a1,0.5      * a4,0.2    * a5,0.1

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...