1 Course Selection Questions
Leetcode has such a problem: There is a code 0,1,2......n-1 of the N-gate course. Some of these courses require additional courses as prerequisites. Use a pair to represent these conditions: [1,0],[1,2], indicating that if you want to choose course 1, you must first take course 0 and course 2. Ask if it is possible to take these n courses all over again.
The problem seems rather complicated. The difficulty is that a course may require multiple courses as a precondition, so it is difficult to find a way to traverse all the courses without repeating it.
The stupid way to do this is to first identify those courses that do not require prerequisites, and then start by them to complete the courses that only require them as prerequisites. And then the cycle repeats. Until after a certain traversal, no new courses have been built. Finally, check to see if there are any courses that have not been repaired enough. In this case, at worst, we are at the worst of time complexity to reach O (n^3). Using Unordered_multimap as the Auxiliary tool, the code is accepted. But the running time is 10 times times the best coder. So, study the algorithm of others carefully.
2 BFS
In fact. This problem is difficult to use one-dimensional data structure to express these prerequisites, it is easy to increase the complexity of time. So, consider using a two-dimensional data structure to represent.
Figure! The conditions given in the question are obviously one-to-many. Then, it is suitable to be represented by adjacency linked list method. It is represented by the data structure vector<unordered_set<int>> map. Each element of the vector represents a set of courses that are subject to a course as a precondition. Prerequisites for each course you can use a set of vectors to represent the vector<int> de. We call this value in degrees, that is, in the figure, how many paths are entered into the node. For courses that do not require preconditions, the degree of entry is 0.
BFs method, breadth first. We first find a class with a degree of 0, and then the degree to which it is a prerequisite can be reduced by 1, because the course is learned, and it is not required. And the course that presupposes it is the adjacency node of the node. In this way, every time we learn a course, N times we can learn all the lessons, if one time did not learn the new curriculum, then eventually certainly cannot finish.
On the code:
classSolution { Public: BOOLCanfinish (intN, vector<pair<int,int>>&pre) {Vector<unordered_set<int> >GRA (n); Vector<int> De (N,0); //Make graph for(Auto A:pre) {Gra[a.second].insert (A.first); } //Calculate the Indegree for(intI=0; i<n;++i) { for(Auto A:gra[i]) De[a]++; } for(intI=0; i<n;++i) {intj=0; for(; j<n;++j) {if(de[j]==0) Break; } if(j==n)return 0;//This time could not learn a new lesson and we won ' t make itde[j]=-1;//In case next time we'll learn this lesson again for(Auto a:gra[j]) De[a]--;//Any lesson take this lesson as it ' s prerequisites, it's indegree decrease 1 } return 1; }};3 DFS
So what's the idea of Dfs (depth-first search)? In fact, if there is a ring in this diagram, then certainly cannot complete the task. So under what circumstances will it be impossible to finish the task? That is to say 1->2,2->3,3->1, that is to say, form a ring. So we turn the original question into whether there is a ring in the test diagram. How to detect rings? From a node, if it goes back to that node, then this graph has a ring!
We start from any node in turn, traversing all the nodes that the node can reach, and we use a vector vector<bool> OnePath to record the nodes that go from that node. To prevent duplication, we use the variable vector<bool> path to record all traversed nodes.
On the code:
classSolution { Public: BOOLCanfinish (intN, vector<pair<int,int>>&pre) {Vector<unordered_set<int> >GRA (n); Vector<BOOL> Path (n,0), OnePath (N,0); //Make graph for(Auto A:pre) {Gra[a.second].insert (A.first); } for(intI=0; i<n;++i) {if(Path[i])Continue; if(Dfs_circle (gra,i,path,onepath))return 0; } return 1; } BOOLDfs_circle (vector<unordered_set<int> >& GRA,intnode,vector<BOOL>& path,vector<BOOL>&OnePath) { if(onepath[node]==1)return true; Path[node]=onepath[node]=1; for(Auto A:gra[node])if(Dfs_circle (gra,a,path,onepath))return 1; Onepath[node]=0; return 0; }};
The solution to this transformation problem is really ingenious! Taizan!
In addition, for many conditions have cross-topic, consider using graph theory method to solve, especially DFS and BFS algorithm.
BFS, DFS and course selection issues