From 58607e2ca048f8ac459bd07324c9f96cb65f31c9 Mon Sep 17 00:00:00 2001 From: James Shubin Date: Sun, 7 Jan 2024 18:05:41 -0500 Subject: [PATCH] pgraph: Filter graph should not include unfiltered vertices This adds a test for another sneaky aspect of filter graph: It should not pull in a vertex because another across the edge was included. If we want this behaviour, then we need a different function or a config option for this filter function. --- pgraph/pgraph.go | 7 ++++--- pgraph/pgraph_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/pgraph/pgraph.go b/pgraph/pgraph.go index 1851667b..a22d24f0 100644 --- a/pgraph/pgraph.go +++ b/pgraph/pgraph.go @@ -512,7 +512,7 @@ func (g *Graph) FilterGraph(name string, vertices []Vertex) (*Graph, error) { } for k2, e := range x { //log.Printf("Filter: %s -> %s # %s", k1.Name, k2.Name, e.Name) - if contains || VertexContains(k2, vertices) { + if contains && VertexContains(k2, vertices) { newGraph.AddEdge(k1, k2, e) } } @@ -538,7 +538,8 @@ func (g *Graph) DisconnectedGraphs() ([]*Graph, error) { // dfs through the graph dfs := g.DFS(start) // filter all the collected elements into a new graph - newgraph, err := g.FilterGraph(g.Name, dfs) + // TODO: is this method of filtering correct here? && or || ? + newGraph, err := g.FilterGraph(g.Name, dfs) if err != nil { return nil, errwrap.Wrapf(err, "could not run DisconnectedGraphs() properly") } @@ -546,7 +547,7 @@ func (g *Graph) DisconnectedGraphs() ([]*Graph, error) { d = append(d, dfs...) // extend // append this new graph to the list - graphs = append(graphs, newgraph) + graphs = append(graphs, newGraph) // if we've found all the elements, then we're done // otherwise loop through to continue... diff --git a/pgraph/pgraph_test.go b/pgraph/pgraph_test.go index 4004eda9..e669458d 100644 --- a/pgraph/pgraph_test.go +++ b/pgraph/pgraph_test.go @@ -213,6 +213,33 @@ func TestFilterGraph2(t *testing.T) { } } +func TestFilterGraph3(t *testing.T) { + G, _ := NewGraph("g5") + v1 := NV("v1") + v2 := NV("v2") + v3 := NV("v3") + v4 := NV("v4") + v5 := NV("v5") + e1 := NE("e1") + e2 := NE("e2") + G.AddEdge(v1, v2, e1) + G.AddEdge(v3, v4, e2) + G.AddVertex(v5) + + save := []Vertex{v1, v2, v3} + out, err := G.FilterGraph("new g5", save) + if err != nil { + t.Errorf("failed with: %v", err) + } + + if c, i := len(save), out.NumVertices(); c != i { + t.Errorf("should have %d vertices instead of: %d", c, i) + } + if c, i := 1, out.NumEdges(); c != i { + t.Errorf("should have %d edges instead of: %d", c, i) + } +} + func TestDisconnectedGraphs1(t *testing.T) { G, _ := NewGraph("g6") v1 := NV("v1")