toposort

Kahn's Algorithm

How do you find the order to run a set of tasks that have dependencies?

Start by identifying all the nodes that have no dependencies (incoming edges), and add those nodes to the candidates stack.

Fig 1. Initializing Candidates to Process

For every candidate, remove each of the candidate's outgoing edges from the graph, then add the candidate to the solution. When you remove a candidate's edge, if the child node the edge is connected to has no incoming edges, then add it to the candidates stack.

Fig 2. Process Candidates Queue

For every candidate, remove each of the candidate's outgoing edges from the graph, then add the candidate to the solution. When you remove a candidate's edge, if the child node the edge is connected to has no incoming edges, then add it to the candidates stack.

For every candidate, remove each of the candidate's outgoing edges from the graph, then add the candidate to the solution. When you remove a candidate's edge, if the child node the edge is connected to has no incoming edges, then add it to the candidates stack.

Detecting cycles

For every candidate, remove each of the candidate's outgoing edges from the graph, then add the candidate to the solution. When you remove a candidate's edge, if the child node the edge is connected to has no incoming edges, then add it to the candidates stack.

Next
v1.7