@grouparoo/core
Version:
The Grouparoo Core
27 lines (26 loc) • 750 B
TypeScript
export declare type Graph = {
[key: string]: string[];
};
/**
* Topological sort algorithm of a directed acyclic graph.<br><br>
* Time complexity: O(|E| + |V|) where E is a number of edges
* and |V| is the number of nodes.
*
* @public
* @param {Array} Graph Adjacency list, which represents the graph.
* @returns {Array} Ordered vertices.
*
* @example
* var topsort =
* require('path-to-algorithms/src/graphs/' +
* 'others/topological-sort').topologicalSort;
* var graph = {
* v1: ['v2', 'v5'],
* v2: [],
* v3: ['v1', 'v2', 'v4', 'v5'],
* v4: [],
* v5: []
* };
* var vertices = topologicalSort(graph); // ['v3', 'v4', 'v1', 'v5', 'v2']
*/
export declare function topologicalSort(graph: Graph): string[];