If it is impossible to finish all courses, return an empty array. If there are multiple answers, return any one.
The first line of input contains an integer 'T' representing the number of the test case.
The first line of each test case contains an integer ‘N’ representing the number of courses.
The second line of each test case contains a given integer ‘M’ representing the number of prerequisite pairs.
The next ‘M’ lines in each test case contain a matrix ‘PREREQUISITES’ containing two integers denoting a prerequisite pair.
For each test case, print a single integer 1 if the returned order of the courses is correct otherwise we return 0.
You don't need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 5
2 <= N <= 100
1 <= M <= min(100,N(N-1)/2)
1 <= PREREQUISITES[i][0], PREREQUISITES[i][1] <= N
Where ‘PREREQUISITES’ denotes the prerequisites matrix.
Time limit: 1 sec
Our current algorithm is based on the idea of the BFS approach. We first process all the courses with 0 in-degree implying no prerequisite courses required. If we remove all these courses from the graph, along with their outgoing edges, we can find out the courses/nodes that should be processed next. These would again be the nodes with 0 in-degree. We can continuously do this until all the courses have been accounted for.
The steps are as follows:
We need to get all the courses that have a particular course as a prerequisite. If a valid ordering of courses is possible, the course ‘A’ would come before all the other set of courses that have it as a prerequisite. This idea for solving the problem can be explored using a depth-first search.
The steps are as follows:
COUNT ISLANDS
Capturing Grid
The Summit
Rotting Oranges
Distance to a Cycle in Undirected Graph