主题:无向图深度、广度优先算法(邻接矩阵)求助
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#define N 100 //图的顶点数
#define E 100 //图的边数
#define flase 0
#define true 1
#define max 20
#define Null 0
typedef char vextype; //顶点的数据类型
typedef int adjtype; //权值类型
typedef struct graph
{
vextype vexs[N]; //顶点
adjtype arcs[E][E]; //权值
adjtype vexnum,arcnum; //顶点数和边数
}G;
int qlength=0;
typedef struct sqqueue
{
int data[max];
int front;
int rear;
}sqqueue;
int visit[max];
locatevex(G *x,char e)
{
G *t;
int i;
char ed;
t=x;
ed=e;
for(i=0;i<t->vexnum;i++)
{
if(t->vexs[i]==ed)
{
return(i);
break;
}
}
}
G *creatudg()
{
G *g;
int i,j,k,col,row;
char v1,v2,t;
g=(struct graph *) malloc (sizeof(struct graph));
printf("\nplease ipnut the vexnum=");
scanf("%d",&g->vexnum);
fflush(stdin);
printf("\nplease input the vexs:\n");
for(i=0;i<g->vexnum;i++)
{
scanf("%c",&g->vexs[i]);
fflush(stdin);
}
printf("\nplease ipnut the arcnum=");
scanf("%d",&g->arcnum);
fflush(stdin);
for(i=0;i<g->vexnum;i++)
{
for(j=0;j<g->vexnum;j++)
{ g->arcs[i][j]=0;
}
}
printf("\nPlease input the(v1,v2)v1->v2\n");
for(k=0;k<g->arcnum;k++)
{
scanf("%c%c",&v1,&v2);
fflush(stdin);
row=locatevex(g,v1);
col=locatevex(g,v2);
printf("row=%d col=%d",row,col);
g->arcs[row][col]=1;
g->arcs[col][row]=g->arcs[row][col];
}
return(g);
}
void printudg(G *t)
{
G *p;
int i,j,k;
p=t;
if(p!=0) printf("The udg has %d vexnum and %d arcsnum\n",p->vexnum,p->arcnum);
printf("The vexs are:\n");
for(i=0;i<p->vexnum;i++)
{
printf("\np->vexs[%d]=%c\n",i,p->vexs[i]);
}
printf("The arcs is:\n");
p=t;
for(i=0;i<p->vexnum;i++)
{
for(j=0;j<=p->vexnum;j++)
printf("\np.arcs[%d][%d]=%d\n",i,j,p->arcs[i][j]);
}
p=t;
for(i=0;i<p->vexnum;i++)
{
for(j=0;j<p->vexnum;j++)
{
printf("%4d",p->arcs[i][j]);
}
printf("\n");
}
}
dfstraverse(G *p,int vi)
{
G *g;
int i,j;
g=p;
for(i=0;i<g->vexnum;++i) visit[i]=flase;
for(i=0;i<g->vexnum;++i)
if(visit[vi]==0) dfs(g,vi);
}
dfs(G *p,int y)
{
int w;
visit[y]=true;
printv(p,y);
for(w=firstadjvex(p,y);w!=-1;w=nextadjvex(p,y,w))
if(visit[w]==0) dfs(p,w);
}
int firstadjvex(G *p, int i)
{
int k;
for (k = 0; k <p->vexnum; k++)
if (p->arcs[i][k] != 0) return k;
return -1;
}
int nextadjvex(G *p, int i, int j)
{
int k;
for(k=j+1; k<p->vexnum; k++)
if(p->arcs[i][k]!=0) return k;
return -1;
}
printv(G *p,int y)
{
int i;
for(i=0;i<p->vexnum;i++)
{
if(i==y) printf("%c",p->vexs[i]);
}
}
int sqinit(sqqueue *p) /*装入队列*/
{
p->front=0;
p->rear=0;
return 1;
}
int enqueue(sqqueue *q, int e) /*入队*/
{
if((q->rear+1)%max==q->front)
return 0;
else
q->data[q->rear]=e;
q->rear=(q->rear+1)%max;
return 1;
}
int dequeue(sqqueue *q) /*出队*/
{
int e;
if (q->front==q->rear) return 0;
e=q->data[q->front];
q->front=(q->front+1)%max;
return e;
}
int empty(sqqueue *q)
{
int v;
if (q->front==q->rear) v=0;
else v=1;
return v;
}
int gethead(sqqueue *q)
{
int e;
if (q->front==q->rear)
e=-1;
else
e=q->data[q->front];
return e;
}
void display(sqqueue *q)
{
int s;
s=q->front;
printf("the sequeue is display:\n");
if (q->front==q->rear)
printf("the sequeue is empty!");
else
{
while(s<q->rear)
{
printf("->%d", q->data[s]);
s=(s+1)%max;
}
printf("\n");
}
}
bfstraverse(G *p,int vb)
{
G *g;
sqqueue *q;
int i,j,w,u;
q=(struct sqqueue *) malloc(sizeof(struct sqqueue));
sqinit(q);
g=p;
for(i=0;i<g->vexnum;++i) visit[i]=flase;
enqueue(q,vb);
while(empty(q)!=0 )
{
u=dequeue(q);
if(visit[u]==flase)
{
visit[u]=true;
printv(g,u);
}
else break;
for(w=firstadjvex(g,u);w!=-1;w=nextadjvex(g,u,w))
{
if(visit[w]==flase)
{
enqueue(q,w);
}
}
}
}
main()
{
G *m,*n;
char v;
int vi,vb;
m=creatudg();
printudg(m);
printf("please input the vex which you want to begin dfs\n");
scanf("%c",&v);
vi=locatevex(m,v);
dfstraverse(m,vi);
printf("please input the vex which you want to begin bfs\n");
scanf("%c",&v);
vb=locatevex(m,v);
bfstraverse(m,vb);
}
这是我的图遍历程序,感觉bfs实现的很别扭,但是我实在想不出该怎样修改,请各位高手指点一下。
#include<string.h>
#include<malloc.h>
#define N 100 //图的顶点数
#define E 100 //图的边数
#define flase 0
#define true 1
#define max 20
#define Null 0
typedef char vextype; //顶点的数据类型
typedef int adjtype; //权值类型
typedef struct graph
{
vextype vexs[N]; //顶点
adjtype arcs[E][E]; //权值
adjtype vexnum,arcnum; //顶点数和边数
}G;
int qlength=0;
typedef struct sqqueue
{
int data[max];
int front;
int rear;
}sqqueue;
int visit[max];
locatevex(G *x,char e)
{
G *t;
int i;
char ed;
t=x;
ed=e;
for(i=0;i<t->vexnum;i++)
{
if(t->vexs[i]==ed)
{
return(i);
break;
}
}
}
G *creatudg()
{
G *g;
int i,j,k,col,row;
char v1,v2,t;
g=(struct graph *) malloc (sizeof(struct graph));
printf("\nplease ipnut the vexnum=");
scanf("%d",&g->vexnum);
fflush(stdin);
printf("\nplease input the vexs:\n");
for(i=0;i<g->vexnum;i++)
{
scanf("%c",&g->vexs[i]);
fflush(stdin);
}
printf("\nplease ipnut the arcnum=");
scanf("%d",&g->arcnum);
fflush(stdin);
for(i=0;i<g->vexnum;i++)
{
for(j=0;j<g->vexnum;j++)
{ g->arcs[i][j]=0;
}
}
printf("\nPlease input the(v1,v2)v1->v2\n");
for(k=0;k<g->arcnum;k++)
{
scanf("%c%c",&v1,&v2);
fflush(stdin);
row=locatevex(g,v1);
col=locatevex(g,v2);
printf("row=%d col=%d",row,col);
g->arcs[row][col]=1;
g->arcs[col][row]=g->arcs[row][col];
}
return(g);
}
void printudg(G *t)
{
G *p;
int i,j,k;
p=t;
if(p!=0) printf("The udg has %d vexnum and %d arcsnum\n",p->vexnum,p->arcnum);
printf("The vexs are:\n");
for(i=0;i<p->vexnum;i++)
{
printf("\np->vexs[%d]=%c\n",i,p->vexs[i]);
}
printf("The arcs is:\n");
p=t;
for(i=0;i<p->vexnum;i++)
{
for(j=0;j<=p->vexnum;j++)
printf("\np.arcs[%d][%d]=%d\n",i,j,p->arcs[i][j]);
}
p=t;
for(i=0;i<p->vexnum;i++)
{
for(j=0;j<p->vexnum;j++)
{
printf("%4d",p->arcs[i][j]);
}
printf("\n");
}
}
dfstraverse(G *p,int vi)
{
G *g;
int i,j;
g=p;
for(i=0;i<g->vexnum;++i) visit[i]=flase;
for(i=0;i<g->vexnum;++i)
if(visit[vi]==0) dfs(g,vi);
}
dfs(G *p,int y)
{
int w;
visit[y]=true;
printv(p,y);
for(w=firstadjvex(p,y);w!=-1;w=nextadjvex(p,y,w))
if(visit[w]==0) dfs(p,w);
}
int firstadjvex(G *p, int i)
{
int k;
for (k = 0; k <p->vexnum; k++)
if (p->arcs[i][k] != 0) return k;
return -1;
}
int nextadjvex(G *p, int i, int j)
{
int k;
for(k=j+1; k<p->vexnum; k++)
if(p->arcs[i][k]!=0) return k;
return -1;
}
printv(G *p,int y)
{
int i;
for(i=0;i<p->vexnum;i++)
{
if(i==y) printf("%c",p->vexs[i]);
}
}
int sqinit(sqqueue *p) /*装入队列*/
{
p->front=0;
p->rear=0;
return 1;
}
int enqueue(sqqueue *q, int e) /*入队*/
{
if((q->rear+1)%max==q->front)
return 0;
else
q->data[q->rear]=e;
q->rear=(q->rear+1)%max;
return 1;
}
int dequeue(sqqueue *q) /*出队*/
{
int e;
if (q->front==q->rear) return 0;
e=q->data[q->front];
q->front=(q->front+1)%max;
return e;
}
int empty(sqqueue *q)
{
int v;
if (q->front==q->rear) v=0;
else v=1;
return v;
}
int gethead(sqqueue *q)
{
int e;
if (q->front==q->rear)
e=-1;
else
e=q->data[q->front];
return e;
}
void display(sqqueue *q)
{
int s;
s=q->front;
printf("the sequeue is display:\n");
if (q->front==q->rear)
printf("the sequeue is empty!");
else
{
while(s<q->rear)
{
printf("->%d", q->data[s]);
s=(s+1)%max;
}
printf("\n");
}
}
bfstraverse(G *p,int vb)
{
G *g;
sqqueue *q;
int i,j,w,u;
q=(struct sqqueue *) malloc(sizeof(struct sqqueue));
sqinit(q);
g=p;
for(i=0;i<g->vexnum;++i) visit[i]=flase;
enqueue(q,vb);
while(empty(q)!=0 )
{
u=dequeue(q);
if(visit[u]==flase)
{
visit[u]=true;
printv(g,u);
}
else break;
for(w=firstadjvex(g,u);w!=-1;w=nextadjvex(g,u,w))
{
if(visit[w]==flase)
{
enqueue(q,w);
}
}
}
}
main()
{
G *m,*n;
char v;
int vi,vb;
m=creatudg();
printudg(m);
printf("please input the vex which you want to begin dfs\n");
scanf("%c",&v);
vi=locatevex(m,v);
dfstraverse(m,vi);
printf("please input the vex which you want to begin bfs\n");
scanf("%c",&v);
vb=locatevex(m,v);
bfstraverse(m,vb);
}
这是我的图遍历程序,感觉bfs实现的很别扭,但是我实在想不出该怎样修改,请各位高手指点一下。