educative.io

Educative

Isn't this a simpler solution for detecting cycles in a graph?

def detect_cycle(graph):
    return detect_cycle_helper(graph, 0, [False] * graph.V)


def detect_cycle_helper(graph, current, visited):
    if visited[current]:
        return True

    visited[current] = True

    temp = graph.graph[current]

    while temp:
        if detect_cycle_helper(graph, temp.data, visited):
            return True

        temp = temp.next

    return False

Yes, you’re correct!