I have a directed graph G given as a list of adjacency lists:
newtype Graph Int = Graph [(Int, [Int])]
G has n vertices and m edges. I'm trying to implement BFS algorithm in Haskell that runs in O(m) time (maybe amortized), but best solution I was able to come up with runs in O(m * log n) and uses data structure from Data.Map
module.
My idea of linear solution is as follows: Use structure from Data.Sequence
as efficient FIFO queue and do everything as imperative BFS would do, but I'm stuck at point where I have to mark nodes as visited.
My question is: Is it possible to implement BFS in Haskell (or any other purely functional language) that runs in O(m)? And if It's not, what argument can you use to prove such statement?