Skip to content

Commit

Permalink
change API to add multiple edges in one call
Browse files Browse the repository at this point in the history
  • Loading branch information
David Rupp committed May 13, 2017
1 parent e1b343b commit 2926975
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ pom.xml.asc
.lein-failures
.lein-plugins
.lein-repl-history
.nrepl-port
8 changes: 6 additions & 2 deletions src/graffy/core.clj
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
(ns graffy.core)

(defn add-edge
"graph: a map of sources to sets of dests; adds a bi-directional edge"
(defn- edge
[graph source dest]
(-> graph
(assoc source (or (graph source) (sorted-set)))
(assoc dest (or (graph dest) (sorted-set)))
(update-in [source] conj dest)
(update-in [dest] conj source)))

(defn edges
"graph: a map of sources to sets of dests; adds a bi-directional edge"
[graph source & dests]
(reduce #(edge %1 source %2) graph dests))

(defn vertices [graph]
(keys graph))

Expand Down
16 changes: 6 additions & 10 deletions test/graffy/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,22 @@
(testing "vertices"
(let [g {}]
(is (= #{} (into #{} (vertices g))))
(is (= #{:a :b} (into #{} (vertices (add-edge g :a :b))))))))
(is (= #{:a :b} (into #{} (vertices (edges g :a :b))))))))

(deftest test-neighbors
(testing "neighbors"
(let [g (add-edge {} :a :b)]
(let [g (edges {} :a :b)]
(is (= #{:b} (into #{} (neighbors g :a))))
(is (= #{:a} (into #{} (neighbors g :b))))
(is (= #{} (into #{} (neighbors g :c)))))))

(deftest test-traversal
(testing "depth-first, breadth-first traversal"
(let [g (-> {}
(add-edge 0 1)
(add-edge 0 2)
(add-edge 0 5)
(add-edge 0 6)
(add-edge 3 4)
(add-edge 4 6)
(add-edge 5 3)
(add-edge 5 4))]
(edges 0 1 2 5 6)
(edges 3 4)
(edges 4 6)
(edges 5 3 4))]
(is (= [0 1 2 5 3 4 6] (dft g 0)))
(is (= [5 0 1 2 6 4 3] (dft g 5)))
(is (= [0 1 2 5 6 3 4] (bft g 0)))
Expand Down

0 comments on commit 2926975

Please sign in to comment.