forked from aalhour/C-Sharp-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BreadthFirstSearcher.cs
167 lines (137 loc) · 6.03 KB
/
BreadthFirstSearcher.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/***
* Implements the the Breadth-First Search algorithm.
*
* Provides multiple functions for traversing graphs:
* 1. PrintAll(),
* 2. VisitAll(Action<T> forEachFunc),
* 3. FindFirstMatch(Predicate<T> match).
*
* The VisitAll() applies a function to every graph node. The FindFirstMatch() function searches the graph for a predicate match.
*/
using System;
using System.Collections.Generic;
using DataStructures.Graphs;
namespace Algorithms.Graphs
{
public static class BreadthFirstSearcher
{
/// <summary>
/// Iterative BFS implementation.
/// Traverses nodes in graph starting from a specific node, printing them as they get visited.
/// </summary>
public static void PrintAll<T>(IGraph<T> Graph, T StartVertex) where T : IComparable<T>
{
// Check if graph is empty
if (Graph.VerticesCount == 0)
throw new Exception("Graph is empty!");
// Check if graph has the starting vertex
if (!Graph.HasVertex(StartVertex))
throw new Exception("Starting vertex doesn't belong to graph.");
var visited = new HashSet<T>();
var queue = new Queue<T>(Graph.VerticesCount);
queue.Enqueue (StartVertex);
while (queue.Count > 0)
{
var current = queue.Dequeue();
Console.Write(String.Format("({0}) ", current));
foreach (var adjacent in Graph.Neighbours(current))
{
if (!visited.Contains(adjacent))
{
visited.Add(adjacent);
queue.Enqueue(adjacent);
}
}
}
}
/// <summary>
/// Iterative BFS implementation.
/// Traverses all the nodes in a graph starting from a specific node, applying the passed action to every node.
/// </summary>
public static void VisitAll<T>(ref IGraph<T> Graph, T StartVertex, Action<T> Action) where T : IComparable<T>
{
// Check if graph is empty
if (Graph.VerticesCount == 0)
throw new Exception("Graph is empty!");
// Check if graph has the starting vertex
if (!Graph.HasVertex(StartVertex))
throw new Exception("Starting vertex doesn't belong to graph.");
int level = 0; // keeps track of level
var frontiers = new List<T>(); // keeps track of previous levels, i - 1
var levels = new Dictionary<T, int>(Graph.VerticesCount); // keeps track of visited nodes and their distances
var parents = new Dictionary<T, object>(Graph.VerticesCount); // keeps track of tree-nodes
frontiers.Add(StartVertex);
levels.Add(StartVertex, 0);
parents.Add(StartVertex, null);
// BFS VISIT CURRENT NODE
Action(StartVertex);
// TRAVERSE GRAPH
while (frontiers.Count > 0)
{
var next = new List<T>(); // keeps track of the current level, i
foreach (var node in frontiers)
{
foreach (var adjacent in Graph.Neighbours(node))
{
if (!levels.ContainsKey(adjacent)) // not visited yet
{
// BFS VISIT NODE STEP
Action(adjacent);
levels.Add(adjacent, level); // level[node] + 1
parents.Add(adjacent, node);
next.Add(adjacent);
}
}
}
frontiers = next;
level = level + 1;
}
}
/// <summary>
/// Iterative BFS Implementation.
/// Given a predicate function and a starting node, this function searches the nodes of the graph for a first match.
/// </summary>
public static T FindFirstMatch<T>(IGraph<T> Graph, T StartVertex, Predicate<T> Match) where T : IComparable<T>
{
// Check if graph is empty
if (Graph.VerticesCount == 0)
throw new Exception("Graph is empty!");
// Check if graph has the starting vertex
if (!Graph.HasVertex(StartVertex))
throw new Exception("Starting vertex doesn't belong to graph.");
int level = 0; // keeps track of levels
var frontiers = new List<T>(); // keeps track of previous levels, i - 1
var levels = new Dictionary<T, int>(Graph.VerticesCount); // keeps track of visited nodes and their distances
var parents = new Dictionary<T, object>(Graph.VerticesCount); // keeps track of tree-nodes
frontiers.Add(StartVertex);
levels.Add(StartVertex, 0);
parents.Add(StartVertex, null);
// BFS VISIT CURRENT NODE
if (Match(StartVertex))
return StartVertex;
// TRAVERSE GRAPH
while (frontiers.Count > 0)
{
var next = new List<T>(); // keeps track of the current level, i
foreach (var node in frontiers)
{
foreach (var adjacent in Graph.Neighbours(node))
{
if (!levels.ContainsKey(adjacent)) // not visited yet
{
// BFS VISIT NODE STEP
if (Match(adjacent))
return adjacent;
levels.Add(adjacent, level); // level[node] + 1
parents.Add(adjacent, node);
next.Add(adjacent);
}
}
}
frontiers = next;
level = level + 1;
}
throw new Exception("Item was not found!");
}
}
}