Topological sort algorithm of a directed acyclic graph.
Time complexity: O(|E|) where E is a number of edges.
Time complexity: O(|E|) where E is a number of edges.
Parameters:
| Name | Type | Description |
|---|---|---|
graph |
Array | Adjacency list, which represents the graph. |
- Source:
Returns:
Ordered vertices.
- Type
- Array
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 = topsort(graph); // ['v3', 'v4', 'v1', 'v5', 'v2']