C++ Fruchterman Reingold的吸引力如何与Boost图形库一起工作

C++ Fruchterman Reingold的吸引力如何与Boost图形库一起工作,c++,algorithm,boost,graph,boost-property-map,C++,Algorithm,Boost,Graph,Boost Property Map,我正在学习Boost图形库中的Fruchterman-Reingold算法。通过阅读文档,我知道算法是根据图形布局计算所有节点的位置,但我的问题是我无法理解Boost图形库中吸引力的计算步骤 例如,如果拓扑是高度为100、宽度为100的矩形,则每个顶点都标记为字符串,每个对顶点之间的关系为: "0" "5" "Kevin" "Martin" "Ryan" "Leo" "Y" "S" "Kevin" "S" "American" "USA" 每行表示两个标记的顶点是连接的。假设每个顶点的吸引力

我正在学习Boost图形库中的Fruchterman-Reingold算法。通过阅读文档,我知道算法是根据图形布局计算所有节点的位置,但我的问题是我无法理解Boost图形库中吸引力的计算步骤

例如,如果拓扑是高度为100、宽度为100的矩形,则每个顶点都标记为字符串,每个对顶点之间的关系为:

"0"  "5"
"Kevin" "Martin"
"Ryan" "Leo"
"Y" "S"
"Kevin" "S"
"American" "USA"
每行表示两个标记的顶点是连接的。假设每个顶点的吸引力公式为:

f = (d^2) / k

其中,
d
是两个顶点之间的距离,
k
是最佳距离。但是我不知道如何在Boost图形库中的Fruchterman Reingold代码中获得距离
d
。在本例中,它是否将每对顶点之间的ASCII值差计算为距离
d
?(ASCII值“0”是48,ASCII值“5”是53。Fruchterman Reingold在BGL中计算53-48=5作为d是真的吗?)如果有人能帮助我,我真的很感激。

Furchterman Reingold实现采用了in/OUT拓扑

它期望拓扑在执行之前被初始化为某种状态。传递到吸引函数的距离将是该迭代中拓扑的距离

注意注意(除非
progressive
设置为
true
),Furterman Reingold将在默认情况下随机初始化拓扑(使用)

以上所有内容均取自

下面是一个使用输入图的小演示,展示了如何实现如此吸引人的力函数:

struct AttractionF {
    template <typename EdgeDescriptor, typename Graph>
        double operator()(EdgeDescriptor /*ed*/, double k, double d, Graph const& /*g*/) const {
            //std::cout << "DEBUG af('" << g[source(ed, g)].name << " -> " << g[target(ed, g)].name << "; k:" << k << "; d:" << d << ")\n";
            return (d*d/k);
        }
};
#include <memory>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/fruchterman_reingold.hpp>
#include <boost/graph/random_layout.hpp>
#include <libs/graph/src/read_graphviz_new.cpp>
#include <boost/graph/topology.hpp>
#include <boost/random.hpp>

using namespace boost;

struct Vertex {
    std::string name;
};

struct AttractionF {
    template <typename EdgeDescriptor, typename Graph>
        double operator()(EdgeDescriptor /*ed*/, double k, double d, Graph const& /*g*/) const {
            //std::cout << "DEBUG af('" << g[source(ed, g)].name << " -> " << g[target(ed, g)].name << "; k:" << k << "; d:" << d << ")\n";
            return (d*d/k);
        }
};
using Graph = adjacency_list<vecS, vecS, undirectedS, Vertex>;

Graph make_sample();

int main() {
    auto g = make_sample();

    using Topology = square_topology<boost::mt19937>;
    using Position = Topology::point_type;

    std::vector<Position> positions(num_vertices(g));
    square_topology<boost::mt19937> topology;

    random_graph_layout(g, 
                make_iterator_property_map(positions.begin(), boost::identity_property_map{}),
                topology);

    fruchterman_reingold_force_directed_layout(
                g,
                make_iterator_property_map(positions.begin(), boost::identity_property_map{}),
                topology,
                attractive_force(AttractionF())
            );

    dynamic_properties dp;
    dp.property("node_id", get(&Vertex::name, g));
    write_graphviz_dp(std::cout, g, dp);
}

Graph make_sample() {
    std::string const sample_dot = R"(
        graph {
            "0"        -- "5";
            "Kevin"    -- "Martin";
            "Ryan"     -- "Leo";
            "Y"        -- "S";
            "Kevin"    -- "S";
            "American" -- "USA";
        }
    )";
    Graph g;

    dynamic_properties dp;
    dp.property("node_id", get(&Vertex::name, g));

    read_graphviz(sample_dot, g, dp);

    return g;
}
fruchterman_reingold_force_directed_layout(
            g,
            make_iterator_property_map(positions.begin(), boost::identity_property_map{}),
            topology,
            attractive_force([](Graph::edge_descriptor, double k, double d, Graph const&) { return (d*d)/k; })
        );