- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose there are N courses, and these are labelled from 1 to N. We also gave a relation array, where relations[i] = [X, Y], is representing a prerequisite relationship between course X and course Y. So, this means course X has to be studied before course Y.

In one semester we can study any number of courses as long as we have studied all the prerequisites for the course we are studying. We have to find the minimum number of semesters needed to study all courses. And if there is no way to study all the courses, then return -1.

So, if the input is like N = 3, relations = [[1,3],[2,3]], then the output will be 2 as In the first semester, courses 1 and 2 are studied. In the second semester, course 3 is studied.

To solve this, we will follow these steps −

courses := n

visited := an array of size n+1, and fill this with false

queue := a new list

graph := a list of n+1 sublists

in_degree := an array of size n+1, and fill this with 0

for each i in relations, do

insert i[1] at the end of graph[i[0]]

in_degree[i[1]] := in_degree[i[1]] + 1

semeseter := 1

for i in range 1 to n+1, do

if in_degree[i] is zero, then

insert i at the end of queue

visited[i] := True

semester := 1

courses := courses - size of queue

while queue is not empty and courses is non-zero, do

current_size := size of queue

while current_size is non-zero, do

current_course := queue[0]

delete first element from queue

current_size := current_size - 1

for each i in graph[current_course], do

in_degree[i] := in_degree[i] - 1

if i is not visited and in_degree[i] is zero, then

courses := courses - 1

insert i at the end of queue

visited[i]:= True

semester := semester + 1

return semester if courses is 0 otherwise -1

Let us see the following implementation to get better understanding −

class Solution(object): def minimumSemesters(self, n, relations): courses = n visited = [False for i in range(n+1)] queue = [] graph = [[] for i in range(n+1)] in_degree = [0 for i in range(n+1)] for i in relations: graph[i[0]].append(i[1]) in_degree[i[1]]+=1 semeseter = 1 for i in range(1,n+1): if not in_degree[i]: queue.append(i) visited[i] = True semester = 1 courses -= len(queue) while queue and courses: current_size = len(queue) while current_size: current_course = queue[0] queue.pop(0) current_size-=1 for i in graph[current_course]: in_degree[i]-=1 if not visited[i] and not in_degree[i]: courses-=1 queue.append(i) visited[i]=True semester+=1 return semester if not courses else -1 ob = Solution() print(ob.minimumSemesters(3,[[1,3],[2,3]]))

3, [[1,3],[2,3]]

-1

- Related Questions & Answers
- Launching parallel tasks in Python
- Resistors in Parallel
- Program to count minimum semesters to cover all different courses in Python
- Drawing multiple figures in parallel in Python with Matplotlib
- Program to check whether we can take all courses or not in Python
- Why is Shell Scripting not included in the courses?
- What are the most promising technical courses in India?
- What are the different IT Career courses in India?
- DoubleStream parallel() method in Java
- IntStream parallel() method in Java
- LongStream parallel() method in Java
- Parallel Data Processing in Java
- Plot parallel coordinates in Matplotlib
- How can I iterate through two lists in parallel in Python?
- Android AsyncTasks Parallel Execution

Advertisements