输出无向图2个节点之间的输出路径--DFS

public ArrayList<Integer> indirectPath0110(Edge edge) {
ArrayList<Integer> path = new ArrayList<Integer>();
Stack<Integer> stack = new Stack<Integer>();
int start = edge.u;
int end = edge.v;
visited[start] = 1;
stack.push(start);
int current = start;
int head = 1;
int total = -1;
ArcNode arcNode = null;
do {
if (head == 1) { // 节点是否在路径中
arcNode = vList[current].firstArc;
head = 0;
} else {
arcNode = arcNode.nextArc;
}
if (arcNode != null) {
if (visited[arcNode.adjvex] == 0) {
visited[arcNode.adjvex] = 1;
stack.push(arcNode.adjvex);
if (arcNode.adjvex == end) {
total = getTotalWeight(stack);
if (total < edge.w) {
path.add(1);
}
visited[end] = 0;
stack.pop();
current = stack.peek();
head = 0;
} else {
current = stack.peek();
head = 1;
}
}
} else {
visited[stack.peek()] = 0;
stack.pop();
if (!stack.isEmpty()) {
arcNode = vList[stack.peek()].firstArc;
while (arcNode.adjvex != current) {
arcNode = arcNode.nextArc;
}
current = stack.peek();
head = 0;
}
}
} while (!stack.isEmpty());
return path;
}

猜你喜欢

转载自blog.csdn.net/u014230945/article/details/79049648
今日推荐