Depth First Search and Breadth First Search Algorithm to Open th
- 时间:2020-09-10 13:03:17
- 分类:网络文摘
- 阅读:90 次
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) —
推荐阅读:预防湿疹复发可常吃清热祛湿食物 调和油乱象:市场价格和行业标准之乱 消费者该如何识别和选择食用油? 月饼是“三高”食品 六类人群不宜多吃 谁来保障转基因食品的公众知情权? 适宜老年人的秋令养阴滋补的菜肴 营养专家建议的老年人健康饮食原则 绑架“第一口奶”该曝光的不仅是多美滋 汇源等多家国产果汁巨头卷入“烂果门” 两性营养保健:哪些食物让男人更持久
- 评论列表
-
- 添加评论