Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java Detect Cycle in a Directed Graph

// A Java Program to detect cycle in a graph
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
  
class Graph {
      
    private final int V;
    private final List<List<Integer>> adj;
  
    public Graph(int V) 
    {
        this.V = V;
        adj = new ArrayList<>(V);
          
        for (int i = 0; i < V; i++)
            adj.add(new LinkedList<>());
    }
      
    // This function is a variation of DFSUtil() in 
    // https://www.geeksforgeeks.org/archives/18212
    private boolean isCyclicUtil(int i, boolean[] visited,
                                      boolean[] recStack) 
    {
          
        // Mark the current node as visited and
        // part of recursion stack
        if (recStack[i])
            return true;
  
        if (visited[i])
            return false;
              
        visited[i] = true;
  
        recStack[i] = true;
        List<Integer> children = adj.get(i);
          
        for (Integer c: children)
            if (isCyclicUtil(c, visited, recStack))
                return true;
                  
        recStack[i] = false;
  
        return false;
    }
  
    private void addEdge(int source, int dest) {
        adj.get(source).add(dest);
    }
  
    // Returns true if the graph contains a 
    // cycle, else false.
    // This function is a variation of DFS() in 
    // https://www.geeksforgeeks.org/archives/18212
    private boolean isCyclic() 
    {
          
        // Mark all the vertices as not visited and
        // not part of recursion stack
        boolean[] visited = new boolean[V];
        boolean[] recStack = new boolean[V];
          
          
        // Call the recursive helper function to
        // detect cycle in different DFS trees
        for (int i = 0; i < V; i++)
            if (isCyclicUtil(i, visited, recStack))
                return true;
  
        return false;
    }
  
    // Driver code
    public static void main(String[] args)
    {
        Graph graph = new Graph(4);
        graph.addEdge(0, 1);
        graph.addEdge(0, 2);
        graph.addEdge(1, 2);
        graph.addEdge(2, 0);
        graph.addEdge(2, 3);
        graph.addEdge(3, 3);
          
        if(graph.isCyclic())
            System.out.println("Graph contains cycle");
        else
            System.out.println("Graph doesn't "
                                    + "contain cycle");
    }
}
  
// This code is contributed by Sagar Shah.
Comment

PREVIOUS NEXT
Code Example
Java :: java list remove 
Java :: module java.base does not "opens java.io" to unnamed module 
Java :: binary tree level traversal 
Java :: java words from file 
Java :: for loop condition java 
Java :: array.swap java 
Java :: intellij set target bytecode version 
Java :: java.lang.SecurityException: Permission Denial: reading androidx.core.content.FileProvider 
Java :: java change frame border 
Java :: android studio clear views of layout 
Java :: what is arraylist 
Java :: python discord embed generator 
Java :: resultset next method 
Java :: Bukkit plugin player variable 
Java :: A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptWithoutKotlincTask$KaptExecutionWorkAction java.lang.reflect.InvocationTargetException (no error message) 
Java :: android studio find all views in layout 
Java :: put in spring rest api 
Java :: java get number at the end of string 
Java :: mongodb spring boot index usage 0 
Java :: Java 2-dimensional Array 
Java :: java break string at comma 
Java :: how to create textview in java android 
Java :: javax dependency android 
Java :: java linked list swap elements 
Java :: java final modifier on method 
Java :: get string size 
Java :: implement queue using array in java 
Java :: array in java 
Java :: spring mvc 
Java :: sum of array in java 
ADD CONTENT
Topic
Content
Source link
Name
5+6 =