dominikbraun / graph

A library for creating generic graph data structures and modifying, analyzing, and visualizing them.
https://graph.dominikbraun.io
Apache License 2.0
1.81k stars 94 forks source link

BFSWithDepth not calculating depth correctly #153

Open amengcs opened 1 year ago

amengcs commented 1 year ago

The following tree has a single "root" and 5 children attached to that root. I expected the depth to be 2.

BFSWithDepth increments and passes "depth" to the visit function with values as high as 6.

For this case, it increments depth for the root and again for each child attached to the root as it loops through the adjancency graph.

resourceHash := func(r Resource) string { return r.Name() }

s.tree = graph.New(resourceHash, graph.Tree(), graph.Directed(), graph.PreventCycles())
s.tree.AddVertex(NullResource{"root"})
s.tree.AddVertex(NullResource{"A"})
s.tree.AddVertex(NullResource{"B"})
s.tree.AddVertex(NullResource{"C"})
s.tree.AddVertex(NullResource{"D"})
s.tree.AddVertex(NullResource{"E"})
s.tree.AddEdge("root", "A")
s.tree.AddEdge("root", "B")
s.tree.AddEdge("root", "C")
s.tree.AddEdge("root", "D")
s.tree.AddEdge("root", "E")
amengcs commented 1 year ago

Using version 0.23.0 (latest) It seems like it is returning "width + 1" instead of "depth".

gcpreston commented 9 months ago

+1 to this 🤕

dominikbraun commented 7 months ago

First of all, sorry for the late response – I'm going to review #153 today.