无向图的深度优先遍历和广度优先遍历(递归)

无向图的深度优先遍历和广度优先遍历(递归)

 

 

queue.h源代码

注释:包括队列数据类型的定义和相关操作

   (出队,入队,判断队空,判断队列中是否存在某元素)

    int searchQ(LinkQueue &Q,int s) 函数的作用:在将邻接顶点放入队列之前需要先判断队     列中是否已存在此元素,通过查找避免队列中有重复元素。

 

    

#include <iostream>

#include <cstdio>

#include <cstdlib>

#include "queen.h"

 

using namespace std;

 

//李艳娟实验指导书版本改编,文档在‘文档’文件夹中

 

//无向无权图---DFS , BFS

 

#ifndef QUEEN_H_INCLUDED

#define QUEEN_H_INCLUDED

 

 

 

typedef struct Qnode{       //链队结点的类型

 

    int data;

 

    struct Qnode *next;

 

}Qnode,*QueuePtr;

 

 

typedef struct

 

{         //链队指针类型

 

   QueuePtr front;

 

   QueuePtr rear;

 

}LinkQueue;

void InitQueue(LinkQueue &Q)

 

{

 

  Q.front=Q.rear=(QueuePtr)malloc(sizeof(Qnode));

 

  if(!Q.front) exit(1); //存储分配失败

 

  Q.front->next=NULL;

 

 }

 

 

 

void EnQueue(LinkQueue &Q,int e)

 

{ QueuePtr p;

 

  p=(QueuePtr)malloc(sizeof(Qnode));

 

  p->data=e;

 

  p->next=NULL;

 

  Q.rear->next=p;

 

  Q.rear=p;

 

}

 

 

 

int QueueEmpty(LinkQueue &Q)

 

{

 

       return(Q.front==Q.rear? 1:0);

 

}

 

 

 

void DeQueue(LinkQueue &Q,int &e)

 

{ QueuePtr p;

 

  if(QueueEmpty(Q))

 

  {

 

    printf("\n Queue is free!");

 

    exit(1);

 

  }//if

 

  p=Q.front->next;

 

  e=p->data;

 

  Q.front->next=p->next;

 

  if(Q.front->next==NULL) Q.rear=Q.front;

 

  free(p);

 

 }

 

 int searchQ(LinkQueue &Q,int s){

      QueuePtr p=Q.front;

       while(p!=NULL){

              if(s==p->data)

              return 1;

              p=p->next;

       }

       return -1;

 }

 

 

#endif // QUEEN_H_INCLUDED

   main.cpp源代码

#include <iostream>

#include <cstdio>

#include <cstdlib>

#include "queen.h"

 

using namespace std;

 

//李艳娟实验指导书版本改编,文档在‘文档’文件夹中

 

//无向无权图---DFS , BFS

 

#define MAX_VERTEX_NUM 20  //顶点最大个数

#define MAXSIZE 20

typedef struct ArcNode

{

    int adjvex;

    struct ArcNode *nextarc;

    int weight; //边的权

} ArcNode; //表结点

#define VertexType int //顶点元素类型

typedef struct VNode

{

    int degree,indegree;//顶点的度,入度

    VertexType data;

    ArcNode *firstarc;

} VNode/*头结点*/,AdjList[MAX_VERTEX_NUM];

typedef struct

{

    AdjList vertices;

    int vexnum,arcnum;//顶点的实际数,边的实际数

} ALGraph;

 

void CreatALGraph(ALGraph &G)

{

    int i,j,k,weight;

    ArcNode *s;

    printf("input vexnum and arcnum:"); //输入顶点数和边数

    scanf("%d%d",&G.vexnum, &G.arcnum);

 

    printf("input vertex information:");  //输入顶点信息

    for(i=0; i<G.vexnum; i++)

    {

        scanf("%d",&(G.vertices[i].data));

        G.vertices[i].firstarc=NULL;

    }

    printf("input arcs, input the xuhao\n");

    for(k=0; k<G.arcnum; k++)

    {

 

        printf("information of %dth arc: ",k);

      //scanf("%d%d%d",&i,&j,&weight);

        scanf("%d%d",&i,&j);

        s=(ArcNode*)malloc(sizeof(ArcNode));

        s->adjvex=j;

        //s->weight=weight;

        s->nextarc=G.vertices[i].firstarc;

        G.vertices[i].firstarc=s;

 

        s=(ArcNode*)malloc(sizeof(ArcNode));

        s->adjvex=i;

        //s->weight=weight;

        s->nextarc=G.vertices[j].firstarc;

        G.vertices[j].firstarc=s;

 

    }

}

void OutputALGraph(ALGraph G)

{

    int i;

 

    for(i=0; i<G.vexnum; i++)

    {

        ArcNode * s;

        printf("%d:%d",i,G.vertices[i].data);  //顶点信息

        s=G.vertices[i].firstarc;

        while(s)

        {

            // printf("\t%d,%d",s->adjvex,s->weight);

            printf("\t%d",s->adjvex);

 

            s=s->nextarc;

        }

    printf("\n");

    }

 

}

int LocateVertex(ALGraph G, VertexType v)

{

    int i;

    for(i=0; i<G.vexnum; i++)

        if(G.vertices[i].data==v)

            return i;

    return -1;

 

}

 

 

int visited[MAXSIZE];

 

 

 

void DFSws(ALGraph &G, int v)

 

{

 

    ArcNode *p;

 

    printf("%d ",G.vertices[v].data);

 

    visited[v]=1;

 

    p=G.vertices[v].firstarc;

 

    while (p)

 

    {

        if (!visited[p->adjvex]) DFSws(G,p->adjvex);

 

        p=p->nextarc;

 

    }

 

}   //从第v个顶点出发DFS

void DFSTraverse(ALGraph G)

{

 

 

    int v;

    for(v=0; v<G.vexnum; v++)

    {

        visited[v]=0;

 

    }

 

    for(v=0; v<G.vexnum; v++)

    {

        if(!visited[v])

            DFSws(G,v);

 

    }

     printf("\n");

}

 

 

 

 

void BFSTraverse(ALGraph &G)

 

{

 

    LinkQueue Q;

 

    for(int v=0; v<G.vexnum; v++) visited[v]=0;

 

    InitQueue(Q);

 

    for(int v=0; v<G.vexnum; v++)

 

        if(!visited[v])

 

        {

 

            EnQueue(Q,v);

 

            while(!QueueEmpty(Q))

 

            {

 

                int u;

 

                DeQueue(Q,u);

 

                visited[u]=1;

 

                printf("%d ",G.vertices[u].data);//visit一下

 

                for(ArcNode *w=G.vertices[u].firstarc; w; w=w->nextarc)

 

                   if(!visited[w->adjvex]&&searchQ(Q,w->adjvex)!=1) EnQueue(Q,w->adjvex);

 

            }//while

 

        }//if

 

}//BFSTraverse

int main()

{

    ALGraph G;

    CreatALGraph(G);

     cout<<"****************"<<endl;

     cout<<"G的邻接表"<<endl;

        OutputALGraph(G);

     cout<<"****************"<<endl;

     cout<<"深度优先遍历顺序"<<endl;

        DFSTraverse(G);

     cout<<"****************"<<endl;

     cout<<"广度优先遍历顺序"<<endl;

       BFSTraverse(G);

return 0;

}

 

程序运行,示例图 图G如下所示:(7顶点,8条边)

 

 

 

 

展开阅读全文

没有更多推荐了,返回首页