func’onal graphs in clojure · 2018. 6. 20. · graphs in clojure: complex graphs how to create...
TRANSCRIPT
![Page 1: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/1.jpg)
&Func'onalGraphs
inClojure
AysyluGreenberg@aysylu22
![Page 2: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/2.jpg)
AysyluGreenberg
@aysylu22
github.com/aysylu/loom
![Page 3: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/3.jpg)
In this Talk
AMomentofGraphTheory GraphAPI
Design Func>onalGraph
AlgorithmsLoom
Overview
![Page 4: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/4.jpg)
In this Talk
AMomentofGraphTheory GraphAPI
Design Func>onalGraph
AlgorithmsLoom
Overview
![Page 5: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/5.jpg)
Graphs
![Page 6: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/6.jpg)
GraphsinRealLife
![Page 7: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/7.jpg)
TypesofGraphs
Simple Graph Weighted Graph
![Page 8: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/8.jpg)
TypesofGraphs
Directed Graph (Digraph) Weighted Digraph
![Page 9: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/9.jpg)
TypesofGraphs
Multigraph Mixed Graph
![Page 10: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/10.jpg)
In this Talk
AMomentofGraphTheory GraphAPI
Design Func>onalGraph
AlgorithmsLoom
Overview
![Page 11: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/11.jpg)
Graphs in OO World class Node { string data;}
class Edge { Node node1; Node node2;}
class Graph { List<Node> nodes; List<Edge> edges;}
![Page 12: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/12.jpg)
Graph Representations
![Page 13: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/13.jpg)
Graph Representations
• AdjacencyList{A:[B],B:[C,D],C:[],…}
![Page 14: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/14.jpg)
Graph Representations
• AdjacencyList• IncidenceList{A:[e1],B:[e2,e3],…,e1:[A,B],e2:[B,C],…}
![Page 15: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/15.jpg)
Graph Representations
• AdjacencyList• IncidenceList• AdjacencyMatrix
Source Des'na'on
A B … EA 0 1 … ∞B ∞ 0 … ∞… … … … …E ∞ 1 … 0
![Page 16: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/16.jpg)
Graph Representations
• AdjacencyList• IncidenceList• AdjacencyMatrix• IncidenceMatrix
Nodes Edgese1 e2 .. e5
A S N … NB D S … D… … … … …E N N … S
![Page 17: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/17.jpg)
Graph Representations
• AdjacencyListsparsegraphs• IncidenceList• AdjacencyMatrixdensegraphs• IncidenceMatrix
![Page 18: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/18.jpg)
Graphs in FP World
• Structs,interfaces&implementa>ons• Flexiblerepresenta>on• Immutablegraphs
![Page 19: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/19.jpg)
Graphs in Clojure: Graph (defprotocol Graph (nodes [g]) (edges [g]) (has-node? [g node]) (has-edge? [g n1 n2]) (successors [g node]) (out-degree [g node]) (out-edges [g node]))
![Page 20: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/20.jpg)
Graphs in Clojure: Digraph (defprotocol Digraph (predecessors [g node]) (in-degree [g node]) (in-edges [g node]) (transpose [g]))
![Page 21: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/21.jpg)
Graphs in Clojure: WeightedGraph (defprotocol WeightedGraph (weight [g n1 n2]))
![Page 22: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/22.jpg)
Graphs in Clojure: EditableGraph (defprotocol EditableGraph (add-nodes* [g nodes]) (add-edges* [g edges]) (remove-nodes* [g nodes]) (remove-edges* [g edges]) (remove-all [g]))
![Page 23: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/23.jpg)
Graphs in Clojure: Complex Graphs
Howtocreatebasiceditabledigraph?(defrecord BasicEditableDigraph [nodes succs preds])
![Page 24: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/24.jpg)
Graphs in Clojure: Complex Graphs
Howtocreatebasiceditabledigraph?(defrecord BasicEditableDigraph [nodes succs preds])(extend BasicEditableDigraph Graph … Digraph … EditableGraph …)
![Page 25: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/25.jpg)
Graphs in Clojure: Complex Graphs
Howtocreatebasiceditabledigraph?(defrecord BasicEditableDigraph [nodes succs preds])(extend BasicEditableDigraph Graph
)
(nodes [g])(edges [g])(has-node? [g node])(has-edge? [g n1 n2])(successors [g] [g node])(out-degree [g node])
Protocoldefini>on(notvalidcode!)
![Page 26: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/26.jpg)
Graphs in Clojure: Complex Graphs
Howtocreatebasiceditabledigraph?(defrecord BasicEditableDigraph [nodes succs preds])(extend BasicEditableDigraph Graph default-graph-impl Digraph … EditableGraph …)
![Page 27: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/27.jpg)
Graphs in Clojure: Complex Graphs (def default-graph-impl {:edges (fn [g] (for [n1 (nodes g) e (out-edges g n1)] e)) :nodes (fn [g] ...) ...})
(nodes [g])(edges [g])(has-node? [g node])(has-edge? [g n1 n2])(successors [g] [g node])(out-degree [g node])
![Page 28: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/28.jpg)
Graphs in Clojure: Complex Graphs
Howtocreatebasiceditabledigraph?(defrecord BasicEditableDigraph [nodes succs preds])(extend BasicEditableDigraph Graph default-graph-impl Digraph … EditableGraph …)
![Page 29: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/29.jpg)
Graphs in Clojure: Complex Graphs
Howtocreatebasiceditabledigraph?(defrecord BasicEditableDigraph [nodes succs preds])(extend BasicEditableDigraph Graph default-graph-impl Digraph default-digraph-impl EditableGraph …)
![Page 30: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/30.jpg)
Graphs in Clojure: Complex Graphs
(def default-digraph-impl {:transpose (fn [g] (assoc g :succs (:preds g) :preds (:succs g))) …}) (predecessors [g]
[g node])(in-degree [g node])(transpose [g])
![Page 31: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/31.jpg)
Graphs in Clojure: Complex Graphs
Howtocreatebasiceditabledigraph?(defrecord BasicEditableDigraph [nodes succs preds])(extend BasicEditableDigraph Graph default-graph-impl Digraph default-digraph-impl EditableGraph …)
![Page 32: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/32.jpg)
Graphs in Clojure: Complex Graphs
Howtocreatebasiceditabledigraph?(defrecord BasicEditableDigraph [nodes succs preds])(extend BasicEditableDigraph Graph default-graph-impl Digraph default-digraph-impl EditableGraph editable-graph-impl)
![Page 33: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/33.jpg)
Graphs in FP World
• Structs,interfaces&implementa>ons• Flexiblerepresenta>on• Immutablegraphs
![Page 34: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/34.jpg)
In this Talk
AMomentofGraphTheory GraphAPI
Design Func>onalGraph
AlgorithmsLoom
Overview
![Page 35: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/35.jpg)
Functional Graph Algorithms: Bellman-Ford
![Page 36: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/36.jpg)
Functional Graph Algorithms: Bellman-Ford
CLRS Introduction to Algorithms
![Page 37: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/37.jpg)
Functional Graph Algorithms: Bellman-Ford
CLRS Introduction to Algorithms
![Page 38: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/38.jpg)
Functional Graph Algorithms: Bellman-Ford
(defn- init-estimates [graph start] (let [nodes (disj (nodes graph) start) costs (interleave nodes(repeat ∞)) paths (interleave nodes (repeat nil))] [(apply assoc {start 0} costs) (apply assoc {start nil} paths)]))
![Page 39: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/39.jpg)
Functional Graph Algorithms: Bellman-Ford
![Page 40: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/40.jpg)
Functional Graph Algorithms: Bellman-Ford
![Page 41: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/41.jpg)
Functional Graph Algorithms: Bellman-Ford
(defn- can-relax-edge? [[u v :as edge] edge-cost costs] (let [vd (get costs v) ud (get costs u) sum (+ ud edge-cost)] (> vd sum)))
![Page 42: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/42.jpg)
Functional Graph Algorithms: Bellman-Ford
(defn- relax-edge [[u v :as edge] uvcost [costs paths :as estimates]] (let [ud (get costs u) sum (+ ud uvcost)] (if (can-relax-edge? edge uvcost costs) [(assoc costs v sum) (assoc paths v u)] estimates)))
![Page 43: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/43.jpg)
Functional Graph Algorithms: Bellman-Ford
![Page 44: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/44.jpg)
Functional Graph Algorithms: Bellman-Ford
(defn- relax-edges [g start estimates] (reduce (fn [estimates [u v :as edge]] (relax-edge edge (weight g u v)
estimates)) estimates (edges g)))
![Page 45: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/45.jpg)
Functional Graph Algorithms: Bellman-Ford
(defn bellman-ford [g start]
(let [initial-estimates (init-estimates g start) ;relax-edges is calculated for all edges V-1 times
[costs paths] (reduce (fn [estimates _] (relax-edges g start estimates)) initial-estimates
(-> g nodes count dec range)) edges (edges g)]
(if (some (fn [[u v :as edge]] (can-relax-edge? edge (wt g u v) costs)) edges)
false [costs
(->> (keys paths) ;remove vertices that are unreachable from source
(remove #(= Double/POSITIVE_INFINITY (get costs %))) (reduce
(fn [final-paths v] (assoc final-paths v
; follows the parent pointers ; to construct path from source to node v
(loop [node v path ()]
(if node (recur (get paths node) (cons node path))
path)))) {}))])))
![Page 46: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/46.jpg)
![Page 47: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/47.jpg)
• Func>oncomposi>on• Func>onsoperateonhelperdatastructures• Higherorderfunc>ons• reducetoiterateoverelements
![Page 48: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/48.jpg)
![Page 49: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/49.jpg)
Functional Graph Algorithms: Bellman-Ford
(defn bellman-ford [g start] (let [initial-estimates (init-estimates g start) ;relax-edges is calculated for all edges V-1 times [costs paths] (reduce (fn [estimates _] (relax-edges g start estimates)) initial-estimates (-> g nodes count dec range)) edges (edges g)]
![Page 50: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/50.jpg)
Functional Graph Algorithms: Bellman-Ford
(if (some (fn [[u v :as edge]] (can-relax-edge? edge
(weight g u v) costs))
edges) false
;;; return paths)))
![Page 51: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/51.jpg)
Functional Graph Algorithms
• Everythingisafunc>on• Func>onsoninternalrepresenta>on• Everyfunc>onreturnsnewrepresenta>on
![Page 52: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/52.jpg)
In this Talk
AMomentofGraphTheory GraphAPI
Design Func>onalGraph
AlgorithmsLoom
Overview
![Page 53: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/53.jpg)
GraphAlgorithms+Visualiza>on
github.com/aysylu/loom
![Page 54: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/54.jpg)
Loom Overview: Supported Graphs
• UndirectedGraph• Digraph• WeightedGraph
• Mul>graph
• FlyGraph• nodes+successorsedges
• successors+startnodes+edges
![Page 55: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/55.jpg)
Loom Overview: Graph Algorithms
• DFS/BFS(+bidirec>onal)
![Page 56: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/56.jpg)
Loom Overview: Graph Algorithms
• DFS/BFS(+bidirec>onal)• TopologicalSort
![Page 57: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/57.jpg)
Loom Overview: Graph Algorithms
• DFS/BFS(+bidirec>onal)• TopologicalSort• ShortestPath(Dijkstra,Bellman-Ford,A*,Johnson’s)
![Page 58: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/58.jpg)
Loom Overview: Graph Algorithms
• DFS/BFS(+bidirec>onal)• TopologicalSort• ShortestPath(Dijkstra,Bellman-Ford,A*,Johnson’s)
• Cliques&SCC(Bron-Kerbosch,Kosaraju)
![Page 59: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/59.jpg)
Loom Overview: Graph Algorithms
• DFS/BFS(+bidirec>onal)• TopologicalSort• ShortestPath(Dijkstra,Bellman-Ford,A*,Johnson’s)
• Cliques&SCC(Bron-Kerbosch,Kosaraju)• Density(edges/nodes)• LonerNodes
![Page 60: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/60.jpg)
Loom Overview: Graph Algorithms
• DFS/BFS(+bidirec>onal)• TopologicalSort• ShortestPath(Dijkstra,Bellman-Ford,A*,Johnson’s)
• Cliques&SCC(Bron-Kerbosch,Kosaraju)• Density(edges/nodes)• LonerNodes• Greedy&TwoColoring
![Page 61: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/61.jpg)
Loom Overview: Graph Algorithms
• DFS/BFS(+bidirec>onal)• TopologicalSort• ShortestPath(Dijkstra,Bellman-Ford,A*,Johnson’s)
• Cliques&SCC(Bron-Kerbosch,Kosaraju)• Density(edges/nodes)• LonerNodes• Greedy&TwoColoring• Max-Flow(Edmonds-Karp)
![Page 62: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/62.jpg)
Functional Graph Algorithms: loom.alg-generic
• Noknowledgeofgraphrepresenta>on• Requiresonlysuccessors
+startforDFS/BFS,topologicalsort,Dijkstra+endforBFSpath
![Page 63: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/63.jpg)
Loom Overview: Visualizing Graphs
![Page 64: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/64.jpg)
![Page 65: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/65.jpg)
WRAPUP:FUNCTIONALGRAPHS
![Page 66: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/66.jpg)
Func>onalDataStructures
![Page 67: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/67.jpg)
DATA&FUNCTIONSSEPARATEDTakeaway#1
![Page 68: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/68.jpg)
FLEXIBLEREPRESENTATIONTakeaway#2
![Page 69: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/69.jpg)
IMMUTABLEGRAPHSTakeaway#3
![Page 70: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/70.jpg)
USELOOMTakeaway#4
![Page 71: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/71.jpg)
https://adambard.com/blog/building-a-suggestion-engine-for-redditlater/
![Page 72: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/72.jpg)
ThankstoLoomcontributors• JohnSzakmeister• RobertLachlan• StephenKocken>edt• AlexEngelberg• KevinDowney• AaronBrooks• MarkEngelberg• DanielShapero• DanielGregoire• TomHancock• HorstDuchêne• AshtonKemerling• Jus>nKramer
• JonyHudson• GuruDevanla• JoshuaDavey• SungPae• FrançoisRey• GeorgiShopov• MaqRevelle• ZackMaril• DanielCompton• ReidDMcKenzie• ZTO• AysyluGreenberg• PaulSnyder
![Page 73: Func’onal Graphs in Clojure · 2018. 6. 20. · Graphs in Clojure: Complex Graphs How to create basic editable digraph? (defrecord BasicEditableDigraph [nodes succs preds]) (extend](https://reader033.vdocuments.mx/reader033/viewer/2022060602/60570ccc3f36a8255a774c32/html5/thumbnails/73.jpg)
&Func'onalGraphs
inClojure
AysyluGreenberg@aysylu22