PIBD-14_Boyko_M_S_Cursach_DFS/Cursach/Realisations/BFS.cs
2024-05-22 00:02:04 +04:00

67 lines
1.6 KiB
C#

using System.Reflection.Metadata;
using Cursach.Parameters;
using Cursach.States;
namespace Cursach.Realisations;
public class BFS
{
public Node StartNode { private set; get; }
public bool IsEnd { private set; get; }
private AdjacencyList adjacencyList;
private bool isCompleted = false;
public List<Node> visited;
public Queue<Node> queue;
public BFS(BFSParameters parameter)
{
StartNode = parameter.StartNode;
adjacencyList = parameter.adjacencyList;
visited = new();
queue = new();
queue.Enqueue(StartNode);
IsEnd = false;
isCompleted = false;
}
public State GetState()
{
return new State(adjacencyList, new List<Node>(visited), new List<Node>(queue));
}
public void SetState(State state)
{
adjacencyList = state.AdjacencyList;
visited = state.visited;
queue = new Queue<Node>(state.queue);
}
public bool Step()
{
if (queue.Count > 0)
{
Node currentNode = queue.Dequeue();
visited.Add(currentNode);
foreach (Node neighbour in adjacencyList.Values(currentNode))
{
if (!visited.Contains(neighbour) && !queue.Contains(neighbour))
{
queue.Enqueue(neighbour);
}
}
}
if (queue.Count <= 0)
{
if (!isCompleted)
{
isCompleted = true;
return true;
}
return false;
}
return true;
}
}