Depth First Search and Breadth First Search Algorithm to Open the Doors to the Rooms with Keys


There are N rooms and you start in room 0. Each room has a distinct number in 0, 1, 2, …, N-1, and each room may have some keys to access the next room.

Formally, each room i has a list of keys rooms[i], and each key rooms[i][j] is an integer in [0, 1, …, N-1] where N = rooms.length. A key rooms[i][j] = v opens the room with number v.

Initially, all the rooms start locked (except for room 0).

You can walk back and forth between rooms freely.

Return true if and only if you can enter every room.

Example 1:
Input: [[1],[2],[3],[]]
Output: true
Explanation:
We start in room 0, and pick up key 1.
We then go to room 1, and pick up key 2.
We then go to room 2, and pick up key 3.
We then go to room 3. Since we were able to go to every room, we return true.

Example 2:
Input: [[1,3],[3,0,1],[2],[0]]
Output: false
Explanation: We can’t enter the room with number 2.
Note:
1 <= rooms.length <= 1000
0 <= rooms[i].length <= 1000
The number of keys in all rooms combined is at most 3000.

Room searching with keys can be solved by using Depth Search Algorithm or Breadth First Search Algorithm. The DFS can be implemented in Recursion or the classic iterative approach with the help of a stack.

Room Traversal using Depth First Search Algorithm

We would need a hash set e.g. unordered_set to remember the rooms that we have been to. Then, as long as we are in the room, we can depth first search the rooms whose keys are in the room. Once the search is finished, we can count the number of the keys in the set, and compare to the number of the rooms.

Depth First Search can be easily implemented using Recursion.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public:
    bool canVisitAllRooms(vector<vector<int>>& rooms) {
        dfs(0, rooms);
        return visited.size() == rooms.size();
    }
private:
    unordered_set<int> visited;
    void dfs(int room, vector<vector<int>>& rooms) {
        if (visited.count(room)) {
            return;
        }
        visited.insert(room);
        for (const auto &n: rooms[room]) {
            dfs(n, rooms);
        }
    }
};
class Solution {
public:
    bool canVisitAllRooms(vector<vector<int>>& rooms) {
        dfs(0, rooms);
        return visited.size() == rooms.size();
    }
private:
    unordered_set<int> visited;
    void dfs(int room, vector<vector<int>>& rooms) {
        if (visited.count(room)) {
            return;
        }
        visited.insert(room);
        for (const auto &n: rooms[room]) {
            dfs(n, rooms);
        }
    }
};

The complexity is O(N + E) where N is the total number of rooms and E is the number of the keys. We need O(N) space as the implicit requirement of using Recursion.

We can simulate the recursion by using the stacks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public:
    bool canVisitAllRooms(vector<vector<int>>& rooms) {
        unordered_set<int> visited;
        stack<int> st;
        st.push(0);
        while (!st.empty()) {
            auto p = st.top();
            st.pop();
            if (visited.count(p)) continue;
            visited.insert(p);
            for (const auto &n: rooms[p]) {
                st.push(n);
            }
        }
        return visited.size() == rooms.size();
    }
};
class Solution {
public:
    bool canVisitAllRooms(vector<vector<int>>& rooms) {
        unordered_set<int> visited;
        stack<int> st;
        st.push(0);
        while (!st.empty()) {
            auto p = st.top();
            st.pop();
            if (visited.count(p)) continue;
            visited.insert(p);
            for (const auto &n: rooms[p]) {
                st.push(n);
            }
        }
        return visited.size() == rooms.size();
    }
};

One subtle change could be to avoid pushing next nodes to the stack by moving the duplication check. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public:
    bool canVisitAllRooms(vector<vector<int>>& rooms) {
        unordered_set<int> visited;
        stack<int> st;
        st.push(0);
        while (!st.empty()) {
            auto p = st.top();
            st.pop();            
            visited.insert(p);
            for (const auto &n: rooms[p]) {
                if (!visited.count(n)) {
                    st.push(n);
                }
            }
        }
        return visited.size() == rooms.size();
    }
};
class Solution {
public:
    bool canVisitAllRooms(vector<vector<int>>& rooms) {
        unordered_set<int> visited;
        stack<int> st;
        st.push(0);
        while (!st.empty()) {
            auto p = st.top();
            st.pop();            
            visited.insert(p);
            for (const auto &n: rooms[p]) {
                if (!visited.count(n)) {
                    st.push(n);
                }
            }
        }
        return visited.size() == rooms.size();
    }
};

Breadth First Search Algorithm to Find the Rooms with Keys

If we are replacing the stack by a queue, we are actually implemented the Breadth First Search.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public:
    bool canVisitAllRooms(vector<vector<int>>& rooms) {
        unordered_set<int> visited;
        queue<int> q;
        q.push(0);
        while (!q.empty()) {
            auto p = q.front();
            q.pop();            
            visited.insert(p);
            for (const auto &n: rooms[p]) {
                if (!visited.count(n)) {
                    q.push(n);
                }
            }
        }
        return visited.size() == rooms.size();
    }
};
class Solution {
public:
    bool canVisitAllRooms(vector<vector<int>>& rooms) {
        unordered_set<int> visited;
        queue<int> q;
        q.push(0);
        while (!q.empty()) {
            auto p = q.front();
            q.pop();            
            visited.insert(p);
            for (const auto &n: rooms[p]) {
                if (!visited.count(n)) {
                    q.push(n);
                }
            }
        }
        return visited.size() == rooms.size();
    }
};

A BFS ensures we expand the nodes level-by-level. Similar complexity compared to DFS algorithm. See also: Teaching Kids Programming – DFS and BFS Algorithm in a Graph (Unlock Rooms with Keys)

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
785 words
Last Post: Dynamic Programming (Memoization) to Sort Integers by The Power Value
Next Post: How to Remove Zero Sum Consecutive Nodes from Linked List using Prefix Sum?

The Permanent URL is: Depth First Search and Breadth First Search Algorithm to Open the Doors to the Rooms with Keys

Leave a Reply