建图

建图

邻接矩阵

#include <iostream>
#include <vector>

using namespace std;

// 点的最大数量
int MAX_N = 11;

// 邻接矩阵方式建图
vector<vector<int>> graph(MAX_N, vector<int>(MAX_N));

// 初始化,下标从 1 开始
void build(int n) {
    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= n; ++j)
            graph[i][j] = 0;
}

// 有向图建图
void directedGraph(vector<vector<int>> &edges) {
    for (const auto &edge: edges)
        graph[edge[0]][edge[1]] = edge[2];
}

// 无向图建图
void undirectedGraph(vector<vector<int>> &edges) {
    for (const auto &edge: edges) {
        graph[edge[0]][edge[1]] = edge[2];
        graph[edge[1]][edge[0]] = edge[2];
    }
}

void traversal(int n) {
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= n; ++j)
            cout << graph[i][j] << " ";
        cout << endl;
    }
}

int main() {
    int n1 = 4;
    vector<vector<int>> edges1 = {{1, 3, 6},
                                  {4, 3, 4},
                                  {2, 4, 2},
                                  {1, 2, 7},
                                  {2, 3, 5},
                                  {3, 1, 1}};
    build(n1);
    directedGraph(edges1);
    traversal(n1);
    cout << endl;
    int n2 = 5;
    vector<vector<int>> edges2 = {{3, 5, 4},
                                  {4, 1, 1},
                                  {3, 4, 2},
                                  {5, 2, 4},
                                  {2, 3, 7},
                                  {1, 5, 5},
                                  {4, 2, 6}};
    build(n2);
    undirectedGraph(edges2);
    traversal(n2);
}

邻接表

#include <iostream>
#include <vector>
#include <forward_list>

using namespace std;

// 点的最大数量
int MAX_N = 11;

// 邻接表方式建图
// 无权
// vector<forward_list<int>> graph(MAX_N);
// 带权
vector<forward_list<pair<int, int>>> graph(MAX_N);

// 初始化,下标从 1 开始
void build(int n) {
    for (int i = 0; i <= n; ++i)
        graph[i].clear();
}

// 有向图建图
void directedGraph(vector<vector<int>> &edges) {
    for (const auto &edge: edges) {
        // edge[0]: u edge[1]: v, u->v
        // edge[2] 为权重
        // forward_list 只能头插,使用 list 可以尾插
        graph[edge[0]].emplace_front(make_pair(edge[1], edge[2]));
    }
}

// 无向图建图
void undirectedGraph(vector<vector<int>> &edges) {
    for (const auto &edge: edges) {
        graph[edge[0]].emplace_front(make_pair(edge[1], edge[2]));
        graph[edge[1]].emplace_front(make_pair(edge[0], edge[2]));
    }
}

void traversal(int n) {
    for (int i = 1; i <= n; ++i) {
        cout << i << "(邻居、边权): ";
        auto it = begin(graph[i]);
        while (it != end(graph[i])) {
            cout << "(" << (*it).first << ", " << (*it).second << ")";
            it++;
        }
        cout << endl;
    }
}

int main() {
    int n1 = 4;
    vector<vector<int>> edges1 = {{1, 3, 6},
                                  {4, 3, 4},
                                  {2, 4, 2},
                                  {1, 2, 7},
                                  {2, 3, 5},
                                  {3, 1, 1}};
    build(n1);
    directedGraph(edges1);
    traversal(n1);
    cout << endl;
    int n2 = 5;
    vector<vector<int>> edges2 = {{3, 5, 4},
                                  {4, 1, 1},
                                  {3, 4, 2},
                                  {5, 2, 4},
                                  {2, 3, 7},
                                  {1, 5, 5},
                                  {4, 2, 6}};
    build(n2);
    undirectedGraph(edges2);
    traversal(n2);
}

链式前向星

#include <iostream>
#include <vector>

using namespace std;

// 点的最大数量
int MAX_N = 11;

// 边的最大数量
// 只有链式前向星方式建图需要这个数量
// 注意如果无向图的最大数量是 m 条边,数量要准备 m*2
// 因为一条无向边要加两条有向边
int MAX_M = 11;

// 链式前向星方式建图
// 下标:顶点编号,值:该顶点第一条边的边号
vector<int> head(MAX_N);
// 下标:边号,值:下一条边的边号
vector<int> nxt(MAX_M);
// 下标:边号,值:去往的顶点编号
vector<int> to(MAX_M);
// 如果边有权重,那么需要这个数组
vector<int> weight(MAX_M);
// 边号计数,从 1 开始,0 表示没有边
int cnt;

// 初始化,下标从 1 开始
void build(int n) {
    // 链式前向星清空
    cnt = 1;
    fill(head.begin(), head.end(), 0);
}

// 链式前向星加边,u->v,w 为权重
void addEdge(int u, int v, int w) {
    // 记录权重
    weight[cnt] = w;
    // 边号为 cnt 的边作为新的头边,插入到旧的头边之前
    nxt[cnt] = head[u];
    to[cnt] = v;
    head[u] = cnt;
    cnt++;
}

// 有向图建图
void directedGraph(vector<vector<int>> &edges) {
    for (const auto &edge: edges)
        addEdge(edge[0], edge[1], edge[2]);
}

// 无向图建图
void undirectedGraph(vector<vector<int>> &edges) {
    for (const auto &edge: edges) {
        addEdge(edge[0], edge[1], edge[2]);
        addEdge(edge[1], edge[0], edge[2]);
    }
}

void traversal(int n) {
    for (int i = 1; i <= n; ++i) {
        cout << i << "(邻居、边权): ";
        for (int ei = head[i]; ei > 0; ei = nxt[ei])
            cout << "(" << to[ei] << "," << weight[ei] << ")";
        cout << endl;
    }
}

int main() {
    int n1 = 4;
    vector<vector<int>> edges1 = {{1, 3, 6},
                                  {4, 3, 4},
                                  {2, 4, 2},
                                  {1, 2, 7},
                                  {2, 3, 5},
                                  {3, 1, 1}};
    build(n1);
    directedGraph(edges1);
    traversal(n1);
    cout << endl;
    int n2 = 5;
    vector<vector<int>> edges2 = {{3, 5, 4},
                                  {4, 1, 1},
                                  {3, 4, 2},
                                  {5, 2, 4},
                                  {2, 3, 7},
                                  {1, 5, 5},
                                  {4, 2, 6}};
    build(n2);
    undirectedGraph(edges2);
    traversal(n2);
}
posted @ 2024-09-12 09:41  n1ce2cv  阅读(12)  评论(0编辑  收藏  举报