数据结构—非连通图的遍历

/*
非连通图的遍历。
*/
#include <iostream>
#include <stdio.h>
#include <malloc.h>
#define MAXV 50
typedef int InfoType;
using namespace std;
int visited[MAXV];
//邻接表类型
typedef struct ANode
{
    int adjvex;             //该边的编号
    struct ANode *nextarc;  //指向下一条边的信息
    InfoType info;          //该弧的相关信息,这里用于存放权值
} ArcNode;                  //边节点的类型

typedef struct Vnode
{
    ArcNode *firstarc;      //指向第一条边
} VNode;                    //邻接表头节点类型

typedef VNode AdjList[MAXV];
typedef struct
{
    AdjList adjlist;       //邻接表
    int n,e;               //图中顶点数n和边数e
} ALGraph;                 //完整的图邻接表类型

void ArrayToList(int *Arr, int n, ALGraph *&G)  //用普通数组构造图的邻接表
{
    int i,j,count=0;  //count用于统计边数,即矩阵中非0元素个数
    ArcNode *p;
    G=(ALGraph *)malloc(sizeof(ALGraph));
    G->n=n;
    for (i=0; i<n; i++)                 //给邻接表中所有头节点的指针域置初值
        G->adjlist[i].firstarc=NULL;
    for (i=0; i<n; i++)                 //检查邻接矩阵中每个元素
        for (j=n-1; j>=0; j--)
            if (Arr[i*n+j]!=0)      //存在一条边,将Arr看作n×n的二维数组,Arr[i*n+j]即是Arr[i][j]
            {
                p=(ArcNode *)malloc(sizeof(ArcNode));   //创建一个节点*p
                p->adjvex=j;
                p->info=Arr[i*n+j];
                p->nextarc=G->adjlist[i].firstarc;      //采用头插法插入*p
                G->adjlist[i].firstarc=p;
            }

    G->e=count;
}

void DFS(ALGraph *G,int v)
{
    ArcNode *p;
    visited[v]=1;          //置已访问标记
    cout<<v;               //输出被访问顶点的编号
    p=G->adjlist[v].firstarc;  //p指向顶点v的第一个邻接点
    while(p!=NULL)
    {
        if(visited[p->adjvex]==0)   //若p->adjvex顶点未被访问,递归访问它
            DFS(G,p->adjvex);
        p=p->nextarc;               //p指向顶点v的下一个邻接点
    }
}

void BFS(ALGraph *G,int v)
{
    ArcNode *p;
    int queue[MAXV],front=0,rear=0;
    int w,i;
    for(i=0; i<G->n; i++)     //访问标志数组初始化
        visited[i]=0;
    cout<<v;                  //输出被访问顶点的编号
    visited[v]=1;
    rear=(rear+1)%MAXV;
    queue[rear]=v;            //v进队
    while(front!=rear)
    {
        front=(front+1)%MAXV;
        w=queue[front];       //出队并赋给w
        p=G->adjlist[w].firstarc;       //找顶点w的第一个邻接点
        while(p!=NULL)
        {
            if(visited[p->adjvex]==0)       //若当前邻接顶点未被访问
            {
                cout<<p->adjvex;            //访问相邻顶点
                visited[p->adjvex]=1;
                rear=(rear+1)%MAXV;         //该顶点入队
                queue[rear]=p->adjvex;
            }
            p=p->nextarc;                  //找顶点w的下一个邻接点
        }
    }
    cout<<endl;
}

void DFS1(ALGraph *G)
{
    int i;
    for(i=0; i<G->n; i++)
        if(visited[i]==0)
            DFS(G,i);
}

void BFS1(ALGraph *G)
{
    int i;
    for(i=0; i<G->n; i++)
        if(visited[i]==0)
            BFS(G,i);
}

//是连通图返回true,否则返回false
bool Connect(ALGraph *G)
{
    int i;
    bool flag=true;
    for (i=0; i<G->n; i++)
        visited[i]=0;
    DFS(G,0);
    for (i=0; i<G->n; i++)
        if (visited[i]==0)
        {
            flag=false;
            break;
        }
    return flag;
}

int main()
{
    int i;
    ALGraph *G;
    int A[8][8]=
    {
        {0,1,0,1,0,0,0,0},
        {1,0,1,0,0,0,0,0},
        {0,1,0,1,1,0,0,0},
        {1,0,1,0,1,0,0,0},
        {0,0,1,1,0,0,0,0},
        {0,0,0,0,0,0,1,0},
        {0,0,0,0,0,1,0,1},
        {0,0,0,0,0,0,1,0},
    };
    ArrayToList(A[0], 8, G);
    for (i=0; i<G->n; i++)
        visited[i]=0; //访问标志数组初始化
    printf("非连通图的深度优先遍历:\n");
    DFS1(G);
    cout<<endl;
    for (i=0; i<G->n; i++)
        visited[i]=0; //访问标志数组初始化
    printf("非连通图的广度优先遍历:\n");
    BFS1(G);
    for (i=0; i<G->n; i++)
        visited[i]=0; //访问标志数组初始化
    cout<<endl;
    if(Connect(G))
        printf(" G是连通图\n");
    else
        printf(" G不是连通图\n");
    return 0;
}

运行结果:

《数据结构—非连通图的遍历》

    原文作者:数据结构之图
    原文地址: https://blog.csdn.net/LY_624/article/details/51598422
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞