forked from YaccConstructor/QuickGraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdjacencyGraph.cs
More file actions
543 lines (477 loc) · 15.4 KB
/
AdjacencyGraph.cs
File metadata and controls
543 lines (477 loc) · 15.4 KB
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.Contracts;
using QuickGraph.Contracts;
using QuickGraph.Collections;
namespace QuickGraph
{
/// <summary>
/// A mutable directed graph data structure efficient for sparse
/// graph representation where out-edge need to be enumerated only.
/// </summary>
/// <typeparam name="TVertex">type of the vertices</typeparam>
/// <typeparam name="TEdge">type of the edges</typeparam>
#if !SILVERLIGHT
[Serializable]
#endif
[DebuggerDisplay("VertexCount = {VertexCount}, EdgeCount = {EdgeCount}")]
public class AdjacencyGraph<TVertex,TEdge>
: IVertexAndEdgeListGraph<TVertex,TEdge>
, IEdgeListAndIncidenceGraph<TVertex,TEdge>
, IMutableEdgeListGraph<TVertex,TEdge>
, IMutableIncidenceGraph<TVertex,TEdge>
, IMutableVertexListGraph<TVertex,TEdge>
, IMutableVertexAndEdgeListGraph<TVertex,TEdge>
#if !SILVERLIGHT
, ICloneable
#endif
where TEdge : IEdge<TVertex>
{
private readonly bool isDirected = true;
private readonly bool allowParallelEdges;
private readonly IVertexEdgeDictionary<TVertex, TEdge> vertexEdges;
private int edgeCount = 0;
private int edgeCapacity = -1;
public AdjacencyGraph()
:this(true)
{}
public AdjacencyGraph(bool allowParallelEdges)
:this(allowParallelEdges,-1)
{
}
public AdjacencyGraph(bool allowParallelEdges, int capacity)
:this(allowParallelEdges, capacity, -1)
{
}
public AdjacencyGraph(bool allowParallelEdges, int capacity, int edgeCapacity)
{
this.allowParallelEdges = allowParallelEdges;
if (capacity > -1)
this.vertexEdges = new VertexEdgeDictionary<TVertex, TEdge>(capacity);
else
this.vertexEdges = new VertexEdgeDictionary<TVertex, TEdge>();
this.edgeCapacity = edgeCapacity;
}
public AdjacencyGraph(
bool allowParallelEdges,
int capacity,
int edgeCapacity,
Func<int, IVertexEdgeDictionary<TVertex, TEdge>> vertexEdgesDictionaryFactory)
{
Contract.Requires(vertexEdgesDictionaryFactory != null);
this.allowParallelEdges = allowParallelEdges;
this.vertexEdges = vertexEdgesDictionaryFactory(capacity);
this.edgeCapacity = edgeCapacity;
}
public bool IsDirected
{
get { return this.isDirected; }
}
public bool AllowParallelEdges
{
[Pure]
get { return this.allowParallelEdges; }
}
public int EdgeCapacity
{
get { return this.edgeCapacity; }
set { this.edgeCapacity = value; }
}
public static Type EdgeType
{
get { return typeof(TEdge); }
}
public bool IsVerticesEmpty
{
get { return this.vertexEdges.Count == 0; }
}
public int VertexCount
{
get { return this.vertexEdges.Count; }
}
public virtual IEnumerable<TVertex> Vertices
{
get { return this.vertexEdges.Keys; }
}
[Pure]
public bool ContainsVertex(TVertex v)
{
return this.vertexEdges.ContainsKey(v);
}
public bool IsOutEdgesEmpty(TVertex v)
{
return this.vertexEdges[v].Count == 0;
}
public int OutDegree(TVertex v)
{
return this.vertexEdges[v].Count;
}
public virtual IEnumerable<TEdge> OutEdges(TVertex v)
{
return this.vertexEdges[v];
}
public virtual bool TryGetOutEdges(TVertex v, out IEnumerable<TEdge> edges)
{
IEdgeList<TVertex, TEdge> list;
if (this.vertexEdges.TryGetValue(v, out list))
{
edges = list;
return true;
}
edges = null;
return false;
}
public TEdge OutEdge(TVertex v, int index)
{
return this.vertexEdges[v][index];
}
/// <summary>
/// Gets a value indicating whether this instance is edges empty.
/// </summary>
/// <value>
/// <c>true</c> if this instance is edges empty; otherwise, <c>false</c>.
/// </value>
public bool IsEdgesEmpty
{
[Pure]
get { return this.edgeCount == 0; }
}
/// <summary>
/// Gets the edge count.
/// </summary>
/// <value>The edge count.</value>
public int EdgeCount
{
get
{
return this.edgeCount;
}
}
[ContractInvariantMethod]
void ObjectInvariant()
{
Contract.Invariant(this.edgeCount >= 0);
}
/// <summary>
/// Gets the edges.
/// </summary>
/// <value>The edges.</value>
public virtual IEnumerable<TEdge> Edges
{
[Pure]
get
{
foreach (var edges in this.vertexEdges.Values)
foreach (var edge in edges)
yield return edge;
}
}
[Pure]
public bool ContainsEdge(TVertex source, TVertex target)
{
IEnumerable<TEdge> outEdges;
if (!this.TryGetOutEdges(source, out outEdges))
return false;
foreach (var outEdge in outEdges)
if (outEdge.Target.Equals(target))
return true;
return false;
}
[Pure]
public bool ContainsEdge(TEdge edge)
{
IEdgeList<TVertex, TEdge> edges;
return
this.vertexEdges.TryGetValue(edge.Source, out edges) &&
edges.Contains(edge);
}
[Pure]
public bool TryGetEdge(
TVertex source,
TVertex target,
out TEdge edge)
{
IEdgeList<TVertex, TEdge> edgeList;
if (this.vertexEdges.TryGetValue(source, out edgeList) &&
edgeList.Count > 0)
{
foreach (var e in edgeList)
{
if (e.Target.Equals(target))
{
edge = e;
return true;
}
}
}
edge = default(TEdge);
return false;
}
[Pure]
public virtual bool TryGetEdges(
TVertex source,
TVertex target,
out IEnumerable<TEdge> edges)
{
IEdgeList<TVertex, TEdge> outEdges;
if (this.vertexEdges.TryGetValue(source, out outEdges))
{
List<TEdge> list = new List<TEdge>(outEdges.Count);
foreach (var edge in outEdges)
if (edge.Target.Equals(target))
list.Add(edge);
edges = list;
return true;
}
else
{
edges = null;
return false;
}
}
public virtual bool AddVertex(TVertex v)
{
if (this.ContainsVertex(v))
return false;
if (this.EdgeCapacity>0)
this.vertexEdges.Add(v, new EdgeList<TVertex,TEdge>(this.EdgeCapacity));
else
this.vertexEdges.Add(v, new EdgeList<TVertex, TEdge>());
this.OnVertexAdded(v);
return true;
}
public virtual int AddVertexRange(IEnumerable<TVertex> vertices)
{
int count = 0;
foreach (var v in vertices)
if (this.AddVertex(v))
count++;
return count;
}
public event VertexAction<TVertex> VertexAdded;
protected virtual void OnVertexAdded(TVertex args)
{
Contract.Requires(args != null);
var eh = this.VertexAdded;
if (eh != null)
eh(args);
}
public virtual bool RemoveVertex(TVertex v)
{
if (!this.ContainsVertex(v))
return false;
// remove outedges
{
var edges = this.vertexEdges[v];
if (this.EdgeRemoved != null) // lazily notify
{
foreach (var edge in edges)
this.OnEdgeRemoved(edge);
}
this.edgeCount -= edges.Count;
edges.Clear();
}
// iterage over edges and remove each edge touching the vertex
var edgeToRemove = new EdgeList<TVertex, TEdge>();
foreach (var kv in this.vertexEdges)
{
if (kv.Key.Equals(v)) continue; // we've already
// collect edge to remove
foreach(var edge in kv.Value)
{
if (edge.Target.Equals(v))
edgeToRemove.Add(edge);
}
// remove edges
foreach (var edge in edgeToRemove)
{
kv.Value.Remove(edge);
this.OnEdgeRemoved(edge);
}
// update count
this.edgeCount -= edgeToRemove.Count;
edgeToRemove.Clear();
}
Contract.Assert(this.edgeCount >= 0);
this.vertexEdges.Remove(v);
this.OnVertexRemoved(v);
return true;
}
public event VertexAction<TVertex> VertexRemoved;
protected virtual void OnVertexRemoved(TVertex args)
{
Contract.Requires(args != null);
var eh = this.VertexRemoved;
if (eh != null)
eh(args);
}
public int RemoveVertexIf(VertexPredicate<TVertex> predicate)
{
var vertices = new VertexList<TVertex>();
foreach (var v in this.Vertices)
if (predicate(v))
vertices.Add(v);
foreach (var v in vertices)
this.RemoveVertex(v);
return vertices.Count;
}
public virtual bool AddVerticesAndEdge(TEdge e)
{
this.AddVertex(e.Source);
this.AddVertex(e.Target);
return this.AddEdge(e);
}
/// <summary>
/// Adds a range of edges to the graph
/// </summary>
/// <param name="edges"></param>
/// <returns>the count edges that were added</returns>
public int AddVerticesAndEdgeRange(IEnumerable<TEdge> edges)
{
int count = 0;
foreach (var edge in edges)
if (this.AddVerticesAndEdge(edge))
count++;
return count;
}
/// <summary>
/// Adds the edge to the graph
/// </summary>
/// <param name="e">the edge to add</param>
/// <returns>true if the edge was added; false if it was already part of the graph</returns>
public virtual bool AddEdge(TEdge e)
{
Contract.Assert(e != null);
if (e == null)
{
throw new System.ArgumentException("Edge can not be null.");
//return false;
}
if (!this.AllowParallelEdges)
{
if (this.ContainsEdge(e.Source, e.Target))
return false;
}
this.vertexEdges[e.Source].Add(e);
this.edgeCount++;
this.OnEdgeAdded(e);
return true;
}
public int AddEdgeRange(IEnumerable<TEdge> edges)
{
int count = 0;
foreach (var edge in edges)
if (this.AddEdge(edge))
count++;
return count;
}
public event EdgeAction<TVertex, TEdge> EdgeAdded;
protected virtual void OnEdgeAdded(TEdge args)
{
var eh = this.EdgeAdded;
if (eh != null)
eh(args);
}
public virtual bool RemoveEdge(TEdge e)
{
IEdgeList<TVertex, TEdge> edges;
if (this.vertexEdges.TryGetValue(e.Source, out edges) &&
edges.Remove(e))
{
this.edgeCount--;
Contract.Assert(this.edgeCount >= 0);
this.OnEdgeRemoved(e);
return true;
}
else
return false;
}
public event EdgeAction<TVertex, TEdge> EdgeRemoved;
protected virtual void OnEdgeRemoved(TEdge args)
{
var eh = this.EdgeRemoved;
if (eh != null)
eh(args);
}
public int RemoveEdgeIf(EdgePredicate<TVertex, TEdge> predicate)
{
var edges = new EdgeList<TVertex, TEdge>();
foreach (var edge in this.Edges)
if (predicate(edge))
edges.Add(edge);
foreach (var edge in edges)
this.RemoveEdge(edge);
return edges.Count;
}
public void ClearOutEdges(TVertex v)
{
var edges = this.vertexEdges[v];
int count = edges.Count;
if (this.EdgeRemoved != null) // call only if someone is listening
{
foreach (var edge in edges)
this.OnEdgeRemoved(edge);
}
edges.Clear();
this.edgeCount -= count;
}
public int RemoveOutEdgeIf(TVertex v, EdgePredicate<TVertex, TEdge> predicate)
{
var edges = this.vertexEdges[v];
var edgeToRemove = new EdgeList<TVertex,TEdge>(edges.Count);
foreach (var edge in edges)
if (predicate(edge))
edgeToRemove.Add(edge);
foreach (var edge in edgeToRemove)
{
edges.Remove(edge);
this.OnEdgeRemoved(edge);
}
this.edgeCount -= edgeToRemove.Count;
return edgeToRemove.Count;
}
public void TrimEdgeExcess()
{
foreach (var edges in this.vertexEdges.Values)
edges.TrimExcess();
}
public void Clear()
{
this.vertexEdges.Clear();
this.edgeCount = 0;
}
#region ICloneable Members
private AdjacencyGraph(
IVertexEdgeDictionary<TVertex, TEdge> vertexEdges,
int edgeCount,
int edgeCapacity,
bool allowParallelEdges
)
{
Contract.Requires(vertexEdges != null);
Contract.Requires(edgeCount >= 0);
this.vertexEdges = vertexEdges;
this.edgeCount = edgeCount;
this.edgeCapacity = edgeCapacity;
this.allowParallelEdges = allowParallelEdges;
}
[Pure]
public AdjacencyGraph<TVertex, TEdge> Clone()
{
return new AdjacencyGraph<TVertex, TEdge>(
this.vertexEdges.Clone(),
this.edgeCount,
this.edgeCapacity,
this.allowParallelEdges
);
}
#if !SILVERLIGHT
object ICloneable.Clone()
{
return this.Clone();
}
#endif
#endregion
}
}