# Ninja and his Courses

Posted: 25 Mar, 2021

Difficulty: Hard

#### You are going to study at Coding Ninja Institute. There are ‘N’ courses in this institute with some dependencies. You have been given a ‘DEPENDENCIES’ array/list of size ‘M’ where ‘DEPENDENCIES[i]’ is equal to [‘Xi’, ‘Yi’] representing that the course ‘Xi’ must be taken before the course ‘Yi’.

#### In one semester, you can take at most ‘K’ courses. So, you have to find the minimum number of semesters he takes all the courses.

#### Note: It is guaranteed that you can take all the courses in some way.

#### For example:

```
Let‘N’ = 3 , ‘K’ = 1 and ‘DEPENDENCIES’ = [ [1, 2] [2, 3] ].
```

```
In this example, if you want to take ‘COURSE_3’ then first you have to take ‘COURSE_2’ in the previous semester. If you want to take ‘COURSE_2’, then first you have to take ‘COURSE_1’ in the previous semester.
The value of ‘K’ is 1 which means in a semester we can choose at most 1 course.
```

##### Input Format

```
The first line of input contains an integer ‘T’ which denotes the number of test cases or queries to be run. Then the test cases follow.
The first line of each test case contains three single space-separated integers ‘N’,‘M’ and ‘K’ represent the number of courses, number of dependencies and maximum courses that you can take in one semester respectively.
The next ‘N’ line of each test case contains two single space-separated integers ‘X’ and ‘Y’ representing that the course ‘X’ must be taken before the course ‘Y’.
```

##### Output Format :

```
For each test case, print the minimum number of semesters required to take all the courses.
Print the output of each test case in a separate line.
```

#### Note:

```
You do not need to print anything; it has already been taken care of. Just implement the given function.
```

##### Constraints:

```
1 <= ‘T’ <= 10
1 <= ‘N’ <= 15
0 <= ‘M’ <= ‘N’ * (‘N’ - 1)/2
1 <= ‘X’, ‘Y’ and ‘K’ <= ‘N’
Time Limit: 1 second
```

Approach 1

As we know we can only take a course which is not dependent on any other course. So first we make a graph having edges *‘DEPENDENCIES[i]’ *and nodes represent each course*. *Then we find all the nodes whose in-degree is 0 i.e. these nodes don’t depend on any other nodes. If the nodes are less than ‘*K*’ then we take all the nodes and subtract the indegree of all the nodes which are connected through these nodes. Else we are trying to select ‘K’ nodes by all possible ways from the current set of nodes and find our answer.

Here is the algorithm:

- We declare a
*‘DP’*array/list where ‘*DP[i]’*represents the minimum semesters required to take all the courses. - We declare a
*‘GRAPH’*list/vector and we make this graph by*‘DEPENDENCIES’*array/list where*‘DEPENDENCIES[i]’*represents the edge of this*‘GRAPH’*. - We declare a variable
*‘MASK’*in which stores all the courses taken by us till now. - We call our helper function
**‘minSemHepler**(‘MASK’). - Finally, we return the answer which is calculated by our helper function.

**minSemHepler***(‘MASK’)*

- Base case: If all courses are taken:
- Return 0.

- If
*‘DP[‘MASK’]’*!= -1 i.e if we previously calculated the result for these courses:- Return
*‘DP[‘MASK’]’*.

- Return
- We declare an
*‘INDEGREE’*list/vector in which we store that the particular course is dependent on how many other courses. - We run a loop for
*‘i’*= 0 to*‘N’*:- If the current course is already taken:
- Continue.

- We run a loop for ‘j’ = 0 to
*‘GRAPH[i].size()’:**‘INDEGREE[i] ++’.*

- If the current course is already taken:
- We declare a variable
*‘AVIL_MASK’*in which we store which courses are left behind that are not taken by you till now. - We run a loop for ‘i’ = 0 to ‘N’:
- If
*‘INDEGREE*[i] is 0 and ‘*i*’th bit in*‘MASK’*is not set:- Set the ‘’’h bit in
*‘AVIL_MASK’.*

- Set the ‘’’h bit in

- If
- We declare a variable
*‘TEMP_MASK’*initialized with*‘AVIL_MASK’.* - Count the number of set bits in
*‘AVIL_MASK’.* - If the number of bits less than or equal to
*‘K’*:- Take all the courses.

- Else:
- Check all possible combinations of ‘K’ set bits from the
*‘AVIL_MASK’*and calculate the answer.

- Check all possible combinations of ‘K’ set bits from the
- Store the answer in
*‘DP[mask]’*and return*‘DP[mask]’.*

SIMILAR PROBLEMS

# Maximum profit

Posted: 28 Jul, 2021

Difficulty: Moderate

# Subset OR

Posted: 31 Jul, 2021

Difficulty: Moderate

# Favourite Operation

Posted: 7 Oct, 2021

Difficulty: Hard

# Collect Maximum Coins in Matrix

Posted: 29 Oct, 2021

Difficulty: Moderate

# Connecting Ropes

Posted: 12 Nov, 2021

Difficulty: Hard