pgraph: Add LookupEdge function

The new LookupEdge function lets you find which vertices are associated
with a particular edge, if any.
This commit is contained in:
James Shubin
2023-04-20 15:45:46 -04:00
parent 3f5957d30e
commit 7075b8b973
2 changed files with 85 additions and 0 deletions

View File

@@ -288,6 +288,21 @@ func (g *Graph) FindEdge(v1, v2 Vertex) Edge {
return edge
}
// LookupEdge takes an edge and tries to find the vertex pair that connects it.
// If it finds a match, then it returns the pair and true. Otherwise it returns
// false.
func (g *Graph) LookupEdge(e Edge) (Vertex, Vertex, bool) {
for v1, x := range g.adjacency {
for v2, edge := range x {
if edge == e {
return v1, v2, true
}
}
}
return nil, nil, false // not found
}
// Vertices returns a randomly sorted slice of all vertices in the graph. The
// order is random, because the map implementation is intentionally so!
func (g *Graph) Vertices() []Vertex {