go get github.com/ninedraft/tsort
A small topological ordering/sorting generic library.
It doesn't define any graph format, instead you can just pass a single callback.
var graph = [][]int{
0: {},
1: {0, 2},
2: {0},
3: {1, 2},
}
sorted, hasCycle := tsort.Sort(
[]int{0, 1, 2, 3},
func(i int) []int {
return graph[i]
})
fmt.Println(sorted, hasCycle)
It also provides a IsSorted function, which checks if provided nodes are sorted on graph:
var graph = [][]int{
0: {},
1: {0, 2},
2: {0},
3: {1, 2},
}
sorted, hasCycle := tsort.Sort(
[]int{0, 1, 2, 3},
func(i int) []int {
return graph[i]
})
fmt.Println(sorted, hasCycle)
Zero dependencies (beside of std and x/exp).