早教吧作业答案频道 -->其他-->
写出对给定的无定向图从V1结点开始广度优先搜索历序列和广度优先生成树.
题目详情
写出对给定的无定向图从V1结点开始广度优先搜索历序列和广度优先生成树.
▼优质解答
答案和解析
#include
#define QueueSize 100
typedef int DataType;
typedef struct{
int front;
int rear;
int count;
DataType data[QueueSize];
}CirQueue;
void InitQueue(CirQueue *Q){
Q->front=Q->rear=0;
Q->count=0;
}
int QueueEmpty(CirQueue *Q){
return Q->count==0;
}
int QueueFull(CirQueue *Q){
return Q->count==QueueSize;
}
void EnQueue(CirQueue *Q,DataType x){
if(QueueFull(Q)){
printf("Queue overflow!");
exit(1);
}
Q->count++;
Q->data[Q->rear]=x;
Q->rear=(Q->rear+1)%QueueSize;
}
DataType DeQueue(CirQueue *Q){
DataType temp;
if(QueueEmpty(Q)){
printf("Queue underflow!");
exit(1);
}
temp=Q->data[Q->front];
Q->count--;
Q->front=(Q->front+1)%QueueSize;
return temp;
}
DataType QueueFront(CirQueue *Q){
if(QueueEmpty(Q)){
printf("Queue is empty!");
exit(1);
}
return Q->data[Q->front];
}
#include "CirQueue.h"
#define MaxVertexNum 100
typedef char VertexType;
typedef int EdgeType;
typedef enum{FALSE,TRUE} Boolean;
Boolean visited[MaxVertexNum];
typedef struct node{
int adjvex;
struct node *next;
}EdgeNode;
typedef struct vnode{
VertexType vertex;
EdgeNode *firstedge;
}VertexNode;
typedef VertexNode AdjList[MaxVertexNum];
typedef struct{
AdjList adjlist;
int n,e;
}ALGraph;
void CreateALGraph(ALGraph *G){
int i,j,k;
EdgeNode *s;
scanf("%d%d",&G->n,&G->e);
getchar();
printf("Enter the name of the nodes:\n");
for(i=0;in;i++){
G->adjlist[i].vertex=getchar();
G->adjlist[i].firstedge=NULL;
getchar();
}
for(k=0;ke;k++){
printf("Enter the edge of the graph:\n");
scanf("%d%d",&i,&j);
s=(EdgeNode*)malloc(sizeof(EdgeNode));
s->adjvex=j;
s->next=G->adjlist[i].firstedge;
G->adjlist[i].firstedge=s;
s=(EdgeNode*)malloc(sizeof(EdgeNode));
s->adjvex=i;
s->next=G->adjlist[j].firstedge;
G->adjlist[j].firstedge=s;
}
}
void DFS(ALGraph *G,int i){
EdgeNode *p;
printf("visit vertex:%c\n",G->adjlist[i].vertex);
visited[i]=TRUE;
p=G->adjlist[i].firstedge;
while(p){
if(!visited[p->adjvex])
DFS(G,p->adjvex);
p=p->next;
}
}
void DFSTraverse(ALGraph *G){
int i;
for(i=0;in;i++)
visited[i]=FALSE;
for(i=0;in;i++)
if(!visited)
DFS(G,i);
}
void BFS(ALGraph *G,int k){
int i;
CirQueue Q;
EdgeNode *p;
InitQueue(&Q);
printf("visit vertex:%c\n",G->adjlist[k].vertex);
visited[k]=TRUE;
EnQueue(&Q,k);
while(!QueueEmpty(&Q)){
i=DeQueue(&Q);
p=G->adjlist[i].firstedge;
while(p){
if(!visited[p->adjvex]){
printf("visit vertex:%c\n",G->adjlist[p->adjvex].vertex);
visited[p->adjvex]=TRUE;
EnQueue(&Q,p->adjvex);
}
p=p->next;
}
}
}
void main(){
ALGraph G;
CreateALGraph(&G);
BFS(&G,0);
}
#define QueueSize 100
typedef int DataType;
typedef struct{
int front;
int rear;
int count;
DataType data[QueueSize];
}CirQueue;
void InitQueue(CirQueue *Q){
Q->front=Q->rear=0;
Q->count=0;
}
int QueueEmpty(CirQueue *Q){
return Q->count==0;
}
int QueueFull(CirQueue *Q){
return Q->count==QueueSize;
}
void EnQueue(CirQueue *Q,DataType x){
if(QueueFull(Q)){
printf("Queue overflow!");
exit(1);
}
Q->count++;
Q->data[Q->rear]=x;
Q->rear=(Q->rear+1)%QueueSize;
}
DataType DeQueue(CirQueue *Q){
DataType temp;
if(QueueEmpty(Q)){
printf("Queue underflow!");
exit(1);
}
temp=Q->data[Q->front];
Q->count--;
Q->front=(Q->front+1)%QueueSize;
return temp;
}
DataType QueueFront(CirQueue *Q){
if(QueueEmpty(Q)){
printf("Queue is empty!");
exit(1);
}
return Q->data[Q->front];
}
#include "CirQueue.h"
#define MaxVertexNum 100
typedef char VertexType;
typedef int EdgeType;
typedef enum{FALSE,TRUE} Boolean;
Boolean visited[MaxVertexNum];
typedef struct node{
int adjvex;
struct node *next;
}EdgeNode;
typedef struct vnode{
VertexType vertex;
EdgeNode *firstedge;
}VertexNode;
typedef VertexNode AdjList[MaxVertexNum];
typedef struct{
AdjList adjlist;
int n,e;
}ALGraph;
void CreateALGraph(ALGraph *G){
int i,j,k;
EdgeNode *s;
scanf("%d%d",&G->n,&G->e);
getchar();
printf("Enter the name of the nodes:\n");
for(i=0;in;i++){
G->adjlist[i].vertex=getchar();
G->adjlist[i].firstedge=NULL;
getchar();
}
for(k=0;ke;k++){
printf("Enter the edge of the graph:\n");
scanf("%d%d",&i,&j);
s=(EdgeNode*)malloc(sizeof(EdgeNode));
s->adjvex=j;
s->next=G->adjlist[i].firstedge;
G->adjlist[i].firstedge=s;
s=(EdgeNode*)malloc(sizeof(EdgeNode));
s->adjvex=i;
s->next=G->adjlist[j].firstedge;
G->adjlist[j].firstedge=s;
}
}
void DFS(ALGraph *G,int i){
EdgeNode *p;
printf("visit vertex:%c\n",G->adjlist[i].vertex);
visited[i]=TRUE;
p=G->adjlist[i].firstedge;
while(p){
if(!visited[p->adjvex])
DFS(G,p->adjvex);
p=p->next;
}
}
void DFSTraverse(ALGraph *G){
int i;
for(i=0;in;i++)
visited[i]=FALSE;
for(i=0;in;i++)
if(!visited)
DFS(G,i);
}
void BFS(ALGraph *G,int k){
int i;
CirQueue Q;
EdgeNode *p;
InitQueue(&Q);
printf("visit vertex:%c\n",G->adjlist[k].vertex);
visited[k]=TRUE;
EnQueue(&Q,k);
while(!QueueEmpty(&Q)){
i=DeQueue(&Q);
p=G->adjlist[i].firstedge;
while(p){
if(!visited[p->adjvex]){
printf("visit vertex:%c\n",G->adjlist[p->adjvex].vertex);
visited[p->adjvex]=TRUE;
EnQueue(&Q,p->adjvex);
}
p=p->next;
}
}
}
void main(){
ALGraph G;
CreateALGraph(&G);
BFS(&G,0);
}
看了 写出对给定的无定向图从V1结...的网友还看了以下:
读“大陆空气柱图”,图中圆柱为空气柱,箭头表示空气垂直运动方向。完成下列各题。小题1:关于a、b两 2020-05-02 …
比例尺、方向和图例是地图的“语言”.读图完成下列问题.(1)图中台中到基隆的图上直线距离约为3.5 2020-07-14 …
如图所示,两列简谐横波分别沿x轴正方向和负方向传播,两波源分别位于x=-0.2m和x=1.2m处, 2020-07-31 …
如图所示,两列简谐横波分别沿x轴正方向和负方向传播,两波源分别位于x=-0.2m和x=1.2m处, 2020-07-31 …
如图,两列简谐横波分别沿x轴正方向和负方向传播,两波源分别位于x=-0.2m和x=1.2m处,两列 2020-07-31 …
如图所示,两列简谐横波分别沿x轴正方向和负方向传播,两波源分别位于x=-0.2m和x=1.2m处, 2020-07-31 …
完成下列两题:(1)已知螺线管内部的小磁针的N极指向如图1,请在图中画出螺线管上线圈的绕向.(2)已 2020-11-01 …
如图所示为一列向某方向传播的简谐横波在某时刻的波形图。在波的传播方向上一质点P在该时刻的振动方向如图 2020-12-15 …
两列简谐横波分别沿x轴正向和负向传播,波源分别位于x=-0.2m和x=1.2m处,两列波的波速均为v 2020-12-15 …
读某地区平面图,完成下列问题:(1)该图用指向标定向法表示方向,该方法只标出了方向,请在图中将指向标 2020-12-15 …