[LeetCode] Course Schedule

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.

 1 // BFS
 2 class Solution {
 3 public:
 4     bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) {
 5         vector<unordered_set<int> > graph = make_graph(numCourses, prerequisites);
 6         vector<int> degrees = compute_indegree(graph);
 7         for (int i = 0; i < numCourses; i++) {
 8             int j = 0;
 9             for (; j < numCourses; j++)
10                 if (!degrees[j]) break;
11             if (j == numCourses) return false;
12             degrees[j] = -1;
13             for (auto itr = graph[j].begin(); itr != graph[j].end(); itr++)
14                 degrees[*itr]--;
15         }
16         return true;
17     }
18 private:
19     vector<unordered_set<int> > make_graph(int numCourses, vector<pair<int, int> >& prerequisites) {
20         vector<unordered_set<int> > graph(numCourses);
21         for (auto pre : prerequisites)
22             graph[pre.second].insert(pre.first);
23         return graph;
24     }
25     vector<int> compute_indegree(vector<unordered_set<int> >& graph) {
26         vector<int> degrees(graph.size(), 0);
27         for (auto neighbors : graph)
28             for (auto neigh : neighbors)
29                 degrees[neigh]++;
30         return degrees;
31     }
32 };

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.

 1 // DFS
 2 class Solution {
 3 public:
 4     bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) {
 5         vector<unordered_set<int> > graph = make_graph(numCourses, prerequisites);
 6         unordered_set<int> curVisit;
 7         vector<bool> visited(numCourses, false);
 8         for (int i = 0; i < numCourses; i++)
 9             if (!visited[i] && dfs_cycle(graph, i, curVisit, visited))
10                 return false;
11         return true;
12     }
13 private:
14     vector<unordered_set<int> > make_graph(int numCourses, vector<pair<int, int> >& prerequisites) {
15         vector<unordered_set<int> > graph(numCourses);
16         for (auto pre : prerequisites)
17             graph[pre.second].insert(pre.first);
18         return graph;
19     }
20     bool dfs_cycle(vector<unordered_set<int> >& graph, int node, unordered_set<int>& curVisit, vector<bool>& visited) {
21         if (visited[node]) return false;
22         visited[node] = true;
23         curVisit.insert(node);
24         for (auto itr = graph[node].begin(); itr != graph[node].end(); itr++)
25             if (curVisit.find(*itr) != curVisit.end() || dfs_cycle(graph, *itr, curVisit, visited))
26                 return true;
27         curVisit.erase(node);
28         return false;
29     }
30 };

 

你可能感兴趣的:(LeetCode)