public BFSTest() { AdjacencyList <int> adjacencyList = new AdjacencyList <int>(); ForBFSNode <int> firstHead = new ForBFSNode <int> { Data = 1 }; _startNode = firstHead; ForBFSNode <int> secondHead = new ForBFSNode <int> { Data = 2 }; ForBFSNode <int> thirdHead = new ForBFSNode <int> { Data = 3 }; ForBFSNode <int> forthHead = new ForBFSNode <int> { Data = 4 }; adjacencyList.Nodes.AddRange(new List <ForBFSNode <int> > { firstHead, secondHead, thirdHead, forthHead }); adjacencyList.NeighborsList.Add(firstHead, new List <Node <int> > { secondHead, forthHead }); adjacencyList.NeighborsList.Add(secondHead, new List <Node <int> > { firstHead, thirdHead, forthHead }); adjacencyList.NeighborsList.Add(thirdHead, new List <Node <int> > { secondHead, forthHead }); adjacencyList.NeighborsList.Add(forthHead, new List <Node <int> > { firstHead, secondHead, thirdHead }); _bfs = new BFS <int>(adjacencyList); }
public void DoSearch(ForBFSNode <DataType> start) { Queue <ForBFSNode <DataType> > queue = new Queue <ForBFSNode <DataType> >(); start.Color = NodeColor.GRAY; start.Parent = null; queue.Enqueue(start); while (queue.Count > 0) { ForBFSNode <DataType> item = queue.Dequeue(); Console.WriteLine("node {0},parent is {1}", item.Data, item.Parent == null?"null": item.Parent.Data.ToString()); foreach (var each in _list.NeighborsList[item]) { ForBFSNode <DataType> bfsNode = (ForBFSNode <DataType>)each; if (bfsNode.Color == NodeColor.WHITE) { bfsNode.Color = NodeColor.GRAY; bfsNode.Parent = item; queue.Enqueue(bfsNode); } } item.Color = NodeColor.BLACK; } }