Java中无向图的邻接列表给出了错误的输出

Java中无向图的邻接列表给出了错误的输出,java,algorithm,arraylist,adjacency-list,Java,Algorithm,Arraylist,Adjacency List,我试图创建一个无向图,给出一个机场的文本文件,以及与之相连的机场的成本。该文件的格式为,例如:LAX DFW 1000 MSY 250。。。其中lax是一个顶点(出发机场),dfw是与lax相邻的另一个顶点(到达机场),其边长度为1000。我试图创建一个邻接列表,其中所有出发机场都是存储在数组列表中的顶点对象,该数组中的每个元素都有另一个与之连接的数组列表,其中连接数组中的元素也是顶点对象。每个顶点对象都有一个名称字段和边长度字段。以下是我到目前为止所拥有的一个样本: import java.i

我试图创建一个无向图,给出一个机场的文本文件,以及与之相连的机场的成本。该文件的格式为,例如:LAX DFW 1000 MSY 250。。。其中lax是一个顶点(出发机场),dfw是与lax相邻的另一个顶点(到达机场),其边长度为1000。我试图创建一个邻接列表,其中所有出发机场都是存储在数组列表中的顶点对象,该数组中的每个元素都有另一个与之连接的数组列表,其中连接数组中的元素也是顶点对象。每个顶点对象都有一个名称字段和边长度字段。以下是我到目前为止所拥有的一个样本:

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


}
如果我在readFile()中取消注释System.out.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

我似乎不明白为什么我得到了错误的输出。感谢您的帮助

邻接矩阵的每个节点都位于头部位置,然后所有节点都连接到第二个列表中的每个头部节点,并具有相对于头部节点的边

 Vertex ver = findVertex(s);

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

            ver.edgeLength = scan.nextInt();

            from.connected.add(ver);
似乎是个问题

  • 理想情况下,“顶点”(我们称之为headList)中的每个节点应具有0 EdgelLength(与自身的距离)
  • 不能从标题列表中拉出顶点并将其推入“已连接”(secondaryList)中。边缘的长度与边缘是真实的 每个次要列表的头顶点

  • 您是否尝试过在
    new ShortestPath()
    之后立即调试和中断以手动检查数据结构?在完全构造顶点之前,您正在向图形添加顶点,这将有助于验证您的错误是在构建图形还是在遍历图形。好主意,谢谢。我还没试过。好吧,我知道了。我明白你的意思,我只是每次创建一个新的顶点,然后把if语句都去掉了。因此,我将“from”顶点添加到“顶点”列表中,“ver”顶点添加到from.connected列表中,它现在可以工作了。谢谢你的帮助。
     Vertex ver = findVertex(s);
    
                if (ver == null)
                {
                    ver = new Vertex(s);
                    this.vertices.add(ver);
                }
    
                ver.edgeLength = scan.nextInt();
    
                from.connected.add(ver);