luet/vendor/github.com/mudler/topsort
Ettore Di Giacinto d7a04465fd update vendor/
2020-11-03 17:21:32 +01:00
..
.gitignore update vendor/ 2020-11-03 17:21:32 +01:00
LICENSE update vendor/ 2020-11-03 17:21:32 +01:00
README.md update vendor/ 2020-11-03 17:21:32 +01:00
topsort.go update vendor/ 2020-11-03 17:21:32 +01:00

topsort

Topological Sorting for Golang

Topological sorting algorithms are especially useful for dependency calculation, and so this particular implementation is mainly intended for this purpose. As a result, the direction of edges and the order of the results may seem reversed compared to other implementations of topological sorting.

For example, if:

  • A depends on B
  • B depends on C

The graph is represented as:

A -> B -> C

Where -> represents a directed edge from one node to another.

The topological ordering of dependencies results in:

[C, B, A]

The code for this example would look something like:

// Initialize the graph.
graph := topsort.NewGraph()
graph.AddNode("A")
graph.AddNode("B")
graph.AddNode("C")

// Add edges.
graph.AddEdge("A", "B")
graph.AddEdge("B", "C")

// Topologically sort node A.
graph.TopSort("A")  // => [C, B, A]