Check if the undirected graph is cyclic Here is my approach. Do DFS and check in stack whether the new node is already in the Stack or not.

public static boolean isCyclicDFS(int[][] adjMat) {
        int len = adjMat.length;
        boolean[] isVisited = new boolean[len];
        for(int i =0; i&lt;len; i++) {
            isVisited[i] = false;
        }

        Stack s = new Stack&lt;&gt;();
        s.add(0);

        while(!s.isEmpty()) {
            int current = (int) s.pop();
            System.out.println(current);
            isVisited[current] = true;
            for(int i =0; i&lt;len; i++) {
                if(adjMat[current][i] ==1 &amp;&amp; isVisited[i] == false) {
                    if(s.contains(i)) {
                        return false;
                    }
                    s.push(i);
                    break;
                }
            }



        }
        return true;

    }


Kindly suggest if any improvement/correction.
Formatting

Your spacing is inconsistent, which makes the code harder to read. By the original conventions released with the language, =, ==, and &lt; should have a whitespace character on either side. Whatever you choose, make it consistent.

Implementation

Your System.out.println is for debugging and should be removed.

Use Arrays.fill() to populate your array.

Declaring len makes the code harder to read. Just refer to the length in your for loop.

As @Marc-Andre said, prefer a Deque to a Stack. And use generics.

Don't do if (foo == false). Use if (!foo).

I'm in the minority, but I prefer explicit boxing rather than autoboxing for clarity.

Don't use abbreviations for variable names. The extra characters enhance readability and cost nothing.

If it's in your control, for a non-weighted graph the adjacency matrix should probably be a boolean[][], not an int[][]. If this is a weighted graph, checking if the value == 1 is almost certainly a bug.

Correctness

You detect a cycle in this graph: { { 0, 1 }, { 1, 0 } }. Is it really a cycle if any two nodes are adjacent to each other? Is this an unweighted directed graph?

You also detect a cycle in this graph: { { 0, 1 }, { 0, 0 } }. Surely that is not correct. Please test before posting. CodeReview is for reviewing working code, not fixing broken code.