图的深度优先搜索算法和广度优先搜索算法

来源:互联网 发布:淘宝促销活动的目的 编辑:程序博客网 时间:2024/06/07 07:42

假设无向图采用邻接表结构表示。编程分别实现图的深度优先搜索算法和广度优先搜索算法。

2.程序的命令包括:

  (1)构建一个图,如右图,用邻接表存储

  (2)深度优先搜索遍历图

  (3)广度优先搜索遍历图

 

 

 

#include<stdio.h>

#include<stdlib.h>

#include<conio.h>

#include<math.h>

#define TRUE 1

#define FALSE 0

#define OK 1

#define ERROR 0

#define OVERFLOW -2

#define NULL 0

#define MAX 20

typedef int Status;

 

typedef struct ArcNode{     /*头节点  */

 int adjvex;              /*该边所指向的顶点的位置*/

 struct ArcNode *nextarc; /*指向下一条边 */

}ArcNode;

 

typedef struct VNode{           /*表节点 */

 int data;               /*顶点信息 */

 ArcNode *firstarc;      /*指向第一条依附该节点的边的指针  */

}VNode,AdjList[MAX];

 

typedef struct{

 AdjList vertices;    /*表节点  */

 int vexnum;           /*节点的个数   */

 int arcnum;           /*边的条数*/

}Graph;

 

 typedef struct Node {

 int elem;

 struct Node *next;

}Node,*QNode;

 

typedef struct{

 QNode front;

 QNode rear;

}Queue;

 

Status InitQueue(Queue *Q)

{

 Q->front=Q->rear=(QNode)malloc(sizeof(Node));

 if(!Q->front)  exit(OVERFLOW);

 Q->front->next=NULL;

 return OK;

}

Status EnQueue(Queue *Q,int e)

{

 QNode p=(QNode)malloc(sizeof(Node));

 if(!p) exit(OVERFLOW);

 p->elem=e;

 p->next=NULL;

 Q->rear->next=p;

 Q->rear=p;

 return OK;

}

Status DeQueue(Queue *Q,int *e)

{QNode p;

 p=Q->front->next;

 Q->front->next=p->next;

 if(Q->rear==p)

   Q->rear=Q->front;

 *e=p->elem;

 free(p);

 return OK;

}

 

Status QueueEmpty(Queue Q)

{if(Q.rear==Q.front)   return TRUE;

 else    return FALSE;

}

 

int LocateVex(Graph *G,int v)    /*返回节点v在图中的位置  */

{int i;

 for(i=0;i<G->vexnum;++i)

   if(G->vertices[i].data==v)  break;

   else     continue;

 if(i<G->vexnum)   return i;

 else    return -1;

}

 

Status CreateGraph(Graph *G)/*以邻接表形式创建无向连通图G  */

{int m,n,i,j,k,v1,v2;

 ArcNode *p,*q;

 printf("Please input the number of VexNode and ArcNode: ");

 scanf("%d%d",&m,&n);

G->vexnum=m;            /*顶点数目 */

 G->arcnum=n;            /*边的数目*/

 for(i=0;i<G->vexnum;++i)

 {G->vertices[i].data=i+1;      /*顶点信息 */

   G->vertices[i].firstarc=NULL;

  }

 

 printf("Output the message of VNode:/n");    /*输出顶点信息  */

 for(i=0;i<G->vexnum;++i)

   printf("v%d  ",G->vertices[i].data);

   printf("/n");

 

 for(k=0;k<G->arcnum;++k)     /*输入邻接表*/

 { printf("Please input the %d edge beginpoint and endpoint: ",k+1);

   scanf("%d%d",&v1,&v2);

   i=LocateVex(G,v1);

   j=LocateVex(G,v2);

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

   q=(ArcNode *)malloc(sizeof(ArcNode));

   p->adjvex=j;

   p->nextarc=G->vertices[i].firstarc;

   G->vertices[i].firstarc=p;

   q->adjvex=i;

   q->nextarc=G->vertices[j].firstarc;

   G->vertices[j].firstarc=q;

  }

 

 printf("The Adjacency List is:/n"); /*输出邻接表  */

 for(i=0;i<G->vexnum;++i)

 { printf("/t%d v%d->",i,G->vertices[i].data);

   p=G->vertices[i].firstarc;

   while(p->nextarc)

   { printf("%d->",p->adjvex);

     p=p->nextarc;

    }

   printf("%d/n",p->adjvex);

 }

 return OK;

}

 

int FirstAdjVex(Graph G,int v)   /*返回v的第一个邻接顶点 */

{if(G.vertices[v].firstarc)

   return G.vertices[v].firstarc->adjvex;

 else    return -1;

}

 

int NextAdjVex(Graph G,int v,int w)   /*返回v中相对于w的下一个邻接顶点 */

{ int flag=0;

  ArcNode *p;

  p=G.vertices[v].firstarc;

 while(p)

 {

   if(p->adjvex==w)

   { flag=1;  break; }

    p=p->nextarc;

 }

 if(flag && p->nextarc)    return p->nextarc->adjvex;

 else    return -1;

}

 

int Visited[MAX];

 

void DFS(Graph G,int v)    /*深度优先遍历  */

{int w;

 Visited[v]=TRUE;

    printf("v%d ",G.vertices[v].data);

 for(w=FirstAdjVex(G,v);w>=0;w=NextAdjVex(G,v,w))

   if(!Visited[w])

    DFS(G,w);

}

 

void DFSTraverse(Graph G)

{int v;

 for(v=0;v<G.vexnum;++v)

   Visited[v]=FALSE;

 for(v=0;v<G.vexnum;++v)

   if(!Visited[v])

    DFS(G,v);       /*递归 */

}

 

void BFSTraverse(Graph G)    /*广度优先遍历  */

{int v,v1,w;

 Queue q;         /*定义一个队列*/

 for(v=0;v<G.vexnum;++v)

   Visited[v]=FALSE;

 InitQueue(&q);    /*初始化队列 */

 for(v=0;v<G.vexnum;++v)

   if(!Visited[v])

   {

    Visited[v]=TRUE;

    printf("v%d ",G.vertices[v].data);

    EnQueue(&q,v);   /*第一个顶点入队*/

    while(!QueueEmpty(q))

    {DeQueue(&q,&v1); /*第一个顶点出队*/

     for(w=FirstAdjVex(G,v1);w>=0;w=NextAdjVex(G,v1,w))

      if(!Visited[w])

      {Visited[w]=TRUE;

       printf("v%d ",G.vertices[w].data);

       EnQueue(&q,w);

      }

    }

   }

}

 

void  main()

{Graph G;

 CreateGraph(&G);

 printf("Depth First Search:/n");

 DFSTraverse(G);

 printf("/nBreadth First Search:/n");

 BFSTraverse(G);

 printf("/n");

 getch();

}

原创粉丝点击