-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBreadthFirstTraversal.cs
More file actions
42 lines (37 loc) · 885 Bytes
/
BreadthFirstTraversal.cs
File metadata and controls
42 lines (37 loc) · 885 Bytes
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
using System.Collections.Generic;
using DA.Graphs;
namespace DA.Algorithms.Searching
{
internal class BreadthFirstTraversal
{
public void BFS_Queue (Graph graph, int index, int[] visited)
{
if (graph.Equals (null))
{
throw new System.ArgumentNullException (nameof(graph));
}
if (index < 0)
{
throw new System.InvalidOperationException ("Index must be greater than zero");
}
int current;
Queue<int> queue = new Queue<int> ();
visited[index] = 1;
queue.Enqueue (index);
while (queue.Count > 0)
{
current = queue.Dequeue ();
Graph.Node currentNode = graph.GetNode(current);
while (currentNode != null)
{
if (visited[currentNode.destination] == 0)
{
visited[currentNode.destination] = 1;
queue.Enqueue (currentNode.destination);
}
currentNode = currentNode.next;
}
}
}
}
}