-
Notifications
You must be signed in to change notification settings - Fork 6
/
graph.d
544 lines (500 loc) · 17.1 KB
/
graph.d
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
// Written in the D programming language.
/**
Basic graph data structures.
Authors: $(LINK2 http://braingam.es/, Joseph Rushton Wakeling)
Copyright: Copyright © 2013 Joseph Rushton Wakeling
License: This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see $(LINK http://www.gnu.org/licenses/).
Credits: The basic graph data structure used here is adapted from the library
$(LINK2 http://igraph.sourceforge.net/, igraph) by Gábor Csárdi and
Tamás Nepusz.
*/
module dgraph.graph;
import std.algorithm, std.array, std.conv, std.range, std.traits;
template isGraph(G)
{
static if (!__traits(hasMember, G, "directed") ||
!__traits(hasMember, G, "edge") ||
!__traits(hasMember, G, "edgeCount") ||
!__traits(hasMember, G, "vertexCount") ||
!__traits(hasMember, G, "isEdge") ||
!__traits(hasMember, G, "edgeID") ||
!__traits(hasMember, G, "addEdge") ||
!__traits(hasMember, G, "degreeIn") ||
!__traits(hasMember, G, "degreeOut") ||
!__traits(hasMember, G, "incidentEdgesIn") ||
!__traits(hasMember, G, "incidentEdgesOut") ||
!__traits(hasMember, G, "neighboursIn") ||
!__traits(hasMember, G, "neighboursOut"))
{
enum bool isGraph = false;
}
else static if (!isBoolean!(typeof(G.directed)))
{
enum bool isGraph = false;
}
else static if (G.directed && (__traits(hasMember, G, "degree") ||
__traits(hasMember, G, "incidentEdges") ||
__traits(hasMember, G, "neighbours")))
{
enum bool isGraph = false;
}
else static if (!G.directed && (!__traits(hasMember, G, "degree") ||
!__traits(hasMember, G, "incidentEdges") ||
!__traits(hasMember, G, "neighbours")))
{
enum bool isGraph = false;
}
else
{
enum bool isGraph = true;
}
}
template isDirectedGraph(G)
{
static if (isGraph!G)
{
enum bool isDirectedGraph = G.directed;
}
else
{
enum bool isDirectedGraph = false;
}
}
template isUndirectedGraph(G)
{
static if (isGraph!G)
{
enum bool isUndirectedGraph = !G.directed;
}
else
{
enum bool isUndirectedGraph = false;
}
}
unittest
{
assert(isGraph!(IndexedEdgeList!true));
assert(isGraph!(IndexedEdgeList!false));
assert(isDirectedGraph!(IndexedEdgeList!true));
assert(!isDirectedGraph!(IndexedEdgeList!false));
assert(!isUndirectedGraph!(IndexedEdgeList!true));
assert(isUndirectedGraph!(IndexedEdgeList!false));
}
final class IndexedEdgeList(bool dir)
{
private:
size_t[] _head;
size_t[] _tail;
size_t[] _indexHead;
size_t[] _indexTail;
size_t[] _sumHead = [0];
size_t[] _sumTail = [0];
void indexEdgesInsertion()
{
assert(_indexHead.length == _indexTail.length);
assert(_head.length == _tail.length);
immutable size_t l = _indexHead.length;
_indexHead.length = _head.length;
_indexTail.length = _tail.length;
foreach (immutable e; l .. _head.length)
{
size_t i, j, lower, upper;
upper = _indexHead[0 .. e].map!(a => _head[a]).assumeSorted.lowerBound(_head[e] + 1).length;
lower = _indexHead[0 .. upper].map!(a => _head[a]).assumeSorted.lowerBound(_head[e]).length;
i = lower + _indexHead[lower .. upper].map!(a => _tail[a]).assumeSorted.lowerBound(_tail[e]).length;
for(j = e; j > i; --j)
_indexHead[j] = _indexHead[j - 1];
_indexHead[i] = e;
upper = _indexTail[0 .. e].map!(a => _tail[a]).assumeSorted.lowerBound(_tail[e] + 1).length;
lower = _indexTail[0 .. upper].map!(a => _tail[a]).assumeSorted.lowerBound(_tail[e]).length;
i = lower + _indexTail[lower .. upper].map!(a => _head[a]).assumeSorted.lowerBound(_head[e]).length;
for(j = e; j > i; --j)
_indexTail[j] = _indexTail[j - 1];
_indexTail[i] = e;
}
assert(_indexHead.length == _indexTail.length);
assert(_indexHead.length == _head.length, text(_indexHead.length, " head indices but ", _head.length, " head values."));
assert(_indexTail.length == _tail.length, text(_indexTail.length, " tail indices but ", _tail.length, " tail values."));
}
void indexEdgesSort()
{
_indexHead ~= iota(_indexHead.length, _head.length).array;
_indexTail ~= iota(_indexTail.length, _tail.length).array;
assert(_indexHead.length == _indexTail.length);
_indexHead.multiSort!((a, b) => _head[a] < _head[b], (a, b) => _tail[a] < _tail[b]);
_indexTail.multiSort!((a, b) => _tail[a] < _tail[b], (a, b) => _head[a] < _head[b]);
}
void sumEdges(ref size_t[] sum, ref size_t[] vertex, ref size_t[] index)
{
assert(sum.length > 1);
size_t v = vertex[index[0]];
sum[0 .. v + 1] = 0;
for(size_t i = 1; i < index.length; ++i)
{
size_t n = vertex[index[i]] - vertex[index[sum[v]]];
sum[v + 1 .. v + n + 1] = i;
v += n;
}
sum[v + 1 .. $] = vertex.length;
}
public:
enum bool directed = dir;
auto edge() @property const pure nothrow
{
return zip(_head, _tail);
}
size_t edgeCount() @property const pure nothrow
{
assert(_head.length == _tail.length);
return _head.length;
}
size_t vertexCount() @property const pure nothrow
{
assert(_sumHead.length == _sumTail.length);
return _sumHead.length - 1;
}
bool isEdge(size_t head, size_t tail) const
{
assert(head < vertexCount);
assert(tail < vertexCount);
static if (!directed)
{
if (tail < head)
{
swap(head, tail);
}
}
size_t headDeg = _sumHead[head + 1] - _sumHead[head];
if (headDeg == 0)
{
return false;
}
size_t tailDeg = _sumTail[tail + 1] - _sumTail[tail];
if (tailDeg == 0)
{
return false;
}
else if (headDeg < tailDeg)
{
// search among the tails of head
foreach (immutable t; iota(_sumHead[head], _sumHead[head + 1]).map!(a => _tail[_indexHead[a]]))
{
if (t == tail)
{
return true;
}
}
return false;
}
else
{
// search among the heads of tail
foreach (immutable h; iota(_sumTail[tail], _sumTail[tail + 1]).map!(a => _head[_indexTail[a]]))
{
if (h == head)
{
return true;
}
}
return false;
}
}
size_t edgeID(size_t head, size_t tail) const
{
assert(head < vertexCount);
assert(tail < vertexCount);
assert(isEdge(head, tail));
static if (!directed)
{
if (tail < head)
{
swap(head, tail);
}
}
size_t headDeg = _sumHead[head + 1] - _sumHead[head];
size_t tailDeg = _sumTail[tail + 1] - _sumTail[tail];
assert(headDeg > 0);
assert(tailDeg > 0);
if (headDeg < tailDeg)
{
// search among the tails of head
foreach (immutable i; iota(_sumHead[head], _sumHead[head + 1]).map!(a => _indexHead[a]))
{
if (_tail[i] == tail)
{
assert(_head[i] == head);
return i;
}
}
assert(false);
}
else
{
// search among the heads of tail
foreach (immutable i; iota(_sumTail[tail], _sumTail[tail + 1]).map!(a => _indexTail[a]))
{
if (_head[i] == head)
{
assert(_tail[i] == tail);
return i;
}
}
assert(false);
}
}
static if (directed)
{
size_t degreeIn(immutable size_t v) const pure nothrow
{
assert(v + 1 < _sumTail.length);
return _sumTail[v + 1] - _sumTail[v];
}
size_t degreeOut(immutable size_t v) const pure nothrow
{
assert(v + 1 < _sumHead.length);
return _sumHead[v + 1] - _sumHead[v];
}
}
else
{
size_t degree(immutable size_t v) const pure nothrow
{
assert(v + 1 < _sumHead.length);
assert(_sumHead.length == _sumTail.length);
return (_sumHead[v + 1] - _sumHead[v])
+ (_sumTail[v + 1] - _sumTail[v]);
}
alias degreeIn = degree;
alias degreeOut = degree;
}
static if (directed)
{
auto incidentEdgesIn(immutable size_t v) const
{
return iota(_sumTail[v], _sumTail[v + 1]).map!(a => _indexTail[a]);
}
auto incidentEdgesOut(immutable size_t v) const
{
return iota(_sumHead[v], _sumHead[v + 1]).map!(a => _indexHead[a]);
}
}
else
{
auto incidentEdges(immutable size_t v) const
{
return chain(iota(_sumTail[v], _sumTail[v + 1]).map!(a => _indexTail[a]),
iota(_sumHead[v], _sumHead[v + 1]).map!(a => _indexHead[a]));
}
alias incidentEdgesIn = incidentEdges;
alias incidentEdgesOut = incidentEdges;
}
static if (directed)
{
auto neighboursIn(immutable size_t v) const
{
return iota(_sumTail[v], _sumTail[v + 1]).map!(a => _head[_indexTail[a]]);
}
auto neighboursOut(immutable size_t v) const
{
return iota(_sumHead[v], _sumHead[v + 1]).map!(a => _tail[_indexHead[a]]);
}
}
else
{
auto neighbours(immutable size_t v) const
{
return chain(iota(_sumTail[v], _sumTail[v + 1]).map!(a => _head[_indexTail[a]]),
iota(_sumHead[v], _sumHead[v + 1]).map!(a => _tail[_indexHead[a]]));
}
alias neighbors = neighbours;
alias neighboursIn = neighbours;
alias neighboursOut = neighbours;
}
alias neighborsIn = neighboursIn;
alias neighborsOut = neighboursOut;
void addVertices(immutable size_t n)
{
immutable size_t l = _sumHead.length;
_sumHead.length += n;
_sumTail.length += n;
assert(_sumHead.length == _sumTail.length);
_sumHead[l .. $] = _sumHead[l - 1];
_sumTail[l .. $] = _sumTail[l - 1];
}
void addEdge()(size_t head, size_t tail)
{
assert(head < this.vertexCount, text("Edge head ", head, " is greater than vertex count ", this.vertexCount));
assert(tail < this.vertexCount, text("Edge tail ", tail, " is greater than vertex count ", this.vertexCount));
static if (!directed)
{
if (tail < head)
{
swap(head, tail);
}
}
_head ~= head;
_tail ~= tail;
indexEdgesInsertion();
++_sumHead[head + 1 .. $];
++_sumTail[tail + 1 .. $];
}
void addEdge(T : size_t)(T[] edgeList)
{
assert(edgeList.length % 2 == 0);
assert(_head.length == _tail.length);
immutable size_t l = _head.length;
_head.length += edgeList.length / 2;
_tail.length += edgeList.length / 2;
foreach (immutable i; 0 .. edgeList.length / 2)
{
size_t head = edgeList[2 * i];
size_t tail = edgeList[2 * i + 1];
assert(head < this.vertexCount, text("Edge head ", head, " is greater than vertex count ", this.vertexCount));
assert(tail < this.vertexCount, text("Edge tail ", tail, " is greater than vertex count ", this.vertexCount));
static if (!directed)
{
if (tail < head)
{
swap(head, tail);
}
}
_head[l + i] = head;
_tail[l + i] = tail;
}
indexEdgesSort();
sumEdges(_sumHead, _head, _indexHead);
sumEdges(_sumTail, _tail, _indexTail);
}
}
unittest
{
import std.stdio;
auto g1 = new IndexedEdgeList!false;
g1.addVertices(10);
assert(g1.vertexCount == 10);
g1.addEdge(5, 8);
g1.addEdge(5, 4);
g1.addEdge(7, 4);
g1.addEdge(3, 4);
g1.addEdge(6, 9);
g1.addEdge(3, 2);
foreach (immutable head, immutable tail; g1.edge)
{
writeln("\t", head, "\t", tail);
}
writeln(g1._indexHead);
writeln(g1._indexTail);
writeln(g1._sumHead);
writeln(g1._sumTail);
foreach (immutable v; 0 .. g1.vertexCount)
{
writeln("\td(", v, ") =\t", g1.degree(v), "\tn(", v, ") = ", g1.neighbours(v), "\ti(", v, ") = ", g1.incidentEdges(v));
foreach (immutable e, immutable n; zip(g1.incidentEdges(v), g1.neighbours(v)))
{
if (g1.edge[e][0] == v)
{
assert(g1.edge[e][1] == n);
}
else
{
assert(g1.edge[e][1] == v);
assert(g1.edge[e][0] == n);
}
}
}
writeln;
assert(iota(g1._head.length).map!(a => g1._head[g1._indexHead[a]]).isSorted);
assert(iota(g1._tail.length).map!(a => g1._tail[g1._indexTail[a]]).isSorted);
foreach (immutable h; 0 .. 10)
{
foreach (immutable t; 0 .. 10)
{
if ((h == 5 && t == 8) || (h == 8 && t == 5) ||
(h == 5 && t == 4) || (h == 4 && t == 5) ||
(h == 7 && t == 4) || (h == 4 && t == 7) ||
(h == 3 && t == 4) || (h == 4 && t == 3) ||
(h == 6 && t == 9) || (h == 9 && t == 6) ||
(h == 3 && t == 2) || (h == 2 && t == 3))
{
assert(g1.isEdge(h, t), text("isEdge failure for edge (", h, ", ", t, ")"));
}
else
{
assert(!g1.isEdge(h, t), text("isEdge false positive for edge (", h, ", ", t, ")"));
}
}
}
foreach (immutable i; 0 .. g1.edgeCount)
{
size_t h = g1._head[i];
size_t t = g1._tail[i];
assert(i == g1.edgeID(h, t));
assert(i == g1.edgeID(t, h));
}
auto g2 = new IndexedEdgeList!true;
g2.addVertices(10);
assert(g2.vertexCount == 10);
g2.addEdge(5, 8);
g2.addEdge(5, 4);
g2.addEdge(7, 4);
g2.addEdge(3, 4);
g2.addEdge(6, 9);
g2.addEdge(3, 2);
foreach (immutable head, immutable tail; g2.edge)
writeln("\t", head, "\t", tail);
writeln(g2._indexHead);
writeln(g2._indexTail);
writeln(g2._sumHead);
writeln(g2._sumTail);
foreach (immutable v; 0 .. g2.vertexCount)
{
writeln("\td_out(", v, ") =\t", g2.degreeOut(v), "\tn_out(", v, ") = ", g2.neighboursOut(v), "\ti_out(", v, ") = ", g2.incidentEdgesOut(v),
"\td_in(", v, ") =\t", g2.degreeIn(v), "\tn_in(", v, ") = ", g2.neighboursIn(v), "\ti_in(", v, ") = ", g2.incidentEdgesIn(v));
foreach (immutable e, immutable n; zip(g2.incidentEdgesIn(v), g2.neighboursIn(v)))
{
assert(g2.edge[e][0] == n);
assert(g2.edge[e][1] == v);
}
foreach (immutable e, immutable n; zip(g2.incidentEdgesOut(v), g2.neighboursOut(v)))
{
assert(g2.edge[e][0] == v);
assert(g2.edge[e][1] == n);
}
}
assert(iota(g2._head.length).map!(a => g2._head[g2._indexHead[a]]).isSorted);
assert(iota(g2._tail.length).map!(a => g2._tail[g2._indexTail[a]]).isSorted);
foreach (immutable h; 0 .. 10)
{
foreach (immutable t; 0 .. 10)
{
if ((h == 5 && t == 8) ||
(h == 5 && t == 4) ||
(h == 7 && t == 4) ||
(h == 3 && t == 4) ||
(h == 6 && t == 9) ||
(h == 3 && t == 2))
{
assert(g2.isEdge(h, t), text("isEdge failure for edge (", h, ", ", t, ")"));
}
else
{
assert(!g2.isEdge(h, t), text("isEdge false positive for edge (", h, ", ", t, ")"));
}
}
}
foreach (immutable i; 0 .. g2.edgeCount)
{
size_t h = g2._head[i];
size_t t = g2._tail[i];
assert(i == g2.edgeID(h, t));
}
}