简体   繁体   English

cpp中最简单的Graph实现

[英]Easiest implementation of Graph in cpp

Currently I am learning graph implementation.目前我正在学习graph实现。 I found 2 implementation namely matrix and adjacency list using vector in cpp.我找到了 2 个实现,即在 cpp 中使用vectormatrixadjacency list But the problems I face are following但我面临的问题如下

With matrix带矩阵

  • Passing to function is hard传递给 function 很难
  • Adding new nodes is not possible无法添加新节点
  • memory inefficient memory 效率低下

Vector implementation of adjacency list邻接表的向量实现

// Code taken from geeksforgeeks
vector<int> adj[V]; 

// adding edge from u to v
adj[u].push_back(v);
  • adding new node is not possible无法添加新节点
  • and also passing to function is hard并且也很难传递给 function

So I want a solution in cpp which can solve above 2 problems.所以我想要一个可以解决以上两个问题的cpp解决方案。

I have efficient and easiest implementation of graph in cpp .我在cpp中有效且最简单地实现了graph It solves all problems you mentioned above.它解决了你上面提到的所有问题。 It is called as vector in vector .它被称为vector in vector

// Suppose at start we have
// number of nodes
int nodes = 5;

// number of edges
int edges = 4;

// define graph
vector<vector<int>> graph(nodes, vector<int>());

int u, v; // edge from u -> v
for(int i=0;i<edges;i++){
    cin >> u >> v;
    graph[u].push_back(v);
}

// suppose we want to add node
graph.push_back(vector<int>());

// print graph content
int index = 0;
for(auto x : graph){
    cout << index << "-> ";
    for(auto y: x){
        cout << y << " ";
    }
    cout << endl;
    index ++;
}

Input to above code输入上述代码

5 4
0 1
0 2
2 1
2 3

Output Output

0-> 1 2 
1-> 
2-> 1 3 
3-> 
4-> 
5-> // new node

Advantages优点

  • Can add node at run time可以在运行时添加节点
  • memory efficient memory高效
  • you can pass to function easily like normal vector您可以像法线向量一样轻松传递给 function
  • you can find nodes in another function without passing it.您可以在另一个 function 中找到节点而不通过它。

This is by no mean efficient implementation, but a perhaps a set of pairs is what you are looking for.这绝不是有效的实现,但也许一组对是您正在寻找的。

#include <iostream>
#include <utility>
#include <set>
#include <algorithm>

template<class T>
class graph
{
public:
    std::set<std::pair<T, T>> storage;

    /* true item added, false otherwise */
    bool add_edge(T v1, T v2)
    {
        auto ret = storage.insert(std::make_pair(v1, v2));
        return ret.second;
    }

    /* return value: edges deleted */
    int delete_edge(T v1, T v2)
    {
        return storage.erase(std::make_pair(v1, v2));
    }

    void print_graph()
    {
        std::for_each(storage.begin(), storage.end(),
        [](std::pair<T, T> const& p) {
            std::cout << p.first << "--> " << p.second << std::endl;
        });
    }
};

#include <iostream>
#include "graph.h"

int main()
{
    graph<int> g;
    g.add_edge(5, 4);
    g.add_edge(5, 4);
    g.add_edge(6, 2);
    g.print_graph();
    g.delete_edge(5, 4);
    g.print_graph();
    return 0;
}

Compile command编译命令

g++ -std=c++17 -Wall -Wextra -pedantic main.cpp

Output Output

5 --> 4
6 --> 2
/* Deleted 5 --> 4 */
6 --> 2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM