-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimpl-graphs.html
681 lines (612 loc) · 70.9 KB
/
impl-graphs.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<!-- 2022-W11-4 02:13 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Implementing Graphs</title>
<meta name="author" content="Inanna" />
<meta name="description" content="Implementing simple graphs in Clojure, along with some code to output representations." />
<meta name="generator" content="Org Mode" />
<link href="/site.css" rel="stylesheet" type="text/css" /><link href="images/website-icon.png" rel="icon" />
</head>
<body>
<div id="preamble" class="status">
<div><a href="/index.html"><img alt="An abstract logo representing a series of three assembly line stamping machines with the words CONS, DEV, and embalzoned in white on each machine." id="site-logo" src="/images/website-logo.png" /></a></div>
</div>
<div id="content" class="content">
<h1 class="title">Implementing Graphs</h1>
<div id="table-of-contents" role="doc-toc">
<h2>Table of Contents</h2>
<div id="text-table-of-contents" role="doc-toc">
<ul>
<li><a href="#org2ef1c22">Introduction</a></li>
<li><a href="#org72bc7a4">Implementation</a>
<ul>
<li><a href="#org8c3b9cd">Common Features</a>
<ul>
<li><a href="#org25190fe">Killing Orphans</a></li>
<li><a href="#org1d81179">Killing Nodes</a></li>
<li><a href="#org9aaaef3">Killing Edges</a></li>
<li><a href="#org5f556ad">Creating Edges</a></li>
<li><a href="#org9fe54af">Creating Nodes</a></li>
<li><a href="#orge54089a">Getting Nodes</a></li>
</ul>
</li>
<li><a href="#orgd356a56">Unweighted Graphs</a>
<ul>
<li><a href="#orgc90be2c">BasicGraph</a></li>
<li><a href="#orgbe09b0e">Undirected Graphs</a></li>
<li><a href="#org3c32186">Directed Graphs</a></li>
</ul>
</li>
<li><a href="#orga3b840c">Weighted Graphs</a>
<ul>
<li><a href="#orge360acc">BasicWeightedGraph Protocol</a></li>
<li><a href="#org22906aa">Undirected Graph</a></li>
<li><a href="#orgf7fba76">Weighted Directed Graph</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
</div>
<div id="outline-container-org2ef1c22" class="outline-2">
<h2 id="org2ef1c22">Introduction</h2>
<div class="outline-text-2" id="text-org2ef1c22">
<p>
<a href="https://en.wikipedia.org/wiki/Graph_%28discrete_mathematics%29">Graphs</a> are a fairly common mathematical structure that represents a series of nodes with edges that connect them to other nodes. Here we will implement some code that implements graphs as well as code to produce representations of graphs. <label class="sidenote-number" for="1"><sup>1</sup></label><input checked="checked" id="1" style="display:none" type="checkbox" /><span class="sidenote"><span class="sidenote-number"> 1</span> Which I think I will extend to representing arbitrary Clojure code in general</span> The code was mostly developed for my <a href="travling-salesman-trolley-problem.html#ID-1e544a78-076c-4448-8af5-45f5d317fadf">possibly correct solution to the travling saleseman trolly problem</a>, so check that out as well.
</p>
</div>
</div>
<div id="outline-container-org72bc7a4" class="outline-2">
<h2 id="org72bc7a4">Implementation</h2>
<div class="outline-text-2" id="text-org72bc7a4">
<p>
So we want to define a graph. But first, let's ask the question, what <i>is</i> a graph? How do graphs work?
</p>
<p>
A graph is simply a series of points called nodes that have links between them called edges. Edges in the most basic form of graph are undirected and unweighted, which is usually interpreted <label class="sidenote-number" for="2"><sup>2</sup></label><input checked="checked" id="2" style="display:none" type="checkbox" /><span class="sidenote"><span class="sidenote-number"> 2</span> As with any abstract structure it is an interpretation, and many others exist. The rules in math are made up anyways, so feel free to add some more!</span> as them either costing nothing to traverse (or providing nothing) and being possible to traverse in either direction.
</p>
<p>
The weighted case is the more interesting one, in which many of the most exciting optimization problems and algorithms in computer science exist, form the traveling salesman problem to Dijkstra's algorithm for the shortest path.
</p>
<p>
The directed case is even more complicated, with each edge serving as a one-way path to another edge. This also has a ton of really exciting applications in the analysis of program states and category theory where the edges are called arrows and map morphisms between categories.
</p>
<p>
Now, of course, the obvious approach would be to create nodes as little objects with pointers to other nodes as edges. However, I'm not doing that<label class="sidenote-number" for="3"><sup>3</sup></label><input checked="checked" id="3" style="display:none" type="checkbox" /><span class="sidenote"><span class="sidenote-number"> 3</span> Searching piles of pointers is irritating no matter how you do it and tends to make any operations a lot messier. It's also, in this case, much slower as you can't just pick any node as easily.</span> and instead I will be implementing the graph as a map of nodes that sets of edges indicating where they go to and where they go from.
</p>
<p>
So a graph might have this sort of structure. The <code>edges-to</code> set is the set of edges that are leaving the node, and <code>edges from</code> are the set of edges that are heading to the node itself.
</p>
<div class="org-src-container">
<pre class="src src-clojure"><span style="color: #494949;">{</span><span style="color: #E53935;">:foo</span> <span style="color: #bbb;">{</span><span style="color: #E53935;">:edges-to</span> #<span style="color: #494949;">{</span><span style="color: #E53935;">:bar</span> <span style="color: #E53935;">:baz</span><span style="color: #494949;">}</span>
<span style="color: #E53935;">:edges-from</span> #<span style="color: #494949;">{</span><span style="color: #E53935;">:bar</span> <span style="color: #E53935;">:baz</span><span style="color: #494949;">}</span><span style="color: #bbb;">}</span>
<span style="color: #E53935;">:bar</span> <span style="color: #bbb;">{</span><span style="color: #E53935;">:edges-to</span> #<span style="color: #494949;">{</span><span style="color: #E53935;">:foo</span><span style="color: #494949;">}</span>
<span style="color: #E53935;">:edges-from</span> #<span style="color: #494949;">{</span><span style="color: #E53935;">:foo</span><span style="color: #494949;">}</span><span style="color: #bbb;">}</span>
<span style="color: #E53935;">:baz</span> <span style="color: #bbb;">{</span><span style="color: #E53935;">:edges-to</span> #<span style="color: #494949;">{</span><span style="color: #E53935;">:foo</span><span style="color: #494949;">}</span>
<span style="color: #E53935;">:edges-from</span> #<span style="color: #494949;">{</span><span style="color: #E53935;">:foo</span><span style="color: #494949;">}</span><span style="color: #bbb;">}</span><span style="color: #494949;">}</span>
</pre>
</div>
<p>
So with the structure out of the way, let's start defining things.
</p>
</div>
<div id="outline-container-org8c3b9cd" class="outline-3">
<h3 id="org8c3b9cd">Common Features</h3>
<div class="outline-text-3" id="text-org8c3b9cd">
<p>
All our graphs will use a collection of standard edges and nodes. In fact the structure of the graph will also be exactly the same too! The only differences will be in interpretation and how the various methods act.
</p>
<div class="org-src-container">
<pre class="src src-clojure"><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">defrecord</span> <span style="color: #E53935;">Edge</span> <span style="color: #bbb;">[</span>src dest weight<span style="color: #bbb;">]</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">defrecord</span> <span style="color: #E53935;">Node</span> <span style="color: #bbb;">[</span>edges-to edges-from<span style="color: #bbb;">]</span><span style="color: #494949;">)</span>
</pre>
</div>
<p>
Unfortunately (<i>and</i> fortunately) Clojure doesn't have multiple inheritance, so you have to define the inherited functions with separate functions and then call these functions from your new thing. Alternatively you can use multimethods, but I don't like those so here we just define a ton of functions.
</p>
</div>
<div id="outline-container-org25190fe" class="outline-4">
<h4 id="org25190fe">Killing Orphans</h4>
<div class="outline-text-4" id="text-org25190fe">
<p>
We also want to be assured that we can kill any orphans that exist in the graph. Since they have no support one might imagine that they will naturally be garbage collected, but it turns out that we must go and do it ourselves.
</p>
<div class="org-src-container">
<pre class="src src-clojure"><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">defn-</span> <span style="font-weight: bold; font-style: italic;">kill-orphans-impl</span> <span style="color: #bbb;">[</span>graph graph-ctor<span style="color: #bbb;">]</span>
<span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">->></span> graph
<span style="color: #E53935;">:node-syms</span>
<span style="color: #494949;">(</span>remove #<span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">and</span> <span style="color: #494949;">(</span>empty? <span style="color: #bbb;">(</span>get <span style="font-style: italic;">%</span> <span style="color: #E53935;">:edges-to</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span> <span style="color: #494949;">(</span>empty? <span style="color: #bbb;">(</span>get <span style="font-style: italic;">%</span> <span style="color: #E53935;">:edges-from</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span> <span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span>apply concat<span style="color: #494949;">)</span>
<span style="color: #494949;">(</span>apply hash-map<span style="color: #494949;">)</span>
graph-ctor<span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org1d81179" class="outline-4">
<h4 id="org1d81179">Killing Nodes</h4>
<div class="outline-text-4" id="text-org1d81179">
<p>
Killing nodes is also something that can be done in all cases through this function. It kills all edges to the node and then kills the node itself.
</p>
<p>
Unfortunately first we have to define a dummy-function so that it can compile before we have a function to kill edges for that specific case.
</p>
<div class="org-src-container">
<pre class="src src-clojure"><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">defn-</span> <span style="font-weight: bold; font-style: italic;">kill-edge</span> <span style="color: #bbb;">[</span>graph from to<span style="color: #bbb;">]</span> <span style="color: #E53935;">nil</span><span style="color: #494949;">)</span>
</pre>
</div>
<p>
Then we define the function itself.
</p>
<div class="org-src-container">
<pre class="src src-clojure"><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">defn-</span> <span style="font-weight: bold; font-style: italic;">kill-node-impl</span> <span style="color: #bbb;">[</span>graph node-sym<span style="color: #bbb;">]</span>
<span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">as-></span> graph g
<span style="color: #494949;">(</span>reduce #<span style="color: #bbb;">(</span>kill-edge <span style="font-style: italic;">%1</span> <span style="font-style: italic;">%2</span> node-sym<span style="color: #bbb;">)</span> g
<span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">->></span> <span style="color: #494949;">(</span>get-in graph <span style="color: #bbb;">[</span><span style="color: #E53935;">:nodes</span> node-sym <span style="color: #E53935;">:edges-from</span><span style="color: #bbb;">]</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span>map #<span style="color: #bbb;">(</span><span style="color: #E53935;">:src</span> <span style="font-style: italic;">%</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span>reduce #<span style="color: #bbb;">(</span>kill-edge <span style="font-style: italic;">%1</span> node-sym <span style="font-style: italic;">%2</span><span style="color: #bbb;">)</span> g
<span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">->></span> <span style="color: #494949;">(</span>get-in graph <span style="color: #bbb;">[</span><span style="color: #E53935;">:nodes</span> node-sym <span style="color: #E53935;">:edges-to</span><span style="color: #bbb;">]</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span>map #<span style="color: #bbb;">(</span><span style="color: #E53935;">:dest</span> <span style="font-style: italic;">%</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span><span style="color: #E53935;">:nodes</span> g<span style="color: #494949;">)</span>
<span style="color: #494949;">(</span>dissoc g node-sym<span style="color: #494949;">)</span>
<span style="color: #494949;">(</span>assoc graph <span style="color: #E53935;">:nodes</span> g<span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org9aaaef3" class="outline-4">
<h4 id="org9aaaef3">Killing Edges</h4>
<div class="outline-text-4" id="text-org9aaaef3">
<p>
We also define a simple function too kill edges as well. This will have to be done twice for the undirected graph case, but can be trivially wrapped in the undirected graph case.
</p>
<div class="org-src-container">
<pre class="src src-clojure"><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">defn-</span> <span style="font-weight: bold; font-style: italic;">kill-edge-impl</span> <span style="color: #bbb;">[</span>graph from to<span style="color: #bbb;">]</span>
<span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">let</span> <span style="color: #494949;">[</span>from->to <span style="color: #bbb;">(</span>->Edge from to 0<span style="color: #bbb;">)</span><span style="color: #494949;">]</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">-></span> graph
<span style="color: #bbb;">(</span>update-in <span style="color: #494949;">[</span><span style="color: #E53935;">:nodes</span> from <span style="color: #E53935;">:edges-to</span><span style="color: #494949;">]</span> remove #<span style="color: #494949;">{</span>from->to<span style="color: #494949;">}</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>update-in <span style="color: #494949;">[</span><span style="color: #E53935;">:nodes</span> to <span style="color: #E53935;">:edges-from</span><span style="color: #494949;">]</span> remove #<span style="color: #494949;">{</span>from->to<span style="color: #494949;">}</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org5f556ad" class="outline-4">
<h4 id="org5f556ad">Creating Edges</h4>
<div class="outline-text-4" id="text-org5f556ad">
<p>
We can also define a function to create an edge given a source and a destination. This is useful as even in the directed graph case it will call this function.
</p>
<div class="org-src-container">
<pre class="src src-clojure"><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">defn-</span> <span style="font-weight: bold; font-style: italic;">add-edge-impl</span> <span style="color: #bbb;">[</span>graph from to weight<span style="color: #bbb;">]</span>
<span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">let</span> <span style="color: #494949;">[</span>from->to <span style="color: #bbb;">(</span>->Edge from to weight<span style="color: #bbb;">)</span><span style="color: #494949;">]</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">-></span> graph
<span style="color: #bbb;">(</span>update-in <span style="color: #494949;">[</span><span style="color: #E53935;">:nodes</span> from <span style="color: #E53935;">:edges-to</span><span style="color: #494949;">]</span> conj from->to<span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>update-in <span style="color: #494949;">[</span><span style="color: #E53935;">:nodes</span> to <span style="color: #E53935;">:edges-from</span><span style="color: #494949;">]</span> conj from->to<span style="color: #bbb;">)</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org9fe54af" class="outline-4">
<h4 id="org9fe54af">Creating Nodes</h4>
<div class="outline-text-4" id="text-org9fe54af">
<p>
Adding nodes is also trivial, with this adding a node with no name.
</p>
<div class="org-src-container">
<pre class="src src-clojure"><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">defn-</span> <span style="font-weight: bold; font-style: italic;">add-node-impl</span> <span style="color: #bbb;">[</span>graph node-sym<span style="color: #bbb;">]</span>
<span style="color: #bbb;">(</span>assoc-in graph <span style="color: #494949;">[</span><span style="color: #E53935;">:nodes</span> node-sym<span style="color: #494949;">]</span> <span style="color: #494949;">(</span>->Node #<span style="color: #bbb;">{}</span> #<span style="color: #bbb;">{}</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-orge54089a" class="outline-4">
<h4 id="orge54089a">Getting Nodes</h4>
<div class="outline-text-4" id="text-orge54089a">
<p>
To get node you just grab it from the map of nodes the graph has. Easy!
</p>
<div class="org-src-container">
<pre class="src src-clojure"><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">defn-</span> <span style="font-weight: bold; font-style: italic;">get-node-impl</span> <span style="color: #bbb;">[</span>graph node-sym<span style="color: #bbb;">]</span>
<span style="color: #bbb;">(</span>get-in graph <span style="color: #494949;">[</span><span style="color: #E53935;">:nodes</span> node-sym<span style="color: #494949;">]</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
</pre>
</div>
</div>
</div>
</div>
<div id="outline-container-orgd356a56" class="outline-3">
<h3 id="orgd356a56">Unweighted Graphs</h3>
<div class="outline-text-3" id="text-orgd356a56">
<p>
The most basic case is the unweighted class, which we implement below.
</p>
</div>
<div id="outline-container-orgc90be2c" class="outline-4">
<h4 id="orgc90be2c">BasicGraph</h4>
<div class="outline-text-4" id="text-orgc90be2c">
<p>
We define a protocol for basic, undirected graphs. To avoid name collisions we call the protocol <code>BasicGraph</code>. It defines the various functions we can expect to call on any graph.
</p>
<div class="org-src-container">
<pre class="src src-clojure"><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">defprotocol</span> <span style="color: #E53935;">BasicGraph</span>
<span style="color: #a4a4a4; font-style: italic;">"A protocol defining a weighted or unweighted graph."</span>
<span style="color: #bbb;">(</span>edge <span style="color: #494949;">[</span>graph from to<span style="color: #494949;">]</span>
<span style="color: #494949;">"Add an edge to the graph."</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>node <span style="color: #494949;">[</span>graph node-sym<span style="color: #494949;">]</span>
<span style="color: #494949;">"Add a node to the graph (node must be a unique identifier)."</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>get-node <span style="color: #494949;">[</span>graph node-sym<span style="color: #494949;">]</span>
<span style="color: #494949;">"Get a node from the graph (including edges)."</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>kill-orphans <span style="color: #494949;">[</span>graph<span style="color: #494949;">]</span>
<span style="color: #494949;">"Remove all orphans from the graph."</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>kill-edge <span style="color: #494949;">[</span>graph to from<span style="color: #494949;">]</span>
<span style="color: #494949;">"Kill an edge given it's description."</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>kill-node <span style="color: #494949;">[</span>graph node-sym<span style="color: #494949;">]</span>
<span style="color: #494949;">"Kill a node within the graph."</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>dot-repr <span style="color: #494949;">[</span>graph<span style="color: #494949;">]</span>
<span style="color: #494949;">"Produce DOT code representing the graph"</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-orgbe09b0e" class="outline-4">
<h4 id="orgbe09b0e">Undirected Graphs</h4>
<div class="outline-text-4" id="text-orgbe09b0e">
<p>
An unweighted graph is implemented simply by ignoring the <code>:weight</code> value in the graph. In this graph the distance between nodes does not matter. We can also construct an unweighted, undirected graph which is in description the simplest, but ironically, due to how we defined the nodes and edges above (and set it up) the most complicated here.
</p>
<p>
However, using the functions we defined above we can simplify it quite a bit. Adding an edge, as you can see, is simply
</p>
<div class="org-src-container">
<pre class="src src-clojure"><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">ns</span> <span style="color: #E53935;">graph-demo</span>
<span style="color: #bbb;">(</span><span style="color: #E53935;">:require</span> <span style="color: #494949;">[</span>clojure.string <span style="color: #E53935;">:as</span> str<span style="color: #494949;">]</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">defrecord</span> <span style="color: #E53935;">Graph</span> <span style="color: #bbb;">[</span>nodes<span style="color: #bbb;">]</span>
BasicGraph
<span style="color: #bbb;">(</span>edge <span style="color: #494949;">[</span>graph from to<span style="color: #494949;">]</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">-></span> graph
<span style="color: #bbb;">(</span>add-edge-impl from to 0<span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>add-edge-impl to from 0<span style="color: #bbb;">)</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>node <span style="color: #494949;">[</span>graph node-sym<span style="color: #494949;">]</span> <span style="color: #494949;">(</span>add-node-impl graph node-sym<span style="color: #494949;">)</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>kill-orphans <span style="color: #494949;">[</span>graph<span style="color: #494949;">]</span> <span style="color: #494949;">(</span>kill-orphans-impl graph ->Graph<span style="color: #494949;">)</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>kill-edge <span style="color: #494949;">[</span>graph from to<span style="color: #494949;">]</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">-></span> graph
<span style="color: #bbb;">(</span>kill-edge-impl from to<span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>kill-edge-impl to from<span style="color: #bbb;">)</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>kill-node <span style="color: #494949;">[</span>graph node-sym<span style="color: #494949;">]</span> <span style="color: #494949;">(</span>kill-node-impl graph node-sym<span style="color: #494949;">)</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>get-node <span style="color: #494949;">[</span>graph node-sym<span style="color: #494949;">]</span> <span style="color: #494949;">(</span>get-node-impl graph node-sym<span style="color: #494949;">)</span><span style="color: #bbb;">)</span>
<<graph-dot-repr>><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">defn</span> <span style="font-weight: bold; font-style: italic;">graph</span> <span style="color: #bbb;">[</span>& nodes<span style="color: #bbb;">]</span>
<span style="color: #bbb;">(</span>reduce node <span style="color: #494949;">(</span>->Graph <span style="color: #bbb;">{}</span><span style="color: #494949;">)</span> nodes<span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
</pre>
</div>
<p>
The only really messy bit of code is the DOT representation generator, but even that is fairly simple.
</p>
<div class="org-src-container">
<pre class="src src-clojure" id="org61242e0"><span style="color: #494949;">(</span>dot-repr <span style="color: #bbb;">[</span>graph<span style="color: #bbb;">]</span>
<span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">letfn</span> <span style="color: #494949;">[</span><span style="color: #bbb;">(</span>remove-dups <span style="color: #494949;">[</span><span style="color: #bbb;">[</span>edge & edge-tuples<span style="color: #bbb;">]</span><span style="color: #494949;">]</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">when</span> edge <span style="color: #bbb;">(</span>cons edge <span style="color: #494949;">(</span>remove-dups <span style="color: #bbb;">(</span>remove #<span style="color: #494949;">{</span>edge<span style="color: #494949;">}</span> <span style="color: #494949;">(</span>map reverse edge-tuples<span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">]</span>
<span style="color: #494949;">(</span>str <span style="color: #494949;">"graph G {"</span>
<span style="color: #494949;">"rankdir=</span><span style="color: #494949; font-weight: bold;">\"</span><span style="color: #494949;">LR</span><span style="color: #494949; font-weight: bold;">\"</span><span style="color: #494949;">;"</span>
<span style="color: #494949;">"size=</span><span style="color: #494949; font-weight: bold;">\"</span><span style="color: #494949;">15,7.5!</span><span style="color: #494949; font-weight: bold;">\"</span><span style="color: #494949;">;"</span>
<span style="color: #494949;">"fontpath=</span><span style="color: #494949; font-weight: bold;">\"</span><span style="color: #494949;">~/.local/share/fonts</span><span style="color: #494949; font-weight: bold;">\"</span><span style="color: #494949;">;"</span>
<span style="color: #494949;">"node[shape=record,"</span>
<span style="color: #494949;">"fontname=</span><span style="color: #494949; font-weight: bold;">\"</span><span style="color: #494949;">Jura Bold</span><span style="color: #494949; font-weight: bold;">\"</span><span style="color: #494949;">,"</span>
<span style="color: #494949;">"penwidth=0,"</span>
<span style="color: #494949;">"style=filled,"</span>
<span style="color: #494949;">"fillcolor=</span><span style="color: #494949; font-weight: bold;">\"</span><span style="color: #000000; background-color: #f5f5f5;">#f5f5f5</span><span style="color: #494949; font-weight: bold;">\"</span><span style="color: #494949;">];"</span>
<span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">->></span> graph
<span style="color: #E53935;">:nodes</span>
<span style="color: #494949;">(</span>mapcat <span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">fn</span> <span style="color: #494949;">[</span><span style="color: #bbb;">[</span>name <span style="color: #494949;">{</span><span style="color: #E53935;">:keys</span> <span style="color: #bbb;">[</span>edges-to<span style="color: #bbb;">]</span><span style="color: #494949;">}</span><span style="color: #bbb;">]</span><span style="color: #494949;">]</span>
<span style="color: #494949;">(</span>map <span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">fn</span> <span style="color: #494949;">[</span>e<span style="color: #494949;">]</span> <span style="color: #494949;">[</span>name <span style="color: #bbb;">(</span><span style="color: #E53935;">:dest</span> e<span style="color: #bbb;">)</span><span style="color: #494949;">]</span><span style="color: #bbb;">)</span> edges-to<span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
remove-dups
<span style="color: #494949;">(</span>map #<span style="color: #bbb;">(</span>format <span style="color: #494949;">"</span><span style="color: #494949; font-weight: bold;">\"</span><span style="color: #494949;">%s</span><span style="color: #494949; font-weight: bold;">\"</span><span style="color: #494949;">--</span><span style="color: #494949; font-weight: bold;">\"</span><span style="color: #494949;">%s</span><span style="color: #494949; font-weight: bold;">\"</span><span style="color: #494949;">;"</span> <span style="color: #494949;">(</span>first <span style="font-style: italic;">%</span><span style="color: #494949;">)</span> <span style="color: #494949;">(</span>second <span style="font-style: italic;">%</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
flatten
<span style="color: #E53935;">str</span>/join<span style="color: #bbb;">)</span>
<span style="color: #494949;">"}"</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
</pre>
</div>
<p>
From here we can just use the code and display the graph.
</p>
<div class="org-src-container">
<pre class="src src-clojure" id="orgd6a769d"><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">-></span> <span style="color: #bbb;">(</span>graph <span style="color: #E53935;">:a</span> <span style="color: #E53935;">:b</span> <span style="color: #E53935;">:c</span> <span style="color: #E53935;">:d</span> <span style="color: #E53935;">:e</span> <span style="color: #E53935;">:f</span> <span style="color: #E53935;">:g</span> <span style="color: #E53935;">:h</span> <span style="color: #E53935;">:i</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>edge <span style="color: #E53935;">:a</span> <span style="color: #E53935;">:b</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>edge <span style="color: #E53935;">:c</span> <span style="color: #E53935;">:d</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>edge <span style="color: #E53935;">:g</span> <span style="color: #E53935;">:h</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>edge <span style="color: #E53935;">:b</span> <span style="color: #E53935;">:d</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>edge <span style="color: #E53935;">:g</span> <span style="color: #E53935;">:a</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>edge <span style="color: #E53935;">:a</span> <span style="color: #E53935;">:c</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>edge <span style="color: #E53935;">:h</span> <span style="color: #E53935;">:d</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>dot-repr<span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
</pre>
</div>
<div id="orgb89f2bf" class="figure">
<p><img src="./images/graph-demo_66094608-5e0c-4abc-a82f-e3b1ed172e49.png" alt="graph-demo_66094608-5e0c-4abc-a82f-e3b1ed172e49.png" />
</p>
</div>
</div>
</div>
<div id="outline-container-org3c32186" class="outline-4">
<h4 id="org3c32186">Directed Graphs</h4>
<div class="outline-text-4" id="text-org3c32186">
<p>
So to start out we basically do the same thing as before, though this time we do less of it. Pretty much all the code is reused.
</p>
<div class="org-src-container">
<pre class="src src-clojure"><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">ns</span> <span style="color: #E53935;">graph-demo</span>
<span style="color: #bbb;">(</span><span style="color: #E53935;">:require</span> <span style="color: #494949;">[</span>clojure.string <span style="color: #E53935;">:as</span> str<span style="color: #494949;">]</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">defrecord</span> <span style="color: #E53935;">DiGraph</span> <span style="color: #bbb;">[</span>nodes<span style="color: #bbb;">]</span>
BasicGraph
<span style="color: #bbb;">(</span>edge <span style="color: #494949;">[</span>graph from to<span style="color: #494949;">]</span> <span style="color: #494949;">(</span>add-edge-impl graph from to 0<span style="color: #494949;">)</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>node <span style="color: #494949;">[</span>graph node-sym<span style="color: #494949;">]</span> <span style="color: #494949;">(</span>add-node-impl graph node-sym<span style="color: #494949;">)</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>kill-orphans <span style="color: #494949;">[</span>graph<span style="color: #494949;">]</span> <span style="color: #494949;">(</span>kill-orphans-impl graph ->DiGraph<span style="color: #494949;">)</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>kill-edge <span style="color: #494949;">[</span>graph from to<span style="color: #494949;">]</span> <span style="color: #494949;">(</span>kill-edge-impl graph from to<span style="color: #494949;">)</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>kill-node <span style="color: #494949;">[</span>graph node-sym<span style="color: #494949;">]</span> <span style="color: #494949;">(</span>kill-node-impl graph node-sym<span style="color: #494949;">)</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>get-node <span style="color: #494949;">[</span>graph node-sym<span style="color: #494949;">]</span> <span style="color: #494949;">(</span>get-node-impl graph node-sym<span style="color: #494949;">)</span><span style="color: #bbb;">)</span>
<<digraph-dot-repr>><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">defn</span> <span style="font-weight: bold; font-style: italic;">digraph</span> <span style="color: #bbb;">[</span>& nodes<span style="color: #bbb;">]</span>
<span style="color: #bbb;">(</span>reduce node <span style="color: #494949;">(</span>->DiGraph <span style="color: #bbb;">{}</span><span style="color: #494949;">)</span> nodes<span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
</pre>
</div>
<p>
Like before the only thing we have to really define is the actual DOT representation code and a slight modification to the edge generation code. Pretty much everything is entirely standard.
</p>
<div class="org-src-container">
<pre class="src src-clojure" id="org4ea909f"><span style="color: #494949;">(</span>dot-repr <span style="color: #bbb;">[</span>graph<span style="color: #bbb;">]</span>
<span style="color: #bbb;">(</span>str <span style="color: #494949;">"digraph G {"</span>
<span style="color: #494949;">"rankdir=</span><span style="color: #494949; font-weight: bold;">\"</span><span style="color: #494949;">LR</span><span style="color: #494949; font-weight: bold;">\"</span><span style="color: #494949;">;"</span>
<span style="color: #494949;">"size=</span><span style="color: #494949; font-weight: bold;">\"</span><span style="color: #494949;">15,7.5!</span><span style="color: #494949; font-weight: bold;">\"</span><span style="color: #494949;">;"</span>
<span style="color: #494949;">"fontpath=</span><span style="color: #494949; font-weight: bold;">\"</span><span style="color: #494949;">~/.local/share/fonts</span><span style="color: #494949; font-weight: bold;">\"</span><span style="color: #494949;">;"</span>
<span style="color: #494949;">"node[shape=record,"</span>
<span style="color: #494949;">"fontname=</span><span style="color: #494949; font-weight: bold;">\"</span><span style="color: #494949;">Jura Bold</span><span style="color: #494949; font-weight: bold;">\"</span><span style="color: #494949;">,"</span>
<span style="color: #494949;">"penwidth=0,"</span>
<span style="color: #494949;">"style=filled,"</span>
<span style="color: #494949;">"fillcolor=</span><span style="color: #494949; font-weight: bold;">\"</span><span style="color: #000000; background-color: #f5f5f5;">#f5f5f5</span><span style="color: #494949; font-weight: bold;">\"</span><span style="color: #494949;">];"</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">->></span> graph
<span style="color: #E53935;">:nodes</span>
<span style="color: #bbb;">(</span>mapcat <span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">fn</span> <span style="color: #bbb;">[</span><span style="color: #494949;">[</span>name <span style="color: #bbb;">{</span><span style="color: #E53935;">:keys</span> <span style="color: #494949;">[</span>edges-to<span style="color: #494949;">]</span><span style="color: #bbb;">}</span><span style="color: #494949;">]</span><span style="color: #bbb;">]</span>
<span style="color: #bbb;">(</span>map <span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">fn</span> <span style="color: #bbb;">[</span>e<span style="color: #bbb;">]</span> <span style="color: #bbb;">[</span>name <span style="color: #494949;">(</span><span style="color: #E53935;">:dest</span> e<span style="color: #494949;">)</span><span style="color: #bbb;">]</span><span style="color: #494949;">)</span> edges-to<span style="color: #bbb;">)</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>map #<span style="color: #494949;">(</span>format <span style="color: #494949;">"</span><span style="color: #494949; font-weight: bold;">\"</span><span style="color: #494949;">%s</span><span style="color: #494949; font-weight: bold;">\"</span><span style="color: #494949;">-></span><span style="color: #494949; font-weight: bold;">\"</span><span style="color: #494949;">%s</span><span style="color: #494949; font-weight: bold;">\"</span><span style="color: #494949;">;"</span> <span style="color: #bbb;">(</span>first <span style="font-style: italic;">%</span><span style="color: #bbb;">)</span> <span style="color: #bbb;">(</span>second <span style="font-style: italic;">%</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span>
flatten
<span style="color: #E53935;">str</span>/join<span style="color: #494949;">)</span>
<span style="color: #494949;">"}"</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
</pre>
</div>
<p>
Finally we can see the fruits of our labor and define a graph that we can also visualize.
</p>
<div class="org-src-container">
<pre class="src src-clojure" id="org466cc0a"><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">ns</span> <span style="color: #E53935;">graph-demo</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">-></span> <span style="color: #bbb;">(</span>digraph <span style="color: #E53935;">:a</span> <span style="color: #E53935;">:b</span> <span style="color: #E53935;">:c</span> <span style="color: #E53935;">:d</span> <span style="color: #E53935;">:e</span> <span style="color: #E53935;">:f</span> <span style="color: #E53935;">:g</span> <span style="color: #E53935;">:h</span> <span style="color: #E53935;">:i</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>edge <span style="color: #E53935;">:a</span> <span style="color: #E53935;">:b</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>edge <span style="color: #E53935;">:c</span> <span style="color: #E53935;">:d</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>edge <span style="color: #E53935;">:g</span> <span style="color: #E53935;">:h</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>edge <span style="color: #E53935;">:b</span> <span style="color: #E53935;">:d</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>edge <span style="color: #E53935;">:g</span> <span style="color: #E53935;">:a</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>edge <span style="color: #E53935;">:a</span> <span style="color: #E53935;">:c</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>kill-edge <span style="color: #E53935;">:a</span> <span style="color: #E53935;">:c</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>edge <span style="color: #E53935;">:h</span> <span style="color: #E53935;">:d</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>dot-repr<span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
</pre>
</div>
<div id="org36c77f9" class="figure">
<p><img src="./images/graph-digraph_92bc97e7-deb7-4d5f-a21a-2918d62b0a4f.png" alt="graph-digraph_92bc97e7-deb7-4d5f-a21a-2918d62b0a4f.png" />
</p>
</div>
<p>
Now you may notice that the directed graph case is ultimately a superset of the undirected graph case. While in the case of the undirected graph the nodes pointed at each other, they don't do that here.
</p>
</div>
</div>
</div>
<div id="outline-container-orga3b840c" class="outline-3">
<h3 id="orga3b840c">Weighted Graphs</h3>
<div class="outline-text-3" id="text-orga3b840c">
<p>
Now we get to weighted graphs, something that truly weighs heavily on my conscience. To implement them is pretty trivial and a minor extension of the previous.
</p>
</div>
<div id="outline-container-orge360acc" class="outline-4">
<h4 id="orge360acc">BasicWeightedGraph Protocol</h4>
<div class="outline-text-4" id="text-orge360acc">
<p>
We also define a protocol for weighted graphs, that is to say graphs where the individual nodes have a weight to them. This is an obvious and trivial development on the previous protocol.
</p>
<div class="org-src-container">
<pre class="src src-clojure"><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">defprotocol</span> <span style="color: #E53935;">BasicWeightedGraph</span>
<span style="color: #a4a4a4; font-style: italic;">"A protocol defining a weighted or unweighted graph."</span>
<span style="color: #bbb;">(</span>edge <span style="color: #494949;">[</span>graph from to weight<span style="color: #494949;">]</span>
<span style="color: #494949;">"Add an edge to the graph."</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>node <span style="color: #494949;">[</span>graph node-sym<span style="color: #494949;">]</span>
<span style="color: #494949;">"Add a node to the graph (node must be a unique identifier)."</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>get-node <span style="color: #494949;">[</span>graph node-sym<span style="color: #494949;">]</span>
<span style="color: #494949;">"Get a node from the graph (including edges)."</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>kill-orphans <span style="color: #494949;">[</span>graph<span style="color: #494949;">]</span>
<span style="color: #494949;">"Remove all orphans from the graph."</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>kill-edge <span style="color: #494949;">[</span>graph to from<span style="color: #494949;">]</span>
<span style="color: #494949;">"Kill an edge given it's description."</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>kill-node <span style="color: #494949;">[</span>graph node-sym<span style="color: #494949;">]</span>
<span style="color: #494949;">"Kill a node within the graph."</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>dot-repr <span style="color: #494949;">[</span>graph<span style="color: #494949;">]</span>
<span style="color: #494949;">"Produce DOT code representing the graph"</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org22906aa" class="outline-4">
<h4 id="org22906aa">Undirected Graph</h4>
<div class="outline-text-4" id="text-org22906aa">
<p>
Finally, a weighted graph is defined. Code is reused from the undirected case with only the weighted graph edge and dot-repr being redefined.
</p>
<p>
Unfortunately though,
</p>
<div class="org-src-container">
<pre class="src src-clojure"><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">ns</span> <span style="color: #E53935;">graph-demo</span>
<span style="color: #bbb;">(</span><span style="color: #E53935;">:require</span> <span style="color: #494949;">[</span>clojure.string <span style="color: #E53935;">:as</span> str<span style="color: #494949;">]</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">defrecord</span> <span style="color: #E53935;">WeightedGraph</span> <span style="color: #bbb;">[</span>nodes<span style="color: #bbb;">]</span>
BasicWeightedGraph
<span style="color: #bbb;">(</span>edge <span style="color: #494949;">[</span>graph from to weight<span style="color: #494949;">]</span>
<span style="color: #494949;">(</span>add-edge-impl graph from to weight<span style="color: #494949;">)</span>
<span style="color: #494949;">(</span>add-edge-impl graph to from weight<span style="color: #494949;">)</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>node <span style="color: #494949;">[</span>graph node-sym<span style="color: #494949;">]</span> <span style="color: #494949;">(</span>add-node-impl graph node-sym<span style="color: #494949;">)</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>kill-orphans <span style="color: #494949;">[</span>graph<span style="color: #494949;">]</span> <span style="color: #494949;">(</span>kill-orphans-impl graph ->WeightedGraph<span style="color: #494949;">)</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>kill-edge <span style="color: #494949;">[</span>graph from to<span style="color: #494949;">]</span>
<span style="color: #494949;">(</span>kill-edge-impl graph from to<span style="color: #494949;">)</span>
<span style="color: #494949;">(</span>kill-edge-impl graph to from<span style="color: #494949;">)</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>kill-node <span style="color: #494949;">[</span>graph node-sym<span style="color: #494949;">]</span> <span style="color: #494949;">(</span>kill-node-impl graph node-sym<span style="color: #494949;">)</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>get-node <span style="color: #494949;">[</span>graph node-sym<span style="color: #494949;">]</span> <span style="color: #494949;">(</span>get-node-impl graph node-sym<span style="color: #494949;">)</span><span style="color: #bbb;">)</span>
<<weighted-graph-dot-repr>><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">defn</span> <span style="font-weight: bold; font-style: italic;">weighted-graph</span> <span style="color: #bbb;">[</span>& nodes<span style="color: #bbb;">]</span>
<span style="color: #bbb;">(</span>reduce node <span style="color: #494949;">(</span>->WeightedGraph <span style="color: #bbb;">{}</span><span style="color: #494949;">)</span> nodes<span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
</pre>
</div>
<p>
The production of a weighted DOT representation is an extra challenge due to the duplication of the edges. However, by reversing only the first two we can effectively remove the edges.
</p>
<div class="org-src-container">
<pre class="src src-clojure" id="org9869dd2"><span style="color: #494949;">(</span>dot-repr <span style="color: #bbb;">[</span>graph<span style="color: #bbb;">]</span>
<span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">letfn</span> <span style="color: #494949;">[</span><span style="color: #bbb;">(</span>remove-dups <span style="color: #494949;">[</span><span style="color: #bbb;">[</span>edge & edge-tuples<span style="color: #bbb;">]</span><span style="color: #494949;">]</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">when</span> edge
<span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">->></span> edge-tuples
<span style="color: #494949;">(</span>map #<span style="color: #bbb;">(</span>concat <span style="color: #494949;">(</span>reverse
<span style="color: #bbb;">(</span>take 2 <span style="font-style: italic;">%</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
<span style="color: #494949;">[</span><span style="color: #bbb;">(</span>last <span style="font-style: italic;">%</span><span style="color: #bbb;">)</span><span style="color: #494949;">]</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span>remove #<span style="color: #bbb;">{</span>edge<span style="color: #bbb;">}</span><span style="color: #494949;">)</span>
remove-dups
<span style="color: #494949;">(</span>cons edge<span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">]</span>
<span style="color: #494949;">(</span>str <span style="color: #494949;">"graph G {"</span>
<span style="color: #494949;">"rankdir=</span><span style="color: #494949; font-weight: bold;">\"</span><span style="color: #494949;">LR</span><span style="color: #494949; font-weight: bold;">\"</span><span style="color: #494949;">;"</span>
<span style="color: #494949;">"size=</span><span style="color: #494949; font-weight: bold;">\"</span><span style="color: #494949;">15,7.5!</span><span style="color: #494949; font-weight: bold;">\"</span><span style="color: #494949;">;"</span>
<span style="color: #494949;">"fontpath=</span><span style="color: #494949; font-weight: bold;">\"</span><span style="color: #494949;">~/.local/share/fonts</span><span style="color: #494949; font-weight: bold;">\"</span><span style="color: #494949;">;"</span>
<span style="color: #494949;">"edge[fontname=</span><span style="color: #494949; font-weight: bold;">\"</span><span style="color: #494949;">Jura Bold</span><span style="color: #494949; font-weight: bold;">\"</span><span style="color: #494949;">]""node[shape=record,"</span>
<span style="color: #494949;">"fontname=</span><span style="color: #494949; font-weight: bold;">\"</span><span style="color: #494949;">Jura Bold</span><span style="color: #494949; font-weight: bold;">\"</span><span style="color: #494949;">,"</span>
<span style="color: #494949;">"penwidth=0,"</span>
<span style="color: #494949;">"style=filled,"</span>
<span style="color: #494949;">"fillcolor=</span><span style="color: #494949; font-weight: bold;">\"</span><span style="color: #000000; background-color: #f5f5f5;">#f5f5f5</span><span style="color: #494949; font-weight: bold;">\"</span><span style="color: #494949;">];"</span>
<span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">->></span> graph
<span style="color: #E53935;">:nodes</span>
<span style="color: #494949;">(</span>mapcat <span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">fn</span> <span style="color: #494949;">[</span><span style="color: #bbb;">[</span>name <span style="color: #494949;">{</span><span style="color: #E53935;">:keys</span> <span style="color: #bbb;">[</span>edges-to<span style="color: #bbb;">]</span><span style="color: #494949;">}</span><span style="color: #bbb;">]</span><span style="color: #494949;">]</span>
<span style="color: #494949;">(</span>map <span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">fn</span> <span style="color: #494949;">[</span><span style="color: #bbb;">{</span><span style="color: #E53935;">:keys</span> <span style="color: #494949;">[</span>src dest weight<span style="color: #494949;">]</span><span style="color: #bbb;">}</span><span style="color: #494949;">]</span> <span style="color: #494949;">[</span>src dest weight<span style="color: #494949;">]</span><span style="color: #bbb;">)</span> edges-to<span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
remove-dups
<span style="color: #494949;">(</span>map <span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">fn</span> <span style="color: #494949;">[</span><span style="color: #bbb;">[</span>name dest weight<span style="color: #bbb;">]</span><span style="color: #494949;">]</span> <span style="color: #494949;">(</span>format <span style="color: #494949;">"</span><span style="color: #494949; font-weight: bold;">\"</span><span style="color: #494949;">%s</span><span style="color: #494949; font-weight: bold;">\"</span><span style="color: #494949;">--</span><span style="color: #494949; font-weight: bold;">\"</span><span style="color: #494949;">%s</span><span style="color: #494949; font-weight: bold;">\"</span><span style="color: #494949;">[label=</span><span style="color: #494949; font-weight: bold;">\"</span><span style="color: #494949;">%s</span><span style="color: #494949; font-weight: bold;">\"</span><span style="color: #494949;">];"</span> name dest weight<span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
flatten
<span style="color: #E53935;">str</span>/join<span style="color: #bbb;">)</span>
<span style="color: #494949;">"}"</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
</pre>
</div>
<p>
Now just to visualize it.
</p>
<div class="org-src-container">
<pre class="src src-clojure" id="org6892d65"><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">ns</span> <span style="color: #E53935;">graph-demo</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">-></span> <span style="color: #bbb;">(</span>weighted-graph <span style="color: #E53935;">:a</span> <span style="color: #E53935;">:b</span> <span style="color: #E53935;">:c</span> <span style="color: #E53935;">:d</span> <span style="color: #E53935;">:e</span> <span style="color: #E53935;">:f</span> <span style="color: #E53935;">:g</span> <span style="color: #E53935;">:h</span> <span style="color: #E53935;">:i</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>edge <span style="color: #E53935;">:a</span> <span style="color: #E53935;">:b</span> 3<span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>edge <span style="color: #E53935;">:c</span> <span style="color: #E53935;">:d</span> 6<span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>edge <span style="color: #E53935;">:g</span> <span style="color: #E53935;">:h</span> 9<span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>edge <span style="color: #E53935;">:b</span> <span style="color: #E53935;">:d</span> 5<span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>edge <span style="color: #E53935;">:g</span> <span style="color: #E53935;">:a</span> 3<span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>edge <span style="color: #E53935;">:a</span> <span style="color: #E53935;">:h</span> 42<span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>edge <span style="color: #E53935;">:h</span> <span style="color: #E53935;">:d</span> 3<span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>dot-repr<span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
</pre>
</div>
<div id="orgfda4796" class="figure">
<p><img src="./images/graph-weighted-digraph_92bc97e7-deb7-4d5f-a21a-2918d62b0a4f.png" alt="graph-weighted-digraph_92bc97e7-deb7-4d5f-a21a-2918d62b0a4f.png" />
</p>
</div>
</div>
</div>
<div id="outline-container-orgf7fba76" class="outline-4">
<h4 id="orgf7fba76">Weighted Directed Graph</h4>
<div class="outline-text-4" id="text-orgf7fba76">
<p>
Now, onto the weighted directed graph comrades. Can you imagine what it contains? You guessed it right! A graph that is weighted <i>and</i> directed. Shocking I know. You will also be (not) surprised to learn that the implementation of the weighted directed graph is fundamentally the same as the regular digraph, just with the addition of weights. Truly a revelation.
</p>
<div class="org-src-container">
<pre class="src src-clojure"><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">ns</span> <span style="color: #E53935;">graph-demo</span>
<span style="color: #bbb;">(</span><span style="color: #E53935;">:require</span> <span style="color: #494949;">[</span>clojure.string <span style="color: #E53935;">:as</span> str<span style="color: #494949;">]</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">defrecord</span> <span style="color: #E53935;">WeightedDiGraph</span> <span style="color: #bbb;">[</span>nodes<span style="color: #bbb;">]</span>
BasicGraph
<span style="color: #bbb;">(</span>edge <span style="color: #494949;">[</span>graph from to weight<span style="color: #494949;">]</span> <span style="color: #494949;">(</span>add-edge-impl graph from to weight<span style="color: #494949;">)</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>node <span style="color: #494949;">[</span>graph node-sym<span style="color: #494949;">]</span> <span style="color: #494949;">(</span>add-node-impl graph node-sym<span style="color: #494949;">)</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>kill-orphans <span style="color: #494949;">[</span>graph<span style="color: #494949;">]</span> <span style="color: #494949;">(</span>kill-orphans-impl graph ->WeightedDiGraph<span style="color: #494949;">)</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>kill-edge <span style="color: #494949;">[</span>graph from to<span style="color: #494949;">]</span> <span style="color: #494949;">(</span>kill-edge-impl graph from to<span style="color: #494949;">)</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>kill-node <span style="color: #494949;">[</span>graph node-sym<span style="color: #494949;">]</span> <span style="color: #494949;">(</span>kill-node-impl graph node-sym<span style="color: #494949;">)</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>get-node <span style="color: #494949;">[</span>graph node-sym<span style="color: #494949;">]</span> <span style="color: #494949;">(</span>get-node-impl graph node-sym<span style="color: #494949;">)</span>
<<weighted-digraph-dot-repr>><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">defn</span> <span style="font-weight: bold; font-style: italic;">weighted-digraph</span> <span style="color: #494949;">[</span>& nodes<span style="color: #494949;">]</span>
<span style="color: #494949;">(</span>reduce node <span style="color: #bbb;">(</span>->WeightedDiGraph <span style="color: #494949;">{}</span><span style="color: #bbb;">)</span> nodes<span style="color: #494949;">)</span><span style="color: #bbb;">)</span>
</pre>
</div>
<p>
We also modify our dot representation code to also add the weight to it, just like with the code above.
</p>
<div class="org-src-container">
<pre class="src src-clojure" id="org4b905f1"><span style="color: #494949;">(</span>dot-repr <span style="color: #bbb;">[</span>graph<span style="color: #bbb;">]</span>
<span style="color: #bbb;">(</span>str <span style="color: #494949;">"digraph G {"</span>
<span style="color: #494949;">"rankdir=</span><span style="color: #494949; font-weight: bold;">\"</span><span style="color: #494949;">LR</span><span style="color: #494949; font-weight: bold;">\"</span><span style="color: #494949;">;"</span>
<span style="color: #494949;">"size=</span><span style="color: #494949; font-weight: bold;">\"</span><span style="color: #494949;">15,7.5!</span><span style="color: #494949; font-weight: bold;">\"</span><span style="color: #494949;">;"</span>
<span style="color: #494949;">"fontpath=</span><span style="color: #494949; font-weight: bold;">\"</span><span style="color: #494949;">~/.local/share/fonts</span><span style="color: #494949; font-weight: bold;">\"</span><span style="color: #494949;">;"</span>
<span style="color: #494949;">"node[shape=record,"</span>
<span style="color: #494949;">"fontname=</span><span style="color: #494949; font-weight: bold;">\"</span><span style="color: #494949;">Jura Bold</span><span style="color: #494949; font-weight: bold;">\"</span><span style="color: #494949;">,"</span>
<span style="color: #494949;">"penwidth=0,"</span>
<span style="color: #494949;">"style=filled,"</span>
<span style="color: #494949;">"fillcolor=</span><span style="color: #494949; font-weight: bold;">\"</span><span style="color: #000000; background-color: #f5f5f5;">#f5f5f5</span><span style="color: #494949; font-weight: bold;">\"</span><span style="color: #494949;">];"</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">->></span> graph
<span style="color: #E53935;">:nodes</span>
<span style="color: #bbb;">(</span>mapcat <span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">fn</span> <span style="color: #bbb;">[</span><span style="color: #494949;">[</span>name <span style="color: #bbb;">{</span><span style="color: #E53935;">:keys</span> <span style="color: #494949;">[</span>edges-to<span style="color: #494949;">]</span><span style="color: #bbb;">}</span><span style="color: #494949;">]</span><span style="color: #bbb;">]</span>
<span style="color: #bbb;">(</span>map <span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">fn</span> <span style="color: #bbb;">[</span><span style="color: #494949;">{</span><span style="color: #E53935;">:keys</span> <span style="color: #bbb;">[</span>dest weight<span style="color: #bbb;">]</span><span style="color: #494949;">}</span><span style="color: #bbb;">]</span> <span style="color: #bbb;">[</span>name dest weight<span style="color: #bbb;">]</span><span style="color: #494949;">)</span> edges-to<span style="color: #bbb;">)</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>map <span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">fn</span> <span style="color: #bbb;">[</span><span style="color: #494949;">[</span>name dest weight<span style="color: #494949;">]</span><span style="color: #bbb;">]</span>
<span style="color: #bbb;">(</span>format <span style="color: #494949;">"</span><span style="color: #494949; font-weight: bold;">\"</span><span style="color: #494949;">%s</span><span style="color: #494949; font-weight: bold;">\"</span><span style="color: #494949;">-></span><span style="color: #494949; font-weight: bold;">\"</span><span style="color: #494949;">%s</span><span style="color: #494949; font-weight: bold;">\"</span><span style="color: #494949;">[label=</span><span style="color: #494949; font-weight: bold;">\"</span><span style="color: #494949;">%s</span><span style="color: #494949; font-weight: bold;">\"</span><span style="color: #494949;">];"</span> name dest weight<span style="color: #bbb;">)</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span>
flatten
<span style="color: #E53935;">str</span>/join<span style="color: #494949;">)</span>
<span style="color: #494949;">"}"</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
</pre>
</div>
<p>
Finally we can see the fruits of our labor and define a graph that we can also visualize.
</p>
<div class="org-src-container">
<pre class="src src-clojure" id="orgd8ecb1e"><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">ns</span> <span style="color: #E53935;">graph-demo</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">-></span> <span style="color: #bbb;">(</span>weighted-digraph <span style="color: #E53935;">:a</span> <span style="color: #E53935;">:b</span> <span style="color: #E53935;">:c</span> <span style="color: #E53935;">:d</span> <span style="color: #E53935;">:e</span> <span style="color: #E53935;">:f</span> <span style="color: #E53935;">:g</span> <span style="color: #E53935;">:h</span> <span style="color: #E53935;">:i</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>edge <span style="color: #E53935;">:a</span> <span style="color: #E53935;">:b</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>edge <span style="color: #E53935;">:c</span> <span style="color: #E53935;">:d</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>edge <span style="color: #E53935;">:g</span> <span style="color: #E53935;">:h</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>edge <span style="color: #E53935;">:b</span> <span style="color: #E53935;">:d</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>edge <span style="color: #E53935;">:g</span> <span style="color: #E53935;">:a</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>edge <span style="color: #E53935;">:a</span> <span style="color: #E53935;">:c</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>kill-edge <span style="color: #E53935;">:a</span> <span style="color: #E53935;">:c</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>edge <span style="color: #E53935;">:h</span> <span style="color: #E53935;">:d</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>dot-repr<span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
</pre>
</div>
<div id="org36976ca" class="figure">
<p><img src="./images/graph-digraph_92bc97e7-deb7-4d5f-a21a-2918d62b0a4f.png" alt="graph-digraph_92bc97e7-deb7-4d5f-a21a-2918d62b0a4f.png" />
</p>
</div>
<p>
Now you may notice that the directed graph case is ultimately a superset of the undirected graph case. While in the case of the undirected graph the nodes pointed at each other, they don't do that here.
</p>
</div>
</div>
</div>
</div>
<!-- Footnotes --><!--
<div class="footdef"><sup><a id="fn.1" class="footnum" href="#fnr.1" role="doc-backlink">1</a></sup> <div class="footpara" role="doc-footnote"><p class="footpara">Which I think I will extend to representing arbitrary Clojure code in general</p></div></div>
<div class="footdef"><sup><a id="fn.2" class="footnum" href="#fnr.2" role="doc-backlink">2</a></sup> <div class="footpara" role="doc-footnote"><p class="footpara">As with any abstract structure it is an interpretation, and many others exist. The rules in math are made up anyways, so feel free to add some more!</p></div></div>
<div class="footdef"><sup><a id="fn.3" class="footnum" href="#fnr.3" role="doc-backlink">3</a></sup> <div class="footpara" role="doc-footnote"><p class="footpara">Searching piles of pointers is irritating no matter how you do it and tends to make any operations a lot messier. It's also, in this case, much slower as you can't just pick any node as easily.</p></div></div>
--></div>
<div id="postamble" class="status">
<p class="date">Last Modified: 2022-W01-6 04:29</p><p class="creator">Generated Using: <a href="https://www.gnu.org/software/emacs/">Emacs</a> 27.2 (<a href="https://orgmode.org">Org</a> mode 9.4.6)</p><p class="license">Except where otherwise noted content on <a href="https://cons.dev">cons.dev</a> is licensed under a <a href="https://creativecommons.org/licenses/by-sa/4.0/" rel="license">Creative Commons Attribution-ShareAlike 4.0 International License</a>.</p>
</div>
</body>
</html>