c++ - 生成具有负边权重且没有负循环的随机图

标签 c++ boost graph

负循环中的边权重之和为负。有了这个概念,有没有什么方法可以生成随机正负边权,并且没有负循环的图呢?这些图表对于测试 bellman_ford_shortest_paths 很有用。方法。


注:在this post他们使用 boost 库在没有这些条件的情况下生成图表。

最佳答案

我建议使用 generate_random_graph 来……生成一个随机图并修复任何负循环作为后处理步骤。

例如:

Live On Coliru

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/random.hpp>
#include <random>
#include <iostream>

using Graph = boost::adjacency_list<
    boost::vecS,
    boost::vecS,
    boost::directedS, 
    boost::no_property,
    boost::property<boost::edge_weight_t, double> >;

int main()
{
    std::mt19937 prng;
    Graph g;
    generate_random_graph(g, 100, 200, prng);

    // find cycles with negative sum and just add a large enough value to one
    // of the participating edges to make it postive
}

关于c++ - 生成具有负边权重且没有负循环的随机图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35958534/

相关文章:

c++ - 我应该在C++中使用哪种工具来创建简单的RISC-V反汇编程序?

c++ - 为什么 shared_ptr 有显式构造函数

c++ - dijkstra 算法的 c++ 程序中的段错误

algorithm - 哈密​​顿路径生成器算法

javascript - Chart Js使用图形线条样式更新图形的图例框

c++ - 为什么无序容器不提供定义最小负载因子的接口(interface)?

c++ - 有没有办法创建一个公共(public)输出流对象以在控制台上打印并打印到 C++ 中的文件?

c++ - strlen() 编译时优化

c++ - Boost asio async_write 回调不会被调用

c++ - boost 日期时间库的夏令时和其他时区相关问题