关闭

图的遍历算法实现DFS,BFS

标签: DFSBFS
295人阅读 评论(0) 收藏 举报
分类:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define NUM 6
typedef struct ArcNode
{
     int ends;
     int length;
     struct ArcNode *nextarc;
} ArcNode;
typedef struct VNode
{
     ArcNode *firstarc;
} VNode;
typedef struct LGraph
{
     VNode adjlist[NUM];
     int n,e;
} LGraph;

typedef struct MGraph
{
     int edges[NUM][NUM];
     int n,e;
} MGraph;

MGraph *build_mgraph();
LGraph *to_lgraph(MGraph *mgraph);
void DFS(LGraph *lgraph, int num);
void DFS_R(LGraph *lgraph, int num);
void BFS(LGraph *lgraph, int num);
int is_visited_all(VNode vnode, int visited[]);

int main(void)
{
     MGraph *mgraph;
     LGraph *lgraph;

     printf("\n*************************************************************\n");
     printf("该图的矩阵表示为:\n");
     mgraph=build_mgraph();

     printf("\n*************************************************************\n");
     printf("该图的邻接表表示为:\n");
     lgraph=to_lgraph(mgraph);

     printf("\n*************************************************************\n");
     printf("该图递归形式的深度优先搜索序列为:\n");
     DFS(lgraph,0);

     printf("\n*************************************************************\n");
     printf("该图非递归形式的深度优先搜索序列为:\n");
     DFS_R(lgraph,0);

     printf("\n*************************************************************\n");
     printf("该图广度优先搜索序列为:\n");
     BFS(lgraph,0);
     printf("\n*************************************************************\n");
     
     return 0;
}

MGraph *build_mgraph()
{
     int i,j;
     int num_e=0;
     MGraph *mgraph=(MGraph *)malloc(sizeof(MGraph));
     int matrix[NUM][NUM]={{0,5,INT_MAX,7,INT_MAX,INT_MAX,INT_MAX},
			   {INT_MAX,0,4,INT_MAX,INT_MAX,INT_MAX},
			   {8,INT_MAX,0,INT_MAX,INT_MAX,9},
			   {INT_MAX,INT_MAX,5,0,INT_MAX,6},
			   {INT_MAX,INT_MAX,INT_MAX,5,0,INT_MAX},
			   {3,INT_MAX,INT_MAX,INT_MAX,1,0}};
     for(i=0;i<NUM;i++)
     {
	  for(j=0;j<NUM;j++)
	  {
	       mgraph->edges[i][j]=matrix[i][j];
	       if(matrix[i][j]!=0 && matrix[i][j]!=INT_MAX)
		    num_e++;
	  }
     }
     mgraph->n=NUM;
     mgraph->e=num_e;

     printf("node=%d;edges=%d\n",mgraph->n,mgraph->e);
     for(i=0;i<NUM;i++)
     {
	  for(j=0;j<NUM;j++)
	  {
	       if(mgraph->edges[i][j]!=INT_MAX)
		    printf("%3d",mgraph->edges[i][j]);
	       else
		    printf("%3c",'&');
	  }
	  printf("\n");
     }

     return mgraph;
}
LGraph *to_lgraph(MGraph *mgraph)
{
     int i,j;
     LGraph *lgraph=(LGraph *)malloc(sizeof(LGraph));
     ArcNode *p;

     lgraph->n=mgraph->n;
     lgraph->e=mgraph->e;
     for(i=0;i<NUM;i++)
     {
	  lgraph->adjlist[i].firstarc=NULL;
	  for(j=NUM-1;j>=0;j--)
	  {
	       if(mgraph->edges[i][j]!=0 && mgraph->edges[i][j]!=INT_MAX)
	       {
		    p=(ArcNode *)malloc(sizeof(ArcNode));
		    p->length=mgraph->edges[i][j];
		    p->ends=j;
		    p->nextarc=lgraph->adjlist[i].firstarc;
		    lgraph->adjlist[i].firstarc=p;
	       }
	  }
     }

     printf("node=%d,edges=%d\n",lgraph->n,lgraph->e);
     for(i=0;i<NUM;i++)
     {
	  p=lgraph->adjlist[i].firstarc;
	  while(p)
	  {
	       printf("%d-%d: %d    ",i,p->ends,p->length);
	       p=p->nextarc;
	  }
	  printf("\n");
     }

     return lgraph;
}
void DFS(LGraph *lgraph, int num)
{
     int static visited[NUM]={0};
     ArcNode *p;

     visited[num]=1;
     printf("%3d",num);
     p=lgraph->adjlist[num].firstarc;
     while(p)
     {
	  if(!visited[p->ends])
	       DFS(lgraph, p->ends);
	  p=p->nextarc;
     }
}
void DFS_R(LGraph *lgraph, int num)
{
     int visited[NUM]={0};
     ArcNode *p;
     VNode stack[NUM];
     int top=-1;

     printf("%3d",num);
     visited[num]=1;
     stack[++top]=lgraph->adjlist[num];

     while(top!=-1)
     {
	  if(is_visited_all(stack[top],visited))
	       top--;
	  else
	  {
	       p=stack[top].firstarc;
	       while(p)
	       {
		    if(visited[p->ends])
			 p=p->nextarc;
		    else
		    {
			 printf("%3d",p->ends);
			 visited[p->ends]=1;
			 stack[++top]=lgraph->adjlist[p->ends];
			 p=stack[top].firstarc;
		    }
	       }
	  }
     }
}

int is_visited_all(VNode vnode, int visited[])
{
     ArcNode *p;

     p=vnode.firstarc;
     while(p)
     {
	  if(!visited[p->ends])
	       return 0;
	  p=p->nextarc;
     }
     return 1;
}

void BFS(LGraph *lgraph, int num)
{
     int visited[NUM]={0};
     ArcNode *p;
     VNode q;
     VNode queue[NUM];
     int rear;
     int front;
     rear=front=-1;

     printf("%3d",num);
     visited[num]=1;
     queue[++rear]=lgraph->adjlist[num];

     while(rear!=front)
     {
	  q=queue[++front];
	  p=q.firstarc;
	  while(p)
	  {
	       if(!visited[p->ends])
	       {
		    printf("%3d",p->ends);
		    visited[p->ends]=1;
		    queue[++rear]=lgraph->adjlist[p->ends];
	       }
	       p=p->nextarc;
	  }
     }
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:15807次
    • 积分:1062
    • 等级:
    • 排名:千里之外
    • 原创:93篇
    • 转载:1篇
    • 译文:1篇
    • 评论:1条
    最新评论