Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python Detect Cycle in a Directed Graph

# Python program to detect cycle 
# in a graph
  
from collections import defaultdict
  
class Graph():
    def __init__(self,vertices):
        self.graph = defaultdict(list)
        self.V = vertices
  
    def addEdge(self,u,v):
        self.graph[u].append(v)
  
    def isCyclicUtil(self, v, visited, recStack):
  
        # Mark current node as visited and 
        # adds to recursion stack
        visited[v] = True
        recStack[v] = True
  
        # Recur for all neighbours
        # if any neighbour is visited and in 
        # recStack then graph is cyclic
        for neighbour in self.graph[v]:
            if visited[neighbour] == False:
                if self.isCyclicUtil(neighbour, visited, recStack) == True:
                    return True
            elif recStack[neighbour] == True:
                return True
  
        # The node needs to be popped from 
        # recursion stack before function ends
        recStack[v] = False
        return False
  
    # Returns true if graph is cyclic else false
    def isCyclic(self):
        visited = [False] * (self.V + 1)
        recStack = [False] * (self.V + 1)
        for node in range(self.V):
            if visited[node] == False:
                if self.isCyclicUtil(node,visited,recStack) == True:
                    return True
        return False
  
g = Graph(4)
g.addEdge(0, 1)
g.addEdge(0, 2)
g.addEdge(1, 2)
g.addEdge(2, 0)
g.addEdge(2, 3)
g.addEdge(3, 3)
if g.isCyclic() == 1:
    print "Graph has a cycle"
else:
    print "Graph has no cycle"
  
# Thanks to Divyanshu Mehta for contributing this code
Comment

PREVIOUS NEXT
Code Example
Python :: python datetime toordinal 
Python :: how to use print function in python stack overflow 
Python :: Unable to locate package python-obexftp 
Python :: plot multiple ROC in python 
Python :: how to install apps in django 
Python :: os scan dir python 2 
Python :: yml file for django 
Python :: pandas get most occurring value for each id 
Python :: How to swapcase of string in python 
Python :: python variable and data structure 
Python :: how to set notepad ++ for run python 
Python :: Reading CSV delimited format 
Python :: pyqt5 running string and clock stackoverfloww 
Python :: how to write def 
Python :: list comperhension condition in python 
Python :: how to detect if a key was press down 
Python :: python extract extension 
Python :: django orm filter equal insensitive 
Python :: python sort_values 
Python :: how to compare the two key from constant value to list of string in python 
Python :: python array_combine 
Python :: python - create frequency table between two columns 
Python :: rotate an image python keras 
Python :: no lapack/blas resources found scipy 
Python :: python Write code that asks users to enter the year they were born. Print out how many years old they will turn in 2019. 
Python :: sqlalchemy create engine SQLite Relative 
Python :: how to call a function in python? 
Python :: Python Class Without Getters and Setters 
Python :: Errors that you will get in the Time class in Python DateTime 
Python :: non linear regression 
ADD CONTENT
Topic
Content
Source link
Name
1+8 =