【原理】
STEP1.开始节点入队列
STEP2.将当前队列出列,出列的节点判断是否有子节点,每个子节点入队列。
STEP3.只要队列没空,重复STEP2
【实现】
public List<CVertex> BreadFirstSearch()
{
List<CVertex> lstRes = new List<CVertex>();
Queue<int> oQueue = new Queue<int>();
this.m_lstVertex[0].IsVisited = true;
oQueue.Enqueue(0);
lstRes.Add(this.m_lstVertex[0]);
int iVertexIndex, iAdjVertexIndex;
while (oQueue.Count > 0)
{
iVertexIndex = oQueue.Dequeue();
iAdjVertexIndex = _GetAdjUnvisitedVertexIndex(iVertexIndex);
while (RET_ISNOT_NOSUCCSSOR_VERTEX != iAdjVertexIndex)
{
this.m_lstVertex[iAdjVertexIndex].IsVisited = true;
lstRes.Add(this.m_lstVertex[iAdjVertexIndex]);
oQueue.Enqueue(iAdjVertexIndex);
iAdjVertexIndex = _GetAdjUnvisitedVertexIndex(iVertexIndex);
}
}
foreach (var oVertex in this.m_lstVertex)
{
oVertex.IsVisited = false;
}
return lstRes;
}
浙公网安备 33010602011771号