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 visited; public Queue 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(visited), new List(queue)); } public void SetState(State state) { adjacencyList = state.AdjacencyList; visited = state.visited; queue = new Queue(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; } }