Fix BFSWithDepth visit callback invoked with incorrect depth#154
Open
s111 wants to merge 3 commits intodominikbraun:mainfrom
Open
Fix BFSWithDepth visit callback invoked with incorrect depth#154s111 wants to merge 3 commits intodominikbraun:mainfrom
s111 wants to merge 3 commits intodominikbraun:mainfrom
Conversation
This commit fixes BFSWithDepth such that it stops after visiting all nodes at the depth the visit callback first returned false. Note: This also affects BFS as it invokes the BFSWithDepth function.
Owner
|
Thanks for your PR! I'm currently having a busy time at work but I'm take a look as soon as I can. |
|
You don't need an extra loop. An extra map diff --git a/traversal.go b/traversal.go
index d7e232c..196dfe2 100644
--- a/traversal.go
+++ b/traversal.go
@@ -126,25 +127,26 @@ func BFSWithDepth[K comparable, T any](g Graph[K, T], start K, visit func(K, int
queue := make([]K, 0)
visited := make(map[K]bool)
+ depths := make(map[K]int)
visited[start] = true
queue = append(queue, start)
- depth := 0
+ depths[start] = 0
for len(queue) > 0 {
currentHash := queue[0]
queue = queue[1:]
- depth++
// Stop traversing the graph if the visit function returns true.
- if stop := visit(currentHash, depth); stop {
+ if stop := visit(currentHash, depths[currentHash]); stop {
break
}
for adjacency := range adjacencyMap[currentHash] {
if _, ok := visited[adjacency]; !ok {
visited[adjacency] = true
+ depths[adjacency] = depths[currentHash] + 1
queue = append(queue, adjacency)
}
} |
|
Apologies for the bump but I've just stubbed my toe on this and it'd be great to see if we can drive it across the line, if someone could review that'd be much appreciated 🙏 If the PR has stagnated or needs review comments addressing I'd be more than happy to do-so but generally things look good, and they're working nicely while using a |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Hi,
Thanks for making this library, awesome work!
I tried to use BFSWithDepth today and it would seem that the depth is not correctly being supplied to the callback. I see that an issue is already reported here #153.
This pull request is my attempt at resolving this bug, I hope that it looks good.
I've added an inner for-loop which ensures that all vertices at the current depth is visited before incrementing the depth again at the start of the outer loop.
It looks like the test that was added with #120 doesn't actually verify the depth, so I've extracted a new test function for BFSWithDepth.
(I also took the liberty to remove what looks like some forgotten debug print statements, I hope that's OK.)