Skip to content

Instantly share code, notes, and snippets.

@owainlewis
Created November 1, 2012 13:47
Show Gist options
  • Save owainlewis/3993713 to your computer and use it in GitHub Desktop.
Save owainlewis/3993713 to your computer and use it in GitHub Desktop.
Clojure graph algorithms
(ns graph.core)
;; Graph algorithms
;; Simple adjacency list using maps
(def weighted-graph
{:A {:B 0, :C 0 }, :B { :C 0 }, :C {} })
;; a --> b
;; \ /
;; c
(def graph
{:vertices #{:a :b :c}
:edges {:a #{:b}
:b #{:c}
:c #{:a}}})
(defrecord Graph [vertices edges])
(def g (ref (Graph. #{} {})))
(defn vertices [graph]
(:vertices @graph))
(defn edges [graph]
(:edges @graph))
(defn add-vertex [g v]
(dosync
(alter g assoc :vertices
(conj (:vertices @g) v))))
(defn remove-vertex [g v]
(dosync
(alter g assoc :vertices
(disj (:vertices @g) v))))
(defn neighbours [g v])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment