简体   繁体   English

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

[英]Adjacency list of a undirected Graph is giving wrong ouptut in Java

I am trying to create an undirected graph given a text file of airports and the cost to each other airport its connected to. 我正在尝试创建一个给定的机场文本文件以及与其连接的每个其他机场的成本的无向图。 The file is in the format, for example: LAX DFW 1000 MSY 250... where lax is a vertex(departing airport) and dfw is another vertex(arriving airport) adjacent to lax and its edge length is 1000. I attempted to create an adjacency list where all the departing airports are Vertex objects stored in an array list and each element in that array has another array list connected to it, where the elements in the connected array are also Vertex objects. 该文件的格式为:LAX DFW 1000 MSY 250 ...其中,lax是一个顶点(离开机场),dfw是与lax相邻的另一个顶点(到达机场),其边长为1000。我试图创建一个邻接列表,其中所有离开的机场都是存储在数组列表中的Vertex对象,并且该数组中的每个元素都有另一个与其相连的数组列表,其中所连接的数组中的元素也是Vertex对象。 Each Vertex object has a name field and edge length field. 每个顶点对象都有一个名称字段和边长字段。 Here is a sample of what I have so far: 这是我到目前为止的样本:

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


}

Here is what I am getting when I run this (this is not correct): 这是运行此命令时得到的结果(这是不正确的):

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

If I uncomment the System.out.println line in readFile(), it gives this, which is the way it should be if you were to draw the graph by hand: 如果我取消注释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

I can't seem to figure out why I am getting the wrong output. 我似乎无法弄清楚为什么我得到了错误的输出。 any help is appreciated! 任何帮助表示赞赏!

An adjacency matrix has every node in the head position and then all the nodes connected to each head node in the secondaryList with edges relative to the head node. 邻接矩阵的每个节点都位于头位置,然后所有节点都连接到secondaryList中的每个头节点,并且其边缘相对于头节点。

 Vertex ver = findVertex(s);

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

            ver.edgeLength = scan.nextInt();

            from.connected.add(ver);

Seems like a problem. 好像是个问题。

  1. Ideally every node in "vertices" (lets call it headList), should have 0 edgeLength (distance from itself). 理想情况下,“顶点”(称为“ headList”)中的每个节点应具有0 edgeLength(与自身的距离)。
  2. You cannot pull a vertex from headList and push it in the "connected"(secondaryList). 您不能从headList中拉出顶点并将其推入“ connected”(secondaryList)中。 The edgeLengths are realtive to the headVertex for each secondaryList. 对于每个secondaryList,edgeLengths都是headVertex的实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM