C语言之图的创建 dfs bfs操作

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_40667484/article/details/80873333

一、实验目的

1、掌握图的邻接矩阵和邻接表存储结构;

2、掌握图的广度优先遍历和深度优先遍历算法。

二、实验环境

PC微机,WindowsVisual C++

三、实验内容

1、图的邻接矩阵和邻接表存储。

1)基本要求

利用图的邻接矩阵和邻接表,编写图的创建、显示算法。图的结构如图1所示:

1 具有10个顶点的图



3)算法实现


邻接矩阵


头文件

#define MaxVex 100

typedef int  VertexType; 

typedef int     EdgeType;

typedef struct  graph

{    

       int vnum, ednum;                                                 //顶点的个数,边的个数

       VertexTypevexs[MaxVex];                                 //用来存顶点数据

       EdgeType   edges[MaxVex][MaxVex];               //用来存边的数据(两点代替一边)

} Mgraph;

 

 

主文件

#include <stdio.h>

#include <stdlib.h>

//#include <string.h>

//#include <malloc.h>

#include "list.h"

 

 

Mgraph *creatMgraph()                                                //创建图

{

       Mgraph*p;

       inti,j,k;

       p=(Mgraph*)malloc(sizeof(Mgraph));

 

       printf("请输出顶点和边的个数\n");

       scanf("%d%d",&p->vnum,&p->ednum);

 

       for(i=0;i<p->vnum;i++)                                                         //矩阵初始化

       {

              for(j=0;j<p->vnum;j++)

              {

                     p->edges[i][j]=0;

              }

       }

 

       printf("输入顶点的信息\n");

       for(i=0;i<p->vnum;i++)

       {

              scanf("%d",&p->vexs[i]);

       }

       printf("输入边的信息\n");

       {

              for(k=0;k<p->ednum;k++)

              {

                     scanf("%d%d",&i,&j);

                     p->edges[i][j]=1;                                            //无向图的创建

                     p->edges[j][i]=1;

              }

       }

       returnp;

}

 

void displayGraph(Mgraph *mG)                                //输出图的信息

{

       inti,j;

       printf("请输入点的信息\n");

       for(i=0;i<mG->vnum;i++)

       {

              printf("%d\t",mG->vexs[i]);

       }

       printf("请输入边的信息\n");

       for(i=0;i<mG->vnum;i++)

       {

              for(j=0;j<mG->vnum;j++)

              {

                     if(mG->edges[i][j]==1)

                     {

                            printf("%d%d ",i,j);

                     }

              }

       }

}

 

void main()

{

       Mgraph*M;

       M=creatMgraph();

       displayGraph(M);

}

邻接表

#include <stdio.h>

#include <stdlib.h>

//#include <string.h>

//#include <malloc.h>

#include "graph.h"

 

头文件

typedef struct anode

{

       intadjvex;

       structanode *next;

}Anode;

 

typedef struct vnode

{

       intvertex;

       Anode*first;

}Vnode;

 

typedef struct

{

       Vnodevex[MaxVex];

       intvnum;

       intednum;

}Lgraph;

 


#include <stdio.h>

#include <stdlib.h>

//#include <string.h>

//#include <malloc.h>

#include "graph.h"

 

 

Lgraph * createGraph()                                        //创建图

{

       Lgraph*p;

       Anode*s;

       inti,j,k;

       p=(Lgraph*)malloc(sizeof(Lgraph));

       printf("请输入顶点和边的个数\n");

       scanf("%d%d",&p->vnum,&p->ednum);

       printf("请输入顶点的信息\n");

       for(i=0;i<p->vnum;i++)

       {

              scanf("%d",&p->vex[i].vertex);

              s=(Anode*)malloc(sizeof(Anode));

              p->vex[i].first=s;

              s->adjvex=NULL;

              s->next=NULL;

       }

       printf("请输入边的信息\n");

       for(k=0;k<p->ednum;k++)

       {

              scanf("%d%d",&i,&j);

              s=(Anode*)malloc(sizeof(Anode));

              s->adjvex=j;

              s->next=p->vex[i].first->next;

              p->vex[i].first->next=s;

             

              s=(Anode*)malloc(sizeof(Anode));

              s->adjvex=i;

              s->next=p->vex[j].first->next;

              p->vex[j].first->next=s;

       }

       returnp;

}

void display(Lgraph *p)                                    //输出图的信息

{

       inti;

       Anode*current;

       printf("输出图的顶点的信息\n");

       for(i=0;i<p->vnum;i++)

       {

              printf("%d\t",p->vex[i].vertex);

       }

       printf("输出图的边的信息\n");

       for(i=0;i<p->vnum;i++)

       {

              current=p->vex[i].first->next;

              while(current!=NULL)

              {

                     printf("%d",p->vex[i].vertex);

                     printf("%d\t",current->adjvex);

                     current=current->next;

              }

       }

}

void main()

{

       Lgraph*G;

       G=createGraph();

       display(G);

}

2、基于图的邻接表结构,编程实现图的广度优先遍历和深度优先遍历算法。

1)基本要求

利用队列,编程实现BFS算法, 打印访问顶点序列。利用递归,实现DFS,打印访问顶点序列。遍历算法的源点提示用户键盘输入。

3)算法实现

此处采用邻接表

头文件

typedef struct anode

{

       intadjvex;

       structanode *next;

}Anode;

 

typedef struct vnode

{

       intvertex;

       Anode*first;

}Vnode;

 

typedef struct

{

       Vnodevex[MaxVex];

       intvnum;

       intednum;

       intvisited[MaxVex];

}Lgraph;

typedef struct node queue                                        //添加一个队列结构体,bfs需要

{

Anode *first;

Anode *rear;

}Queue;

 

#include <stdio.h>

#include <stdlib.h>

//#include <string.h>

//#include <malloc.h>

#include "graph.h"

 

 

Lgraph *createGraph()

{

       Lgraph *p;

       Anode *s;

       int i,j,k;

       p=(Lgraph*)malloc(sizeof(Lgraph));

       printf("请输入顶点和边的个数\n");

       scanf("%d%d",&p->vnum,&p->ednum);

       printf("请输入顶点的信息\n");

       for(i=0;i<p->vnum;i++)

       {

              scanf("%d",&p->vex[i].vertex);

              s=(Anode*)malloc(sizeof(Anode));

              p->vex[i].first=s;

              s->adjvex=NULL;

              s->next=NULL;

       }

       printf("请输入边的信息\n");

       for(k=0;k<p->ednum;k++)

       {

              scanf("%d%d",&i,&j);

              s=(Anode*)malloc(sizeof(Anode));

              s->adjvex=j;

              s->next=p->vex[i].first->next;

              p->vex[i].first->next=s;

             

              s=(Anode*)malloc(sizeof(Anode));

              s->adjvex=i;

              s->next=p->vex[j].first->next;

              p->vex[j].first->next=s;

       }

       return p;

}

void display(Lgraph *p)

{

       int i;

       Anode *current;

       printf("输出图的顶点的信息\n");

       for(i=0;i<p->vnum;i++)

       {

              printf("%d\t",p->vex[i].vertex);

       }

       printf("输出图的边的信息\n");

       for(i=0;i<p->vnum;i++)

       {

              current=p->vex[i].first->next;

              while(current!=NULL)

              {

                     printf("%d",p->vex[i].vertex);

                     printf("%d\t",current->adjvex);

                     current=current->next;

              }

       }

}

Queue *Initiate_queue()                                                                     //初始化队列

{

       Queue *Q;

       Anode *s;

       Q=(Queue*)malloc(sizeof(Queue));

       s=(Anode *)malloc(sizeof(Anode));

       s->adjvex=NULL;

       s->next=NULL;

       Q->first=Q->rear=s;   

       return Q;

}

int De_queue(Queue *q)                                                       //删除队列元素

{     

       Anode *t;

       Anode *p=q->first;

       int u;

       if(q->first==q->rear)

       {

              printf("队列为空\n");

       }

       else

       {

              t=q->first->next;

              q->first->next=t->next;

              u=t->adjvex;

              free(t);

              while(p->next!=NULL)

              {

                     p=p->next;

              }

              q->rear=p;

       }

       return u;

}

void En_queue(Queue *q,int source)                              //source入队列q

{

       Anode *s;

       s=(Anode*)malloc(sizeof(Anode));

       s->adjvex=source;

       s->next=NULL;

       if(q->first==q->rear)

       {

              q->first->next=s;

       }

       else

       {

              q->rear->next=s;

       }

       q->rear=s;

}

int Isempty(Queue *q)                                                                //看队列是否为空

{

       if(q->first==q->rear)

       {

              return 0;

       }

       else

       {

              return 1;

       }

 

}

void BFS(Lgraph *p,Queue *q,int source)                                   //要完成BFS算法,需要队列中的创建 增加删除操作

{

       Anode *current;

       int i,u;

       int visited[MaxVex];

       for(i=0;i<p->vnum;i++)

       {

              visited[i]=0;                                                        //设置一个标志数组用来bfs

       }

       printf("The BFSList\n");

       printf("%d",p->vex[source].vertex);

       visited[source]=1;

       En_queue(q,source);

       while(Isempty(q))

       {

              u=De_queue(q);

              current=p->vex[u].first->next;

              while(current!=NULL)

              {

                     if(visited[current->adjvex]==0)

                     {

                            printf("%d",current->adjvex);

                            visited[current->adjvex]=1;

                            En_queue(q,current->adjvex);

                            current=current->next;

                     }

                     else

                     {

                            current=current->next;

                     }

              }

       }

}

void Initvisited(Lgraph *p)                                                                //设置一个存在结构体内的标志数组,结构体内数组可传入函数

{

       int i;

       for(i=0;i<p->vnum;i++)

       {

              p->visited[i]=0;                                                                         //为0代表未用过,用过修改为1

       }

}

void DFS(Lgraph *p,int source)                                                            

{

       Anode *current;

       printf("%d",p->vex[source].vertex);

       p->visited[source]=1;

       current=p->vex[source].first->next;

       while(current!=NULL)

       {

              if(p->visited[current->adjvex]==0)

              {

                     DFS(p,current->adjvex);

              }

              current=current->next;

       }

}

void main()

{

       Lgraph *G;

       Queue *Q;

       int s;

       G=createGraph();

       display(G);

       Q=Initiate_queue();

       printf("\nPleaseinput the source\n");

       scanf("%d",&s);

       BFS(G,Q,s);

       printf("\nThe DFSlist\n");

       Initvisited(G);

       DFS(G,s);

}


猜你喜欢

转载自blog.csdn.net/qq_40667484/article/details/80873333