java两点之间所有组合_java - 使用DFS查找两个节点之间的所有可能路径 - 堆栈内存溢出...

我正在尝试在图表中找到所有可能的所有路径。 当我运行该程序时,我收到一个错误说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 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 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值