广度优先搜索(BFS)算法类似于树中的层次搜索:
从任意点s出发,先遍历与s相邻的点,然后再遍历于相邻的点相邻的点。注意有向图必须是顺方向的邻接点。
为什么说广度优先搜索可以用来求无权最短路径呢?因为,广度优先搜索每次都会先发现距离s为k的所有顶点,然后才会 发现距离s为k+1的所有顶点。 s为起始点。
void BFS(Graph& g, Vertex& s)
{
queue q;
for each vertex v in g
{
v.distance = INFINITY;
}
s.distance = 0;
q.enqueue(s);
while (!q.Empty())
{
v = dequeue(q);
for each w adjenct to v
if (v.distance == INFINITY)
{
w.distance = v. distance + 1;
w.path = v;
q.enqueue(w);
}
}
}
对于外面的while循环,会执行|V|次,因为每个顶点入队出队一次,而里面的for循环会看到一共会执行|E|次,即变长, 所以该算法时间复杂度为O(|V|+|E|)。
打印最短路径:
void PrintPath(Graph& g, Vertex& target)
{
if (target.path is a vertex)
{
PrintPath(g, target.path);
}
cout << target;
}
#if 1 #include #include using namespace std; #define MAX_VERTEX_NUM 20 #define INFINITY 2147483647 enum var{Infinity = -1}; struct adjVertexNode { int adjVertexPosition; adjVertexNode* next; }; //注意此结构体大小,因为对齐要求,第一个char字节数组要扩展成为4个字节。所以this struct 为12个字节 struct VertexNode { char data[2]; adjVertexNode* list; int dist; VertexNode *path; }; struct Graph { VertexNode VertexNode[MAX_VERTEX_NUM]; int vertexNum; int edgeNum; }; void CreateGraph (Graph& g) { int i, j, edgeStart, edgeEnd; adjVertexNode* adjNode; cout << "Please input vertex and edge num (vnum enum):" <> g.vertexNum >> g.edgeNum; cout << "Please input vertex information (v1)/n note: every vertex info end with Enter" <> g.VertexNode[i].data; // vertex data info. g.VertexNode[i].list = NULL; g.VertexNode[i].path = NULL; } cout << "input edge information(start end):" <>edgeStart >>edgeEnd; //链表的插入程序。 adjNode = new adjVertexNode; adjNode->adjVertexPosition = edgeEnd-1; // because array begin from 0, so it is j-1 adjNode->next=g.VertexNode[edgeStart-1].list; g.VertexNode[edgeStart-1].list=adjNode; //每增加一条边,则边的End顶点的入度加1 } } void PrintAdjList(const Graph& g) { cout << "The adjacent list for graph is:" << endl; for (int i=0; i < g.vertexNum; i++) { cout<< g.VertexNode[i].data << "->"; adjVertexNode* head = g.VertexNode[i].list; if (head == NULL) cout << "NULL"; while (head != NULL) { cout << head->adjVertexPosition + 1 <<" "; head = head->next; } cout << endl; } } void DeleteGraph(Graph &g) { for (int i=0; inext; delete tmp; tmp = NULL; } } } #if 1 void BFS(Graph &g,VertexNode &S) { // VertexNode *V = S; queueQ; int i; for(i = 0; i < g.vertexNum; i++) { g.VertexNode[i].dist = Infinity; } S.dist = 0; Q.push(&S); while (!Q.empty()) { VertexNode *v = Q.front(); Q.pop(); adjVertexNode *head = v->list; while (head != NULL) { if (g.VertexNode[head->adjVertexPosition].dist == Infinity) { g.VertexNode[head->adjVertexPosition].dist = v->dist + 1; g.VertexNode[head->adjVertexPosition].path = v; Q.push(&g.VertexNode[head->adjVertexPosition]); } head = head->next; } } } #endif #if 1 void PrintGraph(Graph& g, VertexNode* target) { if (target->path != NULL) { //cout << 1 << " "; PrintGraph(g, target->path); cout << " "; } cout << target->data ; } #endif int main(int argc, const char** argv) { Graph g; CreateGraph(g); PrintAdjList(g); BFS(g, g.VertexNode[0]); cout<<"print the shortest path from v1 to v7:"<