forked from YaccConstructor/QuickGraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayBidirectionalGraph.cs
More file actions
356 lines (315 loc) · 10 KB
/
ArrayBidirectionalGraph.cs
File metadata and controls
356 lines (315 loc) · 10 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics.Contracts;
using System.Diagnostics;
namespace QuickGraph
{
/// <summary>
/// An immutable directed graph data structure efficient for large 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 sealed class ArrayBidirectionalGraph<TVertex, TEdge>
: IBidirectionalGraph<TVertex, TEdge>
#if !SILVERLIGHT
, ICloneable
#endif
where TEdge : IEdge<TVertex>
{
readonly Dictionary<TVertex, InOutEdges> vertexEdges;
readonly int edgeCount;
#if !SILVERLIGHT
[Serializable]
#endif
struct InOutEdges
{
private readonly TEdge[] _outEdges;
private readonly TEdge[] _inEdges;
public InOutEdges(TEdge[] outEdges, TEdge[] inEdges)
{
this._outEdges = outEdges != null && outEdges.Length > 0 ? outEdges : null;
this._inEdges = inEdges != null && inEdges.Length > 0 ? inEdges : null;
}
public bool TryGetOutEdges(out TEdge[] edges)
{
edges= this._outEdges;
return edges != null;
}
public bool TryGetInEdges(out TEdge[] edges)
{
edges= this._inEdges;
return edges != null;
}
}
/// <summary>
/// Constructs a new ArrayBidirectionalGraph instance from a
/// IBidirectionalGraph instance
/// </summary>
/// <param name="visitedGraph"></param>
public ArrayBidirectionalGraph(
IBidirectionalGraph<TVertex, TEdge> visitedGraph
)
{
Contract.Requires(visitedGraph != null);
this.vertexEdges = new Dictionary<TVertex, InOutEdges>(visitedGraph.VertexCount);
this.edgeCount = visitedGraph.EdgeCount;
foreach (var vertex in visitedGraph.Vertices)
{
var outEdges = Enumerable.ToArray(visitedGraph.OutEdges(vertex));
var inEdges = Enumerable.ToArray(visitedGraph.InEdges(vertex));
this.vertexEdges.Add(vertex, new InOutEdges(outEdges, inEdges));
}
}
private ArrayBidirectionalGraph(
Dictionary<TVertex, InOutEdges> vertexEdges,
int edgeCount
)
{
Contract.Requires(vertexEdges != null);
Contract.Requires(edgeCount >= 0);
this.vertexEdges = vertexEdges;
this.edgeCount = edgeCount;
}
#region IIncidenceGraph<TVertex,TEdge> Members
public bool ContainsEdge(TVertex source, TVertex target)
{
TEdge edge;
return this.TryGetEdge(source, target, out edge);
}
public bool TryGetEdges(TVertex source, TVertex target, out IEnumerable<TEdge> edges)
{
InOutEdges es;
if (this.vertexEdges.TryGetValue(source, out es))
{
List<TEdge> _edges = null;
TEdge[] outEdges;
if (es.TryGetOutEdges(out outEdges))
for (int i = 0; i < outEdges.Length; i++)
{
if (outEdges[i].Target.Equals(target))
{
if (_edges == null)
_edges = new List<TEdge>(outEdges.Length - i);
_edges.Add(outEdges[i]);
}
}
edges = _edges;
return edges != null;
}
edges = null;
return false;
}
public bool TryGetEdge(TVertex source, TVertex target, out TEdge edge)
{
InOutEdges io;
TEdge[] edges;
if (this.vertexEdges.TryGetValue(source, out io) &&
(io.TryGetOutEdges(out edges)))
{
for (int i = 0; i < edges.Length; i++)
{
if (edges[i].Target.Equals(target))
{
edge = edges[i];
return true;
}
}
}
edge = default(TEdge);
return false;
}
#endregion
#region IImplicitGraph<TVertex,TEdge> Members
public bool IsOutEdgesEmpty(TVertex v)
{
InOutEdges io;
TEdge[] edges;
if (this.vertexEdges.TryGetValue(v, out io) &&
(io.TryGetOutEdges(out edges)))
return false;
return true;
}
public int OutDegree(TVertex v)
{
InOutEdges io;
TEdge[] edges;
if (this.vertexEdges.TryGetValue(v, out io) &&
(io.TryGetOutEdges(out edges)))
return edges.Length;
return 0;
}
public IEnumerable<TEdge> OutEdges(TVertex v)
{
IEnumerable<TEdge> result;
if (this.TryGetInEdges(v, out result))
return result;
else
return Enumerable.Empty<TEdge>();
}
public bool TryGetOutEdges(TVertex v, out IEnumerable<TEdge> edges)
{
InOutEdges io;
TEdge[] aedges;
if (this.vertexEdges.TryGetValue(v, out io) &&
(io.TryGetOutEdges(out aedges)))
{
edges = aedges;
return true;
}
edges = null;
return false;
}
public TEdge OutEdge(TVertex v, int index)
{
var io = this.vertexEdges[v];
TEdge[] edges;
if (!io.TryGetOutEdges(out edges))
Contract.Assert(false);
return edges[index];
}
#endregion
#region IGraph<TVertex,TEdge> Members
public bool IsDirected
{
get { return true; }
}
public bool AllowParallelEdges
{
get { return true; }
}
#endregion
#region IImplicitVertexSet<TVertex> Members
public bool ContainsVertex(TVertex vertex)
{
return this.vertexEdges.ContainsKey(vertex);
}
#endregion
#region IVertexSet<TVertex> Members
public bool IsVerticesEmpty
{
get { return this.vertexEdges.Count == 0; }
}
public int VertexCount
{
get { return this.vertexEdges.Count; }
}
public IEnumerable<TVertex> Vertices
{
get
{
return this.vertexEdges.Keys;
}
}
#endregion
#region IEdgeSet<TVertex,TEdge> Members
public bool IsEdgesEmpty
{
get { return this.edgeCount == 0; }
}
public int EdgeCount
{
get { return this.edgeCount; }
}
public IEnumerable<TEdge> Edges
{
get
{
foreach (var io in this.vertexEdges.Values)
{
TEdge[] edges;
if (io.TryGetOutEdges(out edges))
for (int i = 0; i < edges.Length; i++)
yield return edges[i];
}
}
}
public bool ContainsEdge(TEdge edge)
{
InOutEdges io;
TEdge[] edges;
if (this.vertexEdges.TryGetValue(edge.Source, out io) &&
(io.TryGetOutEdges(out edges)))
for (int i = 0; i < edges.Length; i++)
if (edges[i].Equals(edge))
return true;
return false;
}
#endregion
#region ICloneable Members
/// <summary>
/// Returns self since this class is immutable
/// </summary>
/// <returns></returns>
public ArrayBidirectionalGraph<TVertex, TEdge> Clone()
{
return this;
}
#if !SILVERLIGHT
object ICloneable.Clone()
{
return this.Clone();
}
#endif
#endregion
#region IBidirectionalGraph<TVertex,TEdge> Members
public bool IsInEdgesEmpty(TVertex v)
{
InOutEdges io;
TEdge[] edges;
if (this.vertexEdges.TryGetValue(v, out io) &&
(io.TryGetInEdges(out edges)))
return false;
return true;
}
public int InDegree(TVertex v)
{
InOutEdges io;
TEdge[] edges;
if (this.vertexEdges.TryGetValue(v, out io) &&
(io.TryGetInEdges(out edges)))
return edges.Length;
return 0;
}
public IEnumerable<TEdge> InEdges(TVertex v)
{
IEnumerable<TEdge> result;
if (this.TryGetInEdges(v, out result))
return result;
else
return Enumerable.Empty<TEdge>();
}
public bool TryGetInEdges(TVertex v, out IEnumerable<TEdge> edges)
{
InOutEdges io;
TEdge[] aedges;
if (this.vertexEdges.TryGetValue(v, out io) &&
(io.TryGetInEdges(out aedges)))
{
edges = aedges;
return true;
}
edges = null;
return false;
}
public TEdge InEdge(TVertex v, int index)
{
var io = this.vertexEdges[v];
TEdge[] edges;
if (!io.TryGetOutEdges(out edges))
Contract.Assert(false);
return edges[index];
}
public int Degree(TVertex v)
{
return InDegree(v) + OutDegree(v);
}
#endregion
}
}