Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug in BFS? #2

Open
apeano opened this issue Oct 14, 2019 · 3 comments
Open

Bug in BFS? #2

apeano opened this issue Oct 14, 2019 · 3 comments

Comments

@apeano
Copy link

apeano commented Oct 14, 2019

Hello,
it seems that the procedure never enters in the "allowTraversal" function. Thus, it loops forever in case of graphs with cycles.

I don't know very well PowerShell, however from a first insight it is weird that the $seen variable is initialized every time allowTraversal is called.

I used this patch:
$graph.getNeighbors($currentVertex).ForEach{ $nextVertex = $_ if (!$seen[$nextVertex.getKey()]) { "NOT SEEN: " + $nextVertex.getKey() $seen[$nextVertex.getKey()] = $true $vertexQueue.enqueue($nextVertex) } }

Cheers

@dfinke
Copy link
Owner

dfinke commented Oct 14, 2019

Probably. I didn't finish building this out.

@apeano
Copy link
Author

apeano commented Oct 18, 2019

Since you have helped me... I try to give back the favour...
This implementation works for me.
`
class GraphAlg {
[Object]breadthFirstSearch($graph, $startVertex) {
$graph.toString()
$startVertex.GetType()
$vertexQueue = New-Object Queue
$edgeQueue = New-Object Queue
# Do initial queue setup.
$vertexQueue.enqueue($startVertex)
$previousVertex = $null
$seen = @{}
$seen[$startVertex.getKey()] = $true

	# Traverse all vertices from the queue.
	while (!$vertexQueue.isEmpty()) {
		$currentVertex = $vertexQueue.dequeue()
					
		# Add all neighbors to the queue for future traversals.
		$graph.getNeighbors($currentVertex).ForEach{
			$nextVertex = $_
			if (!$seen[$nextVertex.getKey()]) {
				$seen[$nextVertex.getKey()] = $true
				$vertexQueue.enqueue($nextVertex)
				#this measures the depth of the node in the tree
				#$nextVertex.data = $currentVertex.data + 1
				$e = $currentVertex.findEdge($nextVertex)
				$edgeQueue.enqueue($e)
			}
		}
	}
	
	$edgeQueue.toString()
	return $edgeQueue
}

}
`

@dfinke
Copy link
Owner

dfinke commented Oct 18, 2019

Thank you! Would you like to try your hand at forking the code, adding this and doing a pull request (PR)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants