如果说图的深度优先搜索类似树的前序遍历,那么图的广度优先遍历就类似于树的层序遍历。
以下是邻接矩阵存储方式的图的BFS操作。
#include<iostream>
#include<queue>
using namespace std;
typedef char VertexType;//顶点数据类型
typedef int EdgeType;//边上的权值数据类型
const int MAXVEX = 100;//最大顶点数
const int INFINITY = 65535;//代表无穷大
typedef struct
{
VertexType vexs[MAXVEX];//顶点表
EdgeType arc[MAXVEX][MAXVEX];//邻接矩阵,可看作边表
int numVertexes, numEdges;//图中当前的顶点数和边数
}MGraph;
void CreateMGraph(MGraph *G)
{
int i,j,k,w;
printf("请输入顶点数和边数:\n");
scanf("%d,%d",&G->numVertexes,&G->numEdges);
for(i=0; i<G->numVertexes; i++)
{
fflush(stdin);
printf("请输入顶点%d的值:\n",i);
scanf("%c",&G->vexs[i]);
}
for(i=0; i<G->numVertexes; i++)//初始化邻接表
{
for(j=0; j<G->numVertexes; j++)
{
if(i == j)
G->arc[i][j] = 0;
else
G->arc[i][j] = INFINITY;
}
}
for(k=0; k<G->numEdges; k++)//在邻接表中写入具体数据
{
printf("请输入边(vi,vj)上的下标i,j和权w:\n");
scanf("%d,%d,%d",&i,&j,&w);
G->arc[i][j] = w;
G->arc[j][i] = G->arc[i][j];//因为是无向图,矩阵对称
}
}
/************************************************************************/
/* 以下是BFS */
/************************************************************************/
bool visited[MAXVEX];
void BFSTraverse(MGraph G)
{
int i,j;
queue<int> Q;
for (i=0; i<G.numVertexes; i++)
{
if(!visited[i])
{
visited[i] = true;
printf("%c ",G.vexs[i]);
Q.push(i);
while (!Q.empty())
{
i = Q.front();
Q.pop();
for (j=0; j<G.numVertexes; j++)
{
if (G.arc[i][j] == 1 && !visited[j])
{
visited[j] = true;
printf("%c ",G.vexs[j]);
Q.push(j);
}
}
}
}
}
}
int main()
{
MGraph G;
CreateMGraph(&G);
for(int i=0; i<G.numVertexes; i++)
{
for(int j=0; j<G.numVertexes; j++)
{
printf("%d ",G.arc[i][j]);
}
printf("\n");
}
BFSTraverse(G);
cout<<endl;
system("pause");
return 0;
}
结果:
下面是邻接表存储方式存储图的BFS:
#include<iostream>
#include <queue>
using namespace std;
typedef char VertexType;//顶点数据类型
typedef int EdgeType;//边上的权值数据类型
const int MAXVEX = 100;//最大顶点数
const int INFINITY = 65535;//代表无穷大
typedef struct EdgeNode//边表结点
{
int adjvex;//邻接点域,存储该点对应的下标
EdgeType weight;//存储权值,对于非网图可以不需要
struct EdgeNode *next; //链域,指向下一个邻接点
}EdgeNode;
typedef struct VertexNode//顶点表结点
{
VertexType data;
EdgeNode *firstedge;
}VertexNode,AdjList[MAXVEX];
typedef struct//图结构
{
AdjList adjList;
int numVertexes,numEdges;
}GraphAdjList;
void CreateALGraph(GraphAdjList *G)
{
int i,j,k,w;
EdgeNode *e;
printf("输入顶点数和边数:\n");
scanf("%d,%d",&G->numVertexes,&G->numEdges);
for(i=0; i<G->numVertexes; i++)
{
fflush(stdin);
printf("请输入顶点%d的值\n",i);
scanf("%c",&G->adjList[i].data);
G->adjList[i].firstedge = NULL;
}
for (k=0; k<G->numEdges; k++)
{
fflush(stdin);
printf("请输入边(vi,vj)上的下标i,j和权w:\n");
scanf("%d,%d,%d",&i,&j,&w);
e = (EdgeNode *)malloc(sizeof(EdgeNode));
e->adjvex = j;
e->weight = w;
e->next = G->adjList[i].firstedge;
G->adjList[i].firstedge = e;
e = (EdgeNode *)malloc(sizeof(EdgeNode));//无向图[i][j]与[j][i]权值相同,可以一次将i->j与j->i都进行赋值,如果是有向图则只需要一次赋值
e->adjvex = i;
e->weight = w;
e->next = G->adjList[j].firstedge;
G->adjList[j].firstedge = e;
}
}
/************************************************************************/
/* 以下是BFS */
/************************************************************************/
bool visited[MAXVEX];
void BFSTraverse(GraphAdjList &GL)
{
int i;
EdgeNode *p;
queue<int> Q;
for (i=0; i<GL.numVertexes; i++)
{
visited[i] = false;
}
for (i=0; i<GL.numVertexes; i++)
{
if(!visited[i])
{
visited[i] = true;
printf("%c ",GL.adjList[i].data);
Q.push(i);
while (!Q.empty())
{
i=Q.front();
Q.pop();
p = GL.adjList[i].firstedge;
while (p)
{
if (!visited[p->adjvex])
{
visited[p->adjvex] = true;
printf("%c ",GL.adjList[p->adjvex].data);
Q.push(p->adjvex);
}
p = p->next;
}
}
}
}
}
int main()
{
GraphAdjList graphA;
CreateALGraph(&graphA);
for (int i=0; i<graphA.numVertexes; i++)
{
cout<<graphA.adjList[i].data<<"-> ";
EdgeNode *tempNode = graphA.adjList[i].firstedge;
while (NULL != tempNode)
{
cout<<"["<< tempNode->adjvex<<"]["<<tempNode->weight<<"]->";
tempNode = tempNode->next;
}
tempNode = NULL;
cout<<endl;
}
BFSTraverse(graphA);
cout<<endl;
system("pause");
return 0;
}
![](https://img-my.csdn.net/uploads/201303/05/1362464381_5320.jpg)
深度优先更适合目标比较明确,以找到目标为主要目的的情况,而广度优先更适合不断扩大遍历范围时找到相对最优解的情况。