程序填空题:广度优先遍历邻接矩阵存储的图
```c++
/* 广度优先遍历邻接矩阵存储的图Graph */
typedef struct {
Vertex G[MAX][MAX];
int Nv;
}Graph;
typedef Graph* MGraph;
typedef struct {
Vertex *vertex;
int len;
int ptr;
int end;
}queue;
typedef queue* Queue;
/* IsEdge(Graph, V, W)检查是否图Graph中的一条边,即W是否V的邻接点。 */
bool IsEdge( MGraph Graph, Vertex V, Vertex W )
{
return Graph->G[V][W]}
/* Visited[]为布尔类型全局数组,已经初始化为false */
void BFS ( MGraph Graph, Vertex S, void (*Visit)(Vertex) )
{
Queue Q;
Vertex V, W;
Q = CreateQueue( MaxSize );
Visit( S );
Visited[S] = true;
@@[AddQ(Q, S)](2);
while ( !IsEmpty(Q) ) {
@@[V = DeleteQ(Q)](2);
for( W=0; WNv; W++ )
if ( @@[!Visited[W] && IsEdge(Graph, V, W)](2) ) {
Visit( W );
Visited[W] = true;
AddQ(Q, W);
}
} /* while结束*/
}
```
答案:
第1空:AddQ(Q, S)
第2空:V = DeleteQ(Q)
第3空:!Visited[W] && IsEdge(Graph, V, W)
/* 广度优先遍历邻接矩阵存储的图Graph */
typedef struct {
Vertex G[MAX][MAX];
int Nv;
}Graph;
typedef Graph* MGraph;
typedef struct {
Vertex *vertex;
int len;
int ptr;
int end;
}queue;
typedef queue* Queue;
/* IsEdge(Graph, V, W)检查
bool IsEdge( MGraph Graph, Vertex V, Vertex W )
{
return Graph->G[V][W]
/* Visited[]为布尔类型全局数组,已经初始化为false */
void BFS ( MGraph Graph, Vertex S, void (*Visit)(Vertex) )
{
Queue Q;
Vertex V, W;
Q = CreateQueue( MaxSize );
Visit( S );
Visited[S] = true;
@@[AddQ(Q, S)](2);
while ( !IsEmpty(Q) ) {
@@[V = DeleteQ(Q)](2);
for( W=0; W
if ( @@[!Visited[W] && IsEdge(Graph, V, W)](2) ) {
Visit( W );
Visited[W] = true;
AddQ(Q, W);
}
} /* while结束*/
}
```
答案:
第1空:AddQ(Q, S)
第2空:V = DeleteQ(Q)
第3空:!Visited[W] && IsEdge(Graph, V, W)