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?