-
Notifications
You must be signed in to change notification settings - Fork 292
/
Graph.cs
342 lines (259 loc) · 10.4 KB
/
Graph.cs
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace Advanced.Algorithms.DataStructures.Graph.AdjacencyMatrix;
/// <summary>
/// A directed graph implementation using dynamically growing/shrinking adjacency matrix array.
/// IEnumerable enumerates all vertices.
/// </summary>
public class Graph<T> : IGraph<T>, IEnumerable<T>
{
private BitArray[] matrix;
private int nextAvailableIndex;
private Dictionary<int, T> reverseVertexIndices;
private Dictionary<T, int> vertexIndices;
private readonly Dictionary<T, GraphVertex<T>> vertexObjects;
public Graph()
{
vertexIndices = new Dictionary<T, int>();
reverseVertexIndices = new Dictionary<int, T>();
vertexObjects = new Dictionary<T, GraphVertex<T>>();
matrix = new BitArray[1];
for (var i = 0; i < MaxSize; i++) matrix[i] = new BitArray(MaxSize);
}
private int MaxSize => matrix.Length;
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public IEnumerator<T> GetEnumerator()
{
return vertexIndices.Select(x => x.Key).GetEnumerator();
}
public int VerticesCount { get; private set; }
public bool IsWeightedGraph => false;
public IGraphVertex<T> ReferenceVertex => GetReferenceVertex();
/// <summary>
/// Do we have an edge between the given source and destination?
/// Time complexity: O(1).
/// </summary>
public bool HasEdge(T source, T dest)
{
if (source == null || dest == null) throw new ArgumentException();
if (!vertexIndices.ContainsKey(source) || !vertexIndices.ContainsKey(dest))
throw new Exception("Source or destination vertex does'nt exist.");
var sourceIndex = vertexIndices[source];
var destIndex = vertexIndices[dest];
if (matrix[sourceIndex].Get(destIndex) && matrix[destIndex].Get(sourceIndex)) return true;
return false;
}
public IEnumerable<IGraphVertex<T>> VerticesAsEnumberable => vertexObjects.Select(x => x.Value);
public bool ContainsVertex(T key)
{
return vertexObjects.ContainsKey(key);
}
public IGraphVertex<T> GetVertex(T key)
{
return vertexObjects[key];
}
IGraph<T> IGraph<T>.Clone()
{
return Clone();
}
private GraphVertex<T> GetReferenceVertex()
{
if (VerticesCount == 0) throw new Exception("Empty graph.");
return vertexObjects[this.First()];
}
/// <summary>
/// Add a new vertex to this graph.
/// Time complexity: O(1).
/// </summary>
public void AddVertex(T value)
{
if (value == null) throw new ArgumentNullException();
if (vertexIndices.ContainsKey(value)) throw new Exception("Vertex exists.");
if (VerticesCount < MaxSize / 2) HalfMatrixSize();
if (nextAvailableIndex == MaxSize) DoubleMatrixSize();
vertexIndices.Add(value, nextAvailableIndex);
reverseVertexIndices.Add(nextAvailableIndex, value);
vertexObjects.Add(value, new GraphVertex<T>(this, value));
nextAvailableIndex++;
VerticesCount++;
}
/// <summary>
/// Remove an existing vertex from graph.
/// Time complexity: O(V) where V is the number of vertices.
/// </summary>
public void RemoveVertex(T value)
{
if (value == null) throw new ArgumentNullException();
if (!vertexIndices.ContainsKey(value)) throw new Exception("Vertex does'nt exist.");
if (VerticesCount <= MaxSize / 2) HalfMatrixSize();
var index = vertexIndices[value];
//clear edges
for (var i = 0; i < MaxSize; i++)
{
matrix[i].Set(index, false);
matrix[index].Set(i, false);
}
reverseVertexIndices.Remove(index);
vertexIndices.Remove(value);
vertexObjects.Remove(value);
VerticesCount--;
}
/// <summary>
/// Add an edge from source to destination vertex.
/// Time complexity: O(1).
/// </summary>
public void AddEdge(T source, T dest)
{
if (source == null || dest == null) throw new ArgumentException();
if (!vertexIndices.ContainsKey(source) || !vertexIndices.ContainsKey(dest))
throw new Exception("Source or destination vertex does'nt exist.");
var sourceIndex = vertexIndices[source];
var destIndex = vertexIndices[dest];
if (matrix[sourceIndex].Get(destIndex) && matrix[destIndex].Get(sourceIndex))
throw new Exception("Edge already exists.");
matrix[sourceIndex].Set(destIndex, true);
matrix[destIndex].Set(sourceIndex, true);
}
/// <summary>
/// Remove an existing edge between source and destination.
/// Time complexity: O(1).
/// </summary>
public void RemoveEdge(T source, T dest)
{
if (source == null || dest == null) throw new ArgumentException();
if (!vertexIndices.ContainsKey(source) || !vertexIndices.ContainsKey(dest))
throw new Exception("Source or destination vertex does'nt exist.");
var sourceIndex = vertexIndices[source];
var destIndex = vertexIndices[dest];
if (!matrix[sourceIndex].Get(destIndex) || !matrix[destIndex].Get(sourceIndex))
throw new Exception("Edge do not exists.");
matrix[sourceIndex].Set(destIndex, false);
matrix[destIndex].Set(sourceIndex, false);
}
public IEnumerable<T> Edges(T vertex)
{
if (!vertexIndices.ContainsKey(vertex)) throw new ArgumentException("vertex is not in this graph.");
var index = vertexIndices[vertex];
for (var i = 0; i < MaxSize; i++)
if (matrix[i].Get(index))
yield return reverseVertexIndices[i];
}
public int EdgesCount(T vertex)
{
if (!vertexIndices.ContainsKey(vertex)) throw new ArgumentException("vertex is not in this graph.");
var count = 0;
var index = vertexIndices[vertex];
for (var i = 0; i < MaxSize; i++)
if (matrix[i].Get(index))
count++;
return count;
}
private void DoubleMatrixSize()
{
var newMatrix = new BitArray[MaxSize * 2];
for (var i = 0; i < MaxSize * 2; i++) newMatrix[i] = new BitArray(MaxSize * 2);
var newVertexIndices = new Dictionary<T, int>();
var newReverseIndices = new Dictionary<int, T>();
var k = 0;
foreach (var vertex in vertexIndices)
{
newVertexIndices.Add(vertex.Key, k);
newReverseIndices.Add(k, vertex.Key);
k++;
}
nextAvailableIndex = k;
for (var i = 0; i < MaxSize; i++)
for (var j = i; j < MaxSize; j++)
if (matrix[i].Get(j) && matrix[j].Get(i)
&& reverseVertexIndices.ContainsKey(i)
&& reverseVertexIndices.ContainsKey(j))
{
var newI = newVertexIndices[reverseVertexIndices[i]];
var newJ = newVertexIndices[reverseVertexIndices[j]];
newMatrix[newI].Set(newJ, true);
newMatrix[newJ].Set(newI, true);
}
matrix = newMatrix;
vertexIndices = newVertexIndices;
reverseVertexIndices = newReverseIndices;
}
private void HalfMatrixSize()
{
var newMatrix = new BitArray[MaxSize / 2];
for (var i = 0; i < MaxSize / 2; i++) newMatrix[i] = new BitArray(MaxSize / 2);
var newVertexIndices = new Dictionary<T, int>();
var newReverseIndices = new Dictionary<int, T>();
var k = 0;
foreach (var vertex in vertexIndices)
{
newVertexIndices.Add(vertex.Key, k);
newReverseIndices.Add(k, vertex.Key);
k++;
}
nextAvailableIndex = k;
for (var i = 0; i < MaxSize; i++)
for (var j = i; j < MaxSize; j++)
if (matrix[i].Get(j) && matrix[j].Get(i)
&& reverseVertexIndices.ContainsKey(i)
&& reverseVertexIndices.ContainsKey(j))
{
var newI = newVertexIndices[reverseVertexIndices[i]];
var newJ = newVertexIndices[reverseVertexIndices[j]];
newMatrix[newI].Set(newJ, true);
newMatrix[newJ].Set(newI, true);
}
matrix = newMatrix;
vertexIndices = newVertexIndices;
reverseVertexIndices = newReverseIndices;
}
public Graph<T> Clone()
{
var graph = new Graph<T>();
foreach (var vertex in this) graph.AddVertex(vertex);
foreach (var vertex in this)
foreach (var edge in Edges(vertex))
graph.AddEdge(vertex, edge);
return graph;
}
private class GraphVertex<T> : IGraphVertex<T>
{
private readonly Graph<T> graph;
private int vertexIndex;
internal GraphVertex(Graph<T> graph, T vertexKey)
{
if (!graph.vertexIndices.ContainsKey(vertexKey))
throw new ArgumentException("vertex is not in this graph.");
this.graph = graph;
Key = vertexKey;
vertexIndex = graph.vertexIndices[vertexKey];
}
private int MaxSize => graph.MaxSize;
private BitArray[] Matrix => graph.matrix;
private Dictionary<T, int> VertexIndices => graph.vertexIndices;
private Dictionary<int, T> ReverseVertexIndices => graph.reverseVertexIndices;
public T Key { get; }
IEnumerable<IEdge<T>> IGraphVertex<T>.Edges => graph.Edges(Key)
.Select(x => new Edge<T, int>(graph.vertexObjects[x], 1));
public IEdge<T> GetEdge(IGraphVertex<T> targetVertex)
{
if (!VertexIndices.ContainsKey(targetVertex.Key))
throw new ArgumentException("vertex is not in this graph.");
var index = VertexIndices[targetVertex.Key];
var key = targetVertex as GraphVertex<T>;
return new Edge<T, int>(targetVertex, 1);
}
public IEdge<T> GetOutEdge(IGraphVertex<T> targetVertex)
{
if (!VertexIndices.ContainsKey(targetVertex.Key))
throw new ArgumentException("vertex is not in this graph.");
var index = VertexIndices[targetVertex.Key];
var key = targetVertex as GraphVertex<T>;
return new Edge<T, int>(targetVertex, 1);
}
}
}