[Leetcode] Course Schedule, Problem solving report

Source: Internet
Author: User

Topics

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.

Ideas

This topic is a typical topology of the graph of the problem, here first briefly introduce the topology sequencing algorithm implementation steps:

    1. The initialization graph adjacency matrix (U depends on V, then matrix[u][v]==1) and each node's in-degree group (U depends on V, then indegree[v]++).
    2. Select a vertex output without a precursor node (that is, indegree[v]==0) from the into degree group.
    3. Delete the vertex and all arcs with it as the head node. (i.e. all matrix[v][otherv]==1, indegre[otherv]–).
    4. Repeat the 2,3 step until all vertices have been output, or there are no vertices without precursors in the diagram. (There is a loop in the illustration).

So, the problem is to detect whether there is a loop in the last constructed graph (that is, if the Indegree array has a vertex value greater than 0).

AC Code

The AC code is as follows:

 Public  class solution {     Public Boolean Canfinish(intNumcourses,int[] Prerequisites) {BooleanCanfinish =true;int[] Indegree =New int[Numcourses];int[] Matrix =New int[Numcourses] [Numcourses];//Initial data         for(inti =0; i < prerequisites.length; i + +) {if(matrix[prerequisites[i][0]][prerequisites[i][1]] ==1)Continue; indegree[prerequisites[i][1]] ++; matrix[prerequisites[i][0]][prerequisites[i][1]] =1; }//Topological sort         for(inti =0; i < numcourses; i + +) { for(intj =0; J < Numcourses; J + +) {if(Indegree[j] = =0) {Indegree[j]--;//Delete node which start is J                     for(intK =0; K < numcourses; K + +) {if(Matrix[j][k] = =1) {Indegree[k]--; MATRIX[J][K] =0; }                    } Break; }            }        }//Output result         for(inti =0; i < numcourses; i + +) {if(Indegree[i] >0) {Canfinish =false; Break; }        }returnCanfinish; }}

[Leetcode] Course Schedule, Problem solving report

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.