Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding
Ninjas X Naukri.com

Last Updated: 21 Dec, 2020

Moderate

```
The first line of the input contains an integer ‘T’ denoting the number of test cases.
The first line of each test case contains two space-separated integers ‘N’ and ‘M’ denoting the number of the tasks and dependencies respectively.
The next ‘M’ lines of each test case contain two space-separated positive integers in the form of ( U, V ) denoting that to perform a task ‘U’ you first need to finish the task ‘V’.
```

```
The only line of output of each test case should contain “Yes” (without quotes) if it is possible to finish all the tasks else “No” (without quotes).
```

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

```
1 <= T <= 100
1 <= N, M <= 10^4
1 <= U, V <= N
Time Limit: 1 sec
```

- We can transform the given problem into a directed graph. We will consider tasks as nodes and dependency as edges so for building the graph we add a directed edge from U -> V, for every pair of vertices given in the dependency list.
- Now if we carefully observe, then we can notice that all the tasks can only be finished only when there is no
**directed cycle**in the graph. Why? Because if there is a cycle present in the graph that means we have tasks waiting to be performed circularly and we can not perform any of these tasks due to their dependencies on the next task present in the cycle.- So for detecting the cycle in the directed graph, we will perform the following steps:

We will use two boolean arrays named as VISITED[] and IN_STACK[]. The VISITED[i] will be true if the ith node is visited and IN_STACK[i] will be true when the ith node is visited but not yet explored and present in our calling stack. - We will perform the DFS and mark the node visited as soon as we reach it.
- Now we will traverse on its adjacent node if any node has IN_STACK[NODE] = true that means this node is currently not explored whole and is present in our stack, this is the case when we can say that there is a cycle present in our graph.

- So for detecting the cycle in the directed graph, we will perform the following steps:
- If we detect the cycle, then we return false else return true.

- The problem is also known as the
**topological sort**problem, which is to find a global order for all nodes in a**DAG**(Directed Acyclic Graph) with respect to their dependencies. - We can transform the given problem into a directed graph as described above. We will also create an INDEGREE[] array to store in-degree of each node, whenever we are adding a directed edge from U -> V, we will increase the value of INDEGREE[V]+=1.
- Now we perform the topological sorting on the graph using BFS. If we are successfully able to top sort our graph, that means there is no cycle in the graph, and we can finish each task.
- So for topological sorting in the directed graph, we will perform the following steps:
- We first start to take the independent tasks (no prerequisites or 0 INDEGREE).
- After taking the courses, we remove it and update the INDEGREE of its dependent tasks ( INDEGREE[V]-=1 ).
- Have a COUNT variable to keep track of taken independent tasks (0 INDEGREE ).
- Repeat the process until-
- If there is a cycle in the graph, which means there are still tasks left to be finished and no tasks have 0 INDEGREE remaining, i.e., COUNT != N.
- Else we finish all courses (COUNT == N), and we return true.

- Pictorial example of the approach