📖 Scenario: Imagine you are building a task scheduler that needs to check if there are any circular dependencies between tasks. Each task depends on other tasks to be completed first. If there is a cycle, the scheduler cannot proceed.
🎯 Goal: You will create a directed graph representing tasks and their dependencies, then write code to detect if there is a cycle in the graph.
📋 What You'll Learn
Create a directed graph using a map where keys are task names (strings) and values are slices of dependent task names.
Add a helper map to track visited tasks during traversal.
Implement a function to detect cycles using Depth-First Search (DFS) with recursion and track recursion stack.
Print whether the graph contains a cycle or not.
💡 Why This Matters
🌍 Real World
Cycle detection is crucial in task scheduling, dependency resolution, and deadlock detection in operating systems.
💼 Career
Understanding graph cycle detection is important for software engineers working on compilers, build systems, and complex workflow automation.
Progress0 / 4 steps
1
Create the Directed Graph
Create a variable called graph of type map[string][]string with these exact entries: "A": {"B"}, "B": {"C"}, "C": {"A"}, "D": {"E"}, "E": {}
DSA Go
Hint
Use a map with string keys and slice of strings as values to represent the graph.
2
Add Visited and Recursion Stack Maps
Add two variables called visited and recStack of type map[string]bool initialized as empty maps to track visited nodes and recursion stack respectively.
DSA Go
Hint
Use make(map[string]bool) to create empty maps for visited and recStack.
3
Implement Cycle Detection Function
Write a function called isCyclic that takes parameters node string, graph map[string][]string, visited map[string]bool, and recStack map[string]bool. It returns bool. Use DFS to detect a cycle: if node is not visited, mark it visited and add to recStack. For each neighbor, recursively call isCyclic. If neighbor is in recStack, return true. Remove node from recStack before returning false.
DSA Go
Hint
Use recursion and track nodes in the recursion stack to detect cycles.
4
Check and Print if Graph Contains Cycle
In main, iterate over each node in graph. For each node, if it is not visited, call isCyclic with the node, graph, visited, and recStack. If isCyclic returns true, print "Graph contains a cycle" and return. If no cycles found, print "Graph does not contain a cycle".
DSA Go
Hint
Loop over all nodes and call isCyclic. Print the result accordingly.
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 the direction of edges
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 that starts and ends at the same node, following edge directions.
Step 2: Identify what cycle detection finds
Cycle detection checks if such a path exists in the graph.
Final Answer:
Whether there is a path that starts and ends at the same node following the direction of edges -> Option A
Quick Check:
Cycle detection = finding directed loops [OK]
Hint: Cycle means a loop following edge directions [OK]
Common Mistakes:
Confusing cycle with shortest path
Ignoring edge directions
Thinking cycle means any repeated node
2. Which of the following Go code snippets correctly declares a graph as an adjacency list for cycle detection?
easy
A. graph := make(map[int][]int)
B. graph := []int{}
C. graph := map[int]int{}
D. graph := [][]int{}
Solution
Step 1: Understand adjacency list representation
An adjacency list for a directed graph maps each node to a list of nodes it points to.
Step 2: Check Go syntax for adjacency list
Using map[int][]int creates a map from int to slice of ints, perfect for adjacency list.
Final Answer:
graph := make(map[int][]int) -> Option A
Quick Check:
Adjacency list = map[int][]int [OK]
Hint: Adjacency list uses map of int to slice of int [OK]
Common Mistakes:
Using slice of int only (no mapping)
Using map[int]int which stores single int per node
Using [][]int without map keys
3. What will be the output of the following Go code snippet detecting cycle in a directed graph?