在无向图中记录 DFS 搜索中的前驱

2024-01-10

我试图使用此线程中的代码:提升DFS back_edge https://stackoverflow.com/questions/19346820/boost-dfs-back-edge/19391511?noredirect=1#comment81785237_19391511,在无向图中记录循环。为此,我需要存储predecessors对于每个 dfs 树,当它找到 back_edge 时。由于这是一个无向图我认为我们不能直接使用on_back_edge() from 活动访客概念 http://www.boost.org/doc/libs/1_57_0/libs/graph/doc/EventVisitor.html。于是我就想把前辈们记录下来void back_edge()下面代码的方法。但我不知道如何执行此操作并返回循环顶点。

下面是代码以及我为记录前辈而添加的部分:

#include <boost/config.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/depth_first_search.hpp>

namespace {
using namespace boost;

  typedef adjacency_list<vecS, vecS, undirectedS, no_property, 
  property<edge_weight_t, int> > Graph;
  typedef graph_traits<Graph>::edge_descriptor Edge;
  typedef std::set<Edge> EdgeSet;
}

struct MyVisitorCycle : default_dfs_visitor {
MyVisitorCycle(EdgeSet &tree_edges, vector<vector<vertex_t> > &cycles1) :
        tree_edges(tree_edges), cycles(cycles1) {}

  void tree_edge(Edge e, const Graph& g) const {
    std::cerr << "tree_edge " << e << std::endl;
    tree_edges.insert(e);
  }
  void back_edge(Edge e, const Graph& g) const {
    if (tree_edges.find(e) == tree_edges.end())
        std::cerr << "back_edge " << e << std::endl;

    /// I added this part
    vertex_t s = vertex (0,g);
    std::vector <vertex_t> p(num_vertices (g));
    depth_first_search (g, s, visitor (boost::record_predecessors (&p[0], 
                                  on_tree_edge())));/// here has problem
    p.push_back (target (e,g)); /// close the cycle
    cycles.push_back (p);
  }

  private:
  EdgeSet& tree_edges;
  vector<vector <vertex_t> > &cycles;
};

int main() {
  Graph g;
  add_edge(0, 1, g);
  add_edge(1, 2, g);
  add_edge(2, 3, g);
  add_edge(3, 0, g);
  add_edge(1, 4, g);
  add_edge(4, 5, g);
  add_edge(5, 6, g);
  add_edge(6, 3, g);
  add_edge(2, 5, g);

  std::set<Edge> tree_edges;
  vector<vector <vertex_t> > cycles;
  MyVisitorCycle vis(tree_edges, cycles);

  depth_first_search(g, visitor(vis));
}

这是一篇简短的文章,稍后我会带一些笔记回来

Live On Coliru http://coliru.stacked-crooked.com/a/7d62a837d5d5dd0c

#include <iostream>
#include <boost/config.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/depth_first_search.hpp>

namespace {
    using namespace boost;

    typedef adjacency_list<vecS, vecS, undirectedS, no_property, property<edge_weight_t, int> > Graph;
    typedef graph_traits<Graph>::edge_descriptor Edge;
    typedef std::set<Edge> EdgeSet;
}

struct MyVisitor : default_dfs_visitor {
    MyVisitor(EdgeSet& tree_edges) : tree_edges(tree_edges) {}

    void tree_edge(Edge e, const Graph& g) const {
        std::cerr << "tree_edge " << e << std::endl;
        tree_edges.insert(e);
    }
    void back_edge(Edge e, const Graph& g) const {
        if (tree_edges.find(e) == tree_edges.end())
            std::cerr << "back_edge " << e << std::endl;
    }

  private: 
    EdgeSet& tree_edges;
};

int main() {
    Graph g;
    add_edge(0, 1, g);
    add_edge(0, 2, g);

    std::set<Edge> tree_edges;
    MyVisitor vis(tree_edges);


    std::vector<Graph::vertex_descriptor> pred(num_vertices(g), Graph::null_vertex());

    depth_first_search(g, visitor(boost::make_dfs_visitor(
            boost::record_predecessors(pred.data(), boost::on_back_edge{})
        )));

    for (auto v : make_iterator_range(vertices(g))) {
        std::cout << "Predecessor for " << v << " is " << pred.at(v) << "\n";
    }
}

Prints

Predecessor for 0 is 2
Predecessor for 1 is 18446744073709551615
Predecessor for 2 is 18446744073709551615
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在无向图中记录 DFS 搜索中的前驱 的相关文章

随机推荐

  • 同一文件的重复资源警告

    我收到这个相当令人困惑的编译器警告 DCC 警告 W1056 警告 重复资源 类型 14 ICON 集团 ID MAICON 文件 C dev dispense trunk dev source mountaintop dispense M
  • 自定义文本块,将其内容转换为不同的颜色

    我将编写一个自定义文本块来分割其文本内容 它将根据条件使文本具有不同的颜色 并且文本将用逗号分隔 逗号将保持黑色 我不知道如何开始 您能提供启动方面的帮助吗 提前致谢 下面的用户控件使用项目控件以某种随机颜色显示每个标记 Usage
  • 合并2个链表并附加到链表的末尾c ++

    到目前为止我还没有太多 但我正在尝试掌握使用链表的窍门 Struct struct Node int value Node next 如何将节点添加到列表末尾 我只是想获取一个列表头的指针和一个 int 值作为新节点添加 当我尝试运行当前的
  • Numpy 中从一个音高到另一个音高的正弦波滑奏

    我一直在开发一个程序 我需要缓慢而平稳地将正弦波的音调从一个音调更改为另一个音调 我能够获得在任何给定时刻音调应有的频率数组 例如 440 526 5 634 2 794 8 880 尽管更长很多 但似乎我无法实际应用该频率来一波 我最好的
  • 如何从 coverity-scan 中删除项目

    我已经注册了一个项目覆盖扫描 https scan coverity com在过去 我现在想从覆盖扫描中删除该项目 或者至少从我的仪表板上删除该项目 但最好我想完全删除该项目 我被困住了 因为网络界面中似乎没有这样的选项 我错过了什么吗 你
  • RxJS first() for Observable.of() - 序列中没有元素

    对于我的测试 我试图用以下方法模拟事件流Observable of 但当我尝试时 const actions Observable of in the function that is tested actions filter actio
  • VS 2015编译cocos2d-x 3.3错误“fatal error C1189: #error: MacroDefinition of snprintf与标准库函数声明冲突”

    当我使用Visual Studio 2015编译cocos2d x 版本3 3 时 出现错误 说 致命错误 C1189 error snprintf 的宏定义与标准库函数声明冲突 编译源文件 base s3tc cpp 源代码是 ifdef
  • 使用Delphi消费oData服务建议

    我即将启动一个需要 Delphi XE Windows 32 客户端来使用的项目oData http www odata org 网络服务 我可以使用一些粗略和可读的测试代码正确查询服务 但是编写一个框架来处理 oData 协议 所有过滤
  • 如何使用 Kotlin 修复此错误,找不到 Fragment 构造函数?

    我正在开发Android使用 Kotlin 的应用程序 在我的应用程序中包含Tab with ViewPager所以我实现了两个选项卡 当我移动到另一个活动并压缩到选项卡视图活动时 应用程序将停止并且logcat显示下面的错误 java l
  • 如何使用模板创建排序映射整数索引

    我有数据结构 template
  • 使用 C# 删除换行符

    我从名为 Description 的数据库字段获取一个字符串 它有换行符 它看起来像这样 项目标题 此处为描述 这是项目的描述 我怎样才能删除换行符 我尝试了以下功能 但它不起作用 public string FormatComments
  • 设置div动态填充剩余高度?

    所以 我的代码类似于 div style width 100 min height 1 display block background color 000 img src header image svg div div Some con
  • 在 Python 中检测 NUMLOCK / CAPSLOCK / SCRLOCK 按键/按键

    在我正在开发的游戏中 我想检测NUMLOCK keypress or keyup 就像在按下时注册一个 回调 函数 我并不是要求阅读它state在某一特定时刻 我已经可以做到了 https github com MestreLion pyr
  • 有没有办法在设置类的任何属性时调用方法?

    因此 我想做的是在设置 C 类中的任何属性时调用单个 propertyWasSet 函数 相反 在获取属性时调用 propertyWasGot 我还想知道调用了哪个属性的 get 我想维护一个 设置 属性的字典 并检查 获取 操作是否已设置
  • Angular 库包依赖项

    我使用 CLI 创建并捆绑了一个 Angular 7 2 0 库 ng g 库 MyLibrary ng 构建 MyLibrary 这给了我my libary umd js我需要的捆绑包 目前 所有依赖项都作为peerDependency
  • 如何在同一场战争的多个 jar 中使用相同的 CamelContext

    我使用的是camel 2 16 2 我需要在多个jar 中使用一个 CamelContext 因为我需要将所有 Camel 路由器放入一个 CamelContext 中 所以我的战争将把所有这些罐子作为 Maven 工件 请告诉我如何处理上
  • 设施位置的动态规划算法

    沿着一条线 在位置 a 1 a 2 a n 处有 n 栋房屋 我们希望沿着同一条线设置移动便盆 以便每间房屋都位于至少一个移动便盆的距离 R 内 这些便携式便盆仅限于指定位置 b 1 b 2 b m 令 c i 为在位置 b i 设置移动便
  • openapi-generator 复制 swagger-ui 中的端点

    openapi generator maven plugin 版本 6 3 0 在 Spring Boot 3 应用程序中配置如下
  • 分区比排序更容易吗?

    这是一个在我脑海里徘徊了一段时间的问题 假设我有一个项目列表和它们的等价关系 并且比较两个项目需要恒定的时间 我想退回一部分物品 例如链表的列表 每个链表包含所有等效项 实现此目的的一种方法是将等价性扩展到项目的排序并对其进行排序 使用排序
  • 在无向图中记录 DFS 搜索中的前驱

    我试图使用此线程中的代码 提升DFS back edge https stackoverflow com questions 19346820 boost dfs back edge 19391511 noredirect 1 commen