Question:
How to Detect cycle in an undirected graph with Java?

Summary:

To determine whether or not an undirected graph with V vertices that are labeled from 0 to V-1 and E edges contains any cycle, it is necessary to examine the graph. The graph is represented as an adjacency list, where adj[i] provides a list of all the nodes that the ith node has an edge with.


Solution:

class Pair{

    int child,parent;

    Pair(int c,int p)

    {

        child = c;

        parent = p;

    }

}


class Solution {

    

    private static boolean help(ArrayList<ArrayList<Integer>> adj, int source, boolean []visited)

    {

        Queue<Pair> queue = new LinkedList<>();

        Pair p = new Pair(source,-1);

        

        queue.offer(p);

      

        while(!queue.isEmpty())

        {

            Pair x = queue.poll();

            

            int child = x.child;

            int parent = x.parent;

            

            visited[child] = true;

            

            ArrayList<Integer> l = adj.get(child);

            for(int ele : l)

            {

                if(!visited[ele])

                {

                    queue.offer(new Pair(ele,child));

                }

                else if(ele != parent)

                {

                    return true;

                }

            }

            

        }        

        

        return false;

    }

    // Function to detect cycle in an undirected graph.

    public boolean isCycle(int V, ArrayList<ArrayList<Integer>> adj) 

    {

        boolean[] visited = new boolean[V];

        

        for(int i=0;i<V;i++)

        {

            if(!visited[i])

            {

                if(help(adj,i,visited))

                {

                    return true;

                }

            }

        }

        

        return false;    

    }

}


Explanation:

The above code defines a Java class named ‘Pair’. The Pair class is used to represent a pair of integers, typically representing a child node and its parent node.


The isCycle method uses a breadth-first search (BFS) algorithm to detect cycles in the graph. It iterates through each vertex in the graph and performs a BFS traversal starting from that vertex. During the BFS traversal, it maintains a queue of pairs representing vertices and their parent vertices. If it encounters an adjacent vertex that is already visited and is not the parent of the current vertex, it indicates the presence of a cycle in the graph and returns true. Otherwise, if no cycles are found after traversing all vertices, it returns false.


Answered by: >gottapupavankalyan

Credit: >GeekforGeeks


Suggested blogs:

>Python Error Solved: load_associated_files do not load a txt file

>Python Error Solved: pg_config executable not found

>Set up Node.js & connect to a MongoDB Database Using Node.js

>Setting up a Cloud Composer environment: Step-by-step guide

>What is microservice architecture, and why is it better than monolithic architecture?

>What is pipe in Angular?

>What makes Python 'flow' with HTML nicely as compared to PHP?

>What to do when MQTT terminates an infinite loop while subscribing to a topic in Python?

>Creating custom required rule - Laravel validation

>How to configure transfers for different accounts in stripe with laravel?


Submit
0 Answers