2013-03-14 119 views
-1

我在這裏有一段代碼。這是我的Dijkstra實現中的一個片段。在這裏,我們有4個頂點被實例化,每個頂點指定了邊和它與頂點(節點)的距離。如果數據將直接輸入到主類中,則此代碼非常簡單直接。Java HashMap文件輸入版本代碼

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) }; 

但是,我的數據來自文件。我必須讀取文件並創建頂點的實例以及每個頂點上的邊(鄰接點)。我有一個實現這個功能的問題。換句話說,我需要一個代碼將上面的這段代碼翻譯成其中的數據來自文件。

這是我的示例數據:

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 

的A0-A10是頂點和a1,1.6有其他表示鄰接或邊緣連接到(頂點,距離)格式的頂點。

到目前爲止,我能夠讀取此文件並將每行放在ArrayList上。我現在的問題基本上是如何實例化每個頂點並添加鄰接關係。

這是我讀文件:

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); 
    } 
} 

輸入是每行包含該文件的所有內容ArrayList中。另一方面,節點將具有所有頂點的列表。

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()); 
    } 
} 

此外,

PS 我還與數據類型的問題。我的arrayList是字符串,頂點是Vertex類型,它是我的代碼中定義的類。

如果此描述不充分,請隨時發表評論。謝謝你,歡呼!

+0

告訴我們您是如何閱讀文件 – Greg 2013-03-14 17:11:25

+0

更新的,@ greg :) – princepiero 2013-03-14 17:16:28

回答

1

我現在正在發佈基於我的實現和@greg給出的信息來回答這個問題。非常感謝他!

private void setVertices() { 

    String[] listEdges; 

    for(int i=0; i<nodes.size(); i++) { 

     //if vertex does not exist, create it 
     if(vertexList.containsKey(nodes.get(i))) { 
      vertexList.put(nodes.get(i), new Vertex(nodes.get(i))); 
     } 

     line = adjLine.get(i); 

     //separate adj edges to * 
     if(line.contains("*")) { 
      listEdges = line.split("[*]"); 
     } else { 
      listEdges = new String[1]; 
      listEdges[0] = line; 
     } 

     //add edges to specified vertex 
     for(int j=0; j < listEdges.length; j++) { 
      String[] word = listEdges[j].split(","); 
      edges.add(new Edge(vertexList.get(word[0]),Double.parseDouble(word[1]))); 
     } 

     map.put(nodes.get(i), edges); 
     edges.clear(); 
    } 
} 

基本上,我使用ArrayList,Map和HashMap可以做到這一點。是!

2

這應該是直截了當

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

我會告訴你的過程而不是代碼

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 

在年底你處理,你應該能夠從您的vertex_list顯示你所有的頂點

至於給一個頂點一個名稱,你可以用幾種不同的方法來做到這一點,簡單的方法是在配置文件中給一個頂點一個名稱。這將需要你在你的列表上做兩遍,第一個用名字生成每個頂點,第二個就像上面的僞代碼一樣,除了不需要創建新的頂點,因爲它們已經在第一遍中創建,你可以簡單地添加邊緣頂點到adj。名單。

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 
+0

謝謝。很好的幫助! 我有創建頂點的問題,@greg 這是因爲輸入列表是字符串,我有命名頂點的問題。我的意思是,這部分: **頂點v0 =新頂點(「Redville」); ** 這是可能的嗎? **頂點[index] =新的頂點(名稱); ** 頂點的名稱是我現在的主要問題。 :) – princepiero 2013-03-14 17:53:35