Depth First Search and Breadth First Search Algorithm to Open th

  • 时间:2020-09-10 13:03:17
  • 分类:网络文摘
  • 阅读:96 次

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.

–EOF (The Ultimate Computing & Technology Blog) —

推荐阅读:
货物的原价是多少元  用去水泥和沙子各多少吨  求AB两站距离的问题  如图小正方形的边长为5cm求阴影部分的面积  全天计划生产消毒药水多少瓶  等候上菜和用餐的时间总和最少是多少  在一次环保知识竞赛中  原上的草、人、蒙古包  时间使我们丰厚  美在我的身边 
评论列表
添加评论