深度和广度优先搜索

  1. #include<iostream>
  2. #include<stdlib.h>
  3. #include<queue>
  4. using namespace std;
  5. #define OK 1
  6. #define OVERFLOW -2
  7. typedef int Status;
  8. typedef char VertexType;
  9. typedef enum{DG,DN,UDG,UDM} GraphKind;
  10. typedef struct ArcNode{
  11.         int adjvex;
  12.         struct ArcNode *nextarc;
  13.         }ArcNode;
  14. typedef struct VNode{
  15.        VertexType data;
  16.        ArcNode *firstarc; 
  17.        }VNode,*AdjList;
  18. typedef struct {
  19.         AdjList vertices;
  20.         int vexnum,arcnum;
  21.         GraphKind kind;
  22.         }ALGraph;
  23. typedef Status (*Visit)(ALGraph G,int v);
  24. int LocateVex(ALGraph G,VertexType v)
  25. {
  26.     for(int i=0;i<G.vexnum;i++)
  27.             if(G.vertices[i].data==v) return i;
  28.     return G.vexnum;}
  29. Status  CreateUDG(ALGraph &G)
  30. {
  31.         G.kind=UDG;               //无向图 
  32.         cin>>G.vexnum>>G.arcnum;
  33.         if(!(G.vertices=new VNode[G.vexnum])) exit(OVERFLOW);
  34.         for(int i=0;i<G.vexnum;i++) {
  35.                 cin>>G.vertices[i].data;
  36.                 G.vertices[i].firstarc=NULL;}
  37.         VertexType v1,v2;
  38.         ArcNode *p,*q;
  39.         int i,j,k=0;
  40.         for(;k<G.arcnum;k++) {
  41.                 do
  42.                 {
  43.                       cin>>v1>>v2;
  44.                       i=LocateVex(G,v1);
  45.                       j=LocateVex(G,v2);
  46.                       }while(i==G.vexnum||j==G.vexnum);
  47.                 p=new ArcNode;
  48.                 q=new ArcNode;
  49.                 if(!p||!q) exit(OVERFLOW); 
  50.                 p->adjvex=j;
  51.                 p->nextarc=G.vertices[i].firstarc;
  52.                 G.vertices[i].firstarc=p;
  53.                 q->adjvex=i;
  54.                 q->nextarc=G.vertices[j].firstarc;
  55.                 G.vertices[j].firstarc=q;
  56.           }
  57.         return OK;
  58. }
  59. Status Print(ALGraph G,int v){
  60.      cout<<G.vertices[v].data<<" "
  61.      return OK; }
  62. Visit vis=Print;     
  63. void DFS(ALGraph G,int v,bool *&visited);
  64. void DFSTraverse(ALGraph G){
  65.      bool *visited=new bool[G.vexnum];
  66.      if(!visited) exit(OVERFLOW);
  67.      int i=0;
  68.      for(;i<G.vexnum;i++)
  69.              visited[i]=false;
  70.       for(i=0;i<G.vexnum;i++)
  71.              if(!visited[i]) DFS(G,i,visited);
  72.      cout<<endl;}       
  73.         
  74. void DFS(ALGraph G,int v,bool *(&visited)){
  75.      visited[v]=true;
  76.      vis(G,v);
  77.      for(ArcNode* w=G.vertices[v].firstarc;w;w=w->nextarc)
  78.                   if(!visited[w->adjvex])
  79.                            DFS(G,w->adjvex,visited);}
  80. void BFSTraverse(ALGraph G){
  81.      bool *visited=new bool[G.vexnum];
  82.      if(!visited) exit(OVERFLOW);
  83.      queue<int> q;
  84.      int i=0,v;
  85.      for(;i<G.vexnum;i++)
  86.              visited[i]=false;
  87.      for(i=0;i<G.vexnum;i++){
  88.              if(!visited[i]){
  89.                    visited[i]=true;
  90.                    vis(G,i);
  91.                    q.push(i);
  92.                    while(!q.empty()){
  93.                           v=q.front();
  94.                           q.pop();
  95.                           for(ArcNode* w=G.vertices[v].firstarc;w;w=w->nextarc)
  96.                                        if(!visited[w->adjvex]){
  97.                                                visited[w->adjvex]=true;
  98.                                                vis(G,w->adjvex);
  99.                                                q.push(w->adjvex);}
  100.                                     }
  101.                             }
  102.      }
  103.      cout<<endl;
  104. }
  105. int main()
  106. {
  107.    ALGraph G;
  108.    CreateUDG(G);
  109.    DFSTraverse(G);
  110.    BFSTraverse(G);
  111.    return 0;}
  112. 输入: 
  113. 8 9
  114. a b c d e f g h
  115. a b
  116. a c
  117. b d
  118. b e
  119. c f
  120. c g
  121. d h
  122. e h
  123. f g
  124. 输出: 
  125. a c g f b e h d
  126. a c b g f e d h
  127. 请按任意键继续. . .
  128.    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值