题目描述
图的广度优先遍历实现 要求: 1、以邻接表形式构造图; 2、打印输出邻接表; 3、输出:广度优先遍历序列;
输入: 顶点数、边数目。 点、边集合表示(1代表存在边,0代表不存在边) 输出: (1)边逻辑正确,则输出:“输入正确!” (2)边逻辑不正确,则输出:“输入边数不对!程序退出!!“ (3)输出邻接表存储; (4)输出广义遍历结果。
输入
5 8 // 5代表顶点数, 8 代表边数目 0 1 0 1 1
1 0 1 1 0
0 1 0 1 1
1 1 1 0 1
1 0 1 1 0
输出
输入正确! 图G的邻接表: 0: 1[1]→ 3[1]→ 4[1]→∧ 1: 0[1]→ 2[1]→ 3[1]→∧ 2: 1[1]→ 3[1]→ 4[1]→∧ 3: 0[1]→ 1[1]→ 2[1]→ 4[1]→∧ 4: 0[1]→ 2[1]→ 3[1]→∧ 广度优先序列: 2 1 3 4 0
样例输入
5 8 0 1 0 1 1
1 0 1 1 0
0 1 0 1 1
1 1 1 0 1
1 0 1 1 1
样例输出
输入边数不对!程序退出!!
代码实现
(1)、图的创建输出销毁
#include <iostream>
#include <stdlib.h>
using namespace std;
#define MAXV 100
#define INF 32767
typedef int InfoType;
typedef struct ANode
{
int adjvex;
struct ANode *nextarc;
int weight;
} ArcNode;
typedef struct Vnode
{
InfoType info;
ArcNode *firstarc;
} VNode;
typedef struct
{
VNode adjlist[MAXV];
int n, e;
}AdjGraph;
void GreateAdj(AdjGraph *&G, int A[MAXV][MAXV], int n, int e)
{
int i, j;
ArcNode *p;
G = (AdjGraph *)malloc(sizeof(AdjGraph));
for(i = 0; i < n; i++)
{
G->adjlist[i].firstarc = NULL;
}
for(i = 0; i < n; i++)
{
for(j = n-1; j >= 0; j--)
{
if(A[i][j] != 0 && A[i][j] != INF)
{
p = (ArcNode *)malloc(sizeof(ArcNode));
p->adjvex = j;
p->weight = A[i][j];
p->nextarc = G->adjlist[i].firstarc;
G->adjlist[i].firstarc = p;
}
}
}
G->n = n;
G->e = e;
}
void DispAdj(AdjGraph *G)
{
int i;
ArcNode *p;
for(i = 0; i < G->n; i++)
{
p = G->adjlist[i].firstarc;
cout<<i<<": ";
while(p != NULL)
{
cout<<p->adjvex<<"["<<p->weight<<"]"<<"-> ";
p = p->nextarc;
}
cout<<"^"<<endl;
}
}
void DistroyAdj(AdjGraph *&G)
{
int i;
ArcNode *pre, *p;
for(i = 0; i < G->n; i++)
{
pre = G->adjlist[i].firstarc;
if(pre != NULL)
{
p = pre->nextarc;
while(p != NULL)
{
free(pre);
pre = p;
p = p->nextarc;
}
free(pre);
}
}
free(G);
}
int main()
{
AdjGraph *G;
int A[MAXV][MAXV];
int n;
int e;
int sum = 0;
cin>>n;
cin>>e;
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
cin>>A[i][j];
if(A[i][j] == 1)
{
sum++;
}
}
}
if(sum != 2*e)
{
cout<<"输入边数不对!程序退出!!"<<endl;
return 0;
}
cout<<"输入正确!"<<endl;
cout<<"图G的邻接表:"<<endl;
GreateAdj(G, A, n, e);
DispAdj(G);
DistroyAdj(G);
return 0;
}
(2)、含广度优先遍历实现代码(c++)
#include <iostream>
#include <stdlib.h>
using namespace std;
#define MAXV 100
#define MaxSize 10
#define INF 32767
typedef int InfoType;
typedef int ElemType;
typedef struct ANode
{
int adjvex;
struct ANode *nextarc;
int weight;
} ArcNode;
typedef struct Vnode
{
InfoType info;
ArcNode *firstarc;
} VNode;
typedef struct
{
VNode adjlist[MAXV];
int n, e;
}AdjGraph;
typedef struct
{
ElemType data[MaxSize];
int front, rear;
} SqQueue;
void GreateAdj(AdjGraph *&G, int A[MAXV][MAXV], int n, int e)
{
int i, j;
ArcNode *p;
G = (AdjGraph *)malloc(sizeof(AdjGraph));
for(i = 0; i < n; i++)
{
G->adjlist[i].firstarc = NULL;
}
for(i = 0; i < n; i++)
{
for(j = n-1; j >= 0; j--)
{
if(A[i][j] != 0 && A[i][j] != INF)
{
p = (ArcNode *)malloc(sizeof(ArcNode));
p->adjvex = j;
p->weight = A[i][j];
p->nextarc = G->adjlist[i].firstarc;
G->adjlist[i].firstarc = p;
}
}
}
G->n = n;
G->e = e;
}
void DispAdj(AdjGraph *G)
{
int i;
ArcNode *p;
for(i = 0; i < G->n; i++)
{
p = G->adjlist[i].firstarc;
cout<<" "<<i<<": ";
while(p != NULL)
{
cout<<" "<<p->adjvex<<"["<<p->weight<<"]"<<"→";
p = p->nextarc;
}
cout<<"^"<<endl;
}
}
void DistroyAdj(AdjGraph *&G)
{
int i;
ArcNode *pre, *p;
for(i = 0; i < G->n; i++)
{
pre = G->adjlist[i].firstarc;
if(pre != NULL)
{
p = pre->nextarc;
while(p != NULL)
{
free(pre);
pre = p;
p = p->nextarc;
}
free(pre);
}
}
free(G);
}
void InitQueue(SqQueue *&q)
{
q = (SqQueue *)malloc(sizeof(SqQueue));
q->front = q->rear = 0;
}
void DestroyQueue(SqQueue *&q)
{
free(q);
}
bool QueueEmpty(SqQueue *q)
{
return (q->front == q->rear);
}
bool enQueue(SqQueue *&q, ElemType e)
{
if((q->rear+1)%MaxSize == q->front)
{
cout<<"队列已满,%d进队不成功!\n"<<e;
return false;
}
q->rear = (q->rear+1)%MaxSize;
q->data[q->rear] = e;
return true;
}
bool deQueue(SqQueue *&q, ElemType &e)
{
if(q->front == q->rear)
return false;
q->front = (q->front + 1)%MaxSize;
e = q->data[q->front];
return true;
}
void BFS(AdjGraph *G, int v)
{
int w, i;
ArcNode *p;
SqQueue *qu;
InitQueue(qu);
int visited[MAXV];
for(i = 0; i < G->n; i++)
{
visited[i] = 0;
}
cout<<"广度优先序列:"<<v;
visited[v] = 1;
enQueue(qu, v);
while(!QueueEmpty(qu))
{
deQueue(qu, w);
p = G->adjlist[w].firstarc;
while(p != NULL)
{
if(visited[p->adjvex] == 0)
{
cout<<" "<<p->adjvex;
visited[p->adjvex] = 1;
enQueue(qu, p->adjvex);
}
p = p->nextarc;
}
}
cout<<endl;
}
int main()
{
AdjGraph *G;
int A[MAXV][MAXV];
int n;
int e;
int sum = 0;
cin>>n;
cin>>e;
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
cin>>A[i][j];
if(A[i][j] == 1)
{
sum++;
}
}
}
if(sum != 2*e)
{
cout<<"输入边数不对!程序退出!!"<<endl;
return 0;
}
cout<<"输入正确!"<<endl;
cout<<"图G的邻接表:"<<endl;
GreateAdj(G, A, n, e);
DispAdj(G);
int v = 2;
BFS(G, v);
DistroyAdj(G);
return 0;
}
(3)、含广度优先遍历实现代码(C)
#define MaxSize 100
#include <stdio.h>
#include <malloc.h>
#define INF 32767
#define MAXV 100
typedef char InfoType;
#include<iostream>
using namespace std;
typedef struct
{ int no;
InfoType info;
} VertexType;
typedef struct
{ int edges[MAXV][MAXV];
int n,e;
VertexType vexs[MAXV];
} MatGraph;
typedef struct ANode
{ int adjvex;
struct ANode *nextarc;
int weight;
} ArcNode;
typedef struct Vnode
{ InfoType info;
int count;
ArcNode *firstarc;
} VNode;
typedef struct
{ VNode adjlist[MAXV];
int n,e;
} AdjGraph;
void CreateMat(MatGraph &g,int A[MAXV][MAXV],int n,int e)
{
int i,j;
g.n=n; g.e=e;
for (i=0;i<g.n;i++)
for (j=0;j<g.n;j++)
g.edges[i][j]=A[i][j];
}
void DispMat(MatGraph g)
{
int i,j;
for (i=0;i<g.n;i++)
{
for (j=0;j<g.n;j++)
if (g.edges[i][j]!=INF)
printf("%4d",g.edges[i][j]);
else
printf("%4s","∞");
printf("\n");
}
}
void CreateAdj(AdjGraph *&G,int A[MAXV][MAXV],int n,int e)
{
int i,j;
ArcNode *p;
G=(AdjGraph *)malloc(sizeof(AdjGraph));
for (i=0;i<n;i++)
G->adjlist[i].firstarc=NULL;
for (i=0;i<n;i++)
for (j=n-1;j>=0;j--)
if (A[i][j]!=0 && A[i][j]!=INF)
{ p=(ArcNode *)malloc(sizeof(ArcNode));
p->adjvex=j;
p->weight=A[i][j];
p->nextarc=G->adjlist[i].firstarc;
G->adjlist[i].firstarc=p;
}
G->n=n; G->e=n;
}
void DispAdj(AdjGraph *G)
{
int i;
ArcNode *p;
for (i=0;i<G->n;i++)
{
p=G->adjlist[i].firstarc;
printf("%3d: ",i);
while (p!=NULL)
{
printf("%3d[%d]→",p->adjvex,p->weight);
p=p->nextarc;
}
printf("∧\n");
}
}
void DestroyAdj(AdjGraph *&G)
{ int i;
ArcNode *pre,*p;
for (i=0;i<G->n;i++)
{ pre=G->adjlist[i].firstarc;
if (pre!=NULL)
{ p=pre->nextarc;
while (p!=NULL)
{ free(pre);
pre=p; p=p->nextarc;
}
free(pre);
}
}
free(G);
}
typedef int ElemType;
typedef struct
{
ElemType data[MaxSize];
int front,rear;
} SqQueue;
void InitQueue(SqQueue *&q)
{ q=(SqQueue *)malloc (sizeof(SqQueue));
q->front=q->rear=0;
}
void DestroyQueue(SqQueue *&q)
{
free(q);
}
bool QueueEmpty(SqQueue *q)
{
return(q->front==q->rear);
}
bool enQueue(SqQueue *&q,ElemType e)
{ if ((q->rear+1)%MaxSize==q->front)
return false;
q->rear=(q->rear+1)%MaxSize;
q->data[q->rear]=e;
return true;
}
bool deQueue(SqQueue *&q,ElemType &e)
{ if (q->front==q->rear)
return false;
q->front=(q->front+1)%MaxSize;
e=q->data[q->front];
return true;
}
void BFS(AdjGraph *G,int v)
{
int w,i;
ArcNode *p;
SqQueue *qu;
InitQueue(qu);
int visited[MAXV];
for (i=0;i<G->n;i++) visited[i]=0;
printf("%2d",v);
visited[v]=1;
enQueue(qu,v);
while (!QueueEmpty(qu))
{
deQueue(qu,w);
p=G->adjlist[w].firstarc;
while (p!=NULL)
{
if (visited[p->adjvex]==0)
{
printf("%2d",p->adjvex);
visited[p->adjvex]=1;
enQueue(qu,p->adjvex);
}
p=p->nextarc;
}
}
printf("\n");
}
int main()
{ int sum1=0;
int sum0=0;
AdjGraph *G;
int A[MAXV][MAXV]={{0,1,0,1,1},{1,0,1,1,0},
{0,1,0,1,1},{1,1,1,0,1},{1,0,1,1,0}};
int n=5, e=8;
cin>>n>>e;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cin>>A[i][j];
}
}
for(int i1=0;i1<n;i1++){
for(int j1=0;j1<n;j1++){
if(A[i1][j1]==1){sum1=sum1+1;}
if(A[i1][j1]==0){sum0=sum0+1;}
}
}
if(sum1==2*e&&sum0==n*n-2*e){
cout<<"输入正确!"<<endl;
}else{
cout<<"输入边数不对!程序退出!!"<<endl;
return 0;
}
CreateAdj(G,A,n,e);
printf("图G的邻接表:\n");
DispAdj(G);
printf("广度优先序列:");BFS(G,2);printf("\n");
DestroyAdj(G);
return 1;
}
运行结果
|