
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python Program for Detect Cycle in a Directed Graph
In this article, we will learn about the solution to the problem statement given below.
Problem statement − We are given a directed graph, we need to check whether the graph contains a cycle or not. The output should be true if the given graph contains at least one cycle, otherwise false.
Now let’s observe the solution in the implementation below −
Example
# collections module from collections import defaultdict # class for creation of graphs class Graph(): # constructor 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): # Marking current node visited and addition to recursion stack visited[v] = True recStack[v] = True # if any neighbour is visited and in recStack then graph is cyclic in nature 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 # pop the node after the end of recursion recStack[v] = False return False # Returns true if graph is cyclic def isCyclic(self): visited = [False] * self.V recStack = [False] * self.V 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, 3) g.addEdge(0, 2) g.addEdge(3, 2) g.addEdge(2, 0) g.addEdge(1, 3) g.addEdge(2, 1) if g.isCyclic() == 1: print ("Graph is cyclic in nature") else: print ("Graph is non-cyclic in nature")
Output
Graph is cyclic in nature
All the variables are declared in the local scope and their references are seen in the figure above.
Conclusion
In this article, we have learned about how we can make a Python Program to Detect Cycle in a Directed Graph
- Related Articles
- Detect Cycle in a Directed Graph
- C++ Program to Check Whether a Directed Graph Contains a Eulerian Cycle
- Detect Cycle in a an Undirected Graph
- Program to reverse the directed graph in Python
- Python Program to Detect the Cycle in a Linked List
- Program to find largest color value in a directed graph in Python
- Connectivity in a directed graph
- Euler Circuit in a Directed Graph
- Python Program for Cycle Sort
- Longest Path in a Directed Acyclic Graph
- Shortest Path in a Directed Acyclic Graph
- C++ Program to Generate a Random Directed Acyclic Graph DAC for a Given Number of Edges
- Directed Acyclic Graph (DAG)
- C++ Program to Check Whether it is Weakly Connected or Strongly Connected for a Directed Graph
- C++ Program to Check Whether a Directed Graph Contains a Eulerian Path

Advertisements