help in the Donalds B. Johnson's algorithm, i cannot understand the pseudo code (PART II)

前端 未结 4 1245
后悔当初
后悔当初 2020-11-29 11:50

i cannot understand a certain part of the paper published by Donald Johnson about finding cycles (Circuits) in a graph.

More specific i cannot understand what is the

4条回答
  •  难免孤独
    2020-11-29 12:18

    The following variation produces unique cycles. Based on this example, it is adapted from an answer supplied by @user1406062.

    Code:

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.Stack;
    
    /**
     * @see https://en.wikipedia.org/wiki/Johnson%27s_algorithm
     * @see https://stackoverflow.com/questions/2908575
     * @see https://stackoverflow.com/questions/2939877
     * @see http://en.wikipedia.org/wiki/Adjacency_matrix
     * @see http://en.wikipedia.org/wiki/Adjacency_list
     */
    public final class CircuitFinding {
    
        final Stack stack = new Stack();
        final Map> a;
        final List> b;
        final boolean[] blocked;
        final int n;
        Integer s;
    
        public static void main(String[] args) {
            List> a = new ArrayList>();
            a.add(new ArrayList(Arrays.asList(1, 2)));
            a.add(new ArrayList(Arrays.asList(0, 2)));
            a.add(new ArrayList(Arrays.asList(0, 1)));
            CircuitFinding cf = new CircuitFinding(a);
            cf.find();
        }
    
        /**
         * @param a adjacency structure of strong component K with least vertex in
         * subgraph of G induced by {s, s + 1, n};
         */
        public CircuitFinding(List> A) {
            this.a = new HashMap>(A.size());
            for (int i = 0; i < A.size(); i++) {
                this.a.put(i, new ArrayList());
                for (int j : A.get(i)) {
                    this.a.get(i).add(j);
                }
            }
            n = a.size();
            blocked = new boolean[n];
            b = new ArrayList>();
            for (int i = 0; i < n; i++) {
                b.add(new ArrayList());
            }
        }
    
        private void unblock(int u) {
            blocked[u] = false;
            List list = b.get(u);
            for (int w : list) {
                //delete w from B(u);
                list.remove(Integer.valueOf(w));
                if (blocked[w]) {
                    unblock(w);
                }
            }
        }
    
        private boolean circuit(int v) {
            boolean f = false;
            stack.push(v);
            blocked[v] = true;
            L1:
            for (int w : a.get(v)) {
                if (w == s) {
                    //output circuit composed of stack followed by s;
                    for (int i : stack) {
                        System.out.print(i + " ");
                    }
                    System.out.println(s);
                    f = true;
                } else if (!blocked[w]) {
                    if (circuit(w)) {
                        f = true;
                    }
                }
            }
            L2:
            if (f) {
                unblock(v);
            } else {
                for (int w : a.get(v)) {
                    //if (v∉B(w)) put v on B(w);
                    if (!b.get(w).contains(v)) {
                        b.get(w).add(v);
                    }
                }
            }
            v = stack.pop();
            return f;
        }
    
        public void find() {
            s = 0;
            while (s < n) {
                if (!a.isEmpty()) {
                    //s := least vertex in V;
                    L3:
                    for (int i : a.keySet()) {
                        b.get(i).clear();
                        blocked[i] = false;
                    }
                    circuit(s);
                    a.remove(s);
                    for (Integer j : a.keySet()) {
                        if (a.get(j).contains(s)) {
                            a.get(j).remove(s);
                        }
                    }
                    s++;
                } else {
                    s = n;
                }
            }
        }
    }
    

    Output:

    0 1 0
    0 1 2 0
    0 2 0
    0 2 1 0
    1 2 1
    

    All cycles, for reference:

    0 1 0
    0 1 2 0
    0 2 0
    0 2 1 0
    1 0 1
    1 0 2 1
    1 2 0 1
    1 2 1
    2 0 1 2
    2 0 2
    2 1 0 2
    2 1 2
    

提交回复
热议问题