标签:
Well, this problem is spiritually the same to Course Schedule. You only need to store the node in the order you visit into a vector (well, for DFS, a final reversal may be required) during BFS or DFS.
Make sure you try this problem only after you have known how to solve Course Schedule. In fact, the code to this problem only differs to that of the first problem as descriped in the first paragraph. I have a passage about Course Schedule in this link. You may refer to it if you like.
Both BFS and DFS can be used to solve this problem using the idea of topological sort. In the third hint following the problem statement, there is a nice link to the wikipedia passage about topological sort, which includes nicely-written pseudo code for toposort in both BFS and DFS. Strong recommendations to take a look at it. It is really well explained!
Now comes the BFS code.
1 // BFS 2 class Solution { 3 public: 4 vector<int> findOrder(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 queue<int> zeros; 8 for (int i = 0; i < degrees.size(); i++) { 9 if (!degrees[i]) { 10 degrees[i]--; 11 zeros.push(i); 12 } 13 } 14 vector<int> toposort; 15 while (!zeros.empty()) { 16 int zero = zeros.front(); 17 zeros.pop(); 18 toposort.push_back(zero); 19 for (int neigh : graph[zero]) { 20 if (--degrees[neigh] == 0) 21 zeros.push(neigh); 22 } 23 numCourses--; 24 } 25 if (numCourses) toposort.clear(); 26 return toposort; 27 } 28 private: 29 vector<unordered_set<int> > make_graph(int numCourses, vector<pair<int, int> >& prerequisites) { 30 vector<unordered_set<int> > graph(numCourses); 31 for (auto pre : prerequisites) 32 graph[pre.second].insert(pre.first); 33 return graph; 34 } 35 vector<int> compute_indegree(vector<unordered_set<int> >& graph) { 36 vector<int> degrees(graph.size(), 0); 37 for (auto neighbors : graph) 38 for (auto neigh : neighbors) 39 degrees[neigh]++; 40 return degrees; 41 } 42 };
Then the DFS code.
1 // DFS 2 class Solution { 3 public: 4 vector<int> findOrder(int numCourses, vector<pair<int, int>>& prerequisites) { 5 vector<unordered_set<int> > graph = make_graph(numCourses, prerequisites); 6 vector<int> toposort; 7 unordered_set<int> curVisit; 8 vector<bool> visited(numCourses, false); 9 for (int i = 0; i < (int)graph.size(); i++) { 10 if (!visited[i] && dfs(graph, i, curVisit, visited, toposort)) { 11 toposort.clear(); 12 return toposort; 13 } 14 } 15 reverse(toposort.begin(), toposort.end()); 16 return toposort; 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 bool dfs(vector<unordered_set<int> >& graph, int node, unordered_set<int>& curVisit, vector<bool>& visited, vector<int>& toposort) { 26 if (visited[node]) return false; 27 visited[node] = true; 28 curVisit.insert(node); 29 for (auto itr = graph[node].begin(); itr != graph[node].end(); itr++) 30 if (curVisit.find(*itr) != curVisit.end() || dfs(graph, *itr, curVisit, visited, toposort)) 31 return true; 32 curVisit.erase(node); 33 toposort.push_back(node); 34 return false; 35 } 36 };
标签:
原文地址:http://www.cnblogs.com/jcliBlogger/p/4605039.html