Depth First Search and Breadth First Search Algorithm to Open th

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

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) —

推荐阅读:
描写五莲山风景的作文  诗词名句鉴赏:少壮不努力,老大徒伤悲。  诗词名句鉴赏:秋风萧萧愁杀人  诗词名句鉴赏:树欲静而风不止  诗词名句鉴赏:忧艰常早至,欢会常苦晚。  春王正月原文及翻译  诗词名句鉴赏:风萧萧兮易水寒,壮士一去兮不复还!  申胥谏许越成原文及翻译  诸稽郢行成于吴原文及翻译  诗词名句鉴赏:秋风起兮白云飞,草木黄落兮雁南归。 
评论列表
添加评论