Detect Cycle in Directed Graph (DFS with Stack)

beginner
11 min

Detect Cycle in Directed Graph (DFS with Stack)

Welcome to this comprehensive guide on detecting cycles in a directed graph using Depth-First Search (DFS) and a stack! By the end of this lesson, you'll be able to navigate through a graph like a pro šŸŽÆ.

What is a Directed Graph? šŸ“

A directed graph is a collection of vertices (or nodes) and edges, where the edges have a direction from one vertex to another. In contrast to an undirected graph, the relationship between vertices in a directed graph is one-way.

Why Detect Cycles in a Directed Graph? šŸ’”

Detecting cycles in a directed graph is essential in various real-world scenarios, such as checking the validity of a computer network, validating a program's flow, or finding the shortest path in a transportation network.

Understanding DFS with Stack šŸ“

DFS is a popular algorithm for traversing graphs. We'll be using a stack to perform the traversal in this lesson. Let's dive in!

Step 1: Initialization āœ…

Initialize a stack and mark all vertices as unvisited. Set the current vertex as the first vertex to explore.

python
stack = [] visited = [False] * vertices_count current_vertex = 0

Step 2: Push and Visit āœ…

Push the current vertex onto the stack and mark it as visited.

python
stack.append(current_vertex) visited[current_vertex] = True

Step 3: Explore Adjacencies āœ…

For each adjacent vertex of the current vertex, recursively call the DFS function if it's not visited yet.

python
for adjacent in adjacency_list[current_vertex]: if not visited[adjacent]: dfs(adjacency_list, visited, stack, adjacent)

Step 4: Stack Operation āœ…

If there are no more unvisited adjacent vertices for the current vertex, pop the vertex from the stack. This indicates that we have completed traversing the connected component of the graph containing the current vertex.

python
if len(stack) > 0: current_vertex = stack.pop() else: return

Step 5: Check for Cycle āœ…

If the DFS process finds a cycle, the current vertex will be popped from the stack and pushed back again before the traversal is complete. This scenario indicates a cycle in the directed graph.

python
if current_vertex == stack[-1]: print("Cycle found!") return

Example: Detecting a Cycle in a Directed Graph šŸŽÆ

Let's look at a practical example to solidify our understanding.

python
vertices_count = 6 adjacency_list = [ [], [2, 3], [0, 4], [0, 5], [1, 5], [1] ] def dfs(adjacency_list, visited, stack, current_vertex): # Your DFS implementation goes here ... dfs(adjacency_list, visited, stack, 0)

In this example, the directed graph contains 6 vertices and edges between them. If you run the DFS algorithm on this graph, you'll find a cycle between vertices 0, 1, and 0 again.

Quick Quiz
Question 1 of 1

Which of the following options correctly represents the purpose of detecting cycles in a directed graph?

By now, you should have a good understanding of how to detect cycles in a directed graph using DFS and a stack. Happy coding! šŸ’”šŸŽÆ