Skip to content

Commit 5bd55ce

Browse files
committed
get edges api
1 parent 735e64d commit 5bd55ce

File tree

8 files changed

+197
-0
lines changed

8 files changed

+197
-0
lines changed

Algorithm.Sandbox/DataStructures/Graph/AdjacencyList/DiGraph.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34

45
namespace Algorithm.Sandbox.DataStructures.Graph.AdjacencyList
56
{
@@ -187,6 +188,26 @@ public bool HasEdge(T source, T dest)
187188
&& Vertices[dest].InEdges.Contains(Vertices[source]);
188189
}
189190

191+
public List<T> GetAllOutEdges(T vertex)
192+
{
193+
if (!Vertices.ContainsKey(vertex))
194+
{
195+
throw new ArgumentException("vertex is not in this graph.");
196+
}
197+
198+
return Vertices[vertex].OutEdges.Select(x=>x.Value).ToList();
199+
}
200+
201+
public List<T> GetAllInEdges(T vertex)
202+
{
203+
if (!Vertices.ContainsKey(vertex))
204+
{
205+
throw new ArgumentException("vertex is not in this graph.");
206+
}
207+
208+
return Vertices[vertex].InEdges.Select(x => x.Value).ToList();
209+
}
210+
190211
/// <summary>
191212
/// returns the vertex object with given value
192213
/// O(1) complexity

Algorithm.Sandbox/DataStructures/Graph/AdjacencyList/Graph.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34

45
namespace Algorithm.Sandbox.DataStructures.Graph.AdjacencyList
56
{
@@ -181,6 +182,16 @@ public bool HasEdge(T source, T dest)
181182
&& Vertices[dest].Edges.Contains(Vertices[source]);
182183
}
183184

185+
public List<T> GetAllEdges(T vertex)
186+
{
187+
if (!Vertices.ContainsKey(vertex))
188+
{
189+
throw new ArgumentException("vertex is not in this graph.");
190+
}
191+
192+
return Vertices[vertex].Edges.Select(x => x.Value).ToList();
193+
}
194+
184195
/// <summary>
185196
/// returns the vertex object with given value
186197
/// O(1) complexity

Algorithm.Sandbox/DataStructures/Graph/AdjacencyList/WeightedDiGraph.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34

45
namespace Algorithm.Sandbox.DataStructures.Graph.AdjacencyList
56
{
@@ -190,6 +191,26 @@ public bool HasEdge(T source, T dest)
190191
&& Vertices[dest].InEdges.ContainsKey(Vertices[source]);
191192
}
192193

194+
public List<Tuple<T, W>> GetAllOutEdges(T vertex)
195+
{
196+
if (!Vertices.ContainsKey(vertex))
197+
{
198+
throw new ArgumentException("vertex is not in this graph.");
199+
}
200+
201+
return Vertices[vertex].OutEdges.Select(x =>new Tuple<T,W>(x.Key.Value, x.Value)).ToList();
202+
}
203+
204+
public List<Tuple<T, W>> GetAllInEdges(T vertex)
205+
{
206+
if (!Vertices.ContainsKey(vertex))
207+
{
208+
throw new ArgumentException("vertex is not in this graph.");
209+
}
210+
211+
return Vertices[vertex].InEdges.Select(x => new Tuple<T, W>(x.Key.Value, x.Value)).ToList();
212+
}
213+
193214
/// <summary>
194215
/// returns the vertex with given value
195216
/// O(1) complexity

Algorithm.Sandbox/DataStructures/Graph/AdjacencyList/WeightedGraph.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34

45
namespace Algorithm.Sandbox.DataStructures.Graph.AdjacencyList
56
{
@@ -183,6 +184,17 @@ public bool HasEdge(T source, T dest)
183184

184185
}
185186

187+
188+
public List<Tuple<T, W>> GetAllEdges(T vertex)
189+
{
190+
if (!Vertices.ContainsKey(vertex))
191+
{
192+
throw new ArgumentException("vertex is not in this graph.");
193+
}
194+
195+
return Vertices[vertex].Edges.Select(x => new Tuple<T, W>(x.Key.Value, x.Value)).ToList();
196+
}
197+
186198
/// <summary>
187199
/// Find the Vertex with given value
188200
/// O(1) complexity

Algorithm.Sandbox/DataStructures/Graph/AdjacencyMatrix/DiGraph.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,50 @@ public bool HasEdge(T source, T dest)
192192
return false;
193193
}
194194

195+
public List<T> GetAllOutEdges(T vertex)
196+
{
197+
if (!vertexIndices.ContainsKey(vertex))
198+
{
199+
throw new ArgumentException("vertex is not in this graph.");
200+
}
201+
202+
var index = vertexIndices[vertex];
203+
204+
var result = new List<T>();
205+
206+
for (int i = 0; i < maxSize; i++)
207+
{
208+
if(matrix[index].Get(i))
209+
{
210+
result.Add(reverseVertexIndices[i]);
211+
}
212+
}
213+
214+
return result;
215+
}
216+
217+
public List<T> GetAllInEdges(T vertex)
218+
{
219+
if (!vertexIndices.ContainsKey(vertex))
220+
{
221+
throw new ArgumentException("vertex is not in this graph.");
222+
}
223+
224+
var index = vertexIndices[vertex];
225+
226+
var result = new List<T>();
227+
228+
for (int i = 0; i < maxSize; i++)
229+
{
230+
if (matrix[i].Get(index))
231+
{
232+
result.Add(reverseVertexIndices[i]);
233+
}
234+
}
235+
236+
return result;
237+
}
238+
195239
private void doubleMatrixSize()
196240
{
197241
var newMatrix = new BitArray[maxSize * 2];

Algorithm.Sandbox/DataStructures/Graph/AdjacencyMatrix/Graph.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,28 @@ public bool HasEdge(T source, T dest)
193193
return false;
194194
}
195195

196+
public List<T> GetAllEdges(T vertex)
197+
{
198+
if (!vertexIndices.ContainsKey(vertex))
199+
{
200+
throw new ArgumentException("vertex is not in this graph.");
201+
}
202+
203+
var index = vertexIndices[vertex];
204+
205+
var result = new List<T>();
206+
207+
for (int i = 0; i < maxSize; i++)
208+
{
209+
if (matrix[i].Get(index))
210+
{
211+
result.Add(reverseVertexIndices[i]);
212+
}
213+
}
214+
215+
return result;
216+
}
217+
196218
private void doubleMatrixSize()
197219
{
198220
var newMatrix = new BitArray[maxSize * 2];

Algorithm.Sandbox/DataStructures/Graph/AdjacencyMatrix/WeightedDiGraph.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,50 @@ public bool HasEdge(T source, T dest)
196196
return false;
197197
}
198198

199+
public List<Tuple<T, W>> GetAllOutEdges(T vertex)
200+
{
201+
if (!vertexIndices.ContainsKey(vertex))
202+
{
203+
throw new ArgumentException("vertex is not in this graph.");
204+
}
205+
206+
var index = vertexIndices[vertex];
207+
208+
var result = new List<Tuple<T, W>>();
209+
210+
for (int i = 0; i < maxSize; i++)
211+
{
212+
if (!matrix[index,i].Equals(default(W)))
213+
{
214+
result.Add(new Tuple<T,W>(reverseVertexIndices[i], matrix[index, i]));
215+
}
216+
}
217+
218+
return result;
219+
}
220+
221+
public List<Tuple<T, W>> GetAllInEdges(T vertex)
222+
{
223+
if (!vertexIndices.ContainsKey(vertex))
224+
{
225+
throw new ArgumentException("vertex is not in this graph.");
226+
}
227+
228+
var index = vertexIndices[vertex];
229+
230+
var result = new List<Tuple<T, W>>();
231+
232+
for (int i = 0; i < maxSize; i++)
233+
{
234+
if (!matrix[i, index].Equals(default(W)))
235+
{
236+
result.Add(new Tuple<T, W>(reverseVertexIndices[i], matrix[i, index]));
237+
}
238+
}
239+
240+
return result;
241+
}
242+
199243
private void doubleMatrixSize()
200244
{
201245
var newMatrix = new W[maxSize * 2, maxSize * 2];

Algorithm.Sandbox/DataStructures/Graph/AdjacencyMatrix/WeightedGraph.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,28 @@ public bool HasEdge(T source, T dest)
200200
return false;
201201
}
202202

203+
public List<Tuple<T,W>> GetAllEdges(T vertex)
204+
{
205+
if (!vertexIndices.ContainsKey(vertex))
206+
{
207+
throw new ArgumentException("vertex is not in this graph.");
208+
}
209+
210+
var index = vertexIndices[vertex];
211+
212+
var result = new List<Tuple<T, W>>();
213+
214+
for (int i = 0; i < maxSize; i++)
215+
{
216+
if (!matrix[i,index].Equals(default(W)))
217+
{
218+
result.Add(new Tuple<T, W>(reverseVertexIndices[i], matrix[i, index]));
219+
}
220+
}
221+
222+
return result;
223+
}
224+
203225
private void doubleMatrixSize()
204226
{
205227
var newMatrix = new W[maxSize * 2, maxSize * 2];

0 commit comments

Comments
 (0)