Graph Traversal with Clojure
I’m going through the Coursera Algorithms I class in my spare time. Like most of my experiences with Coursera I signed up for the course with little expectation of doing the course but wanting to unlock all of the videos and other material for perusal later. So I suck at MOOCs.
I’m on the course section covering graphs. Graphs are awesome and its a nice step up from sorting. I want to take a shot at implementing some graph algorithms using Clojure and figure a good starting point would be graph traversal using breadth first search and depth first search.
WARNING: I am no Clojure expert!
My starting point was creating a function which would traverse a graph and print out each node as it went. I had no idea what I wanted to do so debug print statements seemed as good as any. Then I thought it would be much cooler to have a function passed in which would get called on each node as it traversed the graph. Then I realized I’m implementing my own map function and that I’m probably wrong.
In order to get Clojure’s built in map function to work with the graph I would need to turn it into a sequence. Ah, now that seems like a reasonable idea.
Here is how I’m representing a graph.
The graph structure is a map of nodes to a list of neighbors. So { :1 [:2 :3] }
means :1
has two edges – one to :2
and one to :3
. Here is my very first ever graphviz!
And here is my go at doing a depth first search on a graph returning a sequence (vector in this case).
The course describes the algorithm recursively so I’m using Clojure’s loop
and recur
syntax. In the loop we want to keep track of three things:
- a built up vector of traversed vertices
- a set of explored vertices (explored means we have seen it)
- a stack of frontier vertices (frontier means we have explored it but haven’t explored its neighbors)
In the initial case we have an empty vector of traversed vertices, an explored set including our starting vertex and a frontier stack including our starting vertex.
On each iteration we check if the frontier stack is empty. If it is we are done and return the traversed vertices. If not we pop the stack for a vertex and get the vertex’s neighbors. We then recurse adding the vertex to the traversed vertices, adding the neighbors to the explored set, and adding the unexplored neighbors to the frontier.
This works but its eager – the graph is fully traversed when you call the function. I wanted to try and make a lazy sequence version. In order to do this I use the magic of lazy-seq
.
Instead of loop
and recur
we define a recursive function, rec-dfs
, and use lazy-seq
to build up the sequence with recursive calls. Each call cons
the current vertex to the result of a new recursive call finally ending with a nil
(end of sequence). Through the magic of lazy-seq
we don’t have to worry about blowing the stack and get the benefits of the sequence being lazy. This means if I take 2
from the sequence we only traverse 2 nodes, not the entire graph as we would have done in traverse-graph-dfs
.
Here is a wiki page describing an implementation of Fibonacci using lazy-seq
which might be easier to follow as the algorithm is more instantly recognizable.
With DFS working I wanted to try BFS. Luckily the change is simple – just change the stack to a queue.
This works because all the primitives (peek
, pop
, conj
) do the right things depending on the data structure (stack or queue). It was nice to see this actually work.
Finally, since the only difference is the initial data structure we can make it an argument to a generic function.
Neato.
Again, I’m no Clojure expert so welcome feedback on anything that can do better.