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:
@@ -440,14 +440,10 @@ func (g *Graph) Logf(logf func(format string, v ...interface{})) {
|
|||||||
// IncomingGraphVertices returns an array (slice) of all directed vertices to
|
// IncomingGraphVertices returns an array (slice) of all directed vertices to
|
||||||
// vertex v (??? -> v). OKTimestamp should probably use this.
|
// vertex v (??? -> v). OKTimestamp should probably use this.
|
||||||
func (g *Graph) IncomingGraphVertices(v Vertex) []Vertex {
|
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
|
var s []Vertex
|
||||||
for k := range g.adjacency { // reverse paths
|
for k := range g.adjacency { // reverse paths
|
||||||
for w := range g.adjacency[k] {
|
if _, exists := g.adjacency[k][v]; exists {
|
||||||
if w == v {
|
s = append(s, k)
|
||||||
s = append(s, k)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return s
|
return s
|
||||||
|
|||||||
Reference in New Issue
Block a user