Skip to content

Commit f6f7561

Browse files
committed
use graph interfaces
1 parent 68314de commit f6f7561

32 files changed

+579
-288
lines changed

src/Advanced.Algorithms/DataStructures/Graph/AdjacencyList/DiGraph.cs

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Advanced.Algorithms.DataStructures.Graph.AdjacencyList
99
/// A directed graph implementation.
1010
/// IEnumerable enumerates all vertices.
1111
/// </summary>
12-
public class DiGraph<T> : IEnumerable<T>
12+
public class DiGraph<T> : IDiGraph<T>
1313
{
1414
public int VerticesCount => Vertices.Count;
1515
internal Dictionary<T, DiGraphVertex<T>> Vertices { get; set; }
@@ -33,21 +33,23 @@ public DiGraphVertex<T> ReferenceVertex
3333
{
3434
return enumerator.Current.Value;
3535
}
36-
36+
3737
}
3838

3939
return null;
4040
}
4141
}
4242

43+
IDiGraphVertex<T> IDiGraph<T>.ReferenceVertex => ReferenceVertex;
44+
4345

4446
/// <summary>
4547
/// Add a new vertex to this graph.
4648
/// Time complexity: O(1).
4749
/// </summary>
4850
public DiGraphVertex<T> AddVertex(T value)
4951
{
50-
if ( value == null)
52+
if (value == null)
5153
{
5254
throw new ArgumentNullException();
5355
}
@@ -129,7 +131,7 @@ public void RemoveEdge(T source, T dest)
129131
throw new Exception("Source or Destination Vertex is not in this graph.");
130132
}
131133

132-
if (!Vertices[source].OutEdges.Contains(Vertices[dest])
134+
if (!Vertices[source].OutEdges.Contains(Vertices[dest])
133135
|| !Vertices[dest].InEdges.Contains(Vertices[source]))
134136
{
135137
throw new Exception("Edge do not exists.");
@@ -150,7 +152,7 @@ public bool HasEdge(T source, T dest)
150152
throw new ArgumentException("source or destination is not in this graph.");
151153
}
152154

153-
return Vertices[source].OutEdges.Contains(Vertices[dest])
155+
return Vertices[source].OutEdges.Contains(Vertices[dest])
154156
&& Vertices[dest].InEdges.Contains(Vertices[source]);
155157
}
156158

@@ -161,7 +163,7 @@ public IEnumerable<T> OutEdges(T vertex)
161163
throw new ArgumentException("vertex is not in this graph.");
162164
}
163165

164-
return Vertices[vertex].OutEdges.Select(x=>x.Value);
166+
return Vertices[vertex].OutEdges.Select(x => x.Value);
165167
}
166168

167169
public IEnumerable<T> InEdges(T vertex)
@@ -186,7 +188,7 @@ public DiGraphVertex<T> FindVertex(T value)
186188
/// <summary>
187189
/// Clones this graph.
188190
/// </summary>
189-
internal DiGraph<T> Clone()
191+
public DiGraph<T> Clone()
190192
{
191193
var newGraph = new DiGraph<T>();
192194

@@ -206,35 +208,62 @@ internal DiGraph<T> Clone()
206208
return newGraph;
207209
}
208210

209-
IEnumerator IEnumerable.GetEnumerator()
211+
public bool ContainsVertex(T value)
210212
{
211-
return GetEnumerator();
213+
return Vertices.ContainsKey(value);
212214
}
213215

214-
public IEnumerator<T> GetEnumerator()
216+
public IDiGraphVertex<T> GetVertex(T value)
215217
{
216-
return Vertices.Select(x => x.Key).GetEnumerator();
218+
return Vertices[value];
219+
}
220+
221+
IEnumerator<IDiGraphVertex<T>> IEnumerable<IDiGraphVertex<T>>.GetEnumerator()
222+
{
223+
return GetEnumerator() as IEnumerator<IDiGraphVertex<T>>;
224+
}
225+
226+
public IEnumerator GetEnumerator()
227+
{
228+
return Vertices.Select(x => x.Value).GetEnumerator();
229+
}
230+
231+
IDiGraph<T> IDiGraph<T>.Clone()
232+
{
233+
throw new NotImplementedException();
217234
}
218235
}
219236

220237
/// <summary>
221238
/// DiGraph vertex for adjacency list Graph implementation.
222239
/// IEnumerable enumerates all the outgoing edge destination vertices.
223240
/// </summary>
224-
public class DiGraphVertex<T> : IEnumerable<T>
241+
public class DiGraphVertex<T> : IDiGraphVertex<T>, IEnumerable<T>
225242
{
226243
public T Value { get; set; }
227244

228245
public HashSet<DiGraphVertex<T>> OutEdges { get; set; }
229246
public HashSet<DiGraphVertex<T>> InEdges { get; set; }
230247

248+
IEnumerable<IDiEdge<T>> IDiGraphVertex<T>.OutEdges => OutEdges.Select(x => new DiEdge<T, int>(x, 1));
249+
IEnumerable<IDiEdge<T>> IDiGraphVertex<T>.InEdges => InEdges.Select(x => new DiEdge<T, int>(x, 1));
250+
251+
public int OutEdgeCount => OutEdges.Count;
252+
public int InEdgeCount => InEdges.Count;
253+
231254
public DiGraphVertex(T value)
232255
{
233256
Value = value;
234257
OutEdges = new HashSet<DiGraphVertex<T>>();
235258
InEdges = new HashSet<DiGraphVertex<T>>();
236259
}
237260

261+
262+
public IDiEdge<T> GetOutEdge(IDiGraphVertex<T> targetVertex)
263+
{
264+
return new DiEdge<T, int>(targetVertex, 1);
265+
}
266+
238267
IEnumerator IEnumerable.GetEnumerator()
239268
{
240269
return GetEnumerator();
@@ -244,5 +273,6 @@ public IEnumerator<T> GetEnumerator()
244273
{
245274
return OutEdges.Select(x => x.Value).GetEnumerator();
246275
}
276+
247277
}
248278
}

src/Advanced.Algorithms/DataStructures/Graph/AdjacencyList/Graph.cs

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Advanced.Algorithms.DataStructures.Graph.AdjacencyList
99
/// A graph implementation
1010
/// IEnumerable enumerates all vertices.
1111
/// </summary>
12-
public class Graph<T> : IEnumerable<T>
12+
public class Graph<T> : IGraph<T>
1313
{
1414
public int VerticesCount => Vertices.Count;
1515
internal Dictionary<T, GraphVertex<T>> Vertices { get; set; }
@@ -39,6 +39,8 @@ public GraphVertex<T> ReferenceVertex
3939
}
4040
}
4141

42+
IGraphVertex<T> IGraph<T>.ReferenceVertex => ReferenceVertex;
43+
4244

4345
/// <summary>
4446
/// Add a new vertex to this graph.
@@ -98,7 +100,7 @@ public void AddEdge(T source, T dest)
98100
throw new Exception("Source or Destination Vertex is not in this graph.");
99101
}
100102

101-
if (Vertices[source].Edges.Contains(Vertices[dest])
103+
if (Vertices[source].Edges.Contains(Vertices[dest])
102104
|| Vertices[dest].Edges.Contains(Vertices[source]))
103105
{
104106
throw new Exception("Edge already exists.");
@@ -125,7 +127,7 @@ public void RemoveEdge(T source, T dest)
125127
throw new Exception("Source or Destination Vertex is not in this graph.");
126128
}
127129

128-
if (!Vertices[source].Edges.Contains(Vertices[dest])
130+
if (!Vertices[source].Edges.Contains(Vertices[dest])
129131
|| !Vertices[dest].Edges.Contains(Vertices[source]))
130132
{
131133
throw new Exception("Edge do not exists.");
@@ -146,7 +148,7 @@ public bool HasEdge(T source, T dest)
146148
throw new ArgumentException("source or destination is not in this graph.");
147149
}
148150

149-
return Vertices[source].Edges.Contains(Vertices[dest])
151+
return Vertices[source].Edges.Contains(Vertices[dest])
150152
&& Vertices[dest].Edges.Contains(Vertices[source]);
151153
}
152154

@@ -174,28 +176,67 @@ public GraphVertex<T> FindVertex(T value)
174176
return null;
175177
}
176178

177-
IEnumerator IEnumerable.GetEnumerator()
179+
public bool ContainsVertex(T value)
178180
{
179-
return GetEnumerator();
181+
return Vertices.ContainsKey(value);
180182
}
181183

182-
public IEnumerator<T> GetEnumerator()
184+
public IGraphVertex<T> GetVertex(T value)
183185
{
184-
return Vertices.Select(x => x.Key).GetEnumerator();
186+
return Vertices[value];
185187
}
186188

189+
/// <summary>
190+
/// Clones this graph.
191+
/// </summary>
192+
public Graph<T> Clone()
193+
{
194+
var newGraph = new Graph<T>();
195+
196+
foreach (var vertex in Vertices)
197+
{
198+
newGraph.AddVertex(vertex.Key);
199+
}
200+
201+
foreach (var vertex in Vertices)
202+
{
203+
foreach (var edge in vertex.Value.Edges)
204+
{
205+
newGraph.AddEdge(vertex.Value.Value, edge.Value);
206+
}
207+
}
208+
209+
return newGraph;
210+
}
211+
212+
IEnumerator<IGraphVertex<T>> IEnumerable<IGraphVertex<T>>.GetEnumerator()
213+
{
214+
return GetEnumerator() as IEnumerator<IGraphVertex<T>>;
215+
}
216+
217+
public IEnumerator GetEnumerator()
218+
{
219+
return Vertices.Select(x => x.Value).GetEnumerator();
220+
}
221+
222+
IGraph<T> IGraph<T>.Clone()
223+
{
224+
return Clone();
225+
}
187226
}
188227

189228
/// <summary>
190229
/// Graph vertex for adjacency list Graph implementation.
191230
/// IEnumerable enumerates all the outgoing edge destination vertices.
192231
/// </summary>
193-
public class GraphVertex<T> : IEnumerable<T>
232+
public class GraphVertex<T> : IEnumerable<T>, IGraphVertex<T>
194233
{
195234
public T Value { get; set; }
196235

197236
public HashSet<GraphVertex<T>> Edges { get; set; }
198237

238+
IEnumerable<IEdge<T>> IGraphVertex<T>.Edges => Edges.Select(x => new Edge<T, int>(x, 1));
239+
199240
public GraphVertex(T value)
200241
{
201242
Value = value;

0 commit comments

Comments
 (0)