标签:
As suggested by the hints, this problem is equivalent to detecting a cycle in the graph represented by prerequisites
. Both BFS and DFS can be used to solve this problem using the idea oftopological sort. If you find yourself unfamiliar with these concepts, you need to may refer to their wikipedia pages. Specifically, you may only need to refer to the link in the third hint to solve this problem.
Since pair<int, int>
is inconvenient for the implementation of graph algorithms, we first transform it to a graph. If course u
is a prerequisite of course v
, we will add an edge from nodeu
to node v
.
BFS uses the indegrees of each node. We will first try to find a node with 0
indegree. If we fail to do so, there must be a cycle in the graph and we return false
. Otherwise we have found one. We set its indegree to be -1
to prevent from visiting it again and reduce the indegrees of all its neighbors by 1
. This process will be repeated for n
(number of nodes) times. If we have not returned false
, we will return true
.
The code is as follows, which should be self-explanatory.
// BFS class Solution { public: bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) { vector<unordered_set<int> > graph = make_graph(numCourses, prerequisites); vector<int> degrees = compute_indegree(graph); for (int i = 0; i < numCourses; i++) { int j = 0; for (; j < numCourses; j++) if (!degrees[j]) break; if (j == numCourses) return false; degrees[j] = -1; for (auto itr = graph[j].begin(); itr != graph[j].end(); itr++) degrees[*itr]--; } return true; } private: vector<unordered_set<int> > make_graph(int numCourses, vector<pair<int, int> >& prerequisites) { vector<unordered_set<int> > graph(numCourses); for (auto pre : prerequisites) graph[pre.second].insert(pre.first); return graph; } vector<int> compute_indegree(vector<unordered_set<int> >& graph) { vector<int> degrees(graph.size(), 0); for (auto neighbors : graph) for (auto neigh : neighbors) degrees[neigh]++; return degrees; } };
For DFS, it will first visit a node, then one neighbor of it, then one neighbor of this neighbor... and so on. If it meets a node which was visited in the current process of DFS visit, a cycle is detected and we will return false
. Otherwise it will start from another unvisited node and repeat this process till all the nodes have been visited. Note that you should make two records: one is to record all the visited nodes and the other is to record the visited nodes in the current DFS visit.
The code is as follows. We use a vector<bool>
to record all the visited nodes and anunordered_set<int>
to record the visited nodes of the current DFS visit. Once the current visit is finished, we erase the starting node from the set.
// DFS class Solution { public: bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) { vector<unordered_set<int> > graph = make_graph(numCourses, prerequisites); unordered_set<int> curVisit; vector<bool> visited(numCourses, false); for (int i = 0; i < numCourses; i++) if (!visited[i] && dfs_cycle(graph, i, curVisit, visited)) return false; return true; } private: vector<unordered_set<int> > make_graph(int numCourses, vector<pair<int, int> >& prerequisites) { vector<unordered_set<int> > graph(numCourses); for (auto pre : prerequisites) graph[pre.second].insert(pre.first); return graph; } bool dfs_cycle(vector<unordered_set<int> >& graph, int node, unordered_set<int>& curVisit, vector<bool>& visited) { visited[node] = true; curVisit.insert(node); for (auto itr = graph[node].begin(); itr != graph[node].end(); itr++) if (curVisit.find(*itr) != curVisit.end() || dfs_cycle(graph, *itr, curVisit, visited)) return true; curVisit.erase(node); return false; } };
标签:
原文地址:http://www.cnblogs.com/jcliBlogger/p/4604987.html