📖 Scenario: Imagine you are working on a task scheduler that runs tasks based on their dependencies. If there is a cycle in the dependencies, the scheduler will get stuck forever. You need to detect if such a cycle exists in the task dependency graph.
🎯 Goal: Build a program in C++ that detects if a directed graph has a cycle using Depth-First Search (DFS).
📋 What You'll Learn
Create a directed graph using adjacency list representation
Use DFS to detect cycles in the graph
Maintain visited and recursion stack arrays to track nodes
Print Cycle detected if a cycle exists, otherwise print No cycle detected
💡 Why This Matters
🌍 Real World
Cycle detection is crucial in task scheduling, deadlock detection, and dependency resolution in software and systems.
💼 Career
Understanding cycle detection helps in roles like software developer, system engineer, and data engineer where graph algorithms are applied.
Progress0 / 4 steps
1
Create the directed graph using adjacency list
Create a vector of vectors called graph with size 4 to represent the directed graph. Add edges exactly as follows: from node 0 to 1, from node 1 to 2, from node 2 to 3, and from node 3 to 1.
DSA C++
Hint
Use vector> graph(4); to create the graph. Use graph[node].push_back(neighbor); to add edges.
2
Add visited and recursion stack vectors
Add two boolean vectors called visited and recStack of size 4, initialized with false. These will track visited nodes and nodes in the current recursion stack.
DSA C++
Hint
Use vector visited(4, false); and vector recStack(4, false); to create these vectors.
3
Implement DFS function to detect cycle
Write a function called bool dfs(int node, vector>& graph, vector& visited, vector& recStack) that returns true if a cycle is detected starting from node. Mark node as visited and add it to recStack. For each neighbor, if it is not visited, recursively call dfs. If neighbor is in recStack, return true. Remove node from recStack before returning false. Then, in main, use a for loop from 0 to 3 to call dfs on unvisited nodes and detect cycle.
DSA C++
Hint
Use recursion to explore neighbors. Track nodes in recursion stack to find back edges indicating cycles.
4
Print the result of cycle detection
Add a print statement in main to display Cycle detected if cycleFound is true, otherwise print No cycle detected.
DSA C++
Hint
Use if (cycleFound) cout << "Cycle detected" << endl; else cout << "No cycle detected" << endl;
Practice
(1/5)
1. What does cycle detection in a directed graph help us find?
easy
A. Whether there is a path that starts and ends at the same node following edge directions
B. The shortest path between two nodes
C. The total number of nodes in the graph
D. The maximum degree of any node
Solution
Step 1: Understand cycle in directed graph
A cycle means a path where you can start from a node and follow edges in their direction to come back to the same node.
Step 2: Identify what cycle detection finds
Cycle detection checks if such a path exists anywhere in the graph.
Final Answer:
Whether there is a path that starts and ends at the same node following edge directions -> Option A
Quick Check:
Cycle detection = find directed loops [OK]
Hint: Cycle means path back to start following edge directions [OK]
Common Mistakes:
Confusing cycle with shortest path
Thinking cycle ignores edge directions
Mixing cycle with node count
2. Which of the following is the correct way to declare a graph adjacency list in C++ for cycle detection?
easy
A. vector graph(100);
B. int graph[100][100];
C. map graph;
D. vector<int> graph[100];
Solution
Step 1: Understand adjacency list declaration
For cycle detection, adjacency list is often a vector of vectors to store edges from each node.
Step 2: Check each option
A: vector graph(100); - standard and flexible vector of vectors for adjacency list. B: adjacency matrix, not list. C: map, valid but less common and heavier for sequential nodes. D: array of vectors, works but fixed size and less modern than vector of vectors.
Final Answer:
vector graph(100); -> Option A
Quick Check:
Use vector of vectors for adjacency list [OK]
Hint: Use vector for flexible adjacency list [OK]
Common Mistakes:
Using adjacency matrix instead of list
Confusing map with vector
Using raw arrays limiting flexibility
3. What will be the output of the following code snippet detecting cycle in a directed graph?