当前位置: 首页 > 知识库问答 >
问题:

Boost有向图:比较顶点的边

堵宏毅
2023-03-14

一个简单的背景:我正在构建一个语义图,使用带有邻接表的BGL有向图:

class SemanticGraph
{
  public:
    typedef std::shared_ptr< Node > Node_ptr;
    typedef boost::adjacency_list< boost::vecS, boost::vecS, boost::directedS, Node_ptr > Graph;
    typedef boost::graph_traits< Graph >::vertex_descriptor Vertex;
    typedef boost::graph_traits< Graph >::edge_descriptor Edge;

我需要做的事情之一,是处理一个较小的图(子图)到我的主图。

 void AddSubGraph( SemanticGraph subgraph )
 {
      typename boost::graph_traits<Graph>::vertex_iterator it, end;
      Vertex vertex;

      for ( auto node : subgraph._nodes )
      {
        if ( !findNode( node ) )
           _nodes.push_back( node );

        boost::tie( it, end ) = boost::vertices( _graph );
        std::find_if( it, end, [&]( const Vertex vertex ){ return _graph[*it]->Hash() == node->Hash(); });

        if ( it == end )
          vertex = boost::add_vertex( node, _graph );
        else
          vertex = boost::vertex( *it, _graph );

        boost::tie( it, end ) = boost::vertices( subgraph._graph );
        std::find_if( it, end, [&]( const Vertex vertex ){ return subgraph._graph[*it]->Hash() == node->Hash(); });

        auto subgraph_vertex = boost::vertex( *it, subgraph._graph );
        typename boost::graph_traits<Graph>::out_edge_iterator a, z;

        // Iterate subgraph's vertex out edges
        for ( boost::tie ( a, z ) = boost::out_edges( subgraph_vertex, subgraph._graph );
              a != z;
              ++a )
        {
           typename boost::graph_traits<Graph>::out_edge_iterator my_edge, edge_end;
           boost::tie ( my_edge, edge_end ) = boost::out_edges( vertex, _graph );
           // How can I see if the same edge as the one pointed by edge iterator a, exists in my vertex's edges?
           std::find_if( my_edge, edge_end, [&]( const Edge edge ){ return edge == a; });
        }
      }
    }
‘const Edge {aka const boost::detail::edge_desc_impl<boost::directed_tag, long unsigned int>}’ is not derived from ‘const std::pair<_T1, _T2>’

暗示我的lambda捕获参数应该是一对(我猜是一个实际的边?)。

所以,我的问题是:我怎样才能发现在顶点的外边中是否存在类似的边呢?

共有1个答案

柳正志
2023-03-14

您将A声明为迭代器:

typename boost::graph_traits<Graph>::out_edge_iterator a, z;

迭代器与边进行比较是没有意义的。

相反,取消对迭代器的引用以获得它“指向”的边:

std::find_if(my_edge, edge_end, 
     [&](const Edge edge) { return edge == *a; });
    null

典型的代码将看到

vertex_iterator match = std::find_if(it, end, 
    [&](Vertex const& vertex) { return _graph[*it] == node; });`

if (end != match)
{
    // yes `match` is a match
}
#include <boost/graph/adjacency_list.hpp>
#include <memory>

struct Node
{
    int id;
    size_t Hash() const { return id; }
    bool operator<(const Node& other) const { return id < other.id; }
    bool operator==(const Node& other) const { return id==other.id; }
};

class SemanticGraph
{
public:
    typedef std::shared_ptr< Node > Node_ptr;
    typedef boost::adjacency_list< boost::vecS, boost::vecS, boost::directedS, Node_ptr > Graph;
    typedef boost::graph_traits< Graph >::vertex_descriptor Vertex;
    typedef boost::graph_traits< Graph >::edge_descriptor Edge;

    std::vector<Node_ptr> _nodes;
    Graph _graph;

    bool findNode(Node_ptr const& n) const { return std::find(begin(_nodes), end(_nodes), n) != end(_nodes); }

    void AddSubGraph(SemanticGraph subgraph)
    {
        typename boost::graph_traits<Graph>::vertex_iterator it, end;
        Vertex vertex;
        for(auto node : subgraph._nodes)
        {
            if(!findNode(node))
            {
                _nodes.push_back(node);
            }
            boost::tie(it, end) = boost::vertices(_graph);
            std::find_if(it, end, [&](const Vertex vertex) { return _graph[*it]->Hash() == node->Hash(); });
            if(it == end)
                vertex = boost::add_vertex(node, _graph);
            else
                vertex = boost::vertex(*it, _graph);

            boost::tie(it, end) = boost::vertices(subgraph._graph);

            std::find_if(it, end, [&](const Vertex vertex) { return subgraph._graph[*it]->Hash() == node->Hash(); });

            auto subgraph_vertex = boost::vertex(*it, subgraph._graph);
            typename boost::graph_traits<Graph>::out_edge_iterator a, z;

            // Iterate subgraph's vertex out edges
            for(boost::tie(a, z) = boost::out_edges(subgraph_vertex, subgraph._graph);
                    a != z;
                    ++a)
            {
                typename boost::graph_traits<Graph>::out_edge_iterator my_edge, edge_end;
                boost::tie(my_edge, edge_end) = boost::out_edges(vertex, _graph);
                // How can I see if the same edge as the one pointed by edge iterator a, exists in my vertex's edges?
                std::find_if(my_edge, edge_end, [&](const Edge edge) { return edge == *a; });
            }
        }
    }
};

int main()
{
    SemanticGraph g;
}
 类似资料:
  • 所以我正在研究这个问题: 这是我目前所掌握的 首先,我想对我的答案再作核实。我对有向图不是那么熟悉,对算法的效率/复杂度也不是特别熟练。我想我做得对,但如果我需要的话,我想要一些帮助。我也在寻找任何想法,使它更有效率。这些是我脑海中最先出现的算法,所以我觉得可能有更好的方法来实现它。 谢谢

  • 我必须在这个图上执行一些任务。1)找到所有没有后继的顶点。2)给没有后继的顶点赋值。(我可以用顶点属性来做这件事)。3)回溯图并用子节点的最小值更新每一层的所有父节点(甚至是中间父节点)。 例如, 根据上面的DAG,顶点{2,5,6,7}没有任何后继或外边。假设我将分别为顶点{2,5,6,7}赋值{3,2,4,6}。

  • 我想比较两个较小的有向python igraph图,包括边或节点上的所有属性及其值,以及边的方向。python igraph包中有这样的函数吗? 我看到了G1.isomorphic(G2)和相关的,但是它们似乎不适用于属性,也不适用于边缘的方向性 例子:

  • 我提出的问题与您在这里看到的几乎相同,但有一个约束条件,即必须使用groovy而不是java语法。理想情况下,答案应该非常简洁。 我有一个简单的人顶点图。每个人都有一个“年龄”属性,列出该人的年龄(以年为单位)。还有“worksFor”标记的边连接成对的人顶点。我希望看到所有边缘,边缘两端的人都具有相同的年龄属性。 然后我想问一个类似的问题,两个年龄相差不到3年。 如前所述,这应该是groovy语

  • 我怎样才能找到一个有向图中的所有顶点,这样每一个顶点都可以从这个顶点到达呢?现在我只能“发明”O(v^3)ALGO--从每个顶点得到一个DFS/BFS,但我确信,有一个更快的方法来解决这个问题。 谢谢你!

  • 我试图在Python中提出一种贪婪的算法,该算法在给定某个起始顶点的情况下返回无向图中的顶点。我知道DFS确定是否存在循环,但我正在尝试实际返回形成循环的顶点。我使用邻接矩阵来表示下图: 从图学上讲,这是一个由单个循环组成的无向图。 我当前的思想过程是将起始索引设置为我遇到的第一个<code>1)。然后我会查看行的其余部分,看看是否有另一个<code>1</code>存在,因为这意味着我的当前顶点