gpt4 book ai didi

java - Tarjan 的算法错误地检测周期

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:56:53 31 4
gpt4 key购买 nike

每当我在任何图上运行 tarjans 算法时,它总是声称有一个循环,例如这个图:

A -> B -> C

算法会告诉我有一个循环:

[a]
[b]

一个循环时,例如:

A -> B -> C -> A

输出很奇怪:

[c, b, a]
[a]
[b]

这是我的实现:

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.stream.Collectors;

public class Tarjans {

private static class Node {
public int index = -1, lowLink = -1;
public String name;

public Node(String name) {
this.name = name;
}

public String toString() {
return name;
}
}

HashMap<String, Node> nodes = new HashMap<>();
HashMap<String, ArrayList<Node>> graph = new HashMap<>();

private int index = 0;
private ArrayDeque<Node> visited = new ArrayDeque<>();
private HashSet<String> stack = new HashSet<>();

public ArrayList<ArrayList<Node>> tarjan() {
ArrayList<ArrayList<Node>> cycles = new ArrayList<>();
for (String key : graph.keySet()) {
Node n = nodes.get(key);
if (n == null) {
System.err.println("what is " + n + "?");
return new ArrayList<ArrayList<Node>>();
}

ArrayList<Node> cycle = strongConnect(n);
if (cycle.size() > 0) {
cycles.add(cycle);
}
}
return cycles;
}

private ArrayList<Node> strongConnect(Node node) {
node.index = index;
node.lowLink = index;
index += 1;

visited.push(node);
stack.add(node.name);

ArrayList<Node> neighbours = graph.get(node.name);
if (neighbours == null) return new ArrayList<>();

neighbours.forEach(n -> {
if (n.index == -1) {
strongConnect(n);
node.lowLink = Math.min(node.lowLink, n.lowLink);
}
else if (stack.contains(n.name)) {
node.lowLink = Math.min(node.lowLink, n.index);
}
});

ArrayList<Node> cycle = new ArrayList<>();
if (node.lowLink == node.index) {
Node p = null;
do {
p = visited.pop();
stack.remove(p.name);
cycle.add(p);
} while (p != node);
}
return cycle;
}

private void foo() {
nodes.put("a", new Node("a"));
nodes.put("b", new Node("b"));
nodes.put("c", new Node("c"));

// A -> B -> C -> A
graph.put("a", new ArrayList<>(Arrays.asList(nodes.get("b"))));
graph.put("b", new ArrayList<>(Arrays.asList(nodes.get("c"))));
graph.put("c", new ArrayList<>(Arrays.asList(nodes.get("a"))));

ArrayList<ArrayList<Node>> cycles = tarjan();
for (ArrayList<Node> cycle : cycles) {
System.out.println("[" + cycle.stream().map(Node::toString).collect(Collectors.joining(",")) + "]");
}
}

public static void main(String[] args) {
new Tarjans().foo();
}

}

但我不确定我哪里出错了。我几乎 1:1 关注了关于 tarjans 算法的维基百科文章和伪代码。我对图论和图​​算法非常陌生,所以我无法理解这里的错误是什么。

修复 tarjan()

public ArrayList<ArrayList<Node>> tarjan() {
ArrayList<ArrayList<Node>> cycles = new ArrayList<>();
for (Node n : nodes.values()) {
if (n == null) {
System.err.println("what is " + n + "?");
return new ArrayList<ArrayList<Node>>();
}

if (n.index == -1) {
ArrayList<Node> cycle = strongConnect(n);
if (cycle.size() > 0) {
cycles.add(cycle);
}
}
}
return cycles;
}

最佳答案

从问题中提供的代码的第一次修订,问题归结为 nearly不够接近:I've followed the wikipedia article on [Tarjan's Strongly Connected Components] algorithm nearly 1:1 and the pseudocode .
(并且可能命名(要保存的变量)强连通分量 cycle :如果边 (a, b), (a, c), (b, a), (b, c) and ( c, a) 属于一个图,顶点/节点 a、b 和 c 在一个强连通分量中,它既不是循环也不是碰巧(成对)共享顶点的循环。)

一直有调用strongConnect()对于已经访问过的节点 - 已在修订版 7 中修复。
截至第 7 版,当节点没有邻居/后继节点时,仍然检查节点是否符合强连通组件的条件。
一旦找到强连接组件,处理起来并不容易:有一个Set<Set<Node>>作为“算法(实例)”的数据成员,只需将其添加到。

一旦您的实现工作正常并且代码得到清理和评论,我建议您在 CODE REVIEW 上展示它:有很多机会让每个人的生活(作为 (Java) 编码员)变得更轻松,从您的开始。

关于java - Tarjan 的算法错误地检测周期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40722730/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com