用邻接表实现深度和广度搜索

 //=======================================================================
//Use Adjacency List Implement DFS And BFS
//BY:CHLAWS   
//TIME:08-8-4
//PS:transshipment don't delete this headmark
//=======================================================================

 

#include <stdio.h>
#include <stdlib.h>

#define MAX_VERTEX_NUM 20
int visited[MAX_VERTEX_NUM];

typedef char VertexType;
typedef struct ArcNode{
 int adjvex;       //该弧指向的顶点位置 
 struct ArcNode *nextarc;   //指向下一个表(边)结点 
 int info;       //权值
}ArcNode;  //边结点类型

typedef struct VNode{
 VertexType data;
 ArcNode *firstarc;
}VNode,AdjList[MAX_VERTEX_NUM];

typedef struct{
 AdjList vertices;  //邻接表
 int vexnum,arcnum; //顶点数和弧(边)数
}ALGraph;

#define MAXQSIZE MAX_VERTEX_NUM
typedef struct SQNode{
 VertexType base[MAXQSIZE];
 int front;
 int rear;
}SqQueue;

void InitQueue(SqQueue& Q)

 for(int ix=0; ix<MAXQSIZE; ++ix)
 {
  Q.base[ix] = 0;
 }
 Q.front = Q.rear = 0;
}

int LocateVex(ALGraph G,char u);
void CreateALGraph_adjlist(ALGraph &G);
void DFSTraverse(ALGraph G);
void DFS(ALGraph G, int v);
void BFSTraverse(ALGraph G);
void BFS(ALGraph G,int v);

void main()
{
 ALGraph G;
 printf("/n/n//===================创建邻接表===========================///n/n");
 CreateALGraph_adjlist(G);
 printf("/n/n//===================深度优先搜索===========================///n/n");
 DFSTraverse(G);
 printf("/n/n//===================广度优先搜索===========================///n/n");
 BFSTraverse(G);
}

//查找符合的数据在数组中的下标
int LocateVex(ALGraph G,char u)
{
 int i;
 for (i=0;i<G.vexnum;i++)
 {
  if(u==G.vertices[i].data)
   return i;
 }
 if (i==G.vexnum)
 {
  printf("Error u!/n");
  exit(1);
 }
 return 0;
}
void CreateALGraph_adjlist(ALGraph &G)

 int i,j,k,w;  char v1,v2;
 ArcNode *p;
 printf("Input vexnum & arcnum:/n");
 scanf("%d,%d",&G.vexnum,&G.arcnum);
 printf("Input Vertices:/n");
 for (i=0;i<G.vexnum;i++)
 {
  fflush(stdin);
  scanf("%c",&G.vertices[i].data);
  G.vertices[i].firstarc=NULL;
 }
 printf("Input Arcs(v1,v2,w):/n");
 for (k=0;k<G.arcnum;k++)
 {
  fflush(stdin);
  scanf("%c,%c,%d",&v1,&v2,&w);
  i=LocateVex(G,v1);
  j=LocateVex(G,v2);
  p=(ArcNode*)malloc(sizeof(ArcNode));
  p->adjvex=j;
  p->info = w;
  p->nextarc=G.vertices[i].firstarc;
  G.vertices[i].firstarc=p;
 } return;
}
//思路:
/****************************************************************************
** 使用广度优先搜索在访问了起始顶点 v 之后,          
** 由 v 出发,依次访问 v 的各个未曾被访问过的邻接顶点 w1, w2, …, wt,
** 然后再顺序访问 w1, w2, …, wt 的所有还未被访问过的邻接顶点。
** 再从这些访问过的顶点出发,再访问它们的所有还未被访问过的邻接顶点,
** … 如此做下去,直到图中所有顶点都被访问到为止。
*****************************************************************************/
void BFSTraverse(ALGraph G)
{
 int v;
 for (int v=0;v<G.vexnum;++v)
  visited[v]=0;
 for (v=0;v<G.vexnum;++v)
  if (!visited[v]) BFS(G,v);
}

void BFS(ALGraph G,int v)
{
 ArcNode *p; SqQueue Q;
 InitQueue(Q);
 printf("%c",G.vertices[v].data);
 visited[v]=1;
 Q.base[Q.rear]=v;
 Q.rear=(Q.rear+1)%MAXQSIZE;
 while (Q.front!=Q.rear)
 {
  v=Q.base[Q.front];
  Q.front=(Q.front+1)%MAXQSIZE;
  p=G.vertices[v].firstarc;

  while (p)
  { 
   if (!visited[p->adjvex])
   {
    printf("%c",G.vertices[p->adjvex].data);
    visited[p->adjvex]=1;
    Q.base[Q.rear]=p->adjvex;
    Q.rear=(Q.rear+1)%MAXQSIZE;
   }
   p=p->nextarc;
  }
 }
}

/*
DFS刚好相反,仔细理解就能明白
*/
void DFSTraverse(ALGraph G)
{
 for(int ix=0; ix < G.vexnum; ++ix)
 {
  visited[ix] = 0;
 }
 for(int ver=0; ver < G.vexnum; ++ver)
 {
  if(!visited[ver]) DFS(G,ver);
 }
}

void DFS(ALGraph G, int v)
{
 printf("pos:[%d] data :%c/n",v,G.vertices[v].data);
 visited[v] = 1;

 ArcNode *pArc;
 pArc = G.vertices[v].firstarc;
 while(pArc)
 {
  if(!visited[pArc->adjvex]) DFS(G,pArc->adjvex);
  pArc = pArc->nextarc;
 }
}
//DFS 搜索结果
/*
Input vexnum & arcnum:
9,10
Input Vertices:
a
b
c
d
e
f
g
h
i
Input Arcs(v1,v2 & w):
a,b,1
a,c,1
a,d,1
b,e,1
b,c,1
e,g,1
f,d,1
f,c,1
f,h,1
h,i,1
pos:[0] data :a
pos:[3] data :d
pos:[2] data :c
pos:[1] data :b
pos:[4] data :e
pos:[6] data :g
pos:[5] data :f
pos:[7] data :h
pos:[8] data :i
请按任意键继续. . .
*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值