UNPKG

lgrthms

Version:

Algorithms and data structures for your JavaScript and TypeScript projects 🧑‍💻

44 lines (43 loc) 1.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.topologicalSort = void 0; const Graph_1 = require("../../dataStructures/Graph"); function topologicalSort(array, dependencies, get) { get = get ? get : (element) => element; const graph = buildGraph(array, dependencies, get); const sorted = []; for (const element of array) { const nodeId = get(element); depthFirstTraverseAndPopulateSortedArray(nodeId, graph, sorted); } return sorted; } exports.topologicalSort = topologicalSort; function buildGraph(array, dependencies, get) { const graph = new Graph_1.Graph(); for (const element of array) { const id = get(element); const info = { visited: false, visiting: false }; graph.addNode({ element, info }, id); } for (const { dependency, prerequisite } of dependencies) { graph.addEdge(dependency, prerequisite); } return graph; } function depthFirstTraverseAndPopulateSortedArray(nodeId, graph, sorted) { const node = graph.getNode(nodeId); if (node.value.info.visited) { return; } if (node.value.info.visiting) { throw new Error(`Invalid dependencies array, detected cycle at node ${nodeId}`); } node.value.info.visiting = true; for (const neighborId in node.edges) { depthFirstTraverseAndPopulateSortedArray(neighborId, graph, sorted); } node.value.info.visiting = false; node.value.info.visited = true; sorted.push(node.value.element); }