java - 使用 DFS 查找两个节点之间的所有可能路径

标签 java data-structures

我正在尝试在我的图表中找到所有可能的路径。当我运行该程序时,我收到一条错误消息:ArrayIndexOutOfBoundsException。我需要在哪里修复代码才能解决此错误

private int v;  
 void printAllPaths(String s, String d) {
     int start=airportsHashMap.get(s).intValue();
        int end=airportsHashMap.get(d).intValue();

        //int DaRoute;
        printPaths(start,end);
 }
 public void printPaths(int s, int d)  
 { 

     boolean[] isVisited = new boolean[v]; 
     ArrayList<Integer> pathList = new ArrayList<>(); 

     //add source to path[] 
     pathList.add(s); 

     //Call recursive utility 
     printAllPathsUtil(s, d, isVisited, pathList); 
 } 

 // A recursive function to print 
 // all paths from 'u' to 'd'. 
 // isVisited[] keeps track of 
 // vertices in current path. 
 // localPathList<> stores actual 
 // vertices in the current path 
 private void printAllPathsUtil(Integer u, Integer d, 
                                 boolean[] isVisited, 
                         List<Integer> localPathList) { 

     // Mark the current node 
     isVisited[u] = true; 

     if (u.equals(d))  
     { 
         System.out.println(localPathList); 
         // if match found then no need to traverse more till depth 
         isVisited[u]= false; 
         return ; 
     } 

     // Recur for all the vertices 
     // adjacent to current vertex 
     for (Integer i :adjListArray[u])  
     { 
         if (!isVisited[i]) 
         { 
             // store current node  
             // in path[] 
             localPathList.add(i); 
             printAllPathsUtil(i, d, isVisited, localPathList); 

             // remove current node 
             // in path[] 
             localPathList.remove(i); 
         } 
     } 

     // Mark the current node 
     isVisited[u] = false; 
 } 

我期望输出如下:

EWR 和 PHL 之间的所有路径

EWR IAD ILG PHL EWR IAD PHL EWR PHL

EWR 和 ILG 之间的所有路径

EWR IAD ILG EWR IAD PHL ILG EWR PHL ILG EWR PHL IAD ILG

最佳答案

我认为异常是因为您滥用了删除。您应该传递索引而不是“对象”。

 for (Integer i :adjListArray[u])  
 { 
     if (!isVisited[i]) 
     { 
         // store current node  
         // in path[] 
         localPathList.add(i);                 <=== i the 'object'
         printAllPathsUtil(i, d, isVisited, localPathList); 

         // remove current node 
         // in path[] 
         localPathList.remove(i);              <=== i should be 'index' not object
     } 
 } 

你的感冒应该是这样的:

 for (Integer i :adjListArray[u])  
 { 
     if (!isVisited[i]) 
     { 
         // store current node  
         // in path[] 
         localPathList.add(i);
         printAllPathsUtil(i, d, isVisited, localPathList); 

         // remove current node 
         // in path[] 
         localPathList.remove(localPathList.size()-1);
     } 
 } 

关于java - 使用 DFS 查找两个节点之间的所有可能路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55802621/

相关文章:

java - 一个类如何扩展两个不同的类,其中一个类来自外部库,另一个类是内置的

java - 计算树中的左子节点

java - 将 Gson 与 JSend Standard 结合使用时遇到的问题

java - 如何使用java从twitter上抓取推文

algorithm - 实现迭代单栈二叉树复制函数

c++ - 如何提高具有 100 万个元素和 997 个桶的哈希表的性能?

arrays - 一个数组已经是一个平衡的 B 树,这是真的吗?

java - 参数化数据结构以保存特定大小的数组?

java - 使用 Java 绘制一维图

Java:LSParser 和 DocumentBuilder 有什么区别