2

我已经按照维基百科的文章实现了 Tarjan 的算法,但是我遇到了问题。我想要做的是找到所有大小大于 1 的强连接组件。对于较小的输入,一切正常,但是,当使用input.txt进行测试时,算法开始出现奇怪的行为 - 在更改n(for示例 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();
    }
}
4

1 回答 1

3

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

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

应该

if (index.get(v).equals(lowlink.get(v))) {
于 2013-08-25T12:46:43.650 回答