Skip to content

Instantly share code, notes, and snippets.

@kartikkukreja
Last active August 29, 2015 14:04
Show Gist options
  • Save kartikkukreja/029e79090b7ab8d75e6e to your computer and use it in GitHub Desktop.
Save kartikkukreja/029e79090b7ab8d75e6e to your computer and use it in GitHub Desktop.
Topological Sorting
Let L be an empty list
Let all vertices be initially unmarked
while there are unmarked vertices:
select an unmarked vertex u
dfs(u)
def dfs(vertex u):
mark u
foreach edge u -> v
if v is unmarked:
dfs(v)
add u to head of L
L represents the topological order of the DAG
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment