pgraph: Improve time complexity of IncomingGraphVertices

This goes from O(n^2) to O(n) when map lookup is O(1). I never really
focused on much optimizing, but I noticed this one in passing.
This commit is contained in:
James Shubin
2025-06-27 23:50:58 -04:00
parent f7e446ef6f
commit 73e641120f

View File

@@ -440,14 +440,10 @@ func (g *Graph) Logf(logf func(format string, v ...interface{})) {
// IncomingGraphVertices returns an array (slice) of all directed vertices to
// vertex v (??? -> v). OKTimestamp should probably use this.
func (g *Graph) IncomingGraphVertices(v Vertex) []Vertex {
// TODO: we might be able to implement this differently by reversing
// the Adjacency graph and then looping through it again...
var s []Vertex
for k := range g.adjacency { // reverse paths
for w := range g.adjacency[k] {
if w == v {
s = append(s, k)
}
if _, exists := g.adjacency[k][v]; exists {
s = append(s, k)
}
}
return s