Bird
Raised Fist0
Interview Prepgraphs-dfs-bfsmediumAmazonGoogle

Minimum Number of Vertices to Reach All Nodes

Choose your preparation mode4 modes available
▶
Steps
setup

Initialize in-degree and graph adjacency list

Create an in-degree list initialized to zero for all 6 nodes and an empty adjacency list for each node.

💡 This sets up the data structures needed to track incoming edges and neighbors for each node.
Line:in_degree = [0] * n graph = [[] for _ in range(n)]
💡 We need to know how many edges point to each node to find zero in-degree nodes.
📊
Minimum Number of Vertices to Reach All Nodes - Watch the Algorithm Execute, Step by Step
Watching each step helps you understand how in-degree relates to reachability and why nodes with zero in-degree form the minimal starting set.
Step 1/16
·Active fill★Answer cell
Visiting: none
0
1
2
3
4
5
Visiting: 0
Edge: 0 → 1
0
1
2
3
4
5
Visiting: 0
Edge: 0 → 2
0
1
2
3
4
5
Visiting: 2
Edge: 2 → 5
0
1
2
3
4
5
Visiting: 3
Edge: 3 → 4
0
1
2
3
4
5
Visiting: 4
Edge: 4 → 2
0
1
2
3
4
5
Visiting: none
0
1
2
3
4
5
Queue / Stack
03
Visiting: 0
Edge: 0 → 1
0
1
2
3
4
5
Queue / Stack
31
Visiting: 0
Edge: 0 → 1
0
1
2
3
4
5
Queue / Stack
31
Visiting: 0
Edge: 0 → 2
0
1
2
3
4
5
Queue / Stack
31
Visiting: 3
Edge: 3 → 4
0
1
2
3
4
5
Queue / Stack
14
Visiting: 1
0
1
2
3
4
5
Queue / Stack
4
Visiting: 4
Edge: 4 → 2
0
1
2
3
4
5
Queue / Stack
2
Visiting: 2
Edge: 2 → 5
0
1
2
3
4
5
Queue / Stack
5
Visiting: 5
0
1
2
3
4
5
Visiting: none
0
1
2
3
4
5

Key Takeaways

✓ Nodes with zero in-degree form the minimal set of vertices to reach all nodes.

This is hard to see from code alone because it requires understanding the relationship between in-degree and reachability.

✓ Decrementing in-degree during traversal reveals when nodes become reachable and ready to process.

Visualizing in-degree changes clarifies why nodes enter the queue only after all dependencies are processed.

✓ Disconnected components and cycles do not affect the minimal set beyond their zero in-degree nodes.

The trace shows how separate components are handled independently by the queue initialization.

Practice

(1/5)
1. Consider the following Python code implementing bidirectional BFS to find the shortest path in a binary matrix. Given the input grid below, what is the returned shortest path length?
grid = [
  [0, 1, 0],
  [1, 0, 0],
  [1, 1, 0]
]

# Code snippet uses the bidirectional BFS approach as described.
What is the output of shortestPathBinaryMatrix(grid)?
easy
A. 3
B. 5
C. -1
D. 4

Solution

  1. Step 1: Trace BFS from start (0,0)

    Start at (0,0) with distance 1. Neighbors reachable: (1,1) at distance 2.
  2. Step 2: Trace BFS from end (2,2)

    Start at (2,2) with distance 1. Neighbors reachable: (1,2) at distance 2, (1,1) at distance 2.
  3. Step 3: Meeting point

    Both searches meet at (1,1). Sum distances: 2 (start side) + 2 (end side) = 4, but since both include the meeting cell, shortest path length is 3.
  4. Final Answer:

    Option A -> Option A
  5. Quick Check:

    Shortest path length is 3 steps [OK]
Hint: Bidirectional BFS meets in middle, sum distances minus 1 [OK]
Common Mistakes:
  • Off-by-one in path length
  • Ignoring meeting cell counted twice
2. What is the time complexity of the optimal Kahn's Algorithm solution for the Course Schedule problem, given V courses and E prerequisite pairs?
medium
A. O(V + E) because each node and edge is processed once in BFS
B. O(V * E) due to nested loops over nodes and edges
C. O(V^2) because each node might be checked against all others
D. O(E) only, since edges dominate the processing time

Solution

  1. Step 1: Identify operations on nodes and edges

    Each node is enqueued and dequeued once; each edge is visited once when decrementing in-degree.
  2. Step 2: Combine to get total complexity

    Total time is proportional to number of nodes plus number of edges -> O(V + E).
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Standard BFS topological sort runs in linear time relative to graph size [OK]
Hint: Each edge and node processed once -> O(V + E) [OK]
Common Mistakes:
  • Assuming nested loops cause O(V*E)
  • Confusing dense graph worst case with typical complexity
  • Ignoring that BFS processes nodes once
3. What is the time complexity of the optimal approach using a single trust score array to find the town judge, given n people and m trust pairs?
medium
A. O(n * m) because we scan trust pairs for each person
B. O(n^2) because we check trust relationships pairwise
C. O(m^2) because trust pairs are nested in loops
D. O(n + m) because we process each trust pair once and then scan all people once

Solution

  1. Step 1: Identify loops in the algorithm

    The algorithm iterates once over all trust pairs (m) and then once over all people (n).
  2. Step 2: Combine complexities

    Processing trust pairs is O(m), scanning people is O(n), total O(n + m).
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Single pass over trust and people arrays [OK]
Hint: Single pass over trust pairs and people yields O(n + m) [OK]
Common Mistakes:
  • Assuming nested loops cause O(n*m)
  • Confusing trust pairs count with people count
  • Ignoring linear scans after trust processing
4. What is the time complexity of the optimal BFS approach for the Pacific Atlantic Water Flow problem on an m x n grid, and why?
medium
A. O(m * n) because each cell is enqueued and processed at most once per ocean.
B. O(m * n * log(m * n)) due to priority queue operations in BFS.
C. O(m^2 * n^2) because BFS is run from every cell.
D. O(m + n) because BFS only processes border cells.

Solution

  1. Step 1: Analyze BFS visits

    Each BFS starts from border cells and visits each cell at most once per ocean, so total visits ≤ 2 * m * n.
  2. Step 2: Confirm no repeated work

    Visited arrays prevent revisiting cells, so BFS complexity is linear in number of cells.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Each cell processed once per ocean -> O(m*n) [OK]
Hint: BFS visits each cell once per ocean [OK]
Common Mistakes:
  • Confusing with brute force DFS from every cell
  • Assuming BFS uses priority queue
5. Examine the following buggy code snippet for bidirectional BFS to find the shortest path in a binary matrix. Which line contains the subtle bug that can cause infinite loops or incorrect results?
def visit(queue, visited, other_visited):
    r, c = queue.popleft()
    for dr, dc in directions:
        nr, nc = r + dr, c + dc
        if 0 <= nr < n and 0 <= nc < n and grid[nr][nc] == 0:
            if (nr, nc) in other_visited:
                return visited[(r, c)] + other_visited[(nr, nc)]
            if (nr, nc) not in visited:
                queue.append((nr, nc))  # Bug here
                # Missing visited marking here
    return None
medium
A. Line where the current cell is popped from the queue
B. Line where the cell is checked if in other_visited
C. Line where neighbors are checked for grid boundaries
D. Line where neighbor is appended to queue without marking visited

Solution

  1. Step 1: Identify visited marking usage

    Visited must be marked when neighbor is enqueued to avoid revisiting.
  2. Step 2: Locate missing visited marking

    Neighbor is appended to queue but not marked visited, causing possible infinite revisits.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Marking visited on enqueue prevents infinite loops [OK]
Hint: Mark visited when enqueuing neighbors [OK]
Common Mistakes:
  • Mark visited only when dequeuing
  • Forgetting visited marking entirely
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