图的深度优先搜索算法和广度优先搜索算法
来源:互联网 发布:淘宝促销活动的目的 编辑:程序博客网 时间: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();
}
- 图的深度优先和广度优先搜索算法
- 图的基础算法-广度优先搜索/深度优先搜索
- 算法导论-图的搜索算法之深度优先搜索和广度优先搜索
- 深度优先搜索 广度优先搜索算法
- 图的深度优先搜索算法和广度优先搜索算法
- 图的深度优先搜索和广度优先搜索算法、最小生成树两种算法 --C++实现
- [算法] 基本图算法:深度优先搜索、广度优先搜索
- 算法之深度优先搜索和广度优先搜索
- 搜索算法-广度优先和深度优先搜索
- 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)
- 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)
- 【经典算法】图的深度优先搜索和广度优先搜索
- 算法: 无向图的深度优先搜索(dfs)和广度优先搜索(bfs)
- 第22章:图的基本算法—广度优先搜索和深度优先搜索
- 算法#17--无向图的深度优先搜索和广度优先搜索
- 面试题常见算法之图的广度优先搜索和深度优先搜索
- 图的基本算法--深度优先搜索(dfs) 和 广度优先搜索(bfs)
- 算法二:深度和广度优先搜索
- JDBC参数设置
- Domino调色板
- [转]屏幕保护色的设置
- C#的des加密解密
- org.apache.commons.lang.StringUtil的使用(转帖)
- 图的深度优先搜索算法和广度优先搜索算法
- C# 生成各种格式各种大小的图片类
- 针对ASP.NET页面实时进行GZIP压缩优化的几款压缩模块的使用简介及应用测试!(附源码)
- 什么是阻塞式和非阻塞io流?
- 俞敏洪北大演讲里的七个小故事
- CRM的特点及其局限性
- 使用 ipmitool 实现 Linux 系统下对服务器的 ipmi 管理
- 两个时间段差值,返回小时,分钟
- 什么样的大学女生值得娶回家