topo-kahn
Topological sort (Kahn algorithm) an oriented graph containing any kind of node, using ES6 Maps & Sets.
Examples
let's start with some family members :
; const g = name: "George" ;const mt = name: "Marie-Thérèse" ;const p = name: "Patrice" ;const j = name: "Josette" ;const pj = name: "Pierre-Jean" ;const m = name: "Mathias" ;const pandj = p j; ...
Static style, by passing a Map that represents dependency matrix
... const parents = m pandj g null pj pandj mt null p g mt j null; const sorted = ; // sorted = new Set([g, mt, j, p, pj, m]);
Functional style, by passing a Set that represents family members and a generator function
... const family = m g pj mt p j; const getParents = { }; const sorted = ; // same result !
You can even pass a reversed matrix / generator and work the other way around !
// reverseMatrix is a util function provided by this packageconst sorted = ; // same result ! // or const sorted = ; // same result !
FAQ
- Why should I use it ?
- If you like this way to express a graph
- Or if you already have a function that defines dependencies between nodes
- If you want / need to define any kind of node (objects, strings, numbers, ...) by using ES6 Maps & Sets
- What if there's a loop in my graph ?
topo-kahn
will throw an error - What if I have a matrix containing node's children, and not parents ? Just pass a
type
option set tochildren
; const sorted = ;// eq. to const sorted = sort(parents);
Reverse a matrix
topo-kahn
exports a specific function called reverseMatrix
if you need to.