gpt4 book ai didi

java - 这个 Tarjan 算法的实现有什么问题?

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

我已经按照 Wikipedia's article 实现了 Tarjan 的算法但是我遇到了问题。我要做的是找到所有大小大于 1 的强连通分量。使用较小的输入,一切正常,但是,当使用 input.txt 进行测试时算法开始表现异常 - 在更改 n 的初始值(例如 1、-10、3)时,程序会给出不同的结果。这就是我无法理解的。该算法不应依赖于初始值,因为 n 仅用作时间戳。如果有任何想法,请分享它,这将对我有很大帮助,谢谢。

代码如下:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Stack;

public class GraphAlgorithms {
HashMap<String, HashSet<String>> graph;
int n;

public GraphAlgorithms(String fileName) {
graph = new HashMap<String, HashSet<String>>();
try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {
String line;
while ((line = reader.readLine()) != null) {
String[] tokens = line.split(" +");
HashSet<String> to = new HashSet<String>();
for (int i = 1; i < tokens.length; i++)
to.add(tokens[i]);
graph.put(tokens[0], to);
}
} catch (IOException e) {
e.printStackTrace();
}
}

public GraphAlgorithms(HashMap<String, HashSet<String>> graph) {
this.graph = graph;
}

private void strongConnect(String v, HashMap<String, Integer> index,
HashMap<String, Integer> lowlink, Stack<String> stack,
HashSet<String> inStack, ArrayList<HashSet<String>> ans)
{

index.put(v, n);
lowlink.put(v, n);
++n;
stack.push(v);
inStack.add(v);

HashSet<String> successors = graph.get(v);

for(String w : successors) {
if (!index.containsKey(w)) {
strongConnect(w, index, lowlink, stack, inStack, ans);
if (lowlink.get(w) < lowlink.get(v))
lowlink.put(v, lowlink.get(w));
}
else if (inStack.contains(w)) {
if (index.get(w) < lowlink.get(v))
lowlink.put(v, index.get(w));
}
}

if (index.get(v) == lowlink.get(v)) {
HashSet<String> connectedComponent = new HashSet<String>();
String w;
do {
w = stack.pop();
inStack.remove(w);
connectedComponent.add(w);
}
while (!w.equals(v));

ans.add(connectedComponent);
}
}

public ArrayList<HashSet<String>> getStronglyConnectedComponents() {
HashMap<String, Integer> index = new HashMap<String, Integer>();
HashMap<String, Integer> lowlink = new HashMap<String, Integer>();
Stack <String> stack = new Stack<String>();
HashSet<String> inStack = new HashSet<String>();
ArrayList<HashSet<String>> ans = new ArrayList<HashSet<String>>();

n = 0;
for (String v : graph.keySet()) {
if (!index.containsKey(v))
strongConnect(v, index, lowlink, stack, inStack, ans);
}

return ans;
}

public void printComponentsLargerThanOne() {
for (HashSet<String> component : getStronglyConnectedComponents()) {
if (component.size() > 1) {
for (String element : component) {
System.out.print(element + " ");
}
System.out.println();
}
}
}
}

和主类

public class Main {
public static void main(String[] args) {
GraphAlgorithms ga = new GraphAlgorithms("input.txt");
ga.printComponentsLargerThanOne();
}
}

最佳答案

由于 Java 显然并不总是 intern 类型为 java.lang.Integer 的对象,因此该行

if (index.get(v) == lowlink.get(v)) {

应该是

if (index.get(v).equals(lowlink.get(v))) {

关于java - 这个 Tarjan 算法的实现有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18428021/

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