《算法笔记》第4章 入门篇(2)---算法初步 10.3 图的遍历

前言:

在这里插入图片描述

1.用DFS遍历图:

在这里插入图片描述
在这里插入图片描述

2.DFS的具体实现:

在这里插入图片描述

邻接表版本:

//设置最大顶点数 MAXV为1000
//设置iNF为100000000000000000  表示一个很大的数


//设置int类型 n, 二维数组G
//bool类型 vis  初始化为false

//函数DFS ,形参为 int u 表示当前访问的顶点编号  depth表示为深度
/*
    1.在vis中对当前u标记为true
    2.for循环 v从0到n 如果vis中标记的是false 且 u可到v  继续调用函数DFS, 形参为 v, depth+1

*/

/*
    void类型 遍历图G 函数名:DFSTrave
    1.for循环 u从0~n 
    2.如果在vis中对应的u为 false
    3.则调用DFS(u,1)

*/


const int MAXV=1000;
const int iNF=100000000000;

int n;
bool vis[MAXV]={false};
vector<int> q[maxn];

void DFS(int u, int depth)
{
    vis[u]=true;
    for(int i=0; i<q[u].size(); i++)
    {
        int v=q[u][i];
        if(vis[v]==false)
            DFS(v,depth+1);
    }
}

void DFSTrave()
{
    int u;
    for(u=0; u<n; u++)
    {
        if(vis[u]==false)
            DFS(u,1);
    }
}





邻接矩阵版本:

//设置最大顶点数 MAXV为1000
//设置iNF为100000000000000000  表示一个很大的数


//设置int类型 n, 二维数组G
//bool类型 vis  初始化为false

//函数DFS ,形参为 int u 表示当前访问的顶点编号  depth表示为深度
/*
    1.在vis中对当前u标记为true
    2.for循环 v从0到n 如果vis中标记的是false 且 u可到v  继续调用函数DFS, 形参为 v, depth+1

*/

/*
    void类型 遍历图G 函数名:DFSTrave
    1.for循环 u从0~n 
    2.如果在vis中对应的u为 false
    3.则调用DFS(u,1)

*/

const int MAXV=1000;
const int iNF=100000000000;

int n;
int G[maxn][maxn];
bool vis[maxn]={false};

void DFS(int u, int depth)
{
    vis[u]=true;
    for(int i=0; i<n; i++)
    {
        if(vis[i]==false && G[u][i]!=INF)
        {
            DFS(i,depth+1);
        }
    }
}

void DFSTrave()
{
    int u;
    for(u=0; u<n; u++)
    {
        if(vis[u]==false)
            DFS(u,1);
    }
}

3.BFS遍历图

在这里插入图片描述
在这里插入图片描述

4.BFS的具体实现:

在这里插入图片描述

邻接表版本:

//设置3个值:n,二维数组G,bool类型 inq

/*
    void类型 BFS    参数为int u
    1.定义一个队列q
    2.将初始点u入队
    3.在inq中 标记u为true
    4.while循环 队列q不为空
        1.取出队首元素
        2.队列q 弹出
        3.for循环 v从0开始,一直到n
        4.如果inq中v对应的为false表示没有访问过,同时u可到v
            1.将v入对
            2.标记inq中v是true

*/


/*
    void类型 BFSTrave
    1.for循环v从0开始一直到n
    2.如果u没有到达过队列,则遍历q的联通块

*/


const int maxn=1000;
const int INF=100000000000l;

int n;
bool inq[maxn]={false};
vector<int> dd;




//邻接表版本
void BFS(int u)
{
    queue<int >q;
    q.push(u);
    inq[u]=true;
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        for(int i=0; i<dd[u].size(); i++)
        {
            int v=dd[u][i];
            if(INF[v]==false)
            {
                q.push(v);
                inq[v]=true;
            }
        }
    }
}

void BFSTrave()
{
    for(int v=0; v<n; v++)
        if(inq[v]==false)
            BFS(v);
}

邻接矩阵版本:



const int maxn=1000;
const int INF=100000000000l;

int n;
bool inq[maxn]={false};
int G[maxn][maxn];


//邻接矩阵版本
void BFS(int u)
{
    queue<int >q;
    q.push(u);
    inq[u]=true;
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        for(int i=0; i<n; i++)
        {
            if(inq[i]==false && G[u][i]!=INF)
            {
                q.push(i);
                inq[i]=true;
            }
                
        }
    }
}
void BFSTrave()
{
    for(int v=0; v<n; v++)
        if(inq[v]==false)
            BFS(v);
}

带有结构类型(顶点编号,层数)的BFS

/*
    设置一个结点的结构:
    1.int v顶点编号
    2.int layer顶点层号
*/

//此时的邻接表的类型 为 node

/*
    1.void 类型 BFS  参数为int类型 s  表示起点编号
    2.node类型 队列 q
    3.node类型 起点start 初始化v为s 起始顶点的层号为 0 将strat压入队列q中
    4.在inq中标记strat的顶点编号为true
    5.设置while循环
        1.定义新的结构结点topnode,初始化为q的队首元素
        2.q弹出队列
        3.设置新的int类型 u为结点topnode的值v
        4.for循环:
            1.设置新的结点结构next,初始化为Adj[u][i]
            2.设置next的层数为队首元素的层数+1
            3.if在inq中next.v的值为false
                1.将next入队
                2.在inq中标记next.v的值为true

*/

const int maxn=1000;

struct node
{
    int v;
    int layer;
};

vector<node> dd[maxn];

void BFS(int s)   //s只是表示编号,现在整个用的是结点
{
    queue<node> q;
    
    //定义一个结点结构,并且初始化
    node start; 
    start.v=s;
    start.layer=0;
    
    
    q.push(start);     //将结点结构压入队列
    
    inq[start.v]=true;    //标记inq中的值为true
    
    while(!q.empty())
    {
        node topnode=q.front();       //定义一个新的结构结点来存放 结构队列q的头元素
        q.pop();            //弹出
        int u=topnode.v;
        for(int i=0; i<dd[u].size(); i++)    //遍历vector数组下面的值
        {
            node next=dd[u][i];        //从u出发能到i,定义新的结构类型变量 next
            next.layer=topnode.layer+1;     //此时next下面的层数为 队首元素层数+1
            if(inq[next.v]==false)          //如果这个数之前没有入过队列,则压入队列
            {
                q.push(next);
                inq[next.v]=true;       //并且标记为true
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值