Skip to content

Commit 7175230

Browse files
committed
BFS Searcher and VFS Shortest Paths bugfixs.
1 parent e6a1a44 commit 7175230

File tree

7 files changed

+91
-53
lines changed

7 files changed

+91
-53
lines changed

Algorithms/Graphs/BreadthFirstSearcher.cs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,25 +35,20 @@ public static void PrintAll<T>(IGraph<T> Graph, T StartVertex) where T : ICompar
3535
var visited = new HashSet<T>();
3636
var queue = new Queue<T>(Graph.VerticesCount);
3737

38-
// BFS VISIT NODE STEP
39-
var current = StartVertex;
40-
Console.Write(String.Format("({0}) ", current));
41-
visited.Add(current);
38+
queue.Enqueue (StartVertex);
4239

4340
while (queue.Count > 0)
4441
{
45-
current = queue.Dequeue();
42+
var current = queue.Dequeue();
43+
Console.Write(String.Format("({0}) ", current));
4644

4745
foreach (var adjacent in Graph.Neighbours(current))
4846
{
49-
if (visited.Contains(adjacent))
50-
continue;
51-
52-
// BFS VISIT NODE STEP
53-
Console.Write(String.Format("({0}) ", adjacent));
54-
visited.Add(adjacent);
55-
56-
queue.Enqueue(adjacent);
47+
if (!visited.Contains(adjacent))
48+
{
49+
visited.Add(adjacent);
50+
queue.Enqueue(adjacent);
51+
}
5752
}
5853
}
5954
}

Algorithms/Graphs/BreadthFirstShortestPaths.cs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
using Algorithms.Common;
1111
using DataStructures.Graphs;
12-
using DataStructures.Lists;
1312

1413
namespace Algorithms.Graphs
1514
{
@@ -113,21 +112,18 @@ private void _initializeDataMembers(IGraph<T> Graph)
113112
/// </summary>
114113
private void _breadthFirstSearch(IGraph<T> graph, T source)
115114
{
116-
// Define helper variables.
117-
var current = source;
118-
var queue = new DataStructures.Lists.Queue<T>(_verticesCount);
119-
120115
// Set distance to current to zero
121-
_distances[_nodesToIndices[current]] = 0;
116+
_distances[_nodesToIndices[source]] = 0;
122117

123118
// Set current to visited: true.
124-
_visited[_nodesToIndices[current]] = true;
119+
_visited[_nodesToIndices[source]] = true;
125120

126-
queue.Enqueue(current);
121+
var queue = new Queue<T>(_verticesCount);
122+
queue.Enqueue(source);
127123

128-
while (!queue.IsEmpty)
124+
while (queue.Count > 0)
129125
{
130-
current = queue.Dequeue();
126+
var current = queue.Dequeue();
131127
int indexOfCurrent = _nodesToIndices[current];
132128

133129
foreach (var adjacent in graph.Neighbours(current))
@@ -152,7 +148,7 @@ private void _breadthFirstSearch(IGraph<T> graph, T source)
152148
private void _breadthFirstSearch(IGraph<T> graph, IList<T> sources)
153149
{
154150
// Define helper variables.
155-
var queue = new DataStructures.Lists.Queue<T>(_verticesCount);
151+
var queue = new Queue<T>(_verticesCount);
156152

157153
foreach (var source in sources)
158154
{
@@ -165,7 +161,7 @@ private void _breadthFirstSearch(IGraph<T> graph, IList<T> sources)
165161
queue.Enqueue(source);
166162
}
167163

168-
while (!queue.IsEmpty)
164+
while (queue.Count > 0)
169165
{
170166
var current = queue.Dequeue();
171167
int indexOfCurrent = _nodesToIndices[current];

Algorithms/Graphs/DepthFirstSearcher.cs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,17 @@ public static void PrintAll<T>(IGraph<T> Graph, T StartVertex) where T : ICompar
9595
{
9696
var current = stack.Pop();
9797

98-
if (visited.Contains(current))
99-
continue;
100-
101-
// DFS VISIT NODE STEP
102-
Console.WriteLine(String.Format("({0}) ", current));
103-
visited.Add(current);
104-
105-
// Get the adjacent nodes of current
106-
foreach (var adjacent in Graph.Neighbours(current))
107-
if (!visited.Contains(adjacent))
108-
stack.Push(adjacent);
98+
if (!visited.Contains(current))
99+
{
100+
// DFS VISIT NODE STEP
101+
Console.Write(String.Format("({0}) ", current));
102+
visited.Add(current);
103+
104+
// Get the adjacent nodes of current
105+
foreach (var adjacent in Graph.Neighbours(current))
106+
if (!visited.Contains(adjacent))
107+
stack.Push(adjacent);
108+
}
109109
}
110110

111111
}
@@ -135,24 +135,24 @@ public static T FindFirstMatch<T>(IGraph<T> Graph, T StartVertex, Predicate<T> M
135135
var current = stack.Pop();
136136

137137
// Skip loop if node was already visited
138-
if (parents.ContainsKey(current))
139-
continue;
140-
141-
// Save its parent into the dictionary
142-
// Mark it as visited
143-
parents.Add(current, currentParent);
138+
if (!parents.ContainsKey(current))
139+
{
140+
// Save its parent into the dictionary
141+
// Mark it as visited
142+
parents.Add(current, currentParent);
144143

145-
// DFS VISIT NODE STEP
146-
if (Match(current))
147-
return current;
144+
// DFS VISIT NODE STEP
145+
if (Match(current))
146+
return current;
148147

149-
// Get currents adjacent nodes (might add already visited nodes).
150-
foreach (var adjacent in Graph.Neighbours(current))
151-
if (!parents.ContainsKey(adjacent))
152-
stack.Push(adjacent);
148+
// Get currents adjacent nodes (might add already visited nodes).
149+
foreach (var adjacent in Graph.Neighbours(current))
150+
if (!parents.ContainsKey(adjacent))
151+
stack.Push(adjacent);
153152

154-
// Mark current as the father of its adjacents. This helps keep track of tree-nodes.
155-
currentParent = current;
153+
// Mark current as the father of its adjacents. This helps keep track of tree-nodes.
154+
currentParent = current;
155+
}
156156
}//end-while
157157

158158
throw new Exception("Item was not found!");

C-Sharp-Algorithms.sln

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,44 @@ Global
2828
{8F59D573-A1B8-438F-A67A-A7F11FACF201}.Release|Any CPU.ActiveCfg = Release|Any CPU
2929
{8F59D573-A1B8-438F-A67A-A7F11FACF201}.Release|Any CPU.Build.0 = Release|Any CPU
3030
EndGlobalSection
31+
GlobalSection(MonoDevelopProperties) = preSolution
32+
Policies = $0
33+
$0.DotNetNamingPolicy = $1
34+
$1.DirectoryNamespaceAssociation = None
35+
$1.ResourceNamePolicy = FileFormatDefault
36+
$0.TextStylePolicy = $2
37+
$2.inheritsSet = VisualStudio
38+
$2.inheritsScope = text/plain
39+
$2.scope = text/x-csharp
40+
$0.CSharpFormattingPolicy = $3
41+
$3.IndentSwitchBody = True
42+
$3.IndentBlocksInsideExpressions = True
43+
$3.AnonymousMethodBraceStyle = NextLine
44+
$3.PropertyBraceStyle = NextLine
45+
$3.PropertyGetBraceStyle = NextLine
46+
$3.PropertySetBraceStyle = NextLine
47+
$3.EventBraceStyle = NextLine
48+
$3.EventAddBraceStyle = NextLine
49+
$3.EventRemoveBraceStyle = NextLine
50+
$3.StatementBraceStyle = NextLine
51+
$3.ElseNewLinePlacement = NewLine
52+
$3.CatchNewLinePlacement = NewLine
53+
$3.FinallyNewLinePlacement = NewLine
54+
$3.WhileNewLinePlacement = DoNotCare
55+
$3.ArrayInitializerWrapping = DoNotChange
56+
$3.ArrayInitializerBraceStyle = NextLine
57+
$3.BeforeMethodDeclarationParentheses = False
58+
$3.BeforeMethodCallParentheses = False
59+
$3.BeforeConstructorDeclarationParentheses = False
60+
$3.NewLineBeforeConstructorInitializerColon = NewLine
61+
$3.NewLineAfterConstructorInitializerColon = SameLine
62+
$3.BeforeDelegateDeclarationParentheses = False
63+
$3.NewParentheses = False
64+
$3.SpacesBeforeBrackets = False
65+
$3.inheritsSet = Mono
66+
$3.inheritsScope = text/x-csharp
67+
$3.scope = text/x-csharp
68+
EndGlobalSection
3169
GlobalSection(SolutionProperties) = preSolution
3270
HideSolutionNode = FALSE
3371
EndGlobalSection

MainProgram/AlgorithmsTests/GraphsBreadthFirstSearchTest.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ public static void DoTest ()
2828
graph.AddEdge("c", "v");
2929
graph.AddEdge("v", "f");
3030

31+
// Print the nodes in graph
32+
Console.WriteLine(" [*] BFS PrintAll: ");
33+
BreadthFirstSearcher.PrintAll(graph, "d");
34+
Console.WriteLine ("\r\n");
35+
3136
string searchResult = null;
3237
string startFromNode = "d";
3338
Action<string> writeToConsole = (node) => Console.Write (String.Format ("({0}) ", node));

MainProgram/AlgorithmsTests/GraphsDepthFirstSearchTest.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ public static void DoTest ()
2828
graph.AddEdge("c", "v");
2929
graph.AddEdge("v", "f");
3030

31+
// Print the nodes in graph
32+
Console.WriteLine(" [*] DFS PrintAll: ");
33+
DepthFirstSearcher.PrintAll(graph, "d");
34+
Console.WriteLine("\r\n");
35+
3136
string searchResult = null;
3237
string startFromNode = "d";
3338
Action<string> writeToConsole = (node) => Console.Write (String.Format ("({0}) ", node));

MainProgram/Program.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ public class Program
1212
{
1313
public static void Main(string[] args)
1414
{
15-
TrieMapTest.DoTest();
16-
TrieTest.DoTest();
15+
GraphsBreadthFirstPathsTest.DoTest ();
1716
}
1817
}
1918
}

0 commit comments

Comments
 (0)