生成具有负边权重且无负循环的随机图

Generating random graphs with negative edge weight, and without negative cycle

本文关键字:循环 随机 权重      更新时间:2023-10-16

负循环中边权重的总和为负。有了这个概念,有没有办法生成具有随机正边和负边权重且没有负循环的图形?此类图形对于测试bellman_ford_shortest_paths方法很有用。


注意:在这篇文章中,他们使用库生成没有这些条件boost图形。

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

例如:

住在科里鲁

#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
}