7

Does anyone know the Donald B. Johnson's algorithm, which enumerates all the elementary circuits (cycles) in a directed graph?

I have the paper he had published in 1975, but I cannot understand the pseudocode.

My goal is to implement this algorithm in Java.

Some questions I have, for example, is what is the matrix Ak it refers to. In the pseudocode, it mentions that

Ak:=adjacency structure of strong component K with least 
    vertex in subgraph of G induced by {s,s+1,....n};

Does that mean I have to implement another algorithm that finds the Ak matrix?

Another question is what the following means?

begin logical f; 

Does also the line "logical procedure CIRCUIT (integer value v);" mean that the circuit procedure returns a logical variable? In the pseudocode also has the line "CIRCUIT := f;". What does this mean?

It would be great if someone could translate this 1970's pseudocode to a more modern type of pseudocode so I can understand it

In case you are interested to help but you cannot find the paper please email me at pitelk@hotmail.com and I will send you the paper.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
C.LS
  • 1,319
  • 2
  • 17
  • 35
  • Did you try reading the paper you linked to? It seems to have an accompanying explanation and proof. –  May 25 '10 at 22:33
  • yes i have, but still it doesnt explain the code itself, just the general idea. What i cannot understand is the pseudo code. also i have found another link to the paper in case the first is not working http://dutta.csc.ncsu.edu/csc791_spring07/wrap/circuits_johnson.pdf – C.LS May 26 '10 at 02:45
  • Thanks to you all , you have taken care of the appearance of my question (made it look better; corrected spelling mistakes and changed the code i have wrote to the original of the paper -for some strange reason i couldn't just copy - paste the code so i typed it from scratch.) – C.LS May 28 '10 at 19:05
  • `AK` refers to the edge list of the induced subgraph of vertices `VK`. A *Mathematica* demonstration (and source code) is available [here](http://demonstrations.wolfram.com/EnumeratingCyclesOfADirectedGraph/). – István Zachar Aug 23 '13 at 15:52

2 Answers2

7

The pseudo-code is reminiscent of Algol, Pascal or Ada.

Does that mean I have to implement another algorithm that finds the Ak matrix?

Ak appears to be a list of arrays of input values having the specified properties. It may be related to the corresponding adjacency matrix, but it's not clear to me. I'm guessing something like this:

int[][] a = new int[k][n];
int[][] b = new int[k][n];
boolean[] blocked = new boolean[n];
int s;

What does logical f mean?

This declares a local variable representing a true or false value, similar to Java's boolean.

logical procedure CIRCUIT (integer value v);

This declares a subprogram named CIRCUIT having a single integer parameter v that is passed by value. The subprogram returns a logical result of true or false, and CIRCUIT := f assigns f as the result. In Java,

boolean circuit(int v) {
    boolean f;
    ...
    f = false;
    ...
    return f;
}

The keywords begin and end delimit a block scope that may be nested, so CIRCUIT is nested in the main block and UNBLOCK is nested inside of CIRCUIT. := is assignment; ¬ is not; is element; is empty; is !=; stack and unstack suggest push and pop.

It's only a start, but I hope it helps.

Addendum: On reflection, A and B must be isomorphic.

Here's a very literal outline. I don't know enough about A, B & V to choose a better data structure than arrays.

import java.util.Stack;

public final class CircuitFinding {
    static int k, n;
    int[][] a = new int[k][n];
    int[][] b = new int[k][n];
    boolean[] blocked = new boolean[n];
    int[] v = new int[k];
    int s = 1;
    Stack<Integer> stack = new Stack<Integer>();

    private void unblock(int u) {
        blocked[u] = false;
        for (int w : b[u]) {
            //delete w from B(u)
            if (blocked[w]) {
                unblock(w);
            }
        }
    }

    private boolean circuit(int v) {
        boolean f = false;
        stack.push(v);
        blocked[v] = true;
        L1:
        for (int w : a[v]) {
            if (w == s) {
                //output circuit composed of stack followed by s;
                f = true;
            } else if (!blocked[w]) {
                if (circuit(w)) {
                    f = true;
                }
            }
        }
        L2:
        if (f) {
            unblock(v);
        } else {
            for (int w : a[v]) {
                //if (v∉B(w)) put v on B(w);
            }
        }
        v = stack.pop();
        return f;
    }

    public void main() {
        while (s < n) {
            //A:= adjacency structure of strong component K with least
            //vertex in subgraph of G induced by {s, s+ 1, n};
            if (a[k] != null) {
                //s := least vertex in V;
                for (int i : v) {
                    blocked[i] = false;
                    b[i] = null;
                }
                L3:
                circuit(s);
                s++;
            } else {
                s = n;
            }
        }
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Thank you very much for the information. Still i was not able to make any significant progress. i will try to find out the ada or algol syntax. Until then can you clarify to me something? the CIRCUIT := f returns the value immiadetly or just assigns tha value to be returned later? in other words it is like f = false or like return (f=false) Thanks – C.LS May 27 '10 at 21:45
  • The statement `CIRCUIT := f` assigns the current value of the local variable `f` as the result when the subprogram exits normally after the following statement. The assignment does not _cause_ the return; it merely precedes it. Use of the identifier `CIRCUIT` does not imply recursion, whereas `UNBLOCK` appears to be called recursively. The code strongly resembles Wirth's _Algorithms + Data Structures = Programs_: http://en.wikipedia.org/wiki/Algorithms_%2B_Data_Structures_%3D_Programs – trashgod May 28 '10 at 02:41
  • I think after studying it again and again and with your helpful comments it starts to make sense to me. I will try to write my version of pseudo code and i will post it when it is ready! – C.LS May 28 '10 at 19:07
  • I've added a rough outline. I look forward to your results. – trashgod May 28 '10 at 20:55
  • Thanks for the outline . It is very usefull, also some thing you have wrote is what i was thinking too so that means i am the correct road. The only problem (major one) is i cannot still understand what Vk and Ak (or better , how Vk and Ak are chosen). I have posted this question as a new tread in http://stackoverflow.com/questions/2939877/help-in-the-donalds-b-johnsons-algorithm-i-cannot-understand-the-pseudo-code If i understand this , i think i will be able to write the code to java – C.LS May 30 '10 at 18:54
  • Now I'm a fan of educational answers – stacker Jun 01 '10 at 20:24
  • @stacker: Educational? Yes, but also a little nostalgic. :-) – trashgod Jun 01 '11 at 21:46
  • @trashgod The comment was exactly one year old ;-) – stacker Jun 05 '11 at 14:29
  • @stacker: Egad! You're right; I misread the date, but thanks for the nod. – trashgod Jun 05 '11 at 14:39
1

You can find a Java implementation of this algorithm on github: https://github.com/1123/johnson. It uses the Jung2 java graph library.

user152468
  • 3,202
  • 6
  • 27
  • 57