public BreadthFirstSearch(int totalVertix) { v = totalVertix; adjList = new VertexNode[totalVertix]; for (int i = 0; i < adjList.Length; i++) { adjList[i] = new VertexNode(i); } }
private void BFS(int source) { Queue <VertexNode> queue = new Queue <VertexNode>(); //initializing vertex Queue VertexNode src = adjList[source]; //finding source src.Color = 'G'; //giving color grey to source src.Distance = 0; //0 distance to source src.Parent = null; // np parent for source //marking all other vertices color to white distance is max and parent is null for (int i = 0; i < adjList.Length; i++) { VertexNode u = adjList[i]; if (u.Vertex != source) { u.Color = 'W'; u.Distance = Int32.MaxValue; u.Parent = null; } } queue.Enqueue(src); //enquing source while (queue.Count > 0) { VertexNode u = queue.Dequeue(); VertexNode v = u.Next; //finding linked vertex while (v != null) { VertexNode mainV = adjList[v.Vertex]; // getting actual vertex if (mainV.Color == 'W') //process only if white { mainV.Color = 'G'; //grey for currently processing mainV.Distance = u.Distance + 1; // distance 1+ from parent mainV.Parent = u; //assigning parent queue.Enqueue(mainV); //enqueue for finding connected nodes to this } v = v.Next; //another node connected to u } u.Color = 'B'; //once completed mark color as black } }
private List <VertexNode> RecPrint(VertexNode u, VertexNode v, List <VertexNode> VertexList) { if (u == v) { VertexList.Add(u); } //Console.WriteLine(u.Vertex); else if (v.Parent == null) { VertexList.Clear(); } else { VertexList.Add(v); RecPrint(u, v.Parent, VertexList); //Console.WriteLine(v.Vertex + " "); } return(VertexList); }