Bird
Raised Fist0
DSA Goprogramming~5 mins

Cycle Detection in Directed Graph in DSA Go - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is a cycle in a directed graph?
A cycle in a directed graph is a path that starts and ends at the same vertex, following the direction of edges.
Click to reveal answer
intermediate
Which data structure is commonly used to detect cycles in a directed graph using DFS?
A recursion stack (or call stack) is used along with a visited set to detect back edges indicating cycles.
Click to reveal answer
intermediate
What does a back edge represent in DFS traversal of a directed graph?
A back edge points from a node to one of its ancestors in the DFS tree, indicating a cycle.
Click to reveal answer
advanced
Why can't we use a simple visited set alone to detect cycles in directed graphs?
Because a node can be visited multiple times in different paths; we need to track nodes in the current recursion stack to detect cycles.
Click to reveal answer
beginner
What is the time complexity of cycle detection in a directed graph using DFS?
The time complexity is O(V + E), where V is vertices and E is edges, since each node and edge is visited once.
Click to reveal answer
What indicates a cycle during DFS in a directed graph?
AFinding a back edge to a node in the current recursion stack
BVisiting a node for the first time
CFinding a node with no outgoing edges
DCompleting DFS without revisiting nodes
Which of these is NOT needed for cycle detection in a directed graph using DFS?
AVisited set
BQueue for BFS
CRecursion stack
DGraph adjacency list
What does the recursion stack track in cycle detection?
AAll visited nodes
BNodes with no incoming edges
CNodes currently being explored in the DFS path
DNodes already processed
If no back edge is found during DFS, what can we conclude?
AThe graph is undirected
BThe graph is disconnected
CThe graph has at least one cycle
DThe graph has no cycles
What is the main difference between cycle detection in directed vs undirected graphs?
ADirected graphs require tracking recursion stack; undirected graphs do not
BUndirected graphs use recursion stack; directed graphs do not
CCycle detection is impossible in directed graphs
DBoth use the same method
Explain how DFS can be used to detect a cycle in a directed graph.
Think about tracking nodes currently in the path and revisiting them.
You got /4 concepts.
    Describe why a recursion stack is necessary for cycle detection in directed graphs but not in undirected graphs.
    Consider how cycles appear differently in directed vs undirected graphs.
    You got /4 concepts.

      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

      1. Step 1: Understand cycle in directed graph

        A cycle means a path that starts and ends at the same node, following edge directions.
      2. Step 2: Identify what cycle detection finds

        Cycle detection checks if such a path exists in the graph.
      3. Final Answer:

        Whether there is a path that starts and ends at the same node following the direction of edges -> Option A
      4. 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

      1. Step 1: Understand adjacency list representation

        An adjacency list for a directed graph maps each node to a list of nodes it points to.
      2. 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.
      3. Final Answer:

        graph := make(map[int][]int) -> Option A
      4. 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?
      package main
      import "fmt"
      
      func dfs(node int, graph map[int][]int, visited, recStack map[int]bool) bool {
          if recStack[node] {
              return true
          }
          if visited[node] {
              return false
          }
          visited[node] = true
          recStack[node] = true
          for _, neighbor := range graph[node] {
              if dfs(neighbor, graph, visited, recStack) {
                  return true
              }
          }
          recStack[node] = false
          return false
      }
      
      func main() {
          graph := map[int][]int{
              1: {2},
              2: {3},
              3: {1},
          }
          visited := make(map[int]bool)
          recStack := make(map[int]bool)
          fmt.Println(dfs(1, graph, visited, recStack))
      }
      
      medium
      A. false
      B. true
      C. Compilation error
      D. Runtime panic

      Solution

      1. Step 1: Analyze graph structure

        Graph edges: 1->2, 2->3, 3->1 form a cycle.
      2. Step 2: Trace DFS cycle detection

        DFS from node 1 visits 2, then 3, then back to 1 which is in recursion stack, so returns true.
      3. Final Answer:

        true -> Option B
      4. Quick Check:

        Cycle detected = true [OK]
      Hint: Cycle if node revisited in recursion stack [OK]
      Common Mistakes:
      • Ignoring recursion stack for cycle detection
      • Assuming visited alone detects cycle
      • Confusing false with no cycle
      4. Identify the bug in the following Go code snippet for cycle detection in a directed graph:
      func dfs(node int, graph map[int][]int, visited map[int]bool) bool {
          if visited[node] {
              return true
          }
          visited[node] = true
          for _, neighbor := range graph[node] {
              if dfs(neighbor, graph, visited) {
                  return true
              }
          }
          return false
      }
      medium
      A. Return type should be int, not bool
      B. Visited map should be a slice, not a map
      C. Missing recursion stack to track nodes in current path
      D. Graph should be a slice of slices, not a map

      Solution

      1. Step 1: Understand cycle detection logic

        Cycle detection in directed graph needs to track nodes in current recursion path (recursion stack).
      2. Step 2: Identify missing recursion stack

        This code only uses visited to mark visited nodes, which cannot detect cycles correctly in directed graphs.
      3. Final Answer:

        Missing recursion stack to track nodes in current path -> Option C
      4. Quick Check:

        Recursion stack needed for directed cycle detection [OK]
      Hint: Use recursion stack to detect back edges [OK]
      Common Mistakes:
      • Using only visited to detect cycles
      • Confusing undirected and directed cycle detection
      • Ignoring recursion stack leads to false negatives
      5. Given a directed graph represented as adjacency list:
      graph := map[int][]int{
          1: {2, 3},
          2: {4},
          3: {4, 5},
          4: {6},
          5: {6},
          6: {3},
      }
      Which nodes are part of a cycle in this graph?
      hard
      A. [1, 3, 5]
      B. [1, 2, 5]
      C. [2, 4, 5]
      D. [3, 4, 6]

      Solution

      1. Step 1: Analyze edges for cycles

        Edges: 3->4, 4->6, 6->3 form a cycle involving nodes 3, 4, 6.
      2. Step 2: Confirm cycle nodes

        Nodes 1, 2, 5 lead into cycle but are not part of cycle themselves.
      3. Final Answer:

        [3, 4, 6] -> Option D
      4. Quick Check:

        Cycle nodes = [3,4,6] [OK]
      Hint: Trace back edges to find cycle nodes [OK]
      Common Mistakes:
      • Including nodes that only lead to cycle
      • Missing nodes that form the cycle
      • Confusing cycle membership with reachability
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard