# Find the ordering of tasks from given dependencies

• Difficulty Level : Hard
• Last Updated : 02 Jun, 2022

There are a total of n tasks you have to pick, labeled from 0 to n-1. Some tasks may have prerequisites tasks, for example to pick task 0 you have to first finish tasks 1, which is expressed as a pair: [0, 1] Given the total number of tasks and a list of prerequisite pairs, return the ordering of tasks you should pick to finish all tasks. There may be multiple correct orders, you just need to return one of them. If it is impossible to finish all tasks, return an empty array. Examples:

Input: 2, [[1, 0]] Output: [0, 1] Explanation: There are a total of 2 tasks to pick. To pick task 1 you should have finished task 0. So the correct task order is [0, 1] . Input: 4, [[1, 0], [2, 0], [3, 1], [3, 2]] Output: [0, 1, 2, 3] or [0, 2, 1, 3] Explanation: There are a total of 4 tasks to pick. To pick task 3 you should have finished both tasks 1 and 2. Both tasks 1 and 2 should be pick after you finished task 0. So one correct task order is [0, 1, 2, 3]. Another correct ordering is [0, 2, 1, 3].

## CPP

 `// CPP program to find order to process tasks` `// so that all tasks can be finished. This program` `// mainly uses Kahn's algorithm.` `#include ` `using` `namespace` `std;`   `// Returns adjacency list representation of graph from` `// given set of pairs.` `vector > make_graph(``int` `numTasks,` `             ``vector >& prerequisites)` `{` `    ``vector > graph(numTasks);` `    ``for` `(``auto` `pre : prerequisites)` `        ``graph[pre.second].insert(pre.first);` `    ``return` `graph;` `}`   `// Computes in-degree of every vertex` `vector<``int``> compute_indegree(vector >& graph)` `{` `    ``vector<``int``> degrees(graph.size(), 0);` `    ``for` `(``auto` `neighbors : graph)` `        ``for` `(``int` `neigh : neighbors)` `            ``degrees[neigh]++;` `    ``return` `degrees;` `}`   `// main function for topological sorting` `vector<``int``> findOrder(``int` `numTasks,` `        ``vector >& prerequisites)` `{` `    ``// Create an adjacency list` `    ``vector > graph =` `            ``make_graph(numTasks, prerequisites);`   `    ``// Find vertices of zero degree` `    ``vector<``int``> degrees = compute_indegree(graph);` `    ``queue<``int``> zeros;` `    ``for` `(``int` `i = 0; i < numTasks; i++)` `        ``if` `(!degrees[i])` `            ``zeros.push(i);`   `    ``// Find vertices in topological order` `    ``// starting with vertices of 0 degree` `    ``// and reducing degrees of adjacent.` `    ``vector<``int``> toposort;` `    ``for` `(``int` `i = 0; i < numTasks; i++) {` `        ``if` `(zeros.empty())` `            ``return` `{};` `        ``int` `zero = zeros.front();` `        ``zeros.pop();` `        ``toposort.push_back(zero);` `        ``for` `(``int` `neigh : graph[zero]) {` `            ``if` `(!--degrees[neigh])` `                ``zeros.push(neigh);` `        ``}` `    ``}` `    ``return` `toposort;` `}`   `// Driver code` `int` `main()` `{` `    ``int` `numTasks = 4;` `    ``vector > prerequisites;`   `    ``// for prerequisites: [[1, 0], [2, 1], [3, 2]]`   `    ``prerequisites.push_back(make_pair(1, 0));` `    ``prerequisites.push_back(make_pair(2, 1));` `    ``prerequisites.push_back(make_pair(3, 2));` `    ``vector<``int``> v = findOrder(numTasks, prerequisites);`   `    ``for` `(``int` `i = 0; i < v.size(); i++) {` `        ``cout << v[i] << " ";` `    ``}`   `    ``return` `0;` `}`

## Java

 `// Java program to find order to process tasks` `// so that all tasks can be finished. This program` `// mainly uses Kahn's algorithm.`   `import` `java.util.ArrayList;` `import` `java.util.HashSet;` `import` `java.util.LinkedList;` `import` `java.util.Queue;`   `public` `class` `Dep {`   `    ``// Returns adjacency list representation of graph from` `    ``// given set of pairs.` `    ``static` `ArrayList >` `    ``make_graph(``int` `numTasks, ``int``[][] prerequisites)` `    ``{` `        ``ArrayList > graph` `            ``= ``new` `ArrayList>(numTasks);` `        ``for` `(``int` `i = ``0``; i < numTasks; i++)` `            ``graph.add(``new` `HashSet());` `        ``for` `(``int``[] pre : prerequisites)` `            ``graph.get(pre[``1``]).add(pre[``0``]);` `        ``return` `graph;` `    ``}`   `    ``// Computes in-degree of every vertex` `    ``static` `int``[] compute_indegree(` `        ``ArrayList > graph)` `    ``{` `        ``int``[] degrees = ``new` `int``[graph.size()];` `        ``for` `(HashSet neighbors : graph)` `            ``for` `(``int` `neigh : neighbors)` `                ``degrees[neigh]++;` `        ``return` `degrees;` `    ``}`   `    ``// main function for topological sorting` `    ``static` `ArrayList` `    ``findOrder(``int` `numTasks, ``int``[][] prerequisites)` `    ``{` `        ``// Create an adjacency list` `        ``ArrayList > graph` `            ``= make_graph(numTasks, prerequisites);`   `        ``// Find vertices of zero degree` `        ``int``[] degrees = compute_indegree(graph);` `        ``Queue zeros = ``new` `LinkedList();` `        ``for` `(``int` `i = ``0``; i < numTasks; i++)` `            ``if` `(degrees[i] == ``0``)` `                ``zeros.add(i);`   `        ``// Find vertices in topological order` `        ``// starting with vertices of 0 degree` `        ``// and reducing degrees of adjacent.` `        ``ArrayList toposort` `            ``= ``new` `ArrayList();` `        ``for` `(``int` `i = ``0``; i < numTasks; i++) {` `            ``if` `(zeros.isEmpty())` `                ``return` `new` `ArrayList();` `            ``int` `zero = zeros.peek();` `            ``zeros.poll();` `            ``toposort.add(zero);` `            ``for` `(``int` `neigh : graph.get(zero)) {` `                ``if` `(--degrees[neigh] == ``0``)` `                    ``zeros.add(neigh);` `            ``}` `        ``}` `        ``return` `toposort;` `    ``}`   `    ``// Driver code` `    ``public` `static` `void` `main(String[] args)` `    ``{` `        ``int` `numTasks = ``4``;` `        ``int``[][] prerequisites` `            ``= { { ``1``, ``0` `}, { ``2``, ``1` `}, { ``3``, ``2` `} };`   `        ``ArrayList v` `            ``= findOrder(numTasks, prerequisites);`   `        ``for` `(``int` `i = ``0``; i < v.size(); i++) {` `            ``System.out.print(v.get(i) + ``" "``);` `        ``}` `    ``}` `}`   `// This code is contributed by Lovely Jain`

Output:

`0 1 2 3`

Topological Sorting using DFS: In this implementation, we use DFS based algorithm for Topological Sort

## CPP

 `// CPP program to find Topological sorting using ` `// DFS ` `#include ` `using` `namespace` `std; ` `  `  `// Returns adjacency list representation of graph from ` `// given set of pairs. ` `vector > make_graph(``int` `numTasks, ` `             ``vector >& prerequisites) ` `{ ` `    ``vector > graph(numTasks); ` `    ``for` `(``auto` `pre : prerequisites) ` `        ``graph[pre.second].insert(pre.first); ` `    ``return` `graph; ` `} ` `  `  `// Does DFS and adds nodes to Topological Sort ` `bool` `dfs(vector >& graph, ``int` `node,  ` `           ``vector<``bool``>& onpath, vector<``bool``>& visited,  ` `                                ``vector<``int``>& toposort) ` `{ ` `    ``if` `(visited[node]) ` `        ``return` `false``; ` `    ``onpath[node] = visited[node] = ``true``; ` `    ``for` `(``int` `neigh : graph[node]) ` `        ``if` `(onpath[neigh] || dfs(graph, neigh, onpath, visited, toposort)) ` `            ``return` `true``; ` `    ``toposort.push_back(node); ` `    ``return` `onpath[node] = ``false``; ` `} ` `  `  `// Returns an order of tasks so that all tasks can be ` `// finished. ` `vector<``int``> findOrder(``int` `numTasks, vector >& prerequisites) ` `{ ` `    ``vector > graph = make_graph(numTasks, prerequisites); ` `    ``vector<``int``> toposort; ` `    ``vector<``bool``> onpath(numTasks, ``false``), visited(numTasks, ``false``); ` `    ``for` `(``int` `i = 0; i < numTasks; i++) ` `        ``if` `(!visited[i] && dfs(graph, i, onpath, visited, toposort)) ` `            ``return` `{}; ` `    ``reverse(toposort.begin(), toposort.end()); ` `    ``return` `toposort; ` `} ` `  `  `int` `main() ` `{ ` `    ``int` `numTasks = 4; ` `    ``vector > prerequisites; ` `  `  `    ``// for prerequisites: [[1, 0], [2, 1], [3, 2]] ` `    ``prerequisites.push_back(make_pair(1, 0)); ` `    ``prerequisites.push_back(make_pair(2, 1)); ` `    ``prerequisites.push_back(make_pair(3, 2)); ` `    ``vector<``int``> v = findOrder(numTasks, prerequisites); ` `  `  `    ``for` `(``int` `i = 0; i < v.size(); i++) { ` `        ``cout << v[i] << ``" "``; ` `    ``} ` `  `  `    ``return` `0; ` `}`

## Java

 `// Java program to find Topological sorting using` `// DFS`   `import` `java.util.ArrayList;` `import` `java.util.Collections;` `import` `java.util.HashSet;`   `public` `class` `Dfs1 {`   `    ``// Returns adjacency list representation of graph from` `    ``// given set of pairs.` `    ``static` `ArrayList >` `    ``make_graph(``int` `numTasks, ``int``[][] prerequisites)` `    ``{` `        ``ArrayList > graph` `            ``= ``new` `ArrayList(numTasks);` `        ``for` `(``int` `i = ``0``; i < numTasks; i++)` `            ``graph.add(``new` `HashSet());` `        ``for` `(``int``[] pre : prerequisites)` `            ``graph.get(pre[``1``]).add(pre[``0``]);` `        ``return` `graph;` `    ``}`   `    ``// Does DFS and adds nodes to Topological Sort` `    ``static` `boolean` `dfs(ArrayList > graph,` `                       ``int` `node, ``boolean``[] onpath,` `                       ``boolean``[] visited,` `                       ``ArrayList toposort)` `    ``{` `        ``if` `(visited[node])` `            ``return` `false``;` `        ``onpath[node] = visited[node] = ``true``;` `        ``for` `(``int` `neigh : graph.get(node))` `            ``if` `(onpath[neigh]` `                ``|| dfs(graph, neigh, onpath, visited,` `                       ``toposort))` `                ``return` `true``;` `        ``toposort.add(node);` `        ``return` `onpath[node] = ``false``;` `    ``}`   `    ``// Returns an order of tasks so that all tasks can be` `    ``// finished.` `    ``static` `ArrayList` `    ``findOrder(``int` `numTasks, ``int``[][] prerequisites)` `    ``{` `        ``ArrayList > graph` `            ``= make_graph(numTasks, prerequisites);` `        ``ArrayList toposort` `            ``= ``new` `ArrayList();` `        ``boolean``[] onpath = ``new` `boolean``[numTasks];` `        ``boolean``[] visited = ``new` `boolean``[numTasks];` `        ``for` `(``int` `i = ``0``; i < numTasks; i++)` `            ``if` `(!visited[i]` `                ``&& dfs(graph, i, onpath, visited, toposort))` `                ``return` `new` `ArrayList();` `        ``Collections.reverse(toposort);` `        ``return` `toposort;` `    ``}`   `    ``// Driver code` `    ``public` `static` `void` `main(String[] args)` `    ``{` `        ``int` `numTasks = ``4``;` `        ``int``[][] prerequisites` `            ``= { { ``1``, ``0` `}, { ``2``, ``1` `}, { ``3``, ``2` `} };`   `        ``ArrayList v` `            ``= findOrder(numTasks, prerequisites);`   `        ``for` `(``int` `i = ``0``; i < v.size(); i++) {` `            ``System.out.print(v.get(i) + ``" "``);` `        ``}` `    ``}` `}`   `// This code is contributed by Lovely Jain`

Output:

`0 1 2 3`

Reference: https://leetcode.com/problems/course-schedule-ii/

My Personal Notes arrow_drop_up
Recommended Articles
Page :