一张图可能有多个边缘权重属性图吗? [英] Is it possible to have several edge weight property maps for one graph?

查看:286
本文介绍了一张图可能有多个边缘权重属性图吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将如何创建一个图,以使每个属性图中的属性图(边的权重)不同?是否可以创建这样的属性图? 像一系列的属性图一样?

How would I create a graph, such that the property map (weight of edges) is different in each property map? Is it possible to create such a property map? Like an array of property maps?

我还没有看到互联网上有人使用它,我能举个例子吗?

I have not seen anyone on the Internet using it, could I have an example?

Graph g(10); // graph with 10 nodes
cin>>a>>b>>weight1>>weight2>>weight3>>weight4;

并将每个权重放在属性图中.

and put each weight in a property map.

推荐答案

您可以通过多种方式来组成属性映射.最简单的方法似乎是这样的:

You can compose a property map in various ways. The simplest approach would seem something like:

在Coliru上直播

#include <boost/property_map/function_property_map.hpp>
#include <iostream>

struct weights_t {
    float weight1, weight2, weight3, weight4;
};

using namespace boost;

int main() {
    std::vector<weights_t> weight_data { // index is vertex id
        { 1,2,3,4 },
        { 5,6,7,8 },
        { 9,10,11,12 },
        { 13,14,15,16 }, 
    };

    auto wmap1 = make_function_property_map<unsigned, float>([&weight_data](unsigned vertex_id) { return weight_data.at(vertex_id).weight1; });
    auto wmap2 = make_function_property_map<unsigned, float>([&weight_data](unsigned vertex_id) { return weight_data.at(vertex_id).weight2; });
    auto wmap3 = make_function_property_map<unsigned, float>([&weight_data](unsigned vertex_id) { return weight_data.at(vertex_id).weight3; });
    auto wmap4 = make_function_property_map<unsigned, float>([&weight_data](unsigned vertex_id) { return weight_data.at(vertex_id).weight4; });

    for (unsigned vertex = 0; vertex < weight_data.size(); ++vertex)
        std::cout << wmap1[vertex] << "\t" << wmap2[vertex] << "\t" << wmap3[vertex] << "\t"<< wmap4[vertex] << "\n";
}

将C ++ 03与transform_value_property_map

一起使用

这主要是冗长的:

Using C++03 with transform_value_property_map

This is mainly much more verbose:

在Coliru上直播

#include <boost/property_map/transform_value_property_map.hpp>
#include <iostream>

struct weights_t {
    float weight1, weight2, weight3, weight4;

    weights_t(float w1, float w2, float w3, float w4)
        : weight1(w1), weight2(w2), weight3(w3), weight4(w4)
    { }

    template <int which> struct access {
        typedef float result_type;

        float operator()(weights_t const& w) const { 
            BOOST_STATIC_ASSERT(which >= 1 && which <= 4);
            switch (which) {
                case 1: return w.weight1;
                case 2: return w.weight2;
                case 3: return w.weight3;
                case 4: return w.weight4;
            }
        }
    };
};

using namespace boost;

int main() {
    std::vector<weights_t> weight_data; // index is vertex id
    weight_data.push_back(weights_t(1,2,3,4));
    weight_data.push_back(weights_t(5,6,7,8));
    weight_data.push_back(weights_t(9,10,11,12));
    weight_data.push_back(weights_t(13,14,15,16));

    boost::transform_value_property_map<weights_t::access<1>, weights_t*, float> wmap1 = make_transform_value_property_map(weights_t::access<1>(), &weight_data[0]);
    boost::transform_value_property_map<weights_t::access<2>, weights_t*, float> wmap2 = make_transform_value_property_map(weights_t::access<2>(), &weight_data[0]);
    boost::transform_value_property_map<weights_t::access<3>, weights_t*, float> wmap3 = make_transform_value_property_map(weights_t::access<3>(), &weight_data[0]);
    boost::transform_value_property_map<weights_t::access<4>, weights_t*, float> wmap4 = make_transform_value_property_map(weights_t::access<4>(), &weight_data[0]);

    for (unsigned vertex = 0; vertex < weight_data.size(); ++vertex)
        std::cout << wmap1[vertex] << "\t" << wmap2[vertex] << "\t" << wmap3[vertex] << "\t"<< wmap4[vertex] << "\n";
}

输出

两个示例输出

Output

Both samples output

1   2   3   4
5   6   7   8
9   10  11  12
13  14  15  16

这篇关于一张图可能有多个边缘权重属性图吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆