Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzotinfena committed Apr 4, 2024
1 parent d10878c commit 554eaba
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
27 changes: 27 additions & 0 deletions collections/graph/topologicalsort.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package graph

import "github.com/lorenzotinfena/goji/utils/slices"

// Assumptions:
// - the graph is acyclic
func TopologicalSort[V any](vertices []V, adjacents func(V) []V, setAdd func(V), setContains func(V) bool) []V {
res := []V{}

var dfs func(v V)
dfs = func(v V) {
setAdd(v)
for _, v1 := range adjacents(v) {
if !setContains(v1) {
dfs(v1)
}
}
res = append(res, v)
}
for _, v := range vertices {
if !setContains(v) {
dfs(v)
}
}
slices.Reverse(res)

Check failure on line 25 in collections/graph/topologicalsort.go

View workflow job for this annotation

GitHub Actions / Code style and tests

undefined: slices.Reverse (typecheck)

Check failure on line 25 in collections/graph/topologicalsort.go

View workflow job for this annotation

GitHub Actions / Code style and tests

undefined: slices.Reverse) (typecheck)
return res
}
1 change: 1 addition & 0 deletions collections/linkedlist/circulardoubly.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func NewCircularDoublyLinkedList[T any](equals func(T, T) bool) *CircularDoublyL
equals: equals,
}
}

func (l *CircularDoublyLinkedList[T]) Len() int { return l.length }

func (l *CircularDoublyLinkedList[T]) First() T { return l.first.Value }
Expand Down
1 change: 1 addition & 0 deletions collections/linkedlist/singly.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func NewSinglyLinkedList[T any](equals func(T, T) bool) *SinglyLinkedList[T] {
equals: equals,
}
}

func (l *SinglyLinkedList[T]) Len() int { return l.length }

func (l *SinglyLinkedList[T]) First() T { return l.first.Value }
Expand Down

0 comments on commit 554eaba

Please sign in to comment.