An open API service indexing awesome lists of open source software.

https://github.com/alecthomas/topsort

Topological Sorting for Golang
https://github.com/alecthomas/topsort

Last synced: 6 months ago
JSON representation

Topological Sorting for Golang

Awesome Lists containing this project

README

          

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:

```go
// Initialize the graph.
graph := topsort.NewGraph[string]()

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

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