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

时间:2013-08-25 10:28:09

标签: java algorithm implementation

我在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();
    }
}

1 个答案:

答案 0 :(得分:3)

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

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

应该是

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