资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

图的非连通遍历

1、连通图和非连通图

珠山网站建设公司创新互联,珠山网站设计制作,有大型网站制作公司丰富经验。已为珠山上千家提供企业网站建设服务。企业网站搭建\成都外贸网站制作要多少钱,请找那个售后服务好的珠山做网站的公司定做!

  连通图:任意的一个顶点到任意的另外一个顶点都有着相应的路径所能够到达。

  非连通图:只要找出了有一个顶点不能够到达另外一个顶点。

2、遍历

  对于连通图来说,通过DFS或BFS就可以完成遍历;

  对于非连通图来说,就得从每个顶点出发进行搜索,每一次的从一个新的顶点出发访问,每个顶点都要开始搜索一遍。

3、非连通图的遍历算法

  (1)、不可取的算法:没有必要将非连通图生成森林,在由森林生成我们的遍历树,然后再进行树形结构的访问。

  (2)、比较好的算法:直接调动我们之前编写好的DFS()函数;只要没有访问的顶点,我们就由该顶点出发进行深度优先遍历,这样就最终把整个非连通图就遍历完成。

 (3)强连通图:针对有向图,有A-->B的边,一定也有B-->A的边。

  (4)、遍历算法:

void components(){  //非连通图的遍历
    int n = Graph::getCurVertex();
    bool *visit = new bool[n];

    for(int i = 0; i < n; i++){
        visit[i] = false;
    }
    for(i = 0; i < n; i++){  //对每个顶点都看一下,是否访问过。4
        if(!visit[i]){
            DFS(getValue(i), visit);
        }
    }

    delete []visit;
}

4、完整代码、测试代码、测试结果

  (1)、完整代码

#ifndef _GRAPH_H_
#define _GRAPH_H_

#include
#include
using namespace std;

#define VERTEX_DEFAULT_SIZE        20

template    
class Graph{
public:
    bool isEmpty()const{
        return curVertices == 0;
    }
    bool isFull()const{
        if(curVertices >= maxVertices || curEdges >= curVertices*(curVertices-1)/2)
            return true;  //图满有2种情况:(1)、当前顶点数超过了最大顶点数,存放顶点的空间已满
        return false;     //(2)、当前顶点数并没有满,但是当前顶点所能达到的边数已满
    }
    int getCurVertex()const{
        return curVertices;
    }
    int getCurEdge()const{
        return curEdges;
    }
public:
    virtual bool insertVertex(const Type &v) = 0;  //插入顶点
    virtual bool insertEdge(const Type &v1, const Type &v2) = 0; //插入边
    virtual bool removeVertex(const Type &v) = 0;  //删除顶点
    virtual bool removeEdge(const Type &v1, const Type &v2) = 0; //删除边
    virtual int getFirstNeighbor(const Type &v) = 0; //得到第一个相邻顶点
    virtual int getNextNeighbor(const Type &v, const Type &w) = 0; //得到下一个相邻顶点
public:
    virtual int getVertexIndex(const Type &v)const = 0; //得到顶点下标
    virtual void showGraph()const = 0;  //显示图
    virtual Type getValue(int index)const = 0; 
public:
    virtual void DFS(const Type &v) = 0; //深度优先
    virtual void BFS(const Type &v) = 0; //广度优先
protected:
    int maxVertices;  //最大顶点数
    int curVertices;  //当前顶点数
    int curEdges;  //当前边数
};

template
class GraphMtx : public Graph{ //邻接矩阵继承父类矩阵
#define maxVertices  Graph::maxVertices  //因为是模板,所以用父类的数据或方法都得加上作用域限定符
#define curVertices  Graph::curVertices
#define curEdges     Graph::curEdges
public:
    GraphMtx(int vertexSize = VERTEX_DEFAULT_SIZE){  //初始化邻接矩阵
        maxVertices = vertexSize > VERTEX_DEFAULT_SIZE ? vertexSize : VERTEX_DEFAULT_SIZE;
        vertexList = new Type[maxVertices]; //申请顶点空间
        for(int i = 0; i < maxVertices; i++){  //都初始化为0
            vertexList[i] = 0;
        }
        edge = new int*[maxVertices];  //申请边的行
        for(i = 0; i < maxVertices; i++){ //申请列空间
            edge[i] = new int[maxVertices];
        }
        for(i = 0; i < maxVertices; i++){ //赋初值为0 
            for(int j = 0; j < maxVertices; j++){
                edge[i][j] = 0;
            }
        }
        curVertices = curEdges = 0; //当前顶点和当前边数
    }
    GraphMtx(Type (*mt)[4], int sz){  //通过已有矩阵的初始化
        int e = 0; //统计边数
        maxVertices = sz > VERTEX_DEFAULT_SIZE ? sz : VERTEX_DEFAULT_SIZE;
        vertexList = new Type[maxVertices]; //申请顶点空间
        for(int i = 0; i < maxVertices; i++){  //都初始化为0
            vertexList[i] = 0;
        }
        edge = new int*[maxVertices];  //申请边的行
        for(i = 0; i < maxVertices; i++){ //申请列空间
            edge[i] = new Type[maxVertices];
        }
        for(i = 0; i < maxVertices; i++){ //赋初值为矩阵当中的值
            for(int j = 0; j < maxVertices; j++){
                edge[i][j] = mt[i][j];
                if(edge[i][j] != 0){
                    e++; //统计列的边数
                }
            }
        }
        curVertices = sz;
        curEdges = e/2;
    }
    ~GraphMtx(){}
public:
    bool insertVertex(const Type &v){
        if(curVertices >= maxVertices){
            return false;
        }
        vertexList[curVertices++] = v;
        return true;
    }
    bool insertEdge(const Type &v1, const Type &v2){
        int maxEdges = curVertices*(curVertices-1)/2;
        if(curEdges >= maxEdges){
            return false;
        }

        int v = getVertexIndex(v1);
        int w = getVertexIndex(v2);

        if(v==-1 || w==-1){
            cout<<"edge no exit"<::getCurVertex();
        bool *visit = new bool[n];

        for(int i = 0; i < n; i++){
            visit[i] = false;
        }
        DFS(v, visit);
        delete []visit;
    }
    void BFS(const Type &v){
        int n = Graph::getCurVertex();
        bool *visit = new bool[n];
        for(int i = 0; i < n; i++){
            visit[i] = false;
        }
        cout<";
        int index = getVertexIndex(v);
        visit[index] = true;

        queue q;  //队列中存放的是顶点下标;
        q.push(index);
        int w;
        while(!q.empty()){
            index = q.front();
            q.pop();
            w = getFirstNeighbor(getValue(index));
            while(w != -1){
                if(!visit[w]){
                    cout<";
                    visit[w] = true; 
                    q.push(w);
                }
                
                w = getNextNeighbor(getValue(index), getValue(w));
                
            }
        }

        delete []visit;
    }
    void components(){  //非连通图的遍历
        int n = Graph::getCurVertex();
        bool *visit = new bool[n];

        for(int i = 0; i < n; i++){
            visit[i] = false;
        }
        for(i = 0; i < n; i++){
            if(!visit[i]){
                DFS(getValue(i), visit);
            }
        }

        delete []visit;

    }
protected:
    void DFS(const Type &v, bool *visit){
        cout<";
        int index = getVertexIndex(v);
        visit[index] = true;
        int w = getFirstNeighbor(v);
        while(w != -1){
            if(!visit[w]){
                DFS(getValue(w), visit);
            }
            w = getNextNeighbor(v, getValue(w)); 
        }
    }
private:
    Type *vertexList;  //存放顶点的数组
    int **edge;  //存放边关系的矩阵
};

#endif

  (2)、测试代码

#include"Graph3.h"

int main(void){
   GraphMtx gm;
    gm.insertVertex('A');
    gm.insertVertex('B');
    gm.insertVertex('C'); //B的第一个邻接顶点是C,
    gm.insertVertex('D');
    gm.insertVertex('E');
    gm.insertVertex('F');
    gm.insertVertex('G');
    gm.insertVertex('H');
    gm.insertVertex('I');
    gm.insertVertex('J');
    gm.insertVertex('K');
    gm.insertVertex('L');
    gm.insertVertex('M');

    gm.insertEdge('A','B');
    gm.insertEdge('A','C');
    gm.insertEdge('A','F');
    gm.insertEdge('A','L');
    gm.insertEdge('B','M');
    gm.insertEdge('L','J');
    gm.insertEdge('L','M');
    gm.insertEdge('J','M');
    gm.insertEdge('D','E');
    gm.insertEdge('G','H');
    gm.insertEdge('G','I');
    gm.insertEdge('G','K');
    gm.insertEdge('H','K');

    gm.showGraph();

    cout<<"------------------------------------------------"<

  (3)、测试结果

测试图的模型:

图的非连通遍历

图的非连通遍历


网站栏目:图的非连通遍历
URL网址:http://cdkjz.cn/article/gpdeep.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220