This repository has been archived by the owner on Apr 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbfs.cs
53 lines (50 loc) · 1.41 KB
/
bfs.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DS_Lab1
{
/// <summary>
/// Breadth-first search implementation
/// </summary>
class bfs
{
Graph graph;
bool[] used;
Queue<int> q;
int k;
public bfs(Graph g)
{
this.graph = g;
used = new bool[graph.n];
q = new Queue<int>();
k = 0;
}
/// <summary>
/// BFS graph traversal method
/// </summary>
/// <param name="v">Start vertex</param>
public void BFS(int v)
{
used[v] = true;
q.Enqueue(v);
while (q.Count !=0)
{
k++;
int t = q.Dequeue();
if (q.Count != 0 || k == 1) System.Console.Write("Текущая вершина - {0},BFS-номер - {1},содержание очереди:",t + 1,k);
for (int i = 0;i < graph.n;i++)
{
if (graph.AdjMatr[t,i] != 0 && !used[i])
{
q.Enqueue(i);//Enqueuing unused,adjacent vertices
used[i] = true;
}
}
foreach (int i in q) System.Console.Write(i + 1 + " ");
System.Console.WriteLine();
}
}
}
}