-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
topological.go
42 lines (37 loc) · 1.3 KB
/
topological.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// topological.go
// description: Topological sort
// details: Topological sorting for Directed Acyclic Graph (DAG) is a linear ordering of vertices such that for every directed edge u v, vertex u comes before v in the ordering. Topological Sorting for a graph is not possible if the graph is not a DAG.
// time complexity: O(V+E) where V is the number of vertices and E is the number of edges in the graph
// space complexity: O(V) where V is the number of vertices in the graph
// reference: https://en.wikipedia.org/wiki/Topological_sorting
package graph
// Topological assumes that graph given is valid and that its
// possible to get a topological ordering.
// constraints are array of []int{a, b}, representing
// an edge going from a to b
func Topological(N int, constraints [][]int) []int {
dependencies := make([]int, N)
nodes := make([]int, N)
for i := range nodes {
nodes[i] = i
}
edges := make([][]bool, N)
for i := range edges {
edges[i] = make([]bool, N)
}
for _, c := range constraints {
a := c[0]
b := c[1]
dependencies[b]++
edges[a][b] = true
}
var answer []int
for s := 0; s < N; s++ {
// Only start walking from top level nodes
if dependencies[s] == 0 {
route, _ := DepthFirstSearchHelper(s, N, nodes, edges, true)
answer = append(answer, route...)
}
}
return answer
}