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.
This commit is contained in:
James Shubin
2024-01-07 18:05:41 -05:00
parent cb021e3a25
commit 58607e2ca0
2 changed files with 31 additions and 3 deletions

View File

@@ -512,7 +512,7 @@ func (g *Graph) FilterGraph(name string, vertices []Vertex) (*Graph, error) {
} }
for k2, e := range x { for k2, e := range x {
//log.Printf("Filter: %s -> %s # %s", k1.Name, k2.Name, e.Name) //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) newGraph.AddEdge(k1, k2, e)
} }
} }
@@ -538,7 +538,8 @@ func (g *Graph) DisconnectedGraphs() ([]*Graph, error) {
// dfs through the graph // dfs through the graph
dfs := g.DFS(start) dfs := g.DFS(start)
// filter all the collected elements into a new graph // 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 { if err != nil {
return nil, errwrap.Wrapf(err, "could not run DisconnectedGraphs() properly") 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 d = append(d, dfs...) // extend
// append this new graph to the list // 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 // if we've found all the elements, then we're done
// otherwise loop through to continue... // otherwise loop through to continue...

View File

@@ -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) { func TestDisconnectedGraphs1(t *testing.T) {
G, _ := NewGraph("g6") G, _ := NewGraph("g6")
v1 := NV("v1") v1 := NV("v1")