找到2个节点之间的最小距离

时间:2014-03-28 21:49:38

标签: java

任何人都可以帮我弄清楚如何解决我的问题。我需要找到2个给定节点之间的最短路径。到目前为止,我已经设法将所有可能的路径保存在列表中,现在我正试图找到最小距离。 这是我的代码:

public class A {
    private static int[][] adjacency = new int [6][6];
    static int n = 6;
    private static final int START = 1;
    private static final int END = 4;

    private Map<Integer, LinkedHashSet<Integer>> map = new HashMap();
    private Map<Integer, List<Integer>> pathsFound = new HashMap();

    public void addEdge(int node1, int node2) {
        LinkedHashSet<Integer> adjacent = map.get(node1);
        if(adjacent==null) {
            adjacent = new LinkedHashSet();
            map.put(node1, adjacent);
        }
        adjacent.add(node2);
    }

    public LinkedList<Integer> adjacentNodes(Integer last) {
        LinkedHashSet<Integer> adjacent = map.get(last);
        if(adjacent==null) {
            return new LinkedList();
        }
        return new LinkedList<Integer>(adjacent);
    }

    public static void main(String[] args) {

        LinkedList<Integer> visited = new LinkedList<Integer>();
        visited.add(START);
        A graph = new A();

        for (int i=0;i<n;i++)
            for (int j=0;j<n;j++)
                adjacency[i][j] = 0;    
        adjacency[0][1] = 1;
        adjacency[0][2] = 2;
        adjacency[1][0] = 1;
        adjacency[1][3] = 5;
        adjacency[1][4] = 9;
        adjacency[1][5] = 6;
        adjacency[2][0] = 2;
        adjacency[2][4] = 7;
        adjacency[2][5] = 2;
        adjacency[3][1] = 5;
        adjacency[4][1] = 9;
        adjacency[4][2] = 7;
        adjacency[4][5] = 1;
        adjacency[5][1] = 6;
        adjacency[5][2] = 2;
        adjacency[5][4] = 1;

        graph.addEdge(0,1);
        graph.addEdge(0,2);
        graph.addEdge(1,0);
        graph.addEdge(1,3);
        graph.addEdge(1,4);
        graph.addEdge(1,5);
        graph.addEdge(2,0);
        graph.addEdge(2,4);
        graph.addEdge(2,5);
        graph.addEdge(3,1);
        graph.addEdge(4,1);
        graph.addEdge(4,2);
        graph.addEdge(4,5);
        graph.addEdge(5,1);
        graph.addEdge(5,2);
        graph.addEdge(5,4);

        graph.breadthFirst(visited);
    }

    public void breadthFirst(LinkedList<Integer> visited) {
    LinkedList<Integer> nodes = adjacentNodes(visited.getLast());
    List<List<Integer>> allPaths = new ArrayList<List<Integer>>();
    List<Integer> distances = new ArrayList<Integer>();

    for (int node : nodes) {
        if (visited.contains(node)) 
            continue;
        if (node == END) {
            visited.add(node);
            List<Integer> path = getPath(visited);
            System.out.println(path);
            ??allPaths.add(path);
            visited.removeLast();
            break;
        }
    }

    for (int node : nodes) {
        if (visited.contains(node) || node == END) 
            continue;
        visited.addLast(node);
        breadthFirst(visited);
        visited.removeLast();
    }
    System.out.println(allPaths.get(0));

   }


        public static List<Integer> getPath(LinkedList<Integer> visited) {  
        List<Integer> path = new ArrayList<Integer>();
        for (int node : visited)
            path.add(node);
        return path;    
        }

}

如果我喜欢这个System.out.println(path);,它会打印路径,这意味着函数getPath()有效。 但是,当我想将此路径放入列表时:allPaths.add(path);出现问题,因为当我在for循环结束后调用System.out.println(allPaths.get(0));时,我得到IndexOutOfBoundException。我真的不明白为什么我的allPaths列表是空的......

2 个答案:

答案 0 :(得分:0)

为什么不用foreach迭代?

for (int number : arrlist) {
    System.out.println("Number = " + number);
}

答案 1 :(得分:0)

您是否可以在填写距离集合之前调用最后一个片段(在您的问题中)?你需要在之后调用它,而不是之前。

[旁白:将来,如果你有异常,你真的需要提供异常消息和堆栈跟踪。帮助您更轻松......]