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.8k stars 94 forks source link

feat(#124): full traverse #125

Open williamfzc opened 1 year ago

williamfzc commented 1 year ago

Hi :)

This is an implementation (draft) for #124 .

It already matches my need. However, as a library, there are still some issues that need to be discussed:

dominikbraun commented 1 year ago

Hi! Thanks for the PR. I do have one question in mind: What is the advantage of/difference between FullTraverse and a regular loop over the predecessors? Like this:

predecessorMap, _ := g.PredecessorMap()

for vertex, _ := range predecessorMap {
    _ = BFS(g, vertex, visit)
}
williamfzc commented 1 year ago

Hi! Thanks for the PR. I do have one question in mind: What is the advantage of/difference between FullTraverse and a regular loop over the predecessors? Like this:

predecessorMap, _ := g.PredecessorMap()

for vertex, _ := range predecessorMap {
    _ = BFS(g, vertex, visit)
}

This loop seems it will initiate BFS on all vertices in the graph. But the FullTraverse will only initiate BFS on the entry vertices.

I am not pretty sure that which one is better/expected.

jonbrandenburg commented 1 year ago

Hi! Thanks for the PR. I do have one question in mind: What is the advantage of/difference between FullTraverse and a regular loop over the predecessors? Like this:

predecessorMap, _ := g.PredecessorMap()

for vertex, _ := range predecessorMap {
    _ = BFS(g, vertex, visit)
}

This loop seems it will initiate BFS on all vertices in the graph. But the FullTraverse will only initiate BFS on the entry vertices.

I am not pretty sure that which one is better/expected.

I would argue that FullTraverse is the better solution, since it guarantees each vertex is only visited up to one time O(V). Whereas the sample code above will guarantee O(V^2) for a complete graph. That said however based on how it is currently written it appears FullTraverse will not visit every vertex if the graph has cycles. By that definition I wouldn't consider it a FullTraverse.

If the intention is to simply visit every vertex, then adding a Vertices function to the graph would likely be a better solution or a Visit function at the that took a visit function as a parameter and handled the traversal automatically, preferably without making a clone of the Vertices which is likely what a Vertices function would force the user to use.