第十二周项目3-图的遍历(深度优先)

本文详细介绍了如何使用深度优先搜索(DFS)遍历图,并通过具体的C++代码实现了基于邻接表的DFS算法。文中首先定义了图的基本数据结构,包括邻接矩阵和邻接表,随后展示了如何构建这些数据结构并进行DFS遍历。此外,还提供了从邻接矩阵到邻接表的转换方法,以及输出顶点出度等实用功能。

问题及代码:

/* 
*Copyright (c) 2016,烟台大学计算机学院 
*All right reserved. 
*文件名称:1.cpp 
*作者:张相如 
*完成日期:2016年11月17日 
*版本号:v1.0 
*问题描述: 
深度优先(DFS)遍历序列*/

graph.h:

#define MAXV 100                //最大顶点个数    
#define INF 32767       //INF表示∞    
typedef int InfoType;    
    
//以下定义邻接矩阵类型    
typedef struct    
{    
    int no;                     //顶点编号    
    InfoType info;              //顶点其他信息,在此存放带权图权值    
} VertexType;                   //顶点类型    
    
typedef struct                  //图的定义    
{    
    int edges[MAXV][MAXV];      //邻接矩阵    
    int n,e;                    //顶点数,弧数    
    VertexType vexs[MAXV];      //存放顶点信息    
} MGraph;                       //图的邻接矩阵类型    
    
//以下定义邻接表类型    
typedef struct ANode            //弧的结点结构类型    
{    
    int adjvex;                 //该弧的终点位置    
    struct ANode *nextarc;      //指向下一条弧的指针    
    InfoType info;              //该弧的相关信息,这里用于存放权值    
} ArcNode;    
    
typedef int Vertex;    
    
typedef struct Vnode            //邻接表头结点的类型    
{    
    Vertex data;                //顶点信息    
    int count;                  //存放顶点入度,只在拓扑排序中用    
    ArcNode *firstarc;          //指向第一条弧    
} VNode;    
    
typedef VNode AdjList[MAXV];    //AdjList是邻接表类型    
    
typedef struct    
{    
    AdjList adjlist;            //邻接表    
    int n,e;                    //图中顶点数n和边数e    
} ALGraph;                      //图的邻接表类型    
    
//功能:由一个反映图中顶点邻接关系的二维数组,构造出用邻接矩阵存储的图    
//参数:Arr - 数组名,由于形式参数为二维数组时必须给出每行的元素个数,在此将参数Arr声明为一维数组名(指向int的指针)    
//      n - 矩阵的阶数    
//      g - 要构造出来的邻接矩阵数据结构    
void ArrayToMat(int *Arr, int n, MGraph &g); //用普通数组构造图的邻接矩阵    
void ArrayToList(int *Arr, int n, ALGraph *&); //用普通数组构造图的邻接表    
void MatToList(MGraph g,ALGraph *&G);//将邻接矩阵g转换成邻接表G    
void ListToMat(ALGraph *G,MGraph &g);//将邻接表G转换成邻接矩阵g    
void DispMat(MGraph g);//输出邻接矩阵g    
void DispAdj(ALGraph *G);//输出邻接表G    
int OutDegree(ALGraph *G,int v);//返回图G中编号为v的顶点的出度    
void OutDs(ALGraph *G);//输出图G中每个顶点的出度    
void OutMaxDs(ALGraph *G);//输出图G中出度最大的一个顶点    
void ZeroDs(ALGraph *G);//输出图G中出度为0的顶点数    
bool Arc(ALGraph *G, int i,int j);//返回图G中是否存在边<i,j>  

graph.cpp:

#include<stdio.h>    
#include<malloc.h>    
#include"graph.h"    
//功能:由一个反映图中顶点邻接关系的二维数组,构造出用邻接矩阵存储的图    
//参数:Arr - 数组名,由于形式参数为二维数组时必须给出每行的元素个数,在此将参数Arr声明为一维数组名(指向int的指针)    
//      n - 矩阵的阶数    
//      g - 要构造出来的邻接矩阵数据结构    
    
void ArrayToMat(int *Arr, int n, MGraph &g)    
{    
    int i,j,count=0;  //count用于统计边数,即矩阵中非0元素个数    
    g.n=n;    
    for (i=0; i<g.n; i++)    
        for (j=0; j<g.n; j++)    
        {    
            g.edges[i][j]=Arr[i*n+j]; //将Arr看作n×n的二维数组,Arr[i*n+j]即是Arr[i][j],计算存储位置的功夫在此应用    
            if(g.edges[i][j]!=0)    
                count++;    
        }    
    g.e=count;    
}    
    
    
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 MatToList(MGraph g,ALGraph *&G)//将邻接矩阵g转换成邻接表G    
{    
    int i,j;    
    ArcNode *p;    
    G=(ALGraph *)malloc(sizeof(ALGraph));    
    for (i=0; i<g.n; i++)                   //给邻接表中所有头节点的指针域置初值    
        G->adjlist[i].firstarc=NULL;    
    for (i=0; i<g.n; i++)                   //检查邻接矩阵中每个元素    
        for (j=g.n-1; j>=0; j--)    
            if (g.edges[i][j]!=0)       //存在一条边    
            {    
                p=(ArcNode *)malloc(sizeof(ArcNode));   //创建一个节点*p    
                p->adjvex=j;    
                p->info=g.edges[i][j];    
                p->nextarc=G->adjlist[i].firstarc;      //采用头插法插入*p    
                G->adjlist[i].firstarc=p;    
            }    
    G->n=g.n;    
    G->e=g.e;    
}    
    
    
void ListToMat(ALGraph *G,MGraph &g)//将邻接表G转换成邻接矩阵g    
{    
    int i,j;    
    ArcNode *p;    
    for (i=0; i<g.n; i++)   //先初始化邻接矩阵    
        for (j=0; j<g.n; j++)    
            g.edges[i][j]=0;    
    for (i=0; i<G->n; i++)  //根据邻接表,为邻接矩阵赋值    
    {    
        p=G->adjlist[i].firstarc;    
        while (p!=NULL)    
        {    
            g.edges[i][p->adjvex]=p->info;    
            p=p->nextarc;    
        }    
    }    
    g.n=G->n;    
    g.e=G->e;    
}    
    
    
void DispMat(MGraph g)//输出邻接矩阵g    
{    
    int i,j;    
    for (i=0; i<g.n; i++)    
    {    
        for (j=0; j<g.n; j++)    
            if (g.edges[i][j]==INF)    
                printf("%3s","∞");    
            else    
                printf("%3d",g.edges[i][j]);    
        printf("\n");    
    }    
}    
    
    
void DispAdj(ALGraph *G)//输出邻接表G    
{    
    int i;    
    ArcNode *p;    
    for (i=0; i<G->n; i++)    
    {    
        p=G->adjlist[i].firstarc;    
        printf("%3d: ",i);    
        while (p!=NULL)    
        {    
            printf("-->%d/%d ",p->adjvex,p->info);    
            p=p->nextarc;    
        }    
        printf("\n");    
    }    
}    
//返回图G中编号为v的顶点的出度    
int OutDegree(ALGraph *G,int v)    
{    
    ArcNode *p;    
    int n=0;    
    p=G->adjlist[v].firstarc;    
    while (p!=NULL)    
    {    
        n++;    
        p=p->nextarc;    
    }    
    return n;    
}    
//输出图G中每个顶点的出度    
void OutDs(ALGraph *G)    
{    
    int i;    
    for (i=0; i<G->n; i++)    
        printf("  顶点%d:%d\n",i,OutDegree(G,i));    
}    
    
    
//输出图G中出度最大的一个顶点    
void OutMaxDs(ALGraph *G)    
{    
    int maxv=0,maxds=0,i,x;    
    for (i=0; i<G->n; i++)    
    {    
        x=OutDegree(G,i);    
        if (x>maxds)    
        {    
            maxds=x;    
            maxv=i;    
        }    
    }    
    printf("顶点%d,出度=%d\n",maxv,maxds);    
}    
//输出图G中出度为0的顶点数    
void ZeroDs(ALGraph *G)    
{    
    int i,x;    
    for (i=0; i<G->n; i++)    
    {    
        x=OutDegree(G,i);    
        if (x==0)    
            printf("%2d",i);    
    }    
    printf("\n");    
}    
    
//返回图G中是否存在边<i,j>    
bool Arc(ALGraph *G, int i,int j)    
{    
    ArcNode *p;    
    bool found = false;    
    p=G->adjlist[i].firstarc;    
    while (p!=NULL)    
    {    
        if(p->adjvex==j)    
        {    
            found = true;    
            break;    
        }    
        p=p->nextarc;    
    }    
    return found;    
}    

main.cpp:

#include <stdio.h>  
#include <malloc.h>  
#include "graph.h"  
int visited[MAXV];  
void DFS(ALGraph *G, int v)  
{  
    ArcNode *p;  
    int w;  
    visited[v]=1;  
    printf("%d ", v);  
    p=G->adjlist[v].firstarc;  
    while (p!=NULL)  
    {  
        w=p->adjvex;  
        if (visited[w]==0)  
            DFS(G,w);  
        p=p->nextarc;  
    }  
}  
  
int main()  
{  
    int i;  
    ALGraph *G;  
    int A[5][5]=  
    {  
        {0,1,0,1,0},  
        {1,0,1,0,0},  
        {0,1,0,1,1},  
        {1,0,1,0,1},  
        {0,0,1,1,0}  
    };  
    ArrayToList(A[0], 5, G);  
  
    for(i=0; i<MAXV; i++) visited[i]=0;  
    printf(" 由2开始深度遍历:");  
    DFS(G, 2);  
    printf("\n");  
  
    for(i=0; i<MAXV; i++) visited[i]=0;  
    printf(" 由0开始深度遍历:");  
    DFS(G, 0);  
    printf("\n");  
    return 0;  
} 

运行结果:


知识点总结:学习使用深度优先遍历

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值