简体   繁体   English

如何将这些节点连接为有向图?

[英]How can i connect these nodes as a directed graph?

How can i connect these nodes as a directed graph using vectors? 如何使用向量将这些节点连接为有向图? Also how can i make it go through each edges only once (Eulerian circuit). 另外,我如何才能使它仅通过每个边缘一次(欧拉循环)。

#include <string>
#include <iostream>
#include <vector>
using namespace std;
class node
{
     public:
     string a;
     vector<node> connect;
     node(string b)
     {
          a = b;
     }
};

int main()
{
    vector<node> AllNodes;                 
    AllNodes.push_back(A); 
    AllNodes.push_back(B);
    AllNodes.push_back(C);
}

For making edges, you need edgelist for each node. 为了制作边缘,您需要每个节点的边缘列表。 You already have that in conncection member of class node . 您已经在class node conncection成员中拥有了它。

You just need to add into the list. 您只需要添加到列表中即可。 So, you can just do the following: 因此,您可以执行以下操作:

A.connection.push_back(required_node);

It will be better if you hold a reference than a copy of an object. 如果您持有引用,则比对象的副本更好。 It will save memory. 这样可以节省内存。 So, you can define a member function around this logic. 因此,您可以围绕此逻辑定义成员函数。

Try something like that, add the connection to the node by using address reference 尝试类似的操作,使用地址引用将连接添加到节点

class node
{
public:
    string a;
    vector<node> connect;
    node(string b)
    {
        a = b;
    }

    void add_connect(node &n)
    {
        connect.push_back(n);
    }

    void print()
    {
        cout << "value: " << a << endl;
    }

    void print_connection()
    {
        for each (node var in connect)
        {
            cout << "conection of " << a << ": ";
            var.print();
        }
    }
};


    int main(int argc, char*argv[])
{
    node A("A"),B("B"), C("C");
    vector<node> AllNodes;
    AllNodes.push_back(A);
    AllNodes.push_back(B);
    AllNodes.push_back(C);

    A.add_connect(B);
    B.add_connect(C);

    A.print();
    A.print_connection();

    B.print();
    B.print_connection();
}

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

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