Skip to content

Commit 35a0a1b

Browse files
committed
cleanup graph algorithms
1 parent 6370316 commit 35a0a1b

27 files changed

+279
-318
lines changed

Advanced.Algorithms/Combinatorics/Combination.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ public static List<List<T>> Find<T>(List<T> input, int r, bool withRepetition)
88
{
99
var result = new List<List<T>>();
1010

11-
Recurse(input, r, withRepetition, 0, new List<T>(), new HashSet<int>(), result);
11+
recurse(input, r, withRepetition, 0, new List<T>(), new HashSet<int>(), result);
1212

1313
return result;
1414
}
1515

16-
private static void Recurse<T>(List<T> input, int r, bool withRepetition,
16+
private static void recurse<T>(List<T> input, int r, bool withRepetition,
1717
int k, List<T> prefix, HashSet<int> prefixIndices,
1818
List<List<T>> result)
1919
{
@@ -33,7 +33,7 @@ private static void Recurse<T>(List<T> input, int r, bool withRepetition,
3333
prefix.Add(input[j]);
3434
prefixIndices.Add(j);
3535

36-
Recurse(input, r, withRepetition, j, prefix, prefixIndices, result);
36+
recurse(input, r, withRepetition, j, prefix, prefixIndices, result);
3737

3838
prefix.RemoveAt(prefix.Count - 1);
3939
prefixIndices.Remove(j);

Advanced.Algorithms/Combinatorics/Permutation.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ public static List<List<T>> Find<T>(List<T> input, int r, bool withRepetition =
88
{
99
var result = new List<List<T>>();
1010

11-
Recurse(input, r, withRepetition, new List<T>(), new HashSet<int>(), result);
11+
recurse(input, r, withRepetition, new List<T>(), new HashSet<int>(), result);
1212

1313
return result;
1414
}
1515

16-
private static void Recurse<T>(List<T> input, int r, bool withRepetition,
16+
private static void recurse<T>(List<T> input, int r, bool withRepetition,
1717
List<T> prefix, HashSet<int> prefixIndices,
1818
List<List<T>> result)
1919
{
@@ -33,7 +33,7 @@ private static void Recurse<T>(List<T> input, int r, bool withRepetition,
3333
prefix.Add(input[j]);
3434
prefixIndices.Add(j);
3535

36-
Recurse(input, r, withRepetition, prefix, prefixIndices, result);
36+
recurse(input, r, withRepetition, prefix, prefixIndices, result);
3737

3838
prefix.RemoveAt(prefix.Count - 1);
3939
prefixIndices.Remove(j);

Advanced.Algorithms/Combinatorics/Subset.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ public static List<List<T>> Find<T>(List<T> input)
88
{
99
var result = new List<List<T>>();
1010

11-
Recurse(input, 0, new List<T>(), new HashSet<int>(), result);
11+
recurse(input, 0, new List<T>(), new HashSet<int>(), result);
1212

1313
return result;
1414
}
1515

16-
private static void Recurse<T>(List<T> input,
16+
private static void recurse<T>(List<T> input,
1717
int k, List<T> prefix, HashSet<int> prefixIndices,
1818
List<List<T>> result)
1919
{
@@ -29,7 +29,7 @@ private static void Recurse<T>(List<T> input,
2929
prefix.Add(input[j]);
3030
prefixIndices.Add(j);
3131

32-
Recurse(input, j, prefix, prefixIndices, result);
32+
recurse(input, j, prefix, prefixIndices, result);
3333

3434
prefix.RemoveAt(prefix.Count - 1);
3535
prefixIndices.Remove(j);

Advanced.Algorithms/GraphAlgorithms/ArticulationPoint/TarjansArticulationFinder.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class TarjansArticulationFinder<T>
1818
public List<T> FindArticulationPoints(Graph<T> graph)
1919
{
2020
int visitTime = 0;
21-
return DFS(graph.ReferenceVertex, new List<T>(),
21+
return dfs(graph.ReferenceVertex, new List<T>(),
2222
new Dictionary<T, int>(), new Dictionary<T, int>(),
2323
new Dictionary<T, T>(),
2424
ref visitTime);
@@ -30,13 +30,12 @@ public List<T> FindArticulationPoints(Graph<T> graph)
3030
/// </summary>
3131
/// <param name="currentVertex"></param>
3232
/// <param name="result"></param>
33-
/// <param name="discovery"></param>
3433
/// <param name="discoveryTimeMap"></param>
3534
/// <param name="lowTimeMap"></param>
3635
/// <param name="parent"></param>
3736
/// <param name="discoveryTime"></param>
3837
/// <returns></returns>
39-
private List<T> DFS(GraphVertex<T> currentVertex,
38+
private List<T> dfs(GraphVertex<T> currentVertex,
4039
List<T> result,
4140
Dictionary<T, int> discoveryTimeMap, Dictionary<T, int> lowTimeMap,
4241
Dictionary<T, T> parent, ref int discoveryTime)
@@ -58,7 +57,7 @@ private List<T> DFS(GraphVertex<T> currentVertex,
5857
parent.Add(edge.Value, currentVertex.Value);
5958

6059
discoveryTime++;
61-
DFS(edge, result,
60+
dfs(edge, result,
6261
discoveryTimeMap, lowTimeMap, parent, ref discoveryTime);
6362

6463
//if neighbours lowTime is greater than current
@@ -93,8 +92,8 @@ private List<T> DFS(GraphVertex<T> currentVertex,
9392

9493
//if root of DFS with two or more children
9594
//or visitTime of this Vertex <=lowTime of any neighbour
96-
if ((parent.ContainsKey(currentVertex.Value) == false && discoveryChildCount >= 2) ||
97-
(parent.ContainsKey(currentVertex.Value) && isArticulationPoint))
95+
if (parent.ContainsKey(currentVertex.Value) == false && discoveryChildCount >= 2 ||
96+
parent.ContainsKey(currentVertex.Value) && isArticulationPoint)
9897
{
9998
result.Add(currentVertex.Value);
10099
}

Advanced.Algorithms/GraphAlgorithms/Bridge/TarjansBridgeFinder.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class TarjansBridgeFinder<T>
3030
public List<Bridge<T>> FindBridges(Graph<T> graph)
3131
{
3232
int visitTime = 0;
33-
return DFS(graph.ReferenceVertex, new List<Bridge<T>>(),
33+
return dfs(graph.ReferenceVertex, new List<Bridge<T>>(),
3434
new Dictionary<T, int>(), new Dictionary<T, int>(),
3535
new Dictionary<T, T>(),
3636
ref visitTime);
@@ -42,13 +42,12 @@ public List<Bridge<T>> FindBridges(Graph<T> graph)
4242
/// </summary>
4343
/// <param name="currentVertex"></param>
4444
/// <param name="result"></param>
45-
/// <param name="discovery"></param>
4645
/// <param name="discoveryTimeMap"></param>
4746
/// <param name="lowTimeMap"></param>
4847
/// <param name="parent"></param>
4948
/// <param name="discoveryTime"></param>
5049
/// <returns></returns>
51-
private List<Bridge<T>> DFS(GraphVertex<T> currentVertex,
50+
private List<Bridge<T>> dfs(GraphVertex<T> currentVertex,
5251
List<Bridge<T>> result,
5352
Dictionary<T, int> discoveryTimeMap, Dictionary<T, int> lowTimeMap,
5453
Dictionary<T, T> parent, ref int discoveryTime)
@@ -58,17 +57,15 @@ private List<Bridge<T>> DFS(GraphVertex<T> currentVertex,
5857
lowTimeMap.Add(currentVertex.Value, discoveryTime);
5958

6059
//discovery childs in this iteration
61-
var discoveryChildCount = 0;
6260

6361
foreach (var edge in currentVertex.Edges)
6462
{
6563
if (!discoveryTimeMap.ContainsKey(edge.Value))
6664
{
67-
discoveryChildCount++;
6865
parent.Add(edge.Value, currentVertex.Value);
6966

7067
discoveryTime++;
71-
DFS(edge, result,
68+
dfs(edge, result,
7269
discoveryTimeMap, lowTimeMap, parent, ref discoveryTime);
7370

7471
//propogate lowTime index of neighbour so that ancestors can see check for back edge

Advanced.Algorithms/GraphAlgorithms/Coloring/MColorer.cs

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,30 +28,31 @@ public class MColorer<T, C>
2828
public MColorResult<T, C> Color(Graph<T> graph, C[] colors)
2929
{
3030

31-
GraphVertex<T> first = graph.ReferenceVertex;
31+
var first = graph.ReferenceVertex;
3232

33-
var progress = CanColor(first, colors,
33+
var progress = canColor(first, colors,
3434
new Dictionary<GraphVertex<T>, C>(),
3535
new HashSet<GraphVertex<T>>());
3636

37-
if (progress.Count == graph.VerticesCount)
37+
if (progress.Count != graph.VerticesCount)
3838
{
39-
var result = new Dictionary<C, List<T>>();
39+
return new MColorResult<T, C>(false, null);
40+
}
4041

41-
foreach(var vertex in progress)
42-
{
43-
if(!result.ContainsKey(vertex.Value))
44-
{
45-
result.Add(vertex.Value, new List<T>());
46-
}
42+
var result = new Dictionary<C, List<T>>();
4743

48-
result[vertex.Value].Add(vertex.Key.Value);
44+
foreach(var vertex in progress)
45+
{
46+
if(!result.ContainsKey(vertex.Value))
47+
{
48+
result.Add(vertex.Value, new List<T>());
4949
}
5050

51-
return new MColorResult<T, C>(true, result);
51+
result[vertex.Value].Add(vertex.Key.Value);
5252
}
5353

54-
return new MColorResult<T, C>(false, null);
54+
return new MColorResult<T, C>(true, result);
55+
5556
}
5657

5758
/// <summary>
@@ -62,16 +63,18 @@ public MColorResult<T, C> Color(Graph<T> graph, C[] colors)
6263
/// <param name="progress"></param>
6364
/// <param name="visited"></param>
6465
/// <returns></returns>
65-
private Dictionary<GraphVertex<T>, C> CanColor(GraphVertex<T> vertex, C[] colors,
66+
private Dictionary<GraphVertex<T>, C> canColor(GraphVertex<T> vertex, C[] colors,
6667
Dictionary<GraphVertex<T>, C> progress, HashSet<GraphVertex<T>> visited)
6768
{
68-
for (int i = 0; i < colors.Length; i++)
69+
foreach (var item in colors)
6970
{
70-
if (isSafe(progress, vertex, colors[i]))
71+
if (!isSafe(progress, vertex, item))
7172
{
72-
progress.Add(vertex, colors[i]);
73-
break;
73+
continue;
7474
}
75+
76+
progress.Add(vertex, item);
77+
break;
7578
}
7679

7780
if (visited.Contains(vertex) == false)
@@ -85,7 +88,7 @@ private Dictionary<GraphVertex<T>, C> CanColor(GraphVertex<T> vertex, C[] colors
8588
continue;
8689
}
8790

88-
CanColor(edge, colors, progress, visited);
91+
canColor(edge, colors, progress, visited);
8992
}
9093
}
9194

Advanced.Algorithms/GraphAlgorithms/Connectivity/KosarajuStronglyConnected.cs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,12 @@ public List<List<T>>
2525
{
2626
if(!visited.Contains(vertex.Value.Value))
2727
{
28-
KosarajuStep1(vertex.Value, visited, finishStack);
29-
}
30-
28+
kosarajuStep1(vertex.Value, visited, finishStack);
29+
}
3130
}
3231

3332
//reverse edges
34-
var reverseGraph = ReverseEdges(graph);
33+
var reverseGraph = reverseEdges(graph);
3534

3635
visited.Clear();
3736

@@ -44,7 +43,7 @@ public List<List<T>>
4443

4544
if (!visited.Contains(currentVertex.Value))
4645
{
47-
result.Add(KosarajuStep2(currentVertex, visited,
46+
result.Add(kosarajuStep2(currentVertex, visited,
4847
finishStack, new List<T>()));
4948
}
5049
}
@@ -58,7 +57,7 @@ public List<List<T>>
5857
/// <param name="currentVertex"></param>
5958
/// <param name="visited"></param>
6059
/// <param name="finishStack"></param>
61-
private void KosarajuStep1(DiGraphVertex<T> currentVertex,
60+
private void kosarajuStep1(DiGraphVertex<T> currentVertex,
6261
HashSet<T> visited,
6362
Stack<T> finishStack)
6463
{
@@ -68,7 +67,7 @@ private void KosarajuStep1(DiGraphVertex<T> currentVertex,
6867
{
6968
if(!visited.Contains(edge.Value))
7069
{
71-
KosarajuStep1(edge, visited, finishStack);
70+
kosarajuStep1(edge, visited, finishStack);
7271
}
7372
}
7473

@@ -84,7 +83,7 @@ private void KosarajuStep1(DiGraphVertex<T> currentVertex,
8483
/// <param name="finishStack"></param>
8584
/// <param name="result"></param>
8685
/// <returns></returns>
87-
private List<T> KosarajuStep2(DiGraphVertex<T> currentVertex,
86+
private List<T> kosarajuStep2(DiGraphVertex<T> currentVertex,
8887
HashSet<T> visited, Stack<T> finishStack,
8988
List<T> result)
9089
{
@@ -95,7 +94,7 @@ private List<T> KosarajuStep2(DiGraphVertex<T> currentVertex,
9594
{
9695
if (!visited.Contains(edge.Value))
9796
{
98-
KosarajuStep2(edge, visited, finishStack, result);
97+
kosarajuStep2(edge, visited, finishStack, result);
9998
}
10099
}
101100

@@ -105,9 +104,8 @@ private List<T> KosarajuStep2(DiGraphVertex<T> currentVertex,
105104
/// <summary>
106105
/// create a clone graph with reverse edge directions
107106
/// </summary>
108-
/// <param name="workGraph"></param>
109107
/// <returns></returns>
110-
private DiGraph<T> ReverseEdges(DiGraph<T> graph)
108+
private DiGraph<T> reverseEdges(DiGraph<T> graph)
111109
{
112110
var newGraph = new DiGraph<T>();
113111

@@ -127,7 +125,5 @@ private DiGraph<T> ReverseEdges(DiGraph<T> graph)
127125

128126
return newGraph;
129127
}
130-
131-
132128
}
133129
}

Advanced.Algorithms/GraphAlgorithms/Connectivity/TarjansStronglyConnected.cs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ public List<List<T>> FindStronglyConnectedComponents(DiGraph<T> graph)
4545
/// </summary>
4646
/// <param name="currentVertex"></param>
4747
/// <param name="result"></param>
48-
/// <param name="discovery"></param>
4948
/// <param name="discoveryTimeMap"></param>
5049
/// <param name="lowTimeMap"></param>
51-
/// <param name="parent"></param>
50+
/// <param name="pathStackMap"></param>
5251
/// <param name="discoveryTime"></param>
52+
/// <param name="pathStack"></param>
5353
/// <returns></returns>
5454
private void DFS(DiGraphVertex<T> currentVertex,
5555
List<List<T>> result,
@@ -93,24 +93,25 @@ private void DFS(DiGraphVertex<T> currentVertex,
9393

9494
//if low is high this means we reached head of the DFS tree with strong connectivity
9595
//now print items in the stack
96-
if (lowTimeMap[currentVertex.Value] == discoveryTimeMap[currentVertex.Value])
96+
if (lowTimeMap[currentVertex.Value] != discoveryTimeMap[currentVertex.Value])
9797
{
98-
var strongConnected = new List<T>();
99-
while (!pathStack.Peek().Equals(currentVertex.Value))
100-
{
101-
var vertex = pathStack.Pop();
102-
strongConnected.Add(vertex);
103-
pathStackMap.Remove(vertex);
104-
}
98+
return;
99+
}
105100

106-
//add current vertex
107-
var finalVertex = pathStack.Pop();
108-
strongConnected.Add(finalVertex);
109-
pathStackMap.Remove(finalVertex);
101+
var strongConnected = new List<T>();
102+
while (!pathStack.Peek().Equals(currentVertex.Value))
103+
{
104+
var vertex = pathStack.Pop();
105+
strongConnected.Add(vertex);
106+
pathStackMap.Remove(vertex);
107+
}
110108

111-
result.Add(strongConnected);
109+
//add current vertex
110+
var finalVertex = pathStack.Pop();
111+
strongConnected.Add(finalVertex);
112+
pathStackMap.Remove(finalVertex);
112113

113-
}
114+
result.Add(strongConnected);
114115
}
115116
}
116117
}

Advanced.Algorithms/GraphAlgorithms/Cover/MinVertexCover.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class MinVertexCover<T>
77
{
88
public List<GraphVertex<T>> GetMinVertexCover(Graph<T> graph)
99
{
10-
return GetMinVertexCover(graph.ReferenceVertex, new HashSet<GraphVertex<T>>(),
10+
return getMinVertexCover(graph.ReferenceVertex, new HashSet<GraphVertex<T>>(),
1111
new List<GraphVertex<T>>());
1212
}
1313

@@ -19,7 +19,7 @@ public List<GraphVertex<T>> GetMinVertexCover(Graph<T> graph)
1919
/// <param name="visited"></param>
2020
/// <param name="cover"></param>
2121
/// <returns></returns>
22-
private List<GraphVertex<T>> GetMinVertexCover(GraphVertex<T> vertex,
22+
private List<GraphVertex<T>> getMinVertexCover(GraphVertex<T> vertex,
2323
HashSet<GraphVertex<T>> visited, List<GraphVertex<T>> cover)
2424
{
2525
visited.Add(vertex);
@@ -34,7 +34,7 @@ private List<GraphVertex<T>> GetMinVertexCover(GraphVertex<T> vertex,
3434

3535
if(!visited.Contains(edge))
3636
{
37-
GetMinVertexCover(edge, visited, cover);
37+
getMinVertexCover(edge, visited, cover);
3838
}
3939
}
4040

0 commit comments

Comments
 (0)