• 图论 邻接表广搜


    上一篇是邻接表dfs:点击打开链接



    所以bfs(&G,0)结果为0 2 1 3 4

    bfs(&G,1)结果为1 2 3 4

    bfs(&G,2)结果为2 3 4

    bfs(&G,3)结果为 3 4 

    bfs(&G,4)结果为4

    #include<stdio.h>
    #include<string.h>
    #include<string>
    #include<queue>
    #include<iostream>
    using namespace std;
    int vis[100];
    typedef struct Enode
    {
        int v;
        int weight;
        struct Enode *next;
    } edgenode;
    
    typedef struct Vertexnode
    {
        int v;
        edgenode *first;
    } vertexnode;
    
    typedef struct Graph
    {
        vertexnode Vnode[100];
        int num_v,num_e;
    } mygraph;
    
    void create(mygraph *G)
    {
        int i,j,k,x,y,z;
        edgenode *E;
        cin>>G->num_v>>G->num_e;
        for(i=0; i<G->num_v; i++)
        {
            cin>>G->Vnode[i].v;
            G->Vnode[i].first=NULL;
        }
        for(i=0; i<G->num_e; i++)
        {
            scanf("%d%d%d",&x,&y,&z);
            E=new Enode;
            E->v=y;
            E->weight=z;
            E->next=G->Vnode[x].first;
            G->Vnode[x].first=E;
    
    //如果是无向图就把下面这段代码加
    //        E=new Enode;
    //
    //        E->v=x;
    //        E->weight=z;
    //        E->next=G->Vnode[y].first;
    //        G->Vnode[y].first=E;
        }
    }
    void dfs(mygraph *g,int x)
    {
        vis[x]=1;
        Enode *p;
        int i,j;
        printf("%d ",g->Vnode[x]);
        p=g->Vnode[x].first;
        while(p)
        {
            if(!vis[p->v])
            {
                dfs(g,p->v);
            }
            p=p->next;
    
        }
    }
    queue<int>q;
    void bfs(Graph *G,int x)//广搜
    {
        int i,tmp,tmp1,tmp2;
        Enode *p;
        while(!q.empty())q.pop();
        q.push(x);
        vis[x]=1;
        while(!q.empty())
        {
            tmp=q.front();
            printf("%d ",tmp);
            q.pop();
            p=G->Vnode[tmp].first;
            while(p)
            {
                if(!vis[p->v])
                {
                    q.push(p->v);
                    vis[p->v]=1;
                }
                p=p->next;
    
            }
        }
    }
    int main()
    {
        int i,j,k;
        int tmp,tmp1,tmp2;
    
    
        mygraph G;
        create(&G);
    //    for(i=0;i<G.num_v;i++)
    //    if(!vis[i])dfs(&G,i);
        memset(vis,0,sizeof(vis));
        bfs(&G,0);
        printf("
    ");
    
        memset(vis,0,sizeof(vis));
        bfs(&G,1);
        printf("
    ");
    
        memset(vis,0,sizeof(vis));
        bfs(&G,2);
        printf("
    ");
    
        memset(vis,0,sizeof(vis));
        bfs(&G,3);
        printf("
    ");
    
        return 0;
    
    }
    /*
    5 5
    0 1 2 3 4
    0 1 9
    0 2 2
    1 2 3
    2 3 5
    3 4 1
    */

    测试数据中5  5代表节点数和边数为5,下面是结点名0 1 2 3 4  下面几行中分别为为 弧尾(边的起点),弧首(边的终点),和边的权值

  • 相关阅读:
    交叉熵--损失函数
    Window安装TensorFlow- GPU环境
    使用Tensorflow object detection API——训练模型(Window10系统)
    使用Tensorflow object detection API——环境搭建与测试
    由位图生成区域
    旋转位图
    获得当前颜色深度
    求多边形的面积
    MaskBlt 拷贝非矩形区域图象
    画多边型
  • 原文地址:https://www.cnblogs.com/hjch0708/p/7554833.html
Copyright © 2020-2023  润新知