Skip to content

Commit

Permalink
Adds code comments
Browse files Browse the repository at this point in the history
  • Loading branch information
justinmchase committed Jul 11, 2021
1 parent a31735e commit 7f4ca2e
Showing 1 changed file with 25 additions and 19 deletions.
44 changes: 25 additions & 19 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,31 @@ export class GraphError extends Error {
}
}

// Khan's algorithm for topographical sorting
// https://en.wikipedia.org/wiki/Topological_sorting#Kahn's_algorithm
//
// L ← Empty list that will contain the sorted elements
// S ← Set of all nodes with no incoming edges
// while S is non-empty do
// remove a node n from S
// add n to tail of L
// for each node m with an edge e from n to m do
// remove edge e from the graph
// if m has no other incoming edges then
// insert m into S
// if graph has edges then
// return error (graph has at least one cycle)
// else
// return L (a topologically sorted order)
//
// note: 'no incoming edges' means no dependencies on.
// This means that edges is an index of what other targets depend on it.
/**
* Khan's algorithm for topographical sorting
* https://en.wikipedia.org/wiki/Topological_sorting#Kahn's_algorithm
*
* L ← Empty list that will contain the sorted elements
* S ← Set of all nodes with no incoming edges
* while S is non-empty do
* remove a node n from S
* add n to tail of L
* for each node m with an edge e from n to m do
* remove edge e from the graph
* if m has no other incoming edges then
* insert m into S
* if graph has edges then
* return error (graph has at least one cycle)
* else
* return L (a topologically sorted order)
*
* note:
* 'no incoming edges' means no dependencies on.
* This means that edges is an index of what other targets depend on it.
*
* @param {ITarget[]} targets
* @returns {ITarget[]} The targets sorted
*/
export function topographicalSort(targets: ITarget[]): ITarget[] {
const index: Record<string, ITarget> = {};
const edges: Record<string, string[]> = {};
Expand Down

0 comments on commit 7f4ca2e

Please sign in to comment.