您的位置:首页 > 编程语言 > C语言/C++

《算法导论》中广度优先搜索的实现 c++

2013-04-14 22:47 609 查看
#include<iostream>

#include<stdexcept>

using namespace std;

const int MAXE=10;// the capacity of the queue size is MAX-1

const int MAX_OF_VERTEX=8;

typedef struct// we FLLOW THE convention that the size of the queue is MAX-1

{ int a[MAXE];

  int head;

  int end;

  int size;

} Queue;

typedef struct node//w为权值

{int w;

 int v;//节点编号

 node*point;

}NODE;

// initialize a empty queue

void makeempty(Queue*q)

{q->head=q->end=0;

 q->size=0;

}

// judge whether the queue is empty

bool is_empty(Queue*q)

{return q->size==0? true:false;

}

// insert an element into a queue

void en_queue(int key,Queue*q)

{ if(q->size==MAXE-1)

   throw runtime_error("the queue is full ");

   q->a[q->end]=key;

   ++q->size;

   q->end=(q->end+1)%MAXE;

}

//delete an element of a queue

int de_queue(Queue*q)

{ if(q->size==0)

    throw"the queue is EMPTY";

    q->head=(q->head+1)%MAXE;

    --q->size;

    return q->a[q->head-1];

 }

 //将邻接矩阵转换为邻接表(有权无向图)

  bool graph_array_to_link(int a[][MAX_OF_VERTEX],NODE l[],int V)//or NOED* L[] 邻接表建立成功返回true,下标从1开始 l[1] represent the “Vertex 1”

  { int i=0,

        j=0;

    NODE*p=NULL,

         *pp=NULL;

    for(;i<V;++i)

       {  l[i+1].point=NULL;//在函数里显式保证没有相邻节点的一个节点的point域为NULL

          p=&l[i+1];//p每次都指向头节点,下表从1开始!

          for(j=0;j<V;++j)

             {     if(a[i][j])

                  {

                      try

                      {pp=new NODE;}

                      catch(std::bad_alloc)

                      { cout<<"内存分配不成功,函数终止!"<<endl;

                        return false;

                      }

                     pp->w=a[i][j];

                     pp->v=j+1;

                     pp->point=NULL;

                     p->point=pp;//链接操作

                     p=pp;//p指向了新建立的元素

                    

                  }

             }

        }

        for(int i=1;i<V+1;++i)

        { NODE*p=l[i].point;

          cout<<i<<": ";

          while(p!=NULL)

          {cout<<p->v<<" ";

          p=p->point;

           

          }

          cout<<endl;

        }

        return true;

       

  }

 //3:黑色 2 白色 1 灰色

 //s:搜索源节点,dis:距离矩阵 pi:前趋矩阵

 void breath_first_search(NODE*graph_link,int num_of_vertex,int s,int*dis,int*pi)//the vertexs are represented by integer

 {  Queue Q;

    int color[num_of_vertex+1];//we can use the formal parameter to define the lenght of the array color;

    for(int i=0;i<num_of_vertex+1;++i)                                //%%%%%%%%%%必须都赋白色###############当时忘记了

       color[i]=2;

    int temp;

    NODE*p=NULL;

    dis[s]=0;

    pi[s]=0;

    color[s]=1;// 1 represent for gray,2 represent white

    makeempty(&Q);// avoid to be affect by the uncertain initial values of the Queue, the must step

    en_queue(s,&Q);// 1 is the source vertex

   

    while(!is_empty(&Q))

    { temp=de_queue(&Q);

      

      p=graph_link[temp].point;

      while(p!=NULL)

         {

           if(color[p->v]==2)         

            { ++dis[p->v];

              pi[p->v]=temp;

              color[p->v]=1;

              en_queue(p->v,&Q);

              

            }

            p=p->point;                            //¥¥¥¥¥¥¥¥¥¥忘了¥¥¥¥¥¥¥¥¥¥¥¥4

         }

       color[temp]=3;//3代表黑色

       cout<<temp<<endl;

      

    }

     

 }

 int main()

 {

   NODE graph_link[MAX_OF_VERTEX+1]={0};//每一个元素都是一个头节点,第一个节点不存储元素,为了将下标和节点编号对应                      

   int graph_array[MAX_OF_VERTEX][MAX_OF_VERTEX]={{0,1,1,0,0,0,1,0},

                                                  {1,0,0,1,0,0,0,0},

                                                  {1,0,0,1,1,0,0,0},

                                                  {0,1,1,0,1,0,0,0},

                                                  {0,0,1,1,0,1,0,0},

                                                  {0,0,0,0,1,0,0,1},

                                                  {1,0,0,0,0,0,0,0},

                                                  {0,0,0,0,0,1,0,0}};

   //the adjacency_array of graph

   int dis[MAX_OF_VERTEX+1]={0};

   int pi[MAX_OF_VERTEX+1]={0};

   graph_array_to_link(graph_array,graph_link,MAX_OF_VERTEX);

   breath_first_search(graph_link,MAX_OF_VERTEX,7,dis,pi);

   return 0;

   

   

}

 

 

 

 

 

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数据结构和算法