[Leetcode] Course Schedule Course List

Source: Internet
Author: User
Tags in degrees

There is a total of n courses you have to take, labeled from 0 to n - 1 .

Some courses May has prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a Pair[0,1]

Given the total number of courses and a list of prerequisite pairs, are it possible for your to finish all courses?

For example:

2, [[1,0]]

There is a total of 2 courses to take. To take course 1 should has finished course 0. So it is possible.

2, [[1,0],[0,1]]

There is a total of 2 courses to take. To take course 1 should has finished course 0, and to take course 0 you should also has finished course 1. So it is impossible.

Click to show more hints.

Hints:
    1. This problem was equivalent to finding if a cycle exists in a directed graph. If a cycle exists, no topological ordering exists and therefore it'll be impossible to take all courses.
    2. There is several ways to represent a graph. For example, the input prerequisites are a graph represented by a list of edges. Is this graph representation appropriate?
    3. Topological Sort via dfs-a great video tutorial (minutes) on Coursera explaining the basic concepts of topological so Rt.
    4. Topological sort could also is done via BFS.

The problem of this list of courses for our students should not be unfamiliar, because we often encountered in the course of choosing a course to choose, find out which courses must be taken before selecting it, this problem gives a lot of hints, the first one tells us that the essence of this problem is in the direction of the map detection ring. There are few questions about graphs in leetcode, there is only this one to the graph, and a question about the non-direction graph is the copy of the clone graph without graph. I think the figure of this data structure compared to the number Ah, linked list ah what is more complicated, especially the map, very troublesome. The second tip is about how to represent a forward graph, which can be represented by edges, which consists of two endpoints, with two points representing the edges. The third fourth tip reveals that there are two solutions to this problem, both DFS and BFS can solve the problem. We first look at the solution of BFS, we define a two-dimensional array graph to represent this graph, an array in to represent the degrees of each vertex. We start by starting with the input to build this graph, and also to initialize the group into the degree of good. Then we define a queue variable, put all the points in the 0 into the queue, and then start to traverse the queue, from graph to traverse its connected points, each to a new node, its in degrees minus one, if at this point in the degree of 0, then put into the end of the queue. Until all the values in the queue are traversed, if there are no more than 0 of the nodes at this point, the ring exists, returns false, and vice versa returns True. The code is as follows:

Solution One

classSolution { Public:    BOOLCanfinish (intNumcourses, vector<vector<int>>&Prerequisites) {Vector<vector<int> > Graph (numcourses, vector<int> (0)); Vector<int>inch(Numcourses,0);  for(Auto a:prerequisites) {graph[a[1]].push_back (a[0]); ++inch[a[0]]; } Queue<int>Q;  for(inti =0; i < numcourses; ++i) {if(inch[I] = =0) Q.push (i); }         while(!Q.empty ()) {            intt =Q.front ();            Q.pop ();  for(Auto a:graph[t]) {--inch[A]; if(inch[A] = =0) Q.push (a); }        }         for(inti =0; i < numcourses; ++i) {if(inch[i]! =0)return false; }        return true; }};

Resources:

Http://www.cnblogs.com/easonliu/p/4483437.html

Https://leetcode.com/discuss/34741/python-20-lines-dfs-solution-sharing-with-explanation

[Leetcode] Course Schedule Course List

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.