【问题标题】:Generating random graphs with negative edge weight, and without negative cycle生成具有负边权重且没有负循环的随机图
【发布时间】:2023-03-16 21:45:01
【问题描述】:

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


注意:在this post 中,他们使用boost 库生成没有这些条件的图形。

【问题讨论】:

  • 对我来说,这听起来更像是一个算法问题,而不是一个实际的编程问题,所以可能有其他 SE 网站可以比 SO 更好地帮助你。
  • @BaummitAugen 谢谢。问题已编辑。

标签: c++ boost graph


【解决方案1】:

我建议使用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
}

【讨论】:

  • 谢谢。你会用this post 完成你的答案(comment 部分)吗?
  • 如果您尝试过,我会更喜欢(这是正确的方法)。我没有太多时间(我只是发布了骨架,因为它看起来比你在问题中链接的东西简单得多)。如果您遇到困难,您可以发布一个新问题,我一定会找到的。
猜你喜欢
  • 2018-01-27
  • 2017-10-17
  • 2016-08-07
  • 1970-01-01
  • 2013-10-15
  • 1970-01-01
  • 1970-01-01
  • 2013-05-26
  • 1970-01-01
相关资源
最近更新 更多