图的存储(C++简单实现) 基于5种基础方式

可以有5个类型

  • 邻接矩阵
  • 边集数组
  • 邻接表
  • 链式邻接表
  • 链式前向星

邻接矩阵

#include <iostream>
#include <vector>
using namespace std;
#define N  99

int n,m;
bool vis[N];
int w[N][N];
void dfs(int u) {
    
    vis[u] = true;
    for(int v = 1; v <= n; ++v) {
        if (w[u][v]){
            cout << u <<"->"<< v <<":" << w[u][v] << endl;
            if (vis[v]) continue;
            dfs(v);
        }
        
    }
}

int main()
{
    int a, b, c;
    cout << "the number of vertex and edges:" << endl;
    cin >> n >> m;
    cout<<endl;
    cout << "input the cost of each edge" << endl;
    for (int i = 1; i <= m; ++i) {
        cin >> a >> b >> c;
        w[a][b] = c;
    }
    dfs(1);
    return 0;
}

 

 边集数组

 

#include <iostream>
#include <vector>
using namespace std;
#define N  99
#define M  99
struct edge{
  int u, v, w;
}e[M];

int n, m;
int vis[N];
void dfs(int u) {
    vis[u] = true;
    for(int i = 1; i <= n; ++i) {
        if (e[i].u == u){ // i 表示输入配对的顶点和边的编号。
            int v = e[i].v, w = e[i].w;
            cout << u <<"->"<< v <<":" << w << endl;
            if (vis[v]) continue;
            dfs(e[i].v);
        }
    }
}

int main()
{
    int a, b, c;
    cout << "the number of vertex and edges:" << endl;
    cin >> n >> m;
    cout<<endl;
    cout << "input the cost of each edge" << endl;
    for (int i = 1; i <= m; ++i) {
        cin >> a >> b >> c;
        e[i] = {a, b, c};
    }
    dfs(1);
    return 0;
}

 

 邻接表

 

#include <iostream>
#include <vector>
using namespace std;
#define N  99
struct edge{
  int v, w;
};
vector<edge> e[N];
int n, m;
void dfs(int u, int fa) {
    for(auto ed : e[u]) {
            int v = ed.v, w = ed.w;
            if (v == fa) continue; //fa记录的是u的前向顶点
            cout << u <<"->"<< v <<":" << w << endl;
            dfs(v, u);
    }
}

int main()
{
    int a, b, c;
    cout << "the number of vertex and edges:" << endl;
    cin >> n >> m;
    cout<<endl;
    cout << "input the cost of each edge" << endl;
    for (int i = 1; i <= m; ++i) {
        cin >> a >> b >> c;
        e[a].push_back({b,c});
        e[b].push_back({a,c});
    }
    cout << "result:" << endl;
    dfs(1, 0);
    return 0;
}

 链式邻接表

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;

const int N=510;
int n,m,a,b,c;
struct edge{int u,v,w;};
vector<edge> e;//边集
vector<int> h[N];//二维数组,表示点的所有出边
void add(int a, int b, int c) {
    e.push_back({a, b, c});
    h[a].push_back(e.size() - 1); // h[a]的下标代表顶点,h[a]存编号
}
void dfs(int u, int fa) {
    for(int i = 0; i < h[u].size(); ++i) { 
            int j = h[u][i]; // 存储u点所有出边的编号
            int v = e[j].v, w = e[j].w;
            if (v == fa) continue; //fa记录的是u的前向顶点
            cout << u <<"->"<< v <<":" << w << endl;
            dfs(v, u);
    }
}

int main()
{
    
    cout << "the number of vertex and edges:" << endl;
    cin >> n >> m;
    cout<<endl;
    cout << "input the cost of each edge" << endl;
    for (int i = 1; i <= m; ++i) {
        cin >> a >> b >> c;
        add(a, b, c);
        add(b, a, c);
    }
    cout << "result:" << endl;
    dfs(1, 0);
    return 0;
}

 链式前向星

 

 

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;

const int N=510, M= 3000;
int n,m,a,b,c;
struct edge{int v,w,ne;}; //e[i]表示第i条出边的{终点v, 边权w, 下一条边ne}
edge e[M]; //边集
int idx,h[N]; //点的第一条出边 
void add(int a, int b, int c) {
   e[idx] = {b, c, h[a]};
   h[a] = idx++;
}
void dfs(int u, int fa) {
    for(int i = h[u]; ~ i; i = e[i].ne) { 
            int v = e[i].v, w = e[i].w;
            if (v == fa) continue; //fa记录的是u的前向顶点
            cout << u <<"->"<< v <<":" << w << endl;
            dfs(v, u);
    }
}

int main()
{
    
    cout << "the number of vertex and edges:" << endl;
    cin >> n >> m;
    memset(h, -1, sizeof h);
    cout<<endl;
    cout << "input the cost of each edge" << endl;
    for (int i = 1; i <= m; ++i) {
        cin >> a >> b >> c;
        add(a, b, c);
        add(b, a, c);
    }
    cout << "result:" << endl;
    dfs(1, 0);
    return 0;
}

 

  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值