Skip to content
CampusEduX

Topological Sort

Mediumjavapythonccppjavascript

Given a directed acyclic graph, return the lexicographically smallest topological ordering of its vertices — an ordering in which, for every directed edge u -> v, u comes before v, and among all such orderings the one that is smallest when compared position by position.

Input Format

The first line contains two integers V and E. Each of the next E lines contains a directed edge u v.

Output Format

Print the lexicographically smallest topological ordering, vertices separated by single spaces.

Related Problems

  • Detect Cycle in Undirected Graph
    Medium · java, python, c, cpp, javascript
    Solve Problem
  • Detect Cycle in Directed Graph
    Medium · java, python, c, cpp, javascript
    Solve Problem
  • Shortest Path in Unweighted Graph
    Medium · java, python, c, cpp, javascript
    Solve Problem

Input (stdin)

Output

Run your code to see output here...