Answer:
The idea is to use the concept of topological sorting.
Complexity of this algorithm is equal to complexity of DFS algorithm, that is, O(m+n).
Step-by-step explanation:
The idea is to use the concept of topological sorting.
A topological sort also called a topological ordering of a directed graph is a linear ordering of its vertices in a way that for every directed edge uv from vertex u to vertex v, u comes before v in the ordering. For instance, the vertices of the graph may represent tasks to be performed, and the edges may represent constraints that one task must be performed before another; in this application, a topological ordering is just a valid sequence for the tasks.
A topological sorting is possible if and only if the graph has no directed cycles, that is, if it is a directed acyclic graph (DAG).
For every edge (U - - >V), create a directed edge from U to V.
While creating an edge, if an edge creates a cycle, then it is not possible.
n= number of awardees
m= the number of constraint
Let n represent the vertices of a graph and constraints m is representing edges of a graph.
and graph is represented in adjacent of O(m+n)
Constraint i wants to receive award before j
Graphical representation - - - - - > (i) - - - - - - > (j) j depends on i
1. Presently run topological olding on graph G(n, m) where n is vertices and m is edges.
2. Run DFs on Graph, a temporary stack
3. Firstly call topological sorting for all its adjacent vertices and then push it to a stack. Finally, print content of stack.
4. Vertex is push to stack only when all adjacent vertices are already in stack.
Complexity of this algorithm is equal to complexity of DFS algorithm, that is, O(m+n).