我正在嘗試創建一個給定機場文本文件的無向圖,以及與其連接的每個其他機場的成本。該文件的格式爲: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
我似乎無法找出爲什麼我收到了錯誤的輸出。任何幫助表示讚賞!
您是否嘗試過調試並在'new ShortestPath()'後手動檢查以手動檢查數據結構?在完成構建之前,您正在向圖中添加頂點,這有助於驗證您的bug是在構建圖形還是在遍歷它。 – chrylis
好主意,謝謝。我還沒有嘗試過。 – homegrown