4

Given graph adjacency matrix (for ex. g[][]), graph is directed. Needs find count of all graph cycles (if exists) and print them.

I tried to wrote this algorithm in Java, sometimes it works correctly. If graph has complex cycles, algorithm return crazy cycles. Please, look at my code and help to resolve this problem

public static final int k = 6;

public static int g[][] = { { 0, 1, 0, 0, 0, 0 },
                            { 1, 0, 1, 0, 0, 0 },
                            { 0, 0, 0, 1, 0, 0 },
                            { 0, 0, 0, 0, 1, 0 },
                            { 0, 0, 1, 0, 0, 0 },
                            { 0, 0, 0, 0, 0, 0 } };

public static Vector stack = new Vector();

public static void printStack() {
    System.out.print("stack is: { ");
    for (int i = 0; i < stack.size(); i++) {
        System.out.print(stack.get(i) + " ");
    }
    System.out.println("};");

}

public static boolean checkCycle() {
    boolean res = false;

    for (int i = 0; i < stack.size() - 1; i++) {
        if (stack.get(i).equals(stack.lastElement())) {
            res = true;
            break;
        }

    }
    return res;
}

public static boolean go_to_line(int line) {
    boolean res = false;
    for (int i = 0; i < k; i++) {
        if (g[line][i] == 1) {
            stack.add(i);
            if (checkCycle() == true) {
                System.out.println("Cycle found!");
                res = true;
            } else {
                res = go_to_line(i);
            }
        }
    }

    return res;
}

public static int cycles_count() {
    int res = 0;

    for (int i = 0; i < k; i++) {
        if (g[i][i] == 1) {
            System.out.println("Knot detected at item {" + i + "}!");
            res++;
        }

        for (int j = i + 1; j < k; j++) {
            if (g[j][i] == 1) {
                stack.add(j);
                stack.add(i);

                if (go_to_line(i) == true) {
                    res++;

                    System.out.print("Final ");
                    printStack();
                    stack.removeAllElements();
                }
            }
        }
    }

    return res;
}
Pavel Belov
  • 41
  • 1
  • 3
  • 7
    I doubt anybody's going to just write the code for you. You'll need to show us what you've tried, along with at least some description of the problem(s) you've encountered. – Jerry Coffin Feb 15 '10 at 05:07
  • Have you considered that there may be infinitely many cycles? –  Sep 29 '10 at 10:12

2 Answers2

6

This problem has exponential complexity in the general case. The thing is that if each vertex is connected to each then the count of all graph cycles is more than 2^n (any subset of nodes forms several cycles).

Thus there is no good algorithm in the general case. To find some cycle you may use breadth-first search. To find all cycles you should use brute force algorithm.

Alexey Malistov
  • 26,407
  • 13
  • 68
  • 88
1

If C++ is the problem then change for another language. up to my knowledge C++ does not have particular feature that make it more efficient/convenient to work on graph matters. Alternatively you might want to look for a framework that will abstract the low level, letting you focusing on the high-level question. You might consider Boost Graph library for that purpose.

call me Steve
  • 1,709
  • 3
  • 18
  • 31