Course Schedule II (easy to understand topological sorting algorithm)

Source: Internet
Author: User

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, return the ordering of courses you should take to fini SH all courses.

There may is multiple correct orders, you just need to return one of the them. If It is impossible-to-finish all courses, return an empty array.

For example:

2, [[1,0]]

There is a total of 2 courses to take. To take course 1 should has finished course 0. So the correct course order is[0,1]

4, [[1,0],[2,0],[3,1],[3,2]]

There is a total of 4 courses to take. To take course 3 should has finished both courses 1 and 2. Both Courses 1 and 2 should is taken after you finished course 0. So one correct course order is [0,1,2,3] . Another correct ordering is [0,2,1,3] .

The topic and 207 ideas, just the return value from the bool type for an integer array, the idea has not changed, time-consuming 568ms.

Steps:

(1) initialize an array of size numcourses grap;

(2) Save the degree of the node in the graph into the array, the first node in the array of 0 to find out, and the value of the node in the array is set to-1, all the vertices in the array in the degree of the vertex into the degrees minus one, push it into the vector;

(3) Repeat (2) numcourses times, if the period in the Grap array is not found in the degree 0 vertex, then return null;

1vector<int> FindOrder (intNumcourses, vector<pair<int,int>>&Prerequisites)2 {3     intgrap[numcourses]={0};4vector<int>order;5      for(intI=0; I<prerequisites.size (); i++)6grap[prerequisites[i].first]++;7      for(intj=numcourses-1; j>=0;--j)8     {9         intdel=-1;Ten          for(intk=0; k<numcourses;k++) One         { A             if(grap[k]==0) -             { -Del=K; thegrap[k]=-1; - Order.push_back (k); -                  Break; -             } +         } -         if(del==-1) +         { A order.clear (); at             returnorder; -         } -          for(intI=0; I<prerequisites.size (); i++) -         { -             if(prerequisites[i].second==del) -grap[prerequisites[i].first]--; in         } -     } to     returnorder; +}

Course Schedule II (easy to understand topological sorting algorithm)

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.