Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript Detect Cycle in a Directed Graph

<script>
  
// A JavaScript Program to detect cycle in a graph
  
let V;
let adj=[];
function Graph(v)
{
    V=v;
    for (let i = 0; i < V; i++)
        adj.push([]);
}
  
// This function is a variation of DFSUtil() in
    // https://www.geeksforgeeks.org/archives/18212
function isCyclicUtil(i,visited,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;
        let children = adj[i];
           
        for (let c=0;c< children.length;c++)
            if (isCyclicUtil(children, visited, recStack))
                return true;
                   
        recStack[i] = false;
   
        return false;
}
  
function addEdge(source,dest)
{
    adj.push(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
function isCyclic()
{
    // Mark all the vertices as not visited and
        // not part of recursion stack
        let visited = new Array(V);
        let recStack = new Array(V);
        for(let i=0;i<V;i++)
        {
            visited[i]=false;
            recStack[i]=false;
        }
          
           
        // Call the recursive helper function to
        // detect cycle in different DFS trees
        for (let i = 0; i < V; i++)
            if (isCyclicUtil(i, visited, recStack))
                return true;
   
        return false;
}
  
// Driver code
Graph(4);
addEdge(0, 1);
addEdge(0, 2);
addEdge(1, 2);
addEdge(2, 0);
addEdge(2, 3);
addEdge(3, 3);
  
if(isCyclic())
    document.write("Graph contains cycle");
else
    document.write("Graph doesn't "
                   + "contain cycle");
  
  
// This code is contributed by patel2127
  
</script>
Comment

PREVIOUS NEXT
Code Example
Javascript :: _.extend() Explanation 
Javascript :: check first path of url js 
Javascript :: use stigviewr 
Javascript :: can we Plot observale for ejs 
Javascript :: _.template Underscore Example 
Javascript :: how to cut and paste an element in vanilla javascript 
Javascript :: how to read json data from database in laravel 
Javascript :: modify summernote with js 
Javascript :: prisma multiple queries in one query 
Javascript :: Unable to delete directory react native 
Javascript :: make navigation open when items are active 
Javascript :: mantine progress 
Javascript :: context 
Javascript :: Compiled with problems:X ERROR [eslint] Plugin "react" was conflicted between 
Javascript :: modalInstance.result.then when execute 
Javascript :: convert h2 to h1 jQuery 
Javascript :: function Using onpause and onplay Method to Start and Stop Animationto replace source file path to jpg image 
Javascript :: javascript loop through array backwords 
Javascript :: pencil button in react 
Javascript :: swift urlsession remote json 
Javascript :: random jwt secret key generator 
Javascript :: react-native-wagmi-charts 
Javascript :: nodejs mysql escaping query 
Javascript :: The complete map() method syntax 
Javascript :: autonumeric stimulus 
Javascript :: react-icons/vsc 
Javascript :: Call Injected AngularJs Service In Controller From Blazor Within CustomElement/WebComponent 
Javascript :: Fire "data-ng-change" programatically or another way to change value of input on website using Angular JS 
Javascript :: HTTP Get with looping password validation not working 
Javascript :: change useragent cypress test run 
ADD CONTENT
Topic
Content
Source link
Name
1+5 =