ALGraph* InitGraph(ALGraph* g, int number)创建了完全无向图
ALGraph* CreatTestGraph(ALGraph *g)创建了测试用有向图
ALGraph* InitGraph(ALGraph* g, int number) 实现了完全无向图的生成
BFS(ALGraph *g)与DFS(ALGraph *g)实现了图的广度与深度优先搜索
g是生产的完全无向图
g2是测试用的有向图,如下所示:
邻接表中: 1 ->2 ->4 2->3 3->6 4->3->5 5->7->8
6->NULL 7->6->9 8->NULL 9->NULL
可知:
BFS: 1 2 4 3 5 6 7 8 9
DFS: 1 2 3 6 4 5 7 9 8
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXSIZE 30
typedef struct ArcNode //边表
{
int adjvex;
struct ArcNode* next;
}ArcNode;
typedef struct VNode{ //点表
int data;
ArcNode * first;
}VNode;
typedef struct ALGraph //邻接表
{
VNode* vertcies[MAXSIZE];
int vexnum,arcnum;
}ALGraph;
typedef struct Queue //队列及相关操作
{
VNode *data[MAXSIZE];
int rear,front;
}Queue;
Queue* initQueue(){
Queue* p = (Queue *)malloc(sizeof(Queue));
p->rear=p->front=0;
return p;
}
int isQueueEmpty(Queue *p){
if(p->rear==p->front){
return 1;
}
return -1;
}
int enQueue(Queue *p,VNode *node){
if((p->rear+1)%MAXSIZE==p->front){
printf("full error,please enlarge Maxsize\n");
return -1;
}
p->data[p->rear]=node;
p->rear=(p->rear+1)%MAXSIZE;
return 1;
}
VNode* deQueue(Queue *p){
if(p->rear==p->front){
printf("empty error\n");
return NULL;
}
VNode *temp =p->data[p->front];
p->front = (p->front+1)%MAXSIZE;
return temp;
}
ALGraph* InitGraph(ALGraph* g, int number){ //生成有number个节点的完全无向图
g = (ALGraph*)malloc(sizeof(ALGraph));
VNode *tempV;
g->vexnum=number,g->arcnum=number*(number-1)/2; //统计结点数和边数
for(int i=0;i<number;i++){ //节点初始化
tempV = (VNode *)malloc(sizeof(VNode));
tempV->data=i+1;
g->vertcies[i]=tempV;
}
int k; //k统计连接的次数,总共连接number-1条边
for(int i=0;i<number;i++){
k=1;
int now = (i+k)%(number);
ArcNode *p=(ArcNode*)malloc(sizeof(ArcNode));
p->adjvex =now,p->next=NULL;
g->vertcies[i]->first=p; //first初始化
k++;
while(k<number){
ArcNode* tempA;
tempA =(ArcNode*)malloc(sizeof(ArcNode));
now = (i+k)%(number);
tempA->adjvex=now;
tempA->next=NULL;
p->next = tempA; //边表连接
p=tempA;
k++;
}
}
return g;
}
void VisitingBFS(ALGraph* g,int *visit,int begin,Queue *p){ //BFS队列实现
int num = g->vexnum;
printf("%d ",g->vertcies[begin]->data);
visit[begin]=1;
enQueue(p,g->vertcies[begin]);
VNode * tempV;
ArcNode * tempA;
while (isQueueEmpty(p)!=1)
{
tempV =deQueue(p);
for(tempA=tempV->first;tempA!=NULL;tempA=tempA->next){
if(visit[tempA->adjvex]==0){
tempV = g->vertcies[tempA->adjvex];
printf("%d ",tempV->data);
visit[tempA->adjvex]=1;
enQueue(p,tempV);
}
}
}
}
void BFS(ALGraph *g){ //BFS
printf("BFS:\n");
int num = g->vexnum;
int visit[num];
memset(visit,0,sizeof(visit)); //全赋值0
Queue *p= initQueue();
for(int i=0;i<num;i++){
if(visit[i]==0){
VisitingBFS(g,visit,i,p);
}
}
printf("\n");
}
void VisitingDFS(ALGraph* g,int *visit,int begin){ //DFS递归实现
VNode * tempV = g->vertcies[begin];
ArcNode * tempA;
visit[begin]=1;
printf("%d ",tempV->data);
if(tempV->first!=NULL){
for(tempA=tempV->first;tempA!=NULL;tempA=tempA->next){
if(visit[tempA->adjvex]==0){
VisitingDFS(g,visit,tempA->adjvex);
}
}
}
}
void DFS(ALGraph *g){ //DFS,为求简便此处采取递归实现
printf("DFS:\n");
int num = g->vexnum;
int visit[num];
memset(visit,0,sizeof(visit));
for(int i=0;i<num;i++){
if(visit[i]==0){
VisitingDFS(g,visit,i);
}
}
printf("\n");
}
void printALGRaph(ALGraph* g){ //输出图的基本信息
printf("num of vexnum is %d, num of arcnum is %d\n",g->vexnum,g->arcnum);
int i=0;
ArcNode *tempA;
VNode * tempV;
while (i<g->vexnum)
{
tempV = g->vertcies[i];
printf("data is: %d \n",tempV->data);
tempA =tempV->first;
if (tempA!=NULL)
{
printf("%d -> %d ",i+1,tempA->adjvex+1);
while(tempA->next!=NULL){
printf("-> ");
tempA=tempA->next;
printf("%d ",tempA->adjvex+1);
}
printf("\n");
}
i++;
}
}
ALGraph* CreatTestGraph(ALGraph *g){ //创建测试用有向图
g = (ALGraph*)malloc(sizeof(ALGraph));
g->vexnum=9,g->arcnum=10;
VNode *tempV;
ArcNode *tempA;
for(int i=0;i<9;i++){ //节点初始化
tempV = (VNode *)malloc(sizeof(VNode));
tempV->data=i+1;
tempV->first=NULL;
g->vertcies[i]=tempV;
}
tempA = (ArcNode *)malloc(sizeof(ArcNode)),tempA->next=NULL;
tempA->adjvex=1,g->vertcies[0]->first=tempA;
tempA = (ArcNode *)malloc(sizeof(ArcNode)),tempA->next=NULL;
tempA->adjvex=3,g->vertcies[0]->first->next=tempA;
tempA = (ArcNode *)malloc(sizeof(ArcNode)),tempA->next=NULL;
tempA->adjvex=2,g->vertcies[1]->first=tempA;
tempA = (ArcNode *)malloc(sizeof(ArcNode)),tempA->next=NULL;
tempA->adjvex=5,g->vertcies[2]->first=tempA;
tempA = (ArcNode *)malloc(sizeof(ArcNode)),tempA->next=NULL;
tempA->adjvex=2,g->vertcies[3]->first=tempA;
tempA = (ArcNode *)malloc(sizeof(ArcNode)),tempA->next=NULL;
tempA->adjvex=4,g->vertcies[3]->first->next=tempA;
tempA = (ArcNode *)malloc(sizeof(ArcNode)),tempA->next=NULL;
tempA->adjvex=6,g->vertcies[4]->first=tempA;
tempA = (ArcNode *)malloc(sizeof(ArcNode)),tempA->next=NULL;
tempA->adjvex=7,g->vertcies[4]->first->next=tempA;
tempA = (ArcNode *)malloc(sizeof(ArcNode)),tempA->next=NULL;
tempA->adjvex=5,g->vertcies[6]->first=tempA;
tempA = (ArcNode *)malloc(sizeof(ArcNode)),tempA->next=NULL;
tempA->adjvex=8,g->vertcies[6]->first->next=tempA;
return g;
}
int main(){
ALGraph *g,*g1;
g=InitGraph(g,5);
printf("g:\n");
printALGRaph(g);
BFS(g);
DFS(g);
printf("\ng1:\n");
g1 =CreatTestGraph(g1);
printALGRaph(g1);
BFS(g1);
DFS(g1);
}