Skip to content

Commit f0a7599

Browse files
committed
Minor refact
1 parent e2f4ebf commit f0a7599

File tree

2 files changed

+57
-67
lines changed

2 files changed

+57
-67
lines changed

UnitTest/AlgorithmsTests/GraphsCyclesDetectorTests.cs

Lines changed: 40 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -9,72 +9,67 @@ public static class GraphsCyclesDetectorTests
99
[Fact]
1010
public static void DoTest()
1111
{
12-
string[] V;
13-
DirectedSparseGraph<string> DAG;
14-
UndirectedSparseGraph<string> CyclicGraph;
15-
DirectedSparseGraph<string> DigraphWithCycles;
16-
1712
// Init graph object
18-
DigraphWithCycles = new DirectedSparseGraph<string>();
13+
var digraphWithCycles = new DirectedSparseGraph<string>();
1914

2015
// Init V
21-
V = new string[6] { "r", "s", "t", "x", "y", "z" };
16+
var v = new string[6] { "r", "s", "t", "x", "y", "z" };
2217

2318
// Insert V
24-
DigraphWithCycles.AddVertices(V);
19+
digraphWithCycles.AddVertices(v);
2520

2621
// Insert E
27-
DigraphWithCycles.AddEdge("r", "s");
28-
DigraphWithCycles.AddEdge("r", "t");
29-
DigraphWithCycles.AddEdge("s", "t");
30-
DigraphWithCycles.AddEdge("s", "x");
31-
DigraphWithCycles.AddEdge("t", "x");
32-
DigraphWithCycles.AddEdge("t", "y");
33-
DigraphWithCycles.AddEdge("t", "z");
34-
DigraphWithCycles.AddEdge("x", "y");
35-
DigraphWithCycles.AddEdge("x", "z");
36-
DigraphWithCycles.AddEdge("y", "z");
37-
DigraphWithCycles.AddEdge("z", "r");
38-
DigraphWithCycles.AddEdge("z", "s");
39-
40-
var isCyclic = CyclesDetector.IsCyclic<string>(DigraphWithCycles);
22+
digraphWithCycles.AddEdge("r", "s");
23+
digraphWithCycles.AddEdge("r", "t");
24+
digraphWithCycles.AddEdge("s", "t");
25+
digraphWithCycles.AddEdge("s", "x");
26+
digraphWithCycles.AddEdge("t", "x");
27+
digraphWithCycles.AddEdge("t", "y");
28+
digraphWithCycles.AddEdge("t", "z");
29+
digraphWithCycles.AddEdge("x", "y");
30+
digraphWithCycles.AddEdge("x", "z");
31+
digraphWithCycles.AddEdge("y", "z");
32+
digraphWithCycles.AddEdge("z", "r");
33+
digraphWithCycles.AddEdge("z", "s");
34+
35+
var isCyclic = CyclesDetector.IsCyclic<string>(digraphWithCycles);
4136
Assert.True(isCyclic == true, "Wrong status! The graph has cycles.");
4237

43-
CyclicGraph = new UndirectedSparseGraph<string>();
38+
var cyclicGraph = new UndirectedSparseGraph<string>();
4439

45-
V = new string[] { "A", "B", "C", "D", "E" };
40+
v = new string[] { "A", "B", "C", "D", "E" };
4641

4742
// Insert new values of V
48-
CyclicGraph.AddVertices(V);
43+
cyclicGraph.AddVertices(v);
4944

5045
// Insert new value for edges
51-
CyclicGraph.AddEdge("A", "C");
52-
CyclicGraph.AddEdge("B", "A");
53-
CyclicGraph.AddEdge("B", "C");
54-
CyclicGraph.AddEdge("C", "E");
55-
CyclicGraph.AddEdge("C", "D");
56-
CyclicGraph.AddEdge("D", "B");
57-
CyclicGraph.AddEdge("E", "D");
58-
59-
isCyclic = CyclesDetector.IsCyclic<string>(CyclicGraph);
46+
cyclicGraph.AddEdge("A", "C");
47+
cyclicGraph.AddEdge("B", "A");
48+
cyclicGraph.AddEdge("B", "C");
49+
cyclicGraph.AddEdge("C", "E");
50+
cyclicGraph.AddEdge("C", "D");
51+
cyclicGraph.AddEdge("D", "B");
52+
cyclicGraph.AddEdge("E", "D");
53+
54+
isCyclic = CyclesDetector.IsCyclic<string>(cyclicGraph);
6055
Assert.True(isCyclic == true, "Wrong status! The graph has cycles.");
6156

62-
DAG = new DirectedSparseGraph<string>();
57+
var dag = new DirectedSparseGraph<string>();
6358

64-
V = new string[] { "A", "B", "C", "D", "E", "X" };
59+
v = new string[] { "A", "B", "C", "D", "E", "X" };
6560

6661
// Insert new values of V
67-
DAG.AddVertices(V);
62+
dag.AddVertices(v);
6863

6964
// Insert new value for edges
70-
DAG.AddEdge("A", "B");
71-
DAG.AddEdge("A", "X");
72-
DAG.AddEdge("B", "C");
73-
DAG.AddEdge("C", "D");
74-
DAG.AddEdge("D", "E");
75-
DAG.AddEdge("E", "X");
76-
77-
isCyclic = CyclesDetector.IsCyclic<string>(DAG);
65+
dag.AddEdge("A", "B");
66+
dag.AddEdge("A", "X");
67+
dag.AddEdge("B", "C");
68+
dag.AddEdge("C", "D");
69+
dag.AddEdge("D", "E");
70+
dag.AddEdge("E", "X");
71+
72+
isCyclic = CyclesDetector.IsCyclic<string>(dag);
7873
Assert.True(isCyclic == false, "Wrong status! The graph has no cycles.");
7974
}
8075

UnitTest/AlgorithmsTests/GraphsDijkstraShortestPathsTest.cs

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,18 @@ public static class GraphsDijkstraShortestPathsTest
1111
[Fact]
1212
public static void DoTest()
1313
{
14-
string[] V;
15-
IEnumerable<WeightedEdge<string>> E;
16-
DirectedWeightedSparseGraph<string> graph;
17-
DijkstraShortestPaths<DirectedWeightedSparseGraph<string>, string> dijkstra;
18-
1914
// Init graph object
20-
graph = new DirectedWeightedSparseGraph<string>();
15+
DirectedWeightedSparseGraph<string> graph = new DirectedWeightedSparseGraph<string>();
2116

2217
// Init V
23-
V = new string[6] { "r", "s", "t", "x", "y", "z" };
18+
string[] V = new string[6] { "r", "s", "t", "x", "y", "z" };
2419

2520
// Insert V
2621
graph.AddVertices(V);
2722
Assert.True(graph.VerticesCount == V.Length, "Wrong Vertices Count.");
2823

2924
// Insert E
30-
var status = graph.AddEdge("r", "s", 7);
25+
bool status = graph.AddEdge("r", "s", 7);
3126
Assert.True(status == true);
3227
status = graph.AddEdge("r", "t", 6);
3328
Assert.True(status == true);
@@ -49,30 +44,30 @@ public static void DoTest()
4944
Assert.True(status == true);
5045

5146
// Get E
52-
E = graph.Edges;
47+
IEnumerable<WeightedEdge<string>> E = graph.Edges;
5348
Assert.True(graph.EdgesCount == 10, "Wrong Edges Count.");
5449

5550
// PRINT THE GRAPH
5651
// [*] DIJKSTRA ON DIRECTED WEIGHTED GRAPH - TEST 01:
5752

5853
// Graph representation:
5954
// Init DIJKSTRA
60-
dijkstra = new DijkstraShortestPaths<DirectedWeightedSparseGraph<string>, string>(graph, "s");
55+
DijkstraShortestPaths<DirectedWeightedSparseGraph<string>, string> dijkstra = new DijkstraShortestPaths<DirectedWeightedSparseGraph<string>, string>(graph, "s");
6156

6257
Assert.True(dijkstra.HasPathTo("r") == false);
6358
Assert.True(dijkstra.HasPathTo("z") == true);
6459

6560
// Get shortest path to Z
66-
var pathToZ = string.Empty;
67-
foreach (var node in dijkstra.ShortestPathTo("z"))
61+
string pathToZ = string.Empty;
62+
foreach (string node in dijkstra.ShortestPathTo("z"))
6863
{
6964
pathToZ = String.Format("{0}({1}) -> ", pathToZ, node);
7065
}
7166

7267
pathToZ = pathToZ.TrimEnd(new char[] { ' ', '-', '>' });
7368

74-
var pathToY = string.Empty;
75-
foreach (var node in dijkstra.ShortestPathTo("y"))
69+
string pathToY = string.Empty;
70+
foreach (string node in dijkstra.ShortestPathTo("y"))
7671
{
7772
pathToY = String.Format("{0}({1}) -> ", pathToY, node);
7873
}
@@ -114,24 +109,24 @@ public static void DoTest()
114109
// Init DIJKSTRA
115110
dijkstra = new DijkstraShortestPaths<DirectedWeightedSparseGraph<string>, string>(graph, "A");
116111

117-
var pathToD = string.Empty;
118-
foreach (var node in dijkstra.ShortestPathTo("D"))
112+
string pathToD = string.Empty;
113+
foreach (string node in dijkstra.ShortestPathTo("D"))
119114
{
120115
pathToD = String.Format("{0}({1}) -> ", pathToD, node);
121116
}
122117

123118
pathToD = pathToD.TrimEnd(new char[] { ' ', '-', '>' });
124119

125-
var vertices = graph.Vertices;
126-
var dijkstraAllPairs = new DijkstraAllPairsShortestPaths<DirectedWeightedSparseGraph<string>, string>(graph);
120+
IEnumerable<string> vertices = graph.Vertices;
121+
DijkstraAllPairsShortestPaths<DirectedWeightedSparseGraph<string>, string> dijkstraAllPairs = new DijkstraAllPairsShortestPaths<DirectedWeightedSparseGraph<string>, string>(graph);
127122

128123
// Dijkstra All Pairs Shortest Paths:
129-
foreach (var source in vertices)
124+
foreach (string source in vertices)
130125
{
131-
foreach (var destination in vertices)
126+
foreach (string destination in vertices)
132127
{
133-
var shortestPath = string.Empty;
134-
foreach (var node in dijkstraAllPairs.ShortestPath(source, destination))
128+
string shortestPath = string.Empty;
129+
foreach (string node in dijkstraAllPairs.ShortestPath(source, destination))
135130
shortestPath = String.Format("{0}({1}) -> ", shortestPath, node);
136131

137132
shortestPath = shortestPath.TrimEnd(new char[] { ' ', '-', '>' });

0 commit comments

Comments
 (0)