-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implementation of topological sort (#399)
- Loading branch information
Showing
3 changed files
with
105 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package graph | ||
|
||
// Assumes that graph given is valid and possible to | ||
// get a topo 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 | ||
} | ||
|
||
ans := []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) | ||
ans = append(ans, route...) | ||
} | ||
} | ||
|
||
return ans | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package graph | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
var testCases = []struct { | ||
name string | ||
N int | ||
constraints [][]int | ||
}{ | ||
{ | ||
"basic test", 2, | ||
[][]int{{1, 0}}, | ||
}, | ||
{ | ||
"double path", 7, | ||
[][]int{ | ||
{0, 1}, {1, 3}, {3, 5}, | ||
{0, 2}, {2, 4}, {4, 6}}, | ||
}, | ||
{ | ||
"star shape", 7, | ||
[][]int{ | ||
{0, 1}, {0, 3}, {0, 5}, | ||
{0, 2}, {0, 4}, {0, 6}}, | ||
}, | ||
{ | ||
"tree shape", 7, | ||
[][]int{ | ||
{0, 1}, {1, 3}, {1, 5}, | ||
{0, 2}, {2, 4}, {2, 6}}, | ||
}, | ||
} | ||
|
||
func TestTopological(t *testing.T) { | ||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
actual := Topological(tc.N, tc.constraints) | ||
|
||
visited := make([]bool, tc.N) | ||
positions := make([]int, tc.N) | ||
for i := 0; i < tc.N; i++ { | ||
positions[actual[i]] = i | ||
visited[actual[i]] = true | ||
} | ||
for _, v := range visited { | ||
if !v { | ||
t.Errorf("nodes not all visited, %v", visited) | ||
} | ||
} | ||
for _, c := range tc.constraints { | ||
if positions[c[0]] > positions[c[1]] { | ||
t.Errorf("%v dun satisfy %v", actual, c) | ||
} | ||
} | ||
}) | ||
} | ||
} |