-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathEdgeExtensions.cs
More file actions
375 lines (340 loc) · 14.1 KB
/
EdgeExtensions.cs
File metadata and controls
375 lines (340 loc) · 14.1 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
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using QuickGraph.Collections;
using System.Linq;
using System.Reflection;
namespace QuickGraph
{
public static class EdgeExtensions
{
public const string DebuggerDisplayEdgeFormatString = "{Source}->{Target}";
public const string DebuggerDisplayTaggedEdgeFormatString = "{Source}->{Target}:{Tag}";
public const string DebuggerDisplayUndirectedEdgeFormatString = "{Source}<->{Target}";
public const string DebuggerDisplayTaggedUndirectedEdgeFormatString = "{Source}<->{Target}:{Tag}";
public const string EdgeFormatString = "{0}->{1}";
public const string TaggedEdgeFormatString = "{0}->{1}:{2}";
public const string UndirectedEdgeFormatString = "{0}<->{1}";
public const string TaggedUndirectedEdgeFormatString = "{0}<->{1}:{2}";
/// <summary>
/// Gets a value indicating if the edge is a self edge.
/// </summary>
/// <typeparam name="TVertex">type of the vertices</typeparam>
/// <typeparam name="TEdge">type of the edges</typeparam>
/// <param name="edge"></param>
/// <returns></returns>
[Pure]
public static bool IsSelfEdge<TVertex, TEdge>(
this TEdge edge)
where TEdge : IEdge<TVertex>
{
Contract.Requires(edge != null);
Contract.Ensures(Contract.Result<bool>() == (edge.Source.Equals(edge.Target)));
return edge.Source.Equals(edge.Target);
}
/// <summary>
/// Given a source vertex, returns the other vertex in the edge
/// </summary>
/// <typeparam name="TVertex">type of the vertices</typeparam>
/// <typeparam name="TEdge">type of the edges</typeparam>
/// <param name="edge">must not be a self-edge</param>
/// <param name="vertex"></param>
/// <returns></returns>
[Pure]
public static TVertex GetOtherVertex<TVertex, TEdge>(
this TEdge edge, TVertex vertex)
where TEdge : IEdge<TVertex>
{
Contract.Requires(edge != null);
Contract.Requires(vertex != null);
Contract.Requires(!edge.Source.Equals(edge.Target));
Contract.Requires(edge.Source.Equals(vertex) || edge.Target.Equals(vertex));
Contract.Ensures(Contract.Result<TVertex>() != null);
Contract.Ensures(Contract.Result<TVertex>().Equals(edge.Source.Equals(vertex) ? edge.Target : edge.Source));
return edge.Source.Equals(vertex) ? edge.Target : edge.Source;
}
/// <summary>
/// Gets a value indicating if <paramref name="vertex"/> is adjacent to <paramref name="edge"/>
/// (is the source or target).
/// </summary>
/// <typeparam name="TVertex">type of the vertices</typeparam>
/// <typeparam name="TEdge">type of the edges</typeparam>
/// <param name="edge"></param>
/// <param name="vertex"></param>
/// <returns></returns>
[Pure]
public static bool IsAdjacent<TVertex, TEdge>(
this TEdge edge, TVertex vertex)
where TEdge : IEdge<TVertex>
{
Contract.Requires(edge != null);
Contract.Requires(vertex != null);
//Contract.Ensures(Contract.Result<bool>() ==
// (edge.Source.Equals(vertex) || edge.Target.Equals(vertex))
// );
return edge.Source.Equals(vertex)
|| edge.Target.Equals(vertex);
}
[Pure]
public static bool IsPath<TVertex, TEdge>(
this IEnumerable<TEdge> path)
where TEdge : IEdge<TVertex>
{
Contract.Requires(path != null);
Contract.Requires(typeof(TEdge).GetTypeInfo().IsValueType || Enumerable.All(path, e => e != null));
bool first = true;
TVertex lastTarget = default(TVertex);
foreach (var edge in path)
{
if (first)
{
lastTarget = edge.Target;
first = false;
}
else
{
if (!lastTarget.Equals(edge.Source))
return false;
lastTarget = edge.Target;
}
}
return true;
}
[Pure]
public static bool HasCycles<TVertex, TEdge>(
this IEnumerable<TEdge> path)
where TEdge : IEdge<TVertex>
{
Contract.Requires(path != null);
Contract.Requires(typeof(TEdge).GetTypeInfo().IsValueType || Enumerable.All(path, e => e != null));
var vertices = new Dictionary<TVertex, int>();
bool first = true;
foreach (var edge in path)
{
if (first)
{
if (edge.Source.Equals(edge.Target)) // self-edge
return true;
vertices.Add(edge.Source, 0);
vertices.Add(edge.Target, 0);
first = false;
}
else
{
if (vertices.ContainsKey(edge.Target))
return true;
vertices.Add(edge.Target, 0);
}
}
return false;
}
[Pure]
public static bool IsPathWithoutCycles<TVertex, TEdge>(
this IEnumerable<TEdge> path)
where TEdge : IEdge<TVertex>
{
Contract.Requires(path != null);
Contract.Requires(typeof(TEdge).GetTypeInfo().IsValueType || Enumerable.All(path, e => e != null));
Contract.Requires(IsPath<TVertex, TEdge>(path));
var vertices = new Dictionary<TVertex, int>();
bool first = true;
TVertex lastTarget = default(TVertex);
foreach (var edge in path)
{
if (first)
{
lastTarget = edge.Target;
if (IsSelfEdge<TVertex, TEdge>(edge))
return false;
vertices.Add(edge.Source, 0);
vertices.Add(lastTarget, 0);
first = false;
}
else
{
if (!lastTarget.Equals(edge.Source))
return false;
if (vertices.ContainsKey(edge.Target))
return false;
lastTarget = edge.Target;
vertices.Add(edge.Target, 0);
}
}
return true;
}
/// <summary>
/// Creates a vertex pair (source, target) from the edge
/// </summary>
/// <typeparam name="TVertex">type of the vertices</typeparam>
/// <typeparam name="TEdge">type of the edges</typeparam>
/// <param name="edge"></param>
/// <returns></returns>
public static SEquatableEdge<TVertex> ToVertexPair<TVertex, TEdge>(
this TEdge edge)
where TEdge : IEdge<TVertex>
{
Contract.Requires(edge != null);
Contract.Ensures(Contract.Result<SEquatableEdge<TVertex>>().Source.Equals(edge.Source));
Contract.Ensures(Contract.Result<SEquatableEdge<TVertex>>().Target.Equals(edge.Target));
return new SEquatableEdge<TVertex>(edge.Source, edge.Target);
}
/// <summary>
/// Checks that <paramref name="root"/> is a predecessor of <paramref name="vertex"/>
/// </summary>
/// <typeparam name="TVertex">type of the vertices</typeparam>
/// <typeparam name="TEdge">type of the edges</typeparam>
/// <param name="predecessors"></param>
/// <param name="root"></param>
/// <param name="vertex"></param>
/// <returns></returns>
public static bool IsPredecessor<TVertex, TEdge>(
this IDictionary<TVertex, TEdge> predecessors,
TVertex root,
TVertex vertex)
where TEdge : IEdge<TVertex>
{
Contract.Requires(predecessors != null);
Contract.Requires(root != null);
Contract.Requires(vertex != null);
Contract.Requires(
typeof(TEdge).GetTypeInfo().IsValueType ||
Enumerable.All(predecessors.Values, e => e != null));
var current = vertex;
if (root.Equals(current))
return true;
TEdge predecessor;
while(predecessors.TryGetValue(current, out predecessor))
{
var source = GetOtherVertex(predecessor, current);
if (current.Equals(source))
return false;
if (source.Equals(root))
return true;
current = source;
}
return false;
}
/// <summary>
/// Tries to get the predecessor path, if reachable.
/// </summary>
/// <typeparam name="TVertex">type of the vertices</typeparam>
/// <typeparam name="TEdge">type of the edges</typeparam>
/// <param name="predecessors"></param>
/// <param name="v"></param>
/// <param name="result"></param>
/// <returns></returns>
public static bool TryGetPath<TVertex, TEdge>(
this IDictionary<TVertex, TEdge> predecessors,
TVertex v,
out IEnumerable<TEdge> result)
where TEdge : IEdge<TVertex>
{
Contract.Requires(predecessors != null);
Contract.Requires(v != null);
Contract.Requires(
typeof(TEdge).GetTypeInfo().IsValueType ||
Enumerable.All(predecessors.Values, e => e != null));
Contract.Ensures(
!Contract.Result<bool>() ||
(Contract.ValueAtReturn<IEnumerable<TEdge>>(out result) != null &&
(typeof(TEdge).GetTypeInfo().IsValueType ||
Enumerable.All(
Contract.ValueAtReturn<IEnumerable<TEdge>>(out result),
e => e != null))
)
);
var path = new List<TEdge>();
TVertex vc = v;
TEdge edge;
while (predecessors.TryGetValue(vc, out edge))
{
path.Add(edge);
vc = GetOtherVertex(edge, vc);
}
if (path.Count > 0)
{
path.Reverse();
result = path.ToArray();
return true;
}
else
{
result = null;
return false;
}
}
/// <summary>
/// Returns the most efficient comporer for the particular type of TEdge.
/// If TEdge implements IUndirectedEdge, then only the (source,target) pair
/// has to be compared; if not, (source, target) and (target, source) have to be compared.
/// </summary>
/// <typeparam name="TVertex">type of the vertices</typeparam>
/// <typeparam name="TEdge">type of the edges</typeparam>
/// <returns></returns>
public static EdgeEqualityComparer<TVertex, TEdge> GetUndirectedVertexEquality<TVertex, TEdge>()
where TEdge : IEdge<TVertex>
{
if (typeof(IUndirectedEdge<TVertex>).IsAssignableFrom(typeof(TEdge)))
return new EdgeEqualityComparer<TVertex, TEdge>(SortedVertexEquality<TVertex, TEdge>);
else
return new EdgeEqualityComparer<TVertex, TEdge>(UndirectedVertexEquality<TVertex, TEdge>);
}
/// <summary>
/// Gets a value indicating if the vertices of edge match (source, target) or
/// (target, source)
/// </summary>
/// <typeparam name="TVertex">type of the vertices</typeparam>
/// <typeparam name="TEdge">type of the edges</typeparam>
/// <param name="edge"></param>
/// <param name="source"></param>
/// <param name="target"></param>
/// <returns></returns>
public static bool UndirectedVertexEquality<TVertex, TEdge>(
this TEdge edge,
TVertex source,
TVertex target)
where TEdge : IEdge<TVertex>
{
Contract.Requires(edge != null);
Contract.Requires(source != null);
Contract.Requires(target != null);
return (edge.Source.Equals(source) && edge.Target.Equals(target)) ||
(edge.Target.Equals(source) && edge.Source.Equals(target));
}
/// <summary>
/// Gets a value indicating if the vertices of edge match (source, target)
/// </summary>
/// <typeparam name="TVertex">type of the vertices</typeparam>
/// <typeparam name="TEdge">type of the edges</typeparam>
/// <param name="edge"></param>
/// <param name="source"></param>
/// <param name="target"></param>
/// <returns></returns>
public static bool SortedVertexEquality<TVertex, TEdge>(
this TEdge edge,
TVertex source,
TVertex target)
where TEdge : IEdge<TVertex>
{
Contract.Requires(edge != null);
Contract.Requires(source != null);
Contract.Requires(target != null);
Contract.Requires(Comparer<TVertex>.Default.Compare(source, target) <= 0);
return edge.Source.Equals(source) && edge.Target.Equals(target);
}
/// <summary>
/// Returns a reversed edge enumeration
/// </summary>
/// <param name="edges"></param>
/// <returns></returns>
public static IEnumerable<SReversedEdge<TVertex, TEdge>> ReverseEdges<TVertex, TEdge>(IEnumerable<TEdge> edges)
where TEdge : IEdge<TVertex>
{
Contract.Requires(edges != null);
Contract.Requires(Enumerable.All(edges, e => e != null));
Contract.Ensures(Contract.Result<IEnumerable<SReversedEdge<TVertex, TEdge>>>() != null);
foreach (var edge in edges)
yield return new SReversedEdge<TVertex, TEdge>(edge);
}
}
}