Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Impr/add enums #7

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ You can add the flag `-benchmem` if you want to see the memory allocations:
go test ./... -bench=. -run=xxx -benchmem
```


## Notes:

* Maps are one of the common ways to store graphs in Go. The `TopologicalSort` function input supports `map[interface{}][]interface{}` maps.
Expand Down
4 changes: 2 additions & 2 deletions cmd/example-2/example_2.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ func main() {
3: []int{4, 5},
}

output, err = gorder.TopologicalSort(serialize(digraph), "kahn")
output, err = gorder.TopologicalSort(serialize(digraph), gorder.KAHN)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Solution (Kahn): %d\n", deserialize(output))

output, err = gorder.TopologicalSort(serialize(digraph), "dfsbased")
output, err = gorder.TopologicalSort(serialize(digraph), gorder.DFS)
if err != nil {
log.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/example/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ func main() {
3: []interface{}{"4", "5"},
}

o, err = gorder.TopologicalSort(digraph, "kahn")
o, err = gorder.TopologicalSort(digraph, gorder.KAHN)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Solution (Kahn): %v\n", o)

o, err = gorder.TopologicalSort(digraph, "dfsbased")
o, err = gorder.TopologicalSort(digraph, gorder.DFS)
if err != nil {
log.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/measurements/measurements.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ func main() {
log.Printf("DAG generation time: %v", time.Since(start))

start = time.Now()
s, err := gorder.TopologicalSort(graph, "kahn")
s, err := gorder.TopologicalSort(graph, gorder.KAHN)
log.Printf("Kahn resolve time: %v", time.Since(start))
if err != nil {
log.Fatal(err)
}
spew.Dump(s)

start = time.Now()
s, err = gorder.TopologicalSort(graph, "dfsbased")
s, err = gorder.TopologicalSort(graph, gorder.DFS)
log.Printf("DFS-based resolve time: %v", time.Since(start))
if err != nil {
log.Fatal(err)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module github.com/amwolff/gorder

go 1.16
go 1.20

require github.com/davecgh/go-spew v1.1.1
62 changes: 34 additions & 28 deletions gorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,30 @@ package gorder

import (
"errors"
"regexp"
)

func TopologicalSort(digraph map[interface{}][]interface{}, algorithm string) (solution []interface{}, err error) {
kahnRgxp, err := regexp.Compile(`[Kk]ahn\z`)
if err != nil {
return nil, err
}
dfsBasedRgxp, err := regexp.Compile(`[Dd][Ff][Ss]-?[Bb]ased\z`)
if err != nil {
return nil, err
}
type algo int

const (
DFS algo = iota
KAHN
)

func TopologicalSort[T comparable, V []T](digraph map[T]V, algorithm algo) (solution V, err error) {
// kahnRgxp, err := regexp.Compile(`[Kk]ahn\z`)
// if err != nil {
// return nil, err
// }
// dfsBasedRgxp, err := regexp.Compile(`[Dd][Ff][Ss]-?[Bb]ased\z`)
// if err != nil {
// return nil, err
// }

if kahnRgxp.MatchString(algorithm) {
if algorithm == KAHN {
if solution, err = kahn(digraph); err != nil {
return nil, err
}
} else if dfsBasedRgxp.MatchString(algorithm) {
} else if algorithm == DFS {
if solution, err = dfsBased(digraph); err != nil {
return nil, err
}
Expand All @@ -29,24 +35,24 @@ func TopologicalSort(digraph map[interface{}][]interface{}, algorithm string) (s
return solution, nil
}

func kahn(digraph map[interface{}][]interface{}) ([]interface{}, error) {
indegrees := make(map[interface{}]int)
for u := range digraph {
if digraph[u] != nil {
for _, v := range digraph[u] {
indegrees[v]++
}
func kahn[T comparable, V []T](digraph map[T]V) (V, error) {
indegrees := make(map[T]int)

// loop through all diagraph and add increase indegrees of values
for _, iter := range digraph {
for _, v := range iter {
indegrees[v]++
}
}

var queue []interface{}
var queue V
for u := range digraph {
if _, ok := indegrees[u]; !ok {
queue = append(queue, u)
}
}

var order []interface{}
var order V
for len(queue) > 0 {
u := queue[len(queue)-1]
queue = queue[:(len(queue) - 1)]
Expand All @@ -67,16 +73,16 @@ func kahn(digraph map[interface{}][]interface{}) ([]interface{}, error) {
return order, nil
}

func dfsBased(digraph map[interface{}][]interface{}) ([]interface{}, error) {
func dfsBased[T comparable, V []T](digraph map[T]V) (V, error) {
var (
acyclic = true
order []interface{}
permanentMark = make(map[interface{}]bool)
temporaryMark = make(map[interface{}]bool)
visit func(interface{})
order V
permanentMark = make(map[T]bool)
temporaryMark = make(map[T]bool)
visit func(T)
)

visit = func(u interface{}) {
visit = func(u T) {
if temporaryMark[u] {
acyclic = false
} else if !(temporaryMark[u] || permanentMark[u]) {
Expand All @@ -89,7 +95,7 @@ func dfsBased(digraph map[interface{}][]interface{}) ([]interface{}, error) {
}
delete(temporaryMark, u)
permanentMark[u] = true
order = append([]interface{}{u}, order...)
order = append(V{u}, order...)
}
}

Expand Down
33 changes: 17 additions & 16 deletions gorder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,21 @@ import (
)

func TestTopologicalSort(t *testing.T) {
digraph := map[interface{}][]interface{}{
1: []interface{}{2, 4},
2: []interface{}{3, 5},
3: []interface{}{4, 5},
digraph := map[int][]int{
1: {2, 4},
2: {3, 5},
3: {4, 5},
}

want := []int{1, 2, 3, 5, 4}

_, err := TopologicalSort(digraph, "ultrasuperfast")

_, err := TopologicalSort(digraph, 3)
if err == nil {
t.Fatal("TopologicalSort: should have returned an error")
}

o, err := TopologicalSort(digraph, "kahn")
o, err := TopologicalSort(digraph, KAHN)
if err != nil {
t.Fatalf("Kahn: %v", err)
}
Expand All @@ -30,7 +31,7 @@ func TestTopologicalSort(t *testing.T) {
}
}

o, err = TopologicalSort(digraph, "dfsbased")
o, err = TopologicalSort(digraph, DFS)
if err != nil {
t.Fatalf("DFS-based: %v", err)
}
Expand All @@ -40,17 +41,17 @@ func TestTopologicalSort(t *testing.T) {
}
}

graphWithCycles := map[interface{}][]interface{}{
1: []interface{}{2, 4},
2: []interface{}{3, 5},
3: []interface{}{4, 5},
4: []interface{}{2},
graphWithCycles := map[int][]int{
1: {2, 4},
2: {3, 5},
3: {4, 5},
4: {2},
}
_, err = TopologicalSort(graphWithCycles, "kahn")
_, err = TopologicalSort(graphWithCycles, KAHN)
if err == nil {
t.Fatal("Kahn: should have returned an error")
}
_, err = TopologicalSort(graphWithCycles, "dfsbased")
_, err = TopologicalSort(graphWithCycles, DFS)
if err == nil {
t.Fatal("DFS-based: should have returned an error")
}
Expand All @@ -60,15 +61,15 @@ func BenchmarkTopologicalSort(b *testing.B) {
digraph := dagenerator.Generate(10, 50, 30, 50, 30)
b.Run("kahn", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := TopologicalSort(digraph, "kahn")
_, err := TopologicalSort(digraph, KAHN)
if err != nil {
b.Errorf("Kahn: %v", err)
}
}
})
b.Run("dfs based", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := TopologicalSort(digraph, "dfsbased")
_, err := TopologicalSort(digraph, DFS)
if err != nil {
b.Errorf("DFS-based: %v", err)
}
Expand Down