I have written this code to find the cycle in graph

 public boolean hasCycle(int i, int[][] mat) {

        visited[i] = 1;
        // for( int i = 0; i < numNodes; i++ )
        for (int j = 1; j < mat.length; j++) {
            if (mat[i][j] == 1 && i != j) {
                if (visited[j] == 1)
                    return true;
            } else if (hasCycle(j, mat))
                return true;


        return false;

But it is not giving correct result. Can anyone help?

3 Years
Discussion Span
Last Post by noorullah06

"it is not giving correct result" tells us nothing. If you have a compiler or runtime error message post the complete message plus the actual code it refers to. If your code is giving an incorrect result explain what result you get and what the correct result should be.


hey the programe you have writen for the Car.class was that free of error i want to know about that prorame i have done my own but your programe is bit easy to undrstand

Edited by noorullah06

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.