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
- Host: GitHub
- URL: https://github.com/alecthomas/topsort
- Owner: alecthomas
- License: apache-2.0
- Fork: true (stevenle/topsort)
- Created: 2022-12-20T20:16:03.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2022-12-21T02:44:31.000Z (over 3 years ago)
- Last Synced: 2024-06-21T12:57:31.023Z (about 2 years ago)
- Language: Go
- Size: 11.7 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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]
```