-
Notifications
You must be signed in to change notification settings - Fork 69
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
Adding BFS to Algebra.Graph.AdjacencyMap*.Algorithm #181
Comments
Update: I found a possible problem with my BFS implementation. I am renaming So, I am unsure on how could I proceed to fix this discrepancy, as using a Maybe return type would mean that the user could produce wrong Trees. |
@TheChouzanOne Thanks, adding an implementation of the BFS algorithm would be great! I suggest that as a first try you copy the existing API of DFS exactly -- note that it also needs to return the empty forest when given a nonexistent vertex. Let's see how it works out and adapt it if need be. |
@snowleopard I'm on it. I've already implemented a correct Also, just to be clear, by "copy the existing API of DFS exactly" do you mean to have the same three functions dfsForestFrom :: Ord a => [a] -> AdjacencyMap a -> Forest a
dfs :: Ord a => [a] -> AdjacencyMap a -> [a]
dfsForest :: Ord a => AdjacencyMap a -> Forest a but for BFS? bfsForestFrom :: Ord a => [a] -> AdjacencyMap a -> Forest a
bfs :: Ord a => [a] -> AdjacencyMap a -> [a]
bfsForest :: Ord a => AdjacencyMap a -> Forest a -- Done! :) |
I've coded a cleaner and working version for Also, I wrote a post about its development if you want to read it. I still don't know if you would like to have all three functions ( |
@TheChouzanOne Thanks! For an initial prototype I suggest to implement the following two functions. We might rename them later, and in fact I think we might also rename some DFS functions to get to a more clear naming scheme. -- Run BFS from the list of initial vertices, returning the resulting BFS forest.
bfsForestFrom :: Ord a => [a] -> AdjacencyMap a -> Forest a
-- Run BFS from the list of initial vertices (roots), returning the resulting list of
-- levels, i.e. vertices that are at the same distance from the roots.
bfs :: Ord a => [a] -> AdjacencyMap a -> [[a]] What is currently missing from your blog post and implementation is a good list of examples, like here, and complexity analysis. Feel free to open a draft PR so we can start discussing specific details of the implementation. |
@snowleopard Sounds good! I'll be working on it then. |
I've noticed that there is no BFS implementation for adjacency maps but DFS does have one. It might be useful to have a single source shortest path algorithms for unlabelled graphs.
I've written a small blog/tutorial for my first implementation. It can be found here.
Its type currently is
It takes a root vertex, an AM and returns another AM. The problem is that the API is not similar to DFS, as its type shows:
My algorithm could be modified slightly to return a
Tree a
instead of an AM and then the new functionbfsForest :: Ord a => AdjacencyMap a -> Forest a
can be easily implemented using
bfsTree
as its foundation. Not sure if that would be enough though.The text was updated successfully, but these errors were encountered: