using three colors white gray and black white vertices that are not processed inital state of all vertices gray vertices that are in dfs black fully traversed vertices i e its progenies are also done if while traversing any adjacent node is colored gray that means cycle exists g addedge0 1 g addedge0 2 g addedge1 3 g add_edge 0 1 g add_edge 0 2 g add_edge 1 3
from collections import defaultdict

class Graph:

    def __init__(self, vertices):
        self.vertices = vertices
        self.graph = defaultdict(list)


    def add_edge(self, u, v):
        self.graph[u].append(v)


    def detect_cycle(self):
        color = ['white'] * self.vertices
        visited = [False] * self.vertices

        stack = []

        for v in range(self.vertices):
            if color[v] == 'white':
                color[v] = 'gray'

                stack.append(v)

                while stack:
                    s = stack.pop()

                    for i in self.graph[s]:
                        if color[i] == 'white':
                            stack.append(i)
                            color[i] = 'gray'
                        elif color[i] == 'gray':
                            return "Cycle detected"

            color[v] = 'black'
        
        return "Cycle not present"


g = Graph(4)
g.add_edge(0, 1)
g.add_edge(0, 2)
g.add_edge(1, 2)
g.add_edge(2, 0)
g.add_edge(2, 3)
g.add_edge(3, 3)



print(g.detect_cycle())