图的遍历算法

1.深度优先遍历

基本思想:首先从图中某个顶点v0出发,访问此顶点,然后依次从v0相邻的顶点出发深度优先遍历,直至图中所有与v0路径相通的顶点都被访问了;若此时尚有顶点未被访问,则从中选一个顶点作为起始点,重复上述过程,直到所有的顶点都被访问。可以看出深度优先遍历是一个递归的过程。

如下图中的一个无向图

其深度优先遍历得到的序列为:

0->1->3->7->4->2->5->6

2.广度优先遍历

基本思想:首先,从图的某个顶点v0出发,访问了v0之后,依次访问与v0相邻的未被访问的顶点,然后分别从这些顶点出发,广度优先遍历,直至所有的顶点都被访问完。
这里写图片描述

如上面图中

其广度优先遍历得到的序列为:

0->1->2->3->4->5->6->7

实现代码

#include<iostream>
#include<queue>
#include<stack>
#include<stdlib.h>
#define MAX 100
using namespace std;

typedef struct 
{
    int edges[MAX][MAX];    //邻接矩阵
    int n;                  //顶点数
    int e;                  //边数
}MGraph;

bool visited[MAX];          //标记顶点是否被访问过

void creatMGraph(MGraph &G)    //用引用作参数
{
    int i,j;
    int s,t;                 //存储顶点编号
    int v;                   //存储边的权值
    for(i=0;i<G.n;i++)       //初始化
    {
        for(j=0;j<G.n;j++)
        {
            G.edges[i][j]=0;
        }
        visited[i]=false;
    }
    for(i=0;i<G.e;i++)      //对矩阵相邻的边赋权值
    {
        scanf("%d %d %d",&s,&t,&v);   //输入边的顶点编号以及权值
        G.edges[s][t]=v;
    }
}

void DFS(MGraph G,int v)      //深度优先搜索
{
    int i;
    printf("%d ",v);          //访问结点v
    visited[v]=true;
    for(i=0;i<G.n;i++)       //访问与v相邻的未被访问过的结点
    {
        if(G.edges[v][i]!=0&&visited[i]==false)
        {
            DFS(G,i);
        }
    }
}

void DFS1(MGraph G,int v)   //非递归实现
{
    for(int i=0;i<G.n;i++)       //初始化
    {
        visited[i]=false;
    }
    stack<int> s;
    printf("%d ",v);        //访问初始结点
    visited[v]=true;
    s.push(v);              //入栈
    while(!s.empty())
    {
        int i,j;
        i=s.top();          //取栈顶顶点
        for(j=0;j<G.n;j++)  //访问与顶点i相邻的顶点
        {
            if(G.edges[i][j]!=0&&visited[j]==false)
            {
                printf("%d ",j);     //访问
                visited[j]=true;
                s.push(j);           //访问完后入栈
                break;               //找到一个相邻未访问的顶点,访问之后则跳出循环
            }
        }
        if(j==G.n)                   //如果与i相邻的顶点都被访问过,则将顶点i出栈
            s.pop();
    }
}

void BFS(MGraph G,int v)      //广度优先搜索
{
    for(int i=0;i<G.n;i++)       //初始化
    {
        visited[i]=false;
    }
    queue<int> Q;             //STL模板中的queue
    printf("%d ",v);
    visited[v]=true;
    Q.push(v);
    while(!Q.empty()) 
    {

//      printf("\n 断点2 ");
        int i,j;
        i=Q.front();         //取队首顶点
        Q.pop();

        for(j=0;j<G.n;j++)   //广度遍历
        {
//          printf("\n 断点3 ");
            if(G.edges[i][j]!=0&&visited[j]==false)
            {
//              printf("\n 断点1 ");
                printf("%d ",j);
                visited[j]=true;
                Q.push(j);
            }
        }
    }
}

int main(void)
{
    int n,e;    //建立的图的顶点数和边数
    while(scanf("%d %d",&n,&e)==2&&n>0)
    {
        MGraph G;
        G.n=n;
        G.e=e;
        creatMGraph(G);
        printf("深度递归算法: ");
        DFS(G,0);
        printf("\n");
        printf("深度非递归算法: ");
        DFS1(G,0);
        printf("\n");
        printf("广度遍历算法: ");
        BFS(G,0);
        printf("\n");
    }
    return 0;
}
/*  8 9 
    0 1 1
    0 2 1
    1 3 1
    1 4 1
    2 5 1
    2 6 1
    5 6 1
    3 7 1
    4 7 1     */
// 8 9 0 1 1 0 2 1 1 3 1  1 4 1 2 5 1 2 6 1 5 6 1 3 7 1 4 7 1
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
void CreateGraph Graph graph 的创建 { ENode p q e; int i; cout<<"请输入连通无向顶点数和边数 例如 3 3: n"; scanf "%d %d" &graph >numberOfVerts &graph >numberOfEerts ; for i 1;i< graph >numberOfVerts;i++ { cout<<"请输入第"<<i<<"个顶点的信息: n"; cin>>graph >amlist [i] data; graph >amlist [i] number i; graph >amlist[i] firstedge NULL; graph >amlist [i] mark 0; } for i 1;i< graph >numberOfEerts;i++ { p ENode malloc sizeof ENode ; cout<<"请输入每条边的信息(编号小的在前 例如1 3回车1 2回车2 3) n"; cin>>p >ivex>>p >jvex; p >ilink p >jlink NULL; if graph >amlist [p >ivex ] firstedge NULL graph >amlist [p >ivex ] firstedge p; else { q graph >amlist [p >ivex ] firstedge ; while q NULL { e q; if q >ivex p >ivex q q >ilink ; else q q >jlink ; } if e >ivex p >ivex e >ilink p; else e >jlink p; } if graph >amlist [p >jvex ] firstedge NULL graph >amlist [p >jvex ] firstedge p; else { q graph >amlist [p >jvex ] firstedge ; while q NULL { e q; if q >ivex p >ivex q q >ilink ; else q q >jlink ; } if e >ivex p >ivex e >ilink p; else e >jlink p; } } } void SetMark Graph graph 设置访问标记 { int i; for i 1;i< graph >numberOfVerts ;i++ graph >amlist [i] mark 0; } void DFS Graph graph int v 深度遍历 { ENode p; cout<<v<<" "; graph >amlist [v] mark 1; p graph >amlist [v] firstedge ; while p NULL { if p >ivex v { if graph >amlist [p >jvex ] mark 0 { cout<<"<"<<p >ivex<<" "<<p >jvex<<">"<<endl; DFS graph p >jvex ; } p p >ilink ; } else { if graph >amlist [p >ivex] mark 0 { cout<<"<"<<p >jvex<<" "<<p >ivex<<">"<<endl; DFS graph p >ivex ; } p p >jlink ; } } }">void CreateGraph Graph graph 的创建 { ENode p q e; int i; cout<<"请输入连通无向顶点数和边数 例如 3 3: n"; scanf "%d %d" &graph >numberOfVerts &graph >numberOfEerts ; for i 1;i< graph >numberOfVerts;i++ { cou [更多]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值