【C++】基于邻接矩阵的图的深度优先遍历(DFS)和广度优先遍历(BFS)

 写在前面:本博客为本人原创,严禁任何形式的转载!本博客只允许放在博客园(.cnblogs.com),如果您在其他网站看到这篇博文,请通过下面这个唯一的合法链接转到原文!

本博客全网唯一合法URL:https://www.cnblogs.com/acm-icpcer/p/10458956.html

 

算法思想使用的是殷人昆《数据结构(基于面向对象和C++)》第二版P364页的程序8.9&P366程序8.10

复制代码
#include<iostream>
#include<cstdlib>
#include<cstring>
using namespace std;
const long M=1024;

struct node
{
    int data;
    struct node* next;
};

class Q
{
private:
    node *head,*rear;
public:
    Q()//this actually ruled that we do not use first node
    {
        head=new node();
        rear=head;
    }
    void add(int a)
    {
        node *t=new node();
        t->data=a;
        rear->next=t;
        rear=rear->next;
    }
    int remove()
    {
        node *t=head->next;
        int result=t->data;
        head->next=t->next;
        delete t;
        return result;
    }
    
    node* gethead()
    {
        return head;
    }
    
    bool isempty()
    {
        if(head->next==NULL)
            return true;
        else return false;
    }
};

class G
{
private:
    int map[M][M];
    int visited[M];
    int m;
public:
    G()
    {
        cin>>m;
        reset();
        clean();
        build();
    }
    
    void reset()
    {
        memset(map,0,sizeof(map));
        cout<<"reset over!"<<endl;
    }
    
    void clean()
    {
        memset(visited,0,sizeof(visited));
        cout<<"clean over!"<<endl;
    }
    
    void build()
    {
        for(int i=0;i<m;i++)
            for(int j=0;j<m;j++)
                scanf("%d",&map[i][j]);
    }
    
    void display()
    {
        cout<<"displaying:"<<endl;
        for(int i=0;i<m;i++)
        {
            for(int j=0;j<m;j++)
                cout<<map[i][j];
            cout<<endl;
        }
    }
    
    int getfirstedge(int v)
    {
        if(v>m||v<0)    return -1;
        int i=0;
        while(map[v][i]<=0&&i<m)    i++;
        if(i>m||i<0)    return -1;
        return i;
    }
    
    int getnextedge(int v,int w)
    {
        int i=w+1;
        if(v>m||v<0||i>m||i<0)    return -1;
        while(map[v][i]<=0&&i<m)    i++;
        if(i>m||i<0)    return -1;
        return i;
    }
    /*
    void dfs(int i,int j)
    {
        if(i>m||j>m||i<0||j<0||visited[i][j])
            return;
            
        ++visited[i][j];
            
        if(map[i][j]>0)
            cout<<"node->"<<i<<":"<<map[i][j];
        
        dfs(i-1,j);
        dfs(i,j-1);
        dfs(i+1,j);
        dfs(i,j+1);
    }
    */
    
    void dfs(int v)//v represents a node
    {
        cout<<v;
        visited[v]=1;
        int w=this->getfirstedge(v);
        while(w>=0&&w<m)
        {
            if(visited[w]!=1)
                dfs(w);
            w=this->getnextedge(v,w);
        }
    }
    
    void bfs(int v)
    {
        Q *q=new Q();
        cout<<v;
        visited[v]=1;
        q->add(v);
        while(!q->isempty())
        {
            int loc=q->remove();
            int w=this->getfirstedge(loc);
            while(w>=0&&w<m)
            {
                if(visited[w]==false)
                {
                    cout<<w;
                    visited[w]=1;
                    q->add(w);
                }
                w=this->getnextedge(loc,w);
            }
        }
        
    }
    
};

int main()
{
    G *obj1=new G();
    obj1->display();
    /*
    cout<<obj1->getfirstedge(1)<<endl;
    cout<<obj1->getnextedge(1,obj1->getfirstedge(1))<<endl;
    */
    obj1->clean();
    cout<<"dfs:"<<endl;
    obj1->dfs(0);
    cout<<endl;
    
    obj1->clean();
    cout<<"bfs:"<<endl;
    obj1->bfs(0);
    cout<<endl;
    
    
    return 0;
}
复制代码

 

代码运行说明:

 

 

 

tz@HZAU

2019/3/2

posted on   tuzhuo  阅读(3147)  评论(0编辑  收藏  举报

编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示