diff --git a/CircuitFinder.h b/CircuitFinder.h index f7f9d54..3bcb2c4 100644 --- a/CircuitFinder.h +++ b/CircuitFinder.h @@ -1,14 +1,3 @@ -//===----------------------------------------------------------------------===// -// -// Written by Xing Mingjie (mingjie.xing@gmail.com). -// -// An implementation of the Johnson's circuit finding algorithm [1]. -// -// [1] Donald B. Johnson, Finding all the elementary circuits of a directed -// graph, SIAM Journal on Computing, 1975. -// -//===----------------------------------------------------------------------===// - #ifndef CIRCUIT_FINDER_H #define CIRCUIT_FINDER_H @@ -32,9 +21,9 @@ class CircuitFinder bool circuit(int V); void output(); -public: - CircuitFinder(int Array[N][N]) - : AK(N), Blocked(N), B(N) { + public: + CircuitFinder(int Array[N][N]) + : AK(N), Blocked(N), B(N) { for (int I = 0; I < N; ++I) { for (int J = 0; J < N; ++J) { if (Array[I][J]) { @@ -73,9 +62,9 @@ bool CircuitFinder::circuit(int V) if (W == S) { output(); F = true; - } else if (W > S && !Blocked[W - 1]) { - F = circuit(W); - } + } else if (!Blocked[W - 1]) { + if(circuit(W)) + F = true; } } if (F) { @@ -115,6 +104,11 @@ void CircuitFinder::run() B[I - 1].clear(); } circuit(S); + + // remove this vertex from the graph + for (int I = S+1; I <= N; ++I) + AK[I-1].remove(S); + ++S; } } diff --git a/Example.cpp b/Example.cpp index 24dc8ef..fa5c631 100644 --- a/Example.cpp +++ b/Example.cpp @@ -1,45 +1,17 @@ -//===----------------------------------------------------------------------===// -// -// Written by Xing Mingjie (mingjie.xing@gmail.com). -// -// A test program for circuit finding algorithm. -// -//===----------------------------------------------------------------------===// - - -/* - V5 V3 - +-<---o---<---o---<--+ - | | | - V1 o ^ o V4 - | V2| | - +------>------o--->--+ - / \ - | | - +->-+ - - N = 5 - - / 2 0 0 0 0 \ - | 2 3 4 0 0 | - AK = | 5 0 0 0 0 | - | 3 0 0 0 0 | - \ 1 0 0 0 0 / -*/ - #include "CircuitFinder.h" int main() { - int A[5][5] = { - 2, 0, 0, 0, 0, - 2, 3, 4, 0, 0, - 5, 0, 0, 0, 0, - 3, 0, 0, 0, 0, - 1, 0, 0, 0, 0 + int A[6][6] = { + 2, 5, 0, 0, 0, 0, + 3, 0, 0, 0, 0, 0, + 1, 2, 4, 6, 0, 0, + 5, 0, 0, 0, 0, 0, + 2, 0, 0, 0, 0, 0, + 4, 0, 0, 0, 0, 0, }; - CircuitFinder<5> CF(A); + CircuitFinder<6> CF(A); CF.run(); return 0; } diff --git a/a.out b/a.out new file mode 100755 index 0000000..e9f8a5e Binary files /dev/null and b/a.out differ