数据结构与算法C++之图的遍历

图的遍历:遍历邻边
在这里插入图片描述
下面是程序的实现

#include <iostream>
#include <ctime>
#include <cstdlib>
#include "SparseGraph.h"
#include "DenseGraph.h"

using namespace std;

int main()
{
    int N = 20; //顶点个数
    int M = 100; //边个数

    srand(time(NULL));

    //Sparse Graph
    SparseGraph g1(N, false);
    for(int i = 0; i < M; i++){
        int a = rand()%N;
        int b = rand()%N;
        g1.addEdge(a, b);
    }

    //O(E)
    for( int v = 0; v < N; v++){
        cout<<v<<" : ";
        SparseGraph::adjIterator adj( g1, v );
        for (int w = adj.begin(); !adj.end(); w = adj.next())
            cout<<w<<" ";
        cout<<endl;
    }

    //Dense Graph
    DenseGraph g2(N, false);
    for(int i = 0; i < M; i++){
        int a = rand()%N;
        int b = rand()%N;
        g2.addEdge(a, b);
    }

    //O(v^2)
    for( int v = 0; v < N; v++){
        cout<<v<<" : ";
        DenseGraph::adjIterator adj( g2, v );
        for (int w = adj.begin(); !adj.end(); w = adj.next())
            cout<<w<<" ";
        cout<<endl;
    }

    return 0;
}

稀疏图SparseGraph.h定义为

#include <iostream>
#include <vector>
#include <cassert>

using namespace std;

//稀疏图 - 邻接表
class SparseGraph{

private:
    int n, m;
    bool directed;
    vector< vector<int> > g;

public:
    SparseGraph( int n, bool directed ){
        this->n = n;
        this->m = 0;
        this->directed = false;
        for ( int i = 0; i < n; i++ ){
            g.push_back(vector<int>());
        }
    }

    ~SparseGraph() {

    }

    int V(){return n;}
    int E(){return m;}

    void addEdge(int v, int w){
        assert( v >= 0 && v < n );
        assert( w >= 0 && w < n );

        g[v].push_back(w);
        if ( v != w && !directed )
            g[w].push_back(v);

        m++;
    }

    bool hasEdge(int v, int w){
        assert( v >= 0 && v < n );
        assert( w >= 0 && w < n );
        for (int i = 0; i < g[v].size(); i++){
            if ( g[v][i] == w )
                return true;
        }
        return false;
    }

    class adjIterator{
    private:
        SparseGraph &G;
        int v;
        int index;
    public:
        adjIterator(SparseGraph &graph, int v): G(graph){
            this->v = v;
            this->index = 0;
        }

        int begin(){
            index = 0;
            if (G.g[v].size())
                return G.g[v][index];
            return -1;
        }

        int next(){
            index++;
            if (index < G.g[v].size())
                return G.g[v][index];
            return -1;
        }
        bool end(){
            return index >= G.g[v].size();
        }
    };
};

稠密图DenseGraph.h定义为

#include <iostream>
#include <cassert>
#include <vector>

// 稠密图 - 邻接矩阵
class DenseGraph {

private:
    int n;
    int m;
    bool directed;
    vector< vector<bool> > g;

public:
    DenseGraph( int n, bool directed ){
        this->n = n;
        this->m = 0;
        this->directed = directed;
        for (int i = 0; i < n; i++){
            g.push_back( vector<bool>(n,false) );
        }
    }
    ~DenseGraph() {

    }

    int V(){return n;}
    int E(){return m;}

    void addEdge(int v, int w ){
        assert( v >= 0 && v < n );
        assert( w >= 0 && w < n );

        if ( hasEdge(v, w) )
            return;
        g[v][w] = true;
        if ( !directed )
            g[w][v] = true;

        m++;
    }

    bool hasEdge(int v, int w){
        assert( v >= 0 && v < n );
        assert( w >= 0 && w < n );
        return g[v][w];
    }

    class adjIterator{
    private:
        DenseGraph &G;
        int v;
        int index;
    public:
        adjIterator(DenseGraph &graph, int v): G(graph){
            this->v = v;
            this->index = -1;
        }

        int begin(){
            index = -1;
            return next();
        }

        int next(){
            for (index += 1; index < G.V(); index++){
                if ( G.g[v][index] )
                    return index;
            }
            return -1;
        }
        bool end(){
            return index >= G.V();
        }
    };
};

稀疏图的输出为
在这里插入图片描述
稠密图的输出为
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值