Created
November 1, 2012 13:47
-
-
Save owainlewis/3993713 to your computer and use it in GitHub Desktop.
Clojure graph algorithms
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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