-
Notifications
You must be signed in to change notification settings - Fork 205
Expand file tree
/
Copy pathArrayUndirectedGraph.cs
More file actions
186 lines (162 loc) · 5.02 KB
/
ArrayUndirectedGraph.cs
File metadata and controls
186 lines (162 loc) · 5.02 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics.Contracts;
using System.Diagnostics;
namespace QuickGraph
{
/// <summary>
/// An immutable undirected graph data structure based on arrays.
/// </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 ArrayUndirectedGraph<TVertex, TEdge>
: IUndirectedGraph<TVertex, TEdge>
#if !SILVERLIGHT
, ICloneable
#endif
where TEdge : IEdge<TVertex>
{
readonly EdgeEqualityComparer<TVertex, TEdge> edgeEqualityComparer;
readonly Dictionary<TVertex, TEdge[]> vertexEdges;
readonly int edgeCount;
public ArrayUndirectedGraph(
IUndirectedGraph<TVertex, TEdge> graph)
{
Contract.Requires(graph != null);
this.edgeEqualityComparer = graph.EdgeEqualityComparer;
this.edgeCount = graph.EdgeCount;
this.vertexEdges = new Dictionary<TVertex, TEdge[]>(graph.VertexCount);
foreach (var v in graph.Vertices)
{
var edges = Enumerable.ToArray(graph.AdjacentEdges(v));
this.vertexEdges.Add(v, edges);
}
}
#region IImplicitUndirectedGraph<TVertex,TEdge> Members
public EdgeEqualityComparer<TVertex, TEdge> EdgeEqualityComparer
{
get { return this.edgeEqualityComparer; }
}
public IEnumerable<TEdge> AdjacentEdges(TVertex v)
{
var edges = this.vertexEdges[v];
return edges != null ? edges : Enumerable.Empty<TEdge>();
}
public int AdjacentDegree(TVertex v)
{
var edges = this.vertexEdges[v];
return edges != null ? edges.Length : 0;
}
public bool IsAdjacentEdgesEmpty(TVertex v)
{
return this.vertexEdges[v] != null;
}
public TEdge AdjacentEdge(TVertex v, int index)
{
return this.vertexEdges[v][index];
}
public bool TryGetEdge(TVertex source, TVertex target, out TEdge edge)
{
var edges = this.vertexEdges[source];
if (edges != null)
for (int i = 0; i < edges.Length; i++)
{
if (this.edgeEqualityComparer(edges[i], source, target))
{
edge = edges[i];
return true;
}
}
edge = default(TEdge);
return false;
}
public bool ContainsEdge(TVertex source, TVertex target)
{
TEdge edge;
return this.TryGetEdge(source, target, out edge);
}
#endregion
#region IImplicitVertexSet<TVertex> Members
public bool ContainsVertex(TVertex vertex)
{
return this.vertexEdges.ContainsKey(vertex);
}
#endregion
#region IGraph<TVertex,TEdge> Members
public bool IsDirected
{
get { return false; }
}
public bool AllowParallelEdges
{
get { return true; }
}
#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 edges in this.vertexEdges.Values)
if (edges != null)
for (int i = 0; i < edges.Length; i++)
yield return edges[i];
}
}
public bool ContainsEdge(TEdge edge)
{
var source = edge.Source;
TEdge[] edges;
if (this.vertexEdges.TryGetValue(source, out edges))
for (int i = 0; i < edges.Length; i++)
if (edges[i].Equals(edge))
return true;
return false;
}
#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 ICloneable Members
/// <summary>
/// Returns self
/// </summary>
/// <returns></returns>
public ArrayUndirectedGraph<TVertex, TEdge> Clone()
{
return this;
}
#if !SILVERLIGHT
object ICloneable.Clone()
{
return this;
}
#endif
#endregion
}
}