diff --git a/package.json b/package.json index 8fc86ee..083df24 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@aureooms/js-topological-sorting", "description": "Topological sorting for JavaScript", - "version": "0.1.0", + "version": "0.2.0", "license": "AGPL-3.0", "author": "aureooms", "homepage": "https://aureooms.github.io/js-topological-sorting", diff --git a/src/freeVertices.js b/src/freeVertices.js new file mode 100644 index 0000000..c784aa8 --- /dev/null +++ b/src/freeVertices.js @@ -0,0 +1,12 @@ +/** + * Compute the free vertices of the input graph. + * + * @param {Iterable} graph - The input graph as a list of edges. + * @returns {Iterable} The free vertices of the input graph. + */ +export default function freeVertices(graph) { + const vertices = new Set(); + for (const [u] of graph) vertices.add(u); + for (const [, v] of graph) vertices.delete(v); + return vertices; +} diff --git a/src/index.js b/src/index.js index ff71f27..fb95f8e 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,6 @@ +import freeVertices from './freeVertices.js'; import kahn1962 from './kahn1962.js'; import sorted from './sorted.js'; +import subroutine from './subroutine.js'; -export {kahn1962, sorted}; +export {freeVertices, kahn1962, sorted, subroutine}; diff --git a/src/kahn1962.js b/src/kahn1962.js index 978068e..577494c 100644 --- a/src/kahn1962.js +++ b/src/kahn1962.js @@ -1,5 +1,5 @@ /** - * Kahn's algorithm. + * Kahn's algorithm for topological sorting. * * See https://en.wikipedia.org/wiki/Topological_sorting#CITEREFKahn1962 * diff --git a/src/sorted.js b/src/sorted.js index 202a7a9..ed4c0fb 100644 --- a/src/sorted.js +++ b/src/sorted.js @@ -1,7 +1,7 @@ import Heap from '@aureooms/js-pairing-heap'; import {EfficientlyInvertiblePairs as Pairs} from '@aureooms/js-pairs'; -import kahn1962 from './kahn1962.js'; +import subroutine from './subroutine.js'; /** * Sort the vertices topologically breaking ties according to a given function. @@ -12,15 +12,10 @@ import kahn1962 from './kahn1962.js'; * @throws {Error} If the input graph contains a cycle. */ export default function* sorted(edges, breakTies = undefined) { - const graph = Pairs.from(edges); - const queue = breakTies ? new Heap(breakTies) : []; - const freeVertices = new Set(); - for (const [u] of graph) freeVertices.add(u); - for (const [, v] of graph) freeVertices.delete(v); - for (const u of freeVertices) queue.push(u); + const graph = Pairs.from(edges); - yield* kahn1962(queue, graph); + yield* subroutine(queue, graph); if (graph.size > 0) { throw new Error( diff --git a/src/subroutine.js b/src/subroutine.js new file mode 100644 index 0000000..205bd16 --- /dev/null +++ b/src/subroutine.js @@ -0,0 +1,14 @@ +import freeVertices from './freeVertices.js'; +import kahn1962 from './kahn1962.js'; + +/** + * Sort the vertices topologically using a queue to order the free vertices. + * + * @param {any} queue - The queue that will be used. + * @param {any} graph - The input graph as a list of edges. + * @returns {Iterable} The vertices sorted in topological order. + */ +export default function subroutine(queue, graph) { + for (const u of freeVertices(graph)) queue.push(u); + return kahn1962(queue, graph); +}