Parallel Courses in Python


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 −

Example

 Live Demo

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]]))

Input

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

Output

-1

Updated on: 11-Jul-2020

145 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements