Skip to content
Open
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
23 changes: 23 additions & 0 deletions dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,26 @@ func TransitiveReduction[K comparable, T any](g Graph[K, T]) (Graph[K, T], error

return transitiveReduction, nil
}

// FindSources returns all source vertices in a directed acyclic graph. A source
// vertex is a vertex with no incoming edges.
//
// FindSources only works for directed acyclic graph.
func FindSources[K comparable, T any](g Graph[K, T]) ([]K, error) {
if !g.Traits().IsDirected {
return nil, fmt.Errorf("cannot find source in a non-directed acyclic graph")
}

predecessorMap, err := g.PredecessorMap()
if err != nil {
return nil, fmt.Errorf("failed to get predecessor map: %w", err)
}

var sources []K
for vertex, predecessors := range predecessorMap {
if len(predecessors) == 0 {
sources = append(sources, vertex)
}
}
return sources, nil
}