zoukankan      html  css  js  c++  java
  • 图的遍历

    采用图的邻接表存储结构实现

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <queue>
    
    using namespace std;
    
    #define MAX_NUM 100
    
    //边节点
    typedef struct ArcNode
    {
        int adjvex; //边的终点编号
        int weight; //边权重
        struct ArcNode* next; //指向下一条边
    }ArcNode;
    
    //顶点节点
    typedef struct VNode
    {
        int data; //顶点编号
        ArcNode* first; //顶点指向的第一条边
    }VNode;
    
    //图
    typedef struct Graph
    {
        VNode adjList[MAX_NUM]; //顶点集合
        int vexnum; //顶点个数
        int arcnum; //边的条数
    }ALGraph;
    
    void init(ALGraph &graph)
    {
        memset(graph.adjList, 0, sizeof(graph.adjList));
        graph.arcnum = 0;
        graph.vexnum = 0;
    }
    
    //构建图
    void buildGraph(ALGraph &graph, int edgeInfo[], int n)
    {
        bool vist[MAX_NUM];
        memset(vist, 0, sizeof(vist));
        for(int i = 0; i < n; i = i + 2)
        {
            //新建边节点
            ArcNode* arcNode = (ArcNode*)malloc(sizeof(ArcNode));
            arcNode->adjvex = edgeInfo[i + 1];
            arcNode->next = NULL;
            graph.arcnum++;
    
            //顶点
            if(!vist[edgeInfo[i]])
            {
                graph.vexnum++; vist[edgeInfo[i]] = true;
                graph.adjList[edgeInfo[i]].data = edgeInfo[i];
            }
            if(!vist[edgeInfo[i + 1]])
            {
                graph.vexnum++; vist[edgeInfo[i + 1]] = true;
                graph.adjList[edgeInfo[i + 1]].data = edgeInfo[i + 1];
            }
    
            int vnum = edgeInfo[i];
            if(graph.adjList[vnum].first == NULL)
            {
                graph.adjList[vnum].first = arcNode;
            }
            else
            {
                ArcNode* temp = graph.adjList[vnum].first;
                while(temp->next != NULL)
                    temp = temp->next;
                temp->next = arcNode;
            }
        }
    }
    
    //bfs遍历图
    void bfs(ALGraph graph, int v, bool visit[])
    {
        queue<int> q;
        q.push(v);
        visit[v] = true;
        while(!q.empty())
        {
            int cur = q.front();
            q.pop();
            cout<<graph.adjList[cur].data<<"  ";
            for(ArcNode* edge = graph.adjList[cur].first; edge != NULL; edge = edge->next)
            {
                if(!visit[edge->adjvex])
                {
                    q.push(edge->adjvex);
                    visit[edge->adjvex] = true;
                }
            }
        }
    }
    
    void bfsTraverse(ALGraph graph)
    {
        //初始化访问标记
        bool visit[MAX_NUM];
        memset(visit, 0, sizeof(visit));
        for(int i = 1; i <= graph.vexnum; i++)
            if(!visit[i])
                bfs(graph, i, visit);
        cout<<endl;
    }
    
    void dfs(ALGraph graph, int v, bool visit[])
    {
        cout<<graph.adjList[v].data<<"  "; //v==data
        visit[v] = true;
        for(ArcNode* edge = graph.adjList[v].first; edge != NULL; edge = edge->next)
        {
            if(!visit[edge->adjvex])
            {
                dfs(graph, edge->adjvex, visit);
            }
        }
    }
    
    void dfsTraverse(ALGraph graph)
    {
        bool visit[MAX_NUM];
        memset(visit, 0, sizeof(visit));
        for(int i = 1; i <= graph.vexnum; i++)
            if(!visit[i])
                dfs(graph, i, visit);
        cout<<endl;
    }
    
    int main()
    {
        int edgeInfo[] = {1,2,1,4,2,5,3,1,3,6,4,2,4,6,6,5};
        ALGraph graph;
        init(graph);
    
        buildGraph(graph, edgeInfo, 16);
        cout<<"图的顶点个数: "<<graph.vexnum<<endl;
        cout<<"图的边条数: "<<graph.arcnum<<endl;
        cout<<"广度优先遍历序列: "<<endl;
        bfsTraverse(graph);
        cout<<"深度优先遍历序列: "<<endl;
        dfsTraverse(graph);
        return 0;
    }
    

      

  • 相关阅读:
    团队冲刺第五天
    团队冲刺第一天
    IOS开发(四):开关控件UISwitch
    IOS开发(七):UIActionSheet和UIAlertView
    IOS开发(五):分段控件UISegmentedControl
    [转]IOS开发(一):第一个有交互的APP
    [转]IOS开发(三):UIImageView、UISlider、UIStepper、UIButton
    IOS开发(八):系统声音服务
    [转]Tab Bars和Pickers
    [转]IOS开发(二):ImageView、TextField、键盘
  • 原文地址:https://www.cnblogs.com/wt20/p/7464178.html
Copyright © 2011-2022 走看看