图的创建和遍历c++实现

邻接表实现
#include <iostream>
#include <vector>
#include <cassert>
#include <stack>
#include <queue>
#include <algorithm>

using namespace std;

class SparseGraph{
public:
    int n, m;    //n为节点个数,m为边的条数
    bool directed; //是否为有向图
    vector<vector<int> > g;

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

    }
    int V(){
        return n;
    }
    int E(){
        return m;
    }
    void addEgde(int v, int w){
        assert(v >= 0 && v < n);
        assert(w >= 0 && w < n);
        g[v].push_back(w);
        if(!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;
    }
    void Print(){
        for(int i = 0; i < g.size(); ++i){
            cout << "边" << i << ":";
            for(int j = 0; j < g[i].size(); ++j){
                cout << g[i][j] << " ";
            }
            cout << endl;
        }
    }

    void dfs(int v){
        vector<bool> visited(n, false);
        stack<int> s;
        s.push(v);
        while(!s.empty()){
            int tmp = s.top();
            if(!visited[tmp])
                cout << tmp << " ";
            visited[tmp] = true;
            s.pop();
            int size = g[tmp].size();
            for(int i = 0; i < size; ++i){
                int b = g[tmp][i];
                if(!visited[b])
                    s.push(b);
            }
        }
        cout << endl;
    }
    void bfs(int v){
        vector<bool> visited(n, false);
        queue<int> que;
        que.push(v);
        while(!que.empty()){
            int tmp = que.front();
            if(!visited[tmp])
                cout << tmp << " ";
            visited[tmp] = true;
            que.pop();
            int size = g[tmp].size();
            for(int i = 0; i < size; ++i){
                int b = g[tmp][i];
                if(!visited[b])
                    que.push(b);
            }
        }
        cout << endl;
    }
};

int main(){
    int vertex, edge;
    cin >> vertex >> edge;
    //创建无向图
    SparseGraph myGraph(vertex, false);
    for(int i = 0; i < edge; ++i){
        int from, to;
        cin >> from >> to;
        assert( from >= 0 && from < vertex );
        assert( from >= 0 && from < vertex );
        myGraph.addEgde(from, to);
    }
    myGraph.Print();
    myGraph.dfs(0);
    myGraph.bfs(0);
    return 0;
}

结果

6 8
0 1
0 2
0 5
1 2
1 3
1 4
3 4
3 5
图的邻接表实现:0:1 2 51:0 2 3 42:0 13:1 4 54:1 35:0 3 
深度优先遍历结果: 
0 5 3 4 1 2 
广度优先遍历结果: 
0 1 2 5 3 4 
邻接矩阵实现
#include <iostream>
#include <vector>
#include <cassert>
#include <stack>
#include <queue>

using namespace std;

class DenseGraph{
public:
    int n, m;    //n为节点个数,m为边的条数
    bool directed; //是否为有向图
    vector<vector<int> > g;


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

    }
    int V(){
        return n;
    }
    int E(){
        return m;
    }
    void addEgde(int v, int w){
        assert(v >= 0 && v < n);
        assert(w >= 0 && w < n);
        if(hasEdge(v, w))
            return;
        g[v][w] = 1;
        if(!directed)
            g[w][v] = 1;
        ++m;
    }
    bool hasEdge(int v, int w){
        assert(v >= 0 && v < n);
        assert(w >= 0 && w < n);
        return (g[v][w] == 1);
    }
    void Print(){
        for(int i = 0; i < g.size(); ++i){
            for(int j = 0; j < g[i].size(); ++j){
                cout << g[i][j] << " ";
            }
            cout << endl;
        }
    }
    void dfs(int v){
        assert(v >= 0 && v < n);
        vector<bool> visited(n, false);
        stack<int> s;
        s.push(v);
        while(!s.empty()){
            int tmp = s.top();
            if(!visited[tmp])
                cout << tmp << " ";
            visited[tmp] = true;
            s.pop();
            int size = g[tmp].size();
            for(int i = 0; i < size; ++i){
                int b = g[tmp][i];
                if(b == 1 && !visited[i])
                    s.push(i);
            }
        }
        cout << endl;
    }
    void bfs(int v){
        assert(v >= 0 && v < n);
        vector<bool> visited(n, false);
        queue<int> que;
        que.push(v);
        while(!que.empty()){
            int tmp = que.front();
            if(!visited[tmp])
                cout << tmp << " ";
            visited[tmp] = true;
            que.pop();
            int size = g[tmp].size();
            for(int i = 0; i < size; ++i){
                int b = g[tmp][i];
                if(b == 1 && !visited[i])
                    que.push(i);
            }
        }
        cout << endl;
    }

};

int main(){
    int vertex, edge;
    cin >> vertex >> edge;
    //创建无向图
    DenseGraph myGraph(vertex, false);
    for(int i = 0; i < edge; ++i){
        int from, to;
        cin >> from >> to;
        assert( from >= 0 && from < vertex );
        assert( from >= 0 && from < vertex );
        myGraph.addEgde(from, to);
    }
    myGraph.Print();
    myGraph.dfs(0);
    myGraph.bfs(0);
    return 0;
}

结果

6 8
0 1
0 2
0 5
1 2
1 3
1 4
3 4
3 5
图的邻接矩阵实现: 
0 1 1 0 0 1 
1 0 1 1 1 0 
1 1 0 0 0 0 
0 1 0 0 1 1 
0 1 0 1 0 0 
1 0 0 1 0 0 
深度优先遍历结果: 
0 5 3 4 1 2 
广度优先遍历结果: 
0 1 2 5 3 4 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值