2013-08-01 27 views
1

我正在嘗試創建一個給定機場文本文件的無向圖,以及與其連接的每個其他機場的成本。該文件的格式爲:LAX DFW 1000 MSY 250 ...其中,lax是頂點(離開的機場),dfw是另一個頂點(到達的機場),與lax相鄰,其邊緣長度爲1000.我試圖創建一個鄰接列表,其中所有離開的機場都是存儲在數組列表中的頂點對象,並且該數組中的每個元素都有另一個數組列表連接到它,其中連接數組中的元素也是頂點對象。每個頂點對象都有一個名稱字段和邊長字段。這裏是我到目前爲止的例子:無向圖的鄰接列表在Java中給出錯誤ouptut

import java.io.*; 
import java.util.*; 

public class ShortestPath 
{ 
    public ArrayList<Vertex> vertices; 

    //new Vertex 
    private static class Vertex 
    { 
     public int edgeLength; 
     public ArrayList<Vertex> connected; //list of connected vertices 
     public String name; 

     public Vertex(String s) 
     {  
      name = s; 
     } 
    } 

    public ShortestPath() throws FileNotFoundException 
    { 
     readFile(); 
    } 

    private void readFile() throws FileNotFoundException 
    { 
     File f = new File("airports.txt"); 
     Scanner input = new Scanner(f); 
     this.vertices = new ArrayList<Vertex>(); 

     while(input.hasNextLine()) 
     { 
      String line = input.nextLine(); 
      Scanner scan = new Scanner(line); //scanner for the current line 

      String str = scan.next(); 

      Vertex from = findVertex(str); 

      if (from == null) 
      { 
       from = new Vertex(str); 
       vertices.add(from); 
      } 

      from.connected = new ArrayList<Vertex>(); 

      while(scan.hasNext()) 
      { 
       String s = scan.next(); 
       Vertex ver = findVertex(s); 

       if (ver == null) 
       { 
        ver = new Vertex(s); 
        this.vertices.add(ver); 
       } 

       ver.edgeLength = scan.nextInt(); 

       from.connected.add(ver); 

       //if I use this line, it prints out exactly how the graph should be 
       // System.out.println(from.name + " to " + ver.name + " is " + ver.edgeLength); 
      } 
     } 
    } //end readFile() 

    public static void main(String[] args) 
    { 
     try 
     { 
      ShortestPath sp = new ShortestPath(); 

      for(Vertex v: sp.vertices) 
      { 
       System.out.println(v.name); 

       for (Vertex e: v.connected) 
       { 
        System.out.print(" to " + e.name + " is " + e.edgeLength); 
       } 

       System.out.println(); 
      } 

     } catch (FileNotFoundException e) 
     { 
      e.printStackTrace(); 
     } 
    } 


} 

這裏是我得到當我運行這個(這是不正確的):

ATL 
    to BOS is 250 to DFW is 59 to MOB is 59 
BOS 
    to ATL is 59 to DFW is 59 
DFW 
    to ATL is 59 to AUS is 59 to BOS is 250 to HOU is 59 to LAX is 100 to LIT is 59 to MSY is 128 to OKC is 59 to SHV is 59 to SFO is 100 
MOB 
    to ATL is 59 
AUS 
    to DFW is 59 to HOU is 59 to SAT is 59 
HOU 
    to AUS is 59 to DFW is 59 to SAT is 59 
SAT 
    to AUS is 59 to HOU is 59 
LAX 
    to DFW is 59 to SFO is 100 
LIT 
    to DFW is 59 
MSY 
    to DFW is 59 
OKC 
    to DFW is 59 
SHV 
    to DFW is 59 
SFO 
    to DFW is 59 to LAX is 100 

如果我取消註釋的System.out。在READFILE()的println線,它使這一點,這是它應該是,如果你是手工繪製圖形的方式:

ATL to BOS is 250 
ATL to DFW is 250 
ATL to MOB is 59 
AUS to DFW is 59 
AUS to HOU is 59 
AUS to SAT is 59 
BOS to ATL is 250 
BOS to DFW is 250 
DFW to ATL is 250 
DFW to AUS is 59 
DFW to BOS is 250 
DFW to HOU is 128 
DFW to LAX is 1000 
DFW to LIT is 59 
DFW to MSY is 128 
DFW to OKC is 59 
DFW to SHV is 59 
DFW to SFO is 1200 
HOU to AUS is 59 
HOU to DFW is 128 
HOU to SAT is 59 
LAX to DFW is 1000 
LAX to SFO is 100 
LIT to DFW is 59 
MOB to ATL is 59 
MSY to DFW is 128 
OKC to DFW is 59 
SAT to AUS is 59 
SAT to HOU is 59 
SFO to DFW is 1200 
SFO to LAX is 100 
SHV to DFW is 59 

我似乎無法找出爲什麼我收到了錯誤的輸出。任何幫助表示讚賞!

+0

您是否嘗試過調試並在'new ShortestPath()'後手動檢查以手動檢查數據結構?在完成構建之前,您正在向圖中添加頂點,這有助於驗證您的bug是在構建圖形還是在遍歷它。 – chrylis

+0

好主意,謝謝。我還沒有嘗試過。 – homegrown

回答

0

鄰接矩陣的每個節點都在頭部位置,然後所有節點連接到secondaryList中的每個頭節點,並且相對於頭節點具有邊緣。

Vertex ver = findVertex(s); 

      if (ver == null) 
      { 
       ver = new Vertex(s); 
       this.vertices.add(ver); 
      } 

      ver.edgeLength = scan.nextInt(); 

      from.connected.add(ver); 

似乎是一個問題。

  1. 理想中的 「頂點」 的每一個節點(可以稱之爲headList),應該有0,其邊緣(距離本身)。
  2. 您不能從headList中拉出一個頂點並將它推入「connected」(secondaryList)中。 edgeLengths對於每個secondaryList的headVertex是 。
+0

好的,我明白了。我明白你在說什麼,我每次都創建了一個新的頂點,並一起擺脫了if語句。因此,我將「從」頂點添加到「頂點」列表,並將「ver」頂點添加到from.connected列表中,現在它可以工作。謝謝你的幫助。 – homegrown