Skip to content

Commit c587c53

Browse files
committed
argument checks
1 parent ecf7186 commit c587c53

33 files changed

+547
-374
lines changed

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

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class DiGraph<T> : IDiGraph<T>
1313
{
1414
public int VerticesCount => Vertices.Count;
1515
internal Dictionary<T, DiGraphVertex<T>> Vertices { get; set; }
16+
public bool IsWeightedGraph => false;
1617

1718
public DiGraph()
1819
{
@@ -41,8 +42,7 @@ public DiGraphVertex<T> ReferenceVertex
4142
}
4243

4344
IDiGraphVertex<T> IDiGraph<T>.ReferenceVertex => ReferenceVertex;
44-
45-
45+
4646
/// <summary>
4747
/// Add a new vertex to this graph.
4848
/// Time complexity: O(1).
@@ -163,7 +163,7 @@ public IEnumerable<T> OutEdges(T vertex)
163163
throw new ArgumentException("vertex is not in this graph.");
164164
}
165165

166-
return Vertices[vertex].OutEdges.Select(x => x.Value);
166+
return Vertices[vertex].OutEdges.Select(x => x.Key);
167167
}
168168

169169
public IEnumerable<T> InEdges(T vertex)
@@ -173,7 +173,7 @@ public IEnumerable<T> InEdges(T vertex)
173173
throw new ArgumentException("vertex is not in this graph.");
174174
}
175175

176-
return Vertices[vertex].InEdges.Select(x => x.Value);
176+
return Vertices[vertex].InEdges.Select(x => x.Key);
177177
}
178178

179179
/// <summary>
@@ -201,7 +201,7 @@ public DiGraph<T> Clone()
201201
{
202202
foreach (var edge in vertex.Value.OutEdges)
203203
{
204-
newGraph.AddEdge(vertex.Value.Value, edge.Value);
204+
newGraph.AddEdge(vertex.Value.Key, edge.Key);
205205
}
206206
}
207207

@@ -218,6 +218,11 @@ public IDiGraphVertex<T> GetVertex(T value)
218218
return Vertices[value];
219219
}
220220

221+
IDiGraph<T> IDiGraph<T>.Clone()
222+
{
223+
return Clone();
224+
}
225+
221226
IEnumerator<IDiGraphVertex<T>> IEnumerable<IDiGraphVertex<T>>.GetEnumerator()
222227
{
223228
return GetEnumerator() as IEnumerator<IDiGraphVertex<T>>;
@@ -228,10 +233,6 @@ public IEnumerator GetEnumerator()
228233
return Vertices.Select(x => x.Value).GetEnumerator();
229234
}
230235

231-
IDiGraph<T> IDiGraph<T>.Clone()
232-
{
233-
throw new NotImplementedException();
234-
}
235236
}
236237

237238
/// <summary>
@@ -240,7 +241,7 @@ IDiGraph<T> IDiGraph<T>.Clone()
240241
/// </summary>
241242
public class DiGraphVertex<T> : IDiGraphVertex<T>, IEnumerable<T>
242243
{
243-
public T Value { get; set; }
244+
public T Key { get; set; }
244245

245246
public HashSet<DiGraphVertex<T>> OutEdges { get; set; }
246247
public HashSet<DiGraphVertex<T>> InEdges { get; set; }
@@ -253,7 +254,7 @@ public class DiGraphVertex<T> : IDiGraphVertex<T>, IEnumerable<T>
253254

254255
public DiGraphVertex(T value)
255256
{
256-
Value = value;
257+
Key = value;
257258
OutEdges = new HashSet<DiGraphVertex<T>>();
258259
InEdges = new HashSet<DiGraphVertex<T>>();
259260
}
@@ -271,7 +272,7 @@ IEnumerator IEnumerable.GetEnumerator()
271272

272273
public IEnumerator<T> GetEnumerator()
273274
{
274-
return OutEdges.Select(x => x.Value).GetEnumerator();
275+
return OutEdges.Select(x => x.Key).GetEnumerator();
275276
}
276277

277278
}

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class Graph<T> : IGraph<T>
1313
{
1414
public int VerticesCount => Vertices.Count;
1515
internal Dictionary<T, GraphVertex<T>> Vertices { get; set; }
16+
public bool IsWeightedGraph => false;
1617

1718
public Graph()
1819
{
@@ -159,7 +160,7 @@ public IEnumerable<T> Edges(T vertex)
159160
throw new ArgumentException("vertex is not in this graph.");
160161
}
161162

162-
return Vertices[vertex].Edges.Select(x => x.Value);
163+
return Vertices[vertex].Edges.Select(x => x.Key);
163164
}
164165

165166
/// <summary>
@@ -202,7 +203,7 @@ public Graph<T> Clone()
202203
{
203204
foreach (var edge in vertex.Value.Edges)
204205
{
205-
newGraph.AddEdge(vertex.Value.Value, edge.Value);
206+
newGraph.AddEdge(vertex.Value.Key, edge.Key);
206207
}
207208
}
208209

@@ -231,15 +232,15 @@ IGraph<T> IGraph<T>.Clone()
231232
/// </summary>
232233
public class GraphVertex<T> : IEnumerable<T>, IGraphVertex<T>
233234
{
234-
public T Value { get; set; }
235+
public T Key { get; set; }
235236

236237
public HashSet<GraphVertex<T>> Edges { get; set; }
237238

238239
IEnumerable<IEdge<T>> IGraphVertex<T>.Edges => Edges.Select(x => new Edge<T, int>(x, 1));
239240

240241
public GraphVertex(T value)
241242
{
242-
Value = value;
243+
Key = value;
243244
Edges = new HashSet<GraphVertex<T>>();
244245
}
245246

@@ -250,7 +251,7 @@ IEnumerator IEnumerable.GetEnumerator()
250251

251252
public IEnumerator<T> GetEnumerator()
252253
{
253-
return Edges.Select(x => x.Value).GetEnumerator();
254+
return Edges.Select(x => x.Key).GetEnumerator();
254255
}
255256
}
256257
}

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class WeightedDiGraph<T, TW> : IDiGraph<T> where TW : IComparable
1313
{
1414
public int VerticesCount => Vertices.Count;
1515
internal Dictionary<T, WeightedDiGraphVertex<T, TW>> Vertices { get; set; }
16+
public bool IsWeightedGraph => true;
1617

1718
public WeightedDiGraph()
1819
{
@@ -164,7 +165,7 @@ public IEnumerable<Tuple<T, TW>> OutEdges(T vertex)
164165
throw new ArgumentException("vertex is not in this graph.");
165166
}
166167

167-
return Vertices[vertex].OutEdges.Select(x => new Tuple<T, TW>(x.Key.Value, x.Value));
168+
return Vertices[vertex].OutEdges.Select(x => new Tuple<T, TW>(x.Key.Key, x.Value));
168169
}
169170

170171
public IEnumerable<Tuple<T, TW>> InEdges(T vertex)
@@ -174,7 +175,7 @@ public IEnumerable<Tuple<T, TW>> InEdges(T vertex)
174175
throw new ArgumentException("vertex is not in this graph.");
175176
}
176177

177-
return Vertices[vertex].InEdges.Select(x => new Tuple<T, TW>(x.Key.Value, x.Value));
178+
return Vertices[vertex].InEdges.Select(x => new Tuple<T, TW>(x.Key.Key, x.Value));
178179
}
179180

180181
/// <summary>
@@ -217,7 +218,7 @@ public WeightedDiGraph<T, TW> Clone()
217218
{
218219
foreach (var edge in vertex.Value.OutEdges)
219220
{
220-
newGraph.AddEdge(vertex.Value.Value, edge.Key.Value, edge.Value);
221+
newGraph.AddEdge(vertex.Value.Key, edge.Key.Key, edge.Value);
221222
}
222223
}
223224

@@ -246,7 +247,7 @@ IDiGraph<T> IDiGraph<T>.Clone()
246247
/// </summary>
247248
public class WeightedDiGraphVertex<T, TW> : IDiGraphVertex<T>, IEnumerable<T> where TW : IComparable
248249
{
249-
public T Value { get; private set; }
250+
public T Key { get; private set; }
250251

251252
public Dictionary<WeightedDiGraphVertex<T, TW>, TW> OutEdges { get; set; }
252253
public Dictionary<WeightedDiGraphVertex<T, TW>, TW> InEdges { get; set; }
@@ -259,7 +260,7 @@ public class WeightedDiGraphVertex<T, TW> : IDiGraphVertex<T>, IEnumerable<T> wh
259260

260261
public WeightedDiGraphVertex(T value)
261262
{
262-
Value = value;
263+
Key = value;
263264

264265
OutEdges = new Dictionary<WeightedDiGraphVertex<T, TW>, TW>();
265266
InEdges = new Dictionary<WeightedDiGraphVertex<T, TW>, TW>();
@@ -278,7 +279,7 @@ IEnumerator IEnumerable.GetEnumerator()
278279

279280
public IEnumerator<T> GetEnumerator()
280281
{
281-
return OutEdges.Select(x => x.Key.Value).GetEnumerator();
282+
return OutEdges.Select(x => x.Key.Key).GetEnumerator();
282283
}
283284

284285

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class WeightedGraph<T, TW> : IGraph<T> where TW : IComparable
1313
{
1414
public int VerticesCount => Vertices.Count;
1515
internal Dictionary<T, WeightedGraphVertex<T, TW>> Vertices { get; set; }
16+
public bool IsWeightedGraph => true;
1617

1718
public WeightedGraph()
1819
{
@@ -231,7 +232,7 @@ public class WeightedGraphVertex<T, TW> : IGraphVertex<T>, IEnumerable<T> where
231232
public T Value { get; private set; }
232233

233234
public Dictionary<WeightedGraphVertex<T, TW>, TW> Edges { get; }
234-
T IGraphVertex<T>.Value => Value;
235+
T IGraphVertex<T>.Key => Value;
235236

236237
IEnumerable<IEdge<T>> IGraphVertex<T>.Edges => Edges.Select(x => new Edge<T, TW>(x.Key, x.Value));
237238

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

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,55 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
63

74
namespace Advanced.Algorithms.DataStructures.Graph
85
{
96
public interface IDiGraph<T> : IEnumerable<IDiGraphVertex<T>>
107
{
8+
bool IsWeightedGraph { get; }
9+
1110
bool ContainsVertex(T value);
12-
IDiGraphVertex<T> GetVertex(T value);
11+
IDiGraphVertex<T> GetVertex(T key);
1312
IDiGraphVertex<T> ReferenceVertex { get; }
1413

1514
int VerticesCount { get; }
1615

1716
bool HasEdge(T source, T destination);
1817

19-
IDiGraph<T> Clone();
20-
18+
IDiGraph<T> Clone();
2119
}
2220

2321
public interface IDiGraphVertex<T>
2422
{
25-
T Value { get; }
23+
T Key { get; }
2624
IEnumerable<IDiEdge<T>> OutEdges { get; }
2725
IEnumerable<IDiEdge<T>> InEdges { get; }
2826

2927
IDiEdge<T> GetOutEdge(IDiGraphVertex<T> targetVertex);
3028

3129
int OutEdgeCount { get; }
3230
int InEdgeCount { get; }
33-
34-
3531
}
3632

3733
public interface IDiEdge<T>
3834
{
3935
W Weight<W>() where W : IComparable;
40-
T Value { get; }
41-
IDiGraphVertex<T> Target { get; }
36+
T TargetVertexKey { get; }
37+
IDiGraphVertex<T> TargetVertex { get; }
4238
}
4339

4440
internal class DiEdge<T, C> : IDiEdge<T> where C : IComparable
4541
{
46-
private IDiGraphVertex<T> target;
4742
private object weight;
4843

4944
internal DiEdge(IDiGraphVertex<T> target, C weight)
5045
{
51-
this.target = target;
46+
TargetVertex = target;
5247
this.weight = weight;
5348
}
5449

55-
public T Value => target.Value;
50+
public T TargetVertexKey => TargetVertex.Key;
5651

57-
public IDiGraphVertex<T> Target => target;
52+
public IDiGraphVertex<T> TargetVertex { get; private set; }
5853

5954
public W Weight<W>() where W : IComparable
6055
{

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ namespace Advanced.Algorithms.DataStructures.Graph
88
{
99
public interface IGraph<T> : IEnumerable<IGraphVertex<T>>
1010
{
11+
bool IsWeightedGraph { get; }
12+
1113
int VerticesCount { get; }
1214

1315
IGraphVertex<T> ReferenceVertex { get; }
14-
bool ContainsVertex(T value);
15-
IGraphVertex<T> GetVertex(T value);
16+
bool ContainsVertex(T key);
17+
IGraphVertex<T> GetVertex(T key);
1618

1719
bool HasEdge(T source, T destination);
1820

@@ -21,31 +23,30 @@ public interface IGraph<T> : IEnumerable<IGraphVertex<T>>
2123

2224
public interface IGraphVertex<T>
2325
{
24-
T Value { get; }
26+
T Key { get; }
2527
IEnumerable<IEdge<T>> Edges { get; }
2628
}
2729

2830
public interface IEdge<T>
2931
{
3032
W Weight<W>() where W : IComparable;
31-
T Value { get; }
32-
IGraphVertex<T> Target { get; }
33+
T TargetVertexKey { get; }
34+
IGraphVertex<T> TargetVertex { get; }
3335
}
3436

3537
internal class Edge<T, C> : IEdge<T> where C : IComparable
3638
{
37-
private IGraphVertex<T> target;
3839
private object weight;
3940

4041
internal Edge(IGraphVertex<T> target, C weight)
4142
{
42-
this.target = target;
43+
this.TargetVertex = target;
4344
this.weight = weight;
4445
}
4546

46-
public T Value => target.Value;
47+
public T TargetVertexKey => TargetVertex.Key;
4748

48-
public IGraphVertex<T> Target => target;
49+
public IGraphVertex<T> TargetVertex { get; private set; }
4950

5051
public W Weight<W>() where W : IComparable
5152
{

0 commit comments

Comments
 (0)