From 73e641120f27dd6b208c94ad0c5e3feb9a4eedcb Mon Sep 17 00:00:00 2001 From: James Shubin Date: Fri, 27 Jun 2025 23:50:58 -0400 Subject: [PATCH] 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. --- pgraph/pgraph.go | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/pgraph/pgraph.go b/pgraph/pgraph.go index 07c1585e..d24dd243 100644 --- a/pgraph/pgraph.go +++ b/pgraph/pgraph.go @@ -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