2015-04-29 2 views
1

Я пытаюсь вручную раскрасить вершины графа, используя boost. Я написал код ниже, но я не могу понять, почему сгенерированный файл не имеет никакого цвета.Ручная окраска графов форсирования

int main(int,char*[]) { 
    typedef property<edge_name_t, string> EdgeProperties; 
    typedef property<vertex_name_t, string, property<vertex_color_t, default_color_type>> VertexProperties; 
    typedef adjacency_list<vecS, vecS, directedS, VertexProperties, EdgeProperties> Graph; 
    typedef graph_traits<Graph>::vertex_descriptor Vertex; 
    typedef graph_traits<Graph>::edge_descriptor Edge; 

    Graph g; 
    property_map<Graph, vertex_name_t>::type vertex_label = get(vertex_name, g); 
    property_map<Graph, vertex_color_t>::type color_map = get(vertex_color, g); 
    property_map<Graph, edge_name_t>::type edge_label = get(edge_name, g); 

    Vertex v1 = add_vertex(g); 
    vertex_label[v1] = "v1"; 

    put(color_map, v1, boost::red_color); 

    std::ofstream outf("example.gv"); 
    write_graphviz(outf, g, 
        make_label_writer(vertex_label), 
        make_label_writer(edge_label) 
        ); 
    return 0; 
} 

ответ

1

Вершинные расцветки - это алгоритмическая деталь. Насколько я знаю, нет никакого «автоматического» положения, чтобы перевести его в graphviz.

Вы можете добавить пользовательские атрибуты.

я обычно изменить его, чтобы использовать динамические свойства:

std::ofstream outf("example.gv"); 

dynamic_properties dp; 
dp.property("node_id", get(vertex_index, g)); 
dp.property("label", vertex_label); 
dp.property("label", edge_label); 

write_graphviz_dp(outf, g, dp); 

Теперь это так просто, как добавление нового атрибута вершины в динамическом наборе:

dp.property("color", make_transform_value_property_map(&color_to_string, color_map)); 

Единственным осложнением является то, что вам необходимо предоставить преобразование от default_color_type к тексту. Это еще один симптом того факта, что цветные карты обычно не используются для репрезентативных целей. Вот полностью рабочий пример:

Live On Coliru

#include <boost/graph/adjacency_list.hpp> 
#include <boost/graph/graphviz.hpp> 
#include <iostream> 
using namespace boost; 

inline char const* color_to_string(default_color_type c) { 
    switch(c) { 
     case default_color_type::red_color: return "red"; 
     case default_color_type::green_color: return "green"; 
     case default_color_type::gray_color: return "gray"; 
     case default_color_type::white_color: return "white"; 
     case default_color_type::black_color: return "black"; 
    } 
    return ""; // not known 
} 


int main() { 
    typedef property<edge_name_t, std::string> EdgeProperties; 
    typedef property<vertex_name_t, std::string, property<vertex_color_t, default_color_type>> VertexProperties; 
    typedef adjacency_list<vecS, vecS, directedS, VertexProperties, EdgeProperties> Graph; 
    typedef graph_traits<Graph>::vertex_descriptor Vertex; 

    Graph g; 
    property_map<Graph, vertex_name_t>::type vertex_label = get(vertex_name, g); 
    property_map<Graph, vertex_color_t>::type color_map = get(vertex_color, g); 
    property_map<Graph, edge_name_t>::type edge_label = get(edge_name, g); 

    Vertex v1 = add_vertex(g); 
    vertex_label[v1] = "v1"; 

    put(color_map, v1, boost::red_color); 

    dynamic_properties dp; 
    dp.property("node_id", get(vertex_index, g)); 
    dp.property("label", vertex_label); 
    dp.property("label", edge_label); 
    dp.property("color", make_transform_value_property_map(&color_to_string, color_map)); 

    write_graphviz_dp(std::cout, g, dp); 
} 

Печать:

digraph G { 
0 [color=red, label=v1]; 
} 
Смежные вопросы