[Boost-users] BGL: Write graphviz file with invisible edges

74 views
Skip to first unread message

David Doria

unread,
Jun 21, 2011, 11:27:36 AM6/21/11
to boost...@lists.boost.org
I found out that if you write a graph (.dot) file like this:

graph G {
0;
1;
2;
0--1;
1--2 [style=invis];
}

graphviz will layout the graph as if it has two edges (0--1, and
1--2), but not display the edge between vertices 1 and 2. This is
useful when trying to keep the vertices stationary and show changes to
the connectivity. How would I setup the graph in BGL so I could mark
particular edges so they will be written with [style=invis]?

Thanks,

David
_______________________________________________
Boost-users mailing list
Boost...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Jeremiah Willcock

unread,
Jun 21, 2011, 12:48:59 PM6/21/11
to boost...@lists.boost.org
On Tue, 21 Jun 2011, David Doria wrote:

> I found out that if you write a graph (.dot) file like this:
>
> graph G {
> 0;
> 1;
> 2;
> 0--1;
> 1--2 [style=invis];
> }
>
> graphviz will layout the graph as if it has two edges (0--1, and
> 1--2), but not display the edge between vertices 1 and 2. This is
> useful when trying to keep the vertices stationary and show changes to
> the connectivity. How would I setup the graph in BGL so I could mark
> particular edges so they will be written with [style=invis]?

The strategy for doing that is to create a dynamic_properties object, add
an entry to it for the "style" edge property and some property map, and
then pass that to write_graphviz_dp. An example of what you want to do is
in libs/graph/example/graphviz.cpp; see how the "weight" property is
handled. In your case, the value type of the property would be
std::string.

-- Jeremiah Willcock

David Doria

unread,
Jun 21, 2011, 2:05:37 PM6/21/11
to boost...@lists.boost.org
> The strategy for doing that is to create a dynamic_properties object, add an
> entry to it for the "style" edge property and some property map, and then
> pass that to write_graphviz_dp.  An example of what you want to do is in
> libs/graph/example/graphviz.cpp; see how the "weight" property is handled.
>  In your case, the value type of the property would be std::string.
>
> -- Jeremiah Willcock

Thanks Jeremiah,

Here is what I tried:

#include <iostream>
#include <string>

#include <boost/graph/undirected_graph.hpp>
#include <boost/graph/graphviz.hpp>

struct StyleProperty
{
std::string style;
}

typedef boost::undirected_graph<boost::no_property, StyleProperty> Graph;

int main()
{
Graph g;
Graph::vertex_descriptor v0 = g.add_vertex();
Graph::vertex_descriptor v1 = g.add_vertex();
Graph::vertex_descriptor v2 = g.add_vertex();

StyleProperty styleInvisible;
styleInvisible.style = "invis";

StyleProperty styleNormal;
styleNormal = "normal";

boost::add_edge(v0,v1,styleInvisible, g);
boost::add_edge(v1,v2,styleNormal g);

boost::dynamic_properties dp;
dp.property("style", get(&StyleProperty::style, g));

boost::write_graphviz_dp(std::cout, g, dp, "style");
return 0;
}

However, I get an exception "dynamic property get cannot retrieve
value for property: style."

Can you see where I have gone wrong?

Thanks,

David

Jeremiah Willcock

unread,
Jun 21, 2011, 2:08:39 PM6/21/11
to boost...@lists.boost.org
On Tue, 21 Jun 2011, David Doria wrote:

The last argument here shouldn't be "style" -- it should be the
default "node_id" or something else that can be used to identify vertices
in the graph.

> return 0;
> }
>
> However, I get an exception "dynamic property get cannot retrieve
> value for property: style."

I'm not sure what the issue is for that -- try changing the call to
write_graphviz_dp and see if it recurs.

-- Jeremiah Willcock

David Doria

unread,
Jun 21, 2011, 2:12:01 PM6/21/11
to boost...@lists.boost.org
>> boost::write_graphviz_dp(std::cout, g, dp, "style");
>
> The last argument here shouldn't be "style" -- it should be the default
> "node_id" or something else that can be used to identify vertices in the
> graph.
>
>> return 0;
>> }
>>
>> However, I get an exception "dynamic property get cannot retrieve
>> value for property: style."
>
> I'm not sure what the issue is for that -- try changing the call to
> write_graphviz_dp and see if it recurs.
>
> -- Jeremiah Willcock
> _______________________________________________
> Boost-users mailing list
> Boost...@lists.boost.org
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>

I tried both:
boost::write_graphviz_dp(std::cout, g, dp, "node_id");
and
boost::write_graphviz_dp(std::cout, g, dp);

and both produce the exception "dynamic property cannot retrieve value
for property: "node_id".

Jeremiah Willcock

unread,
Jun 21, 2011, 2:46:15 PM6/21/11
to boost...@lists.boost.org
On Tue, 21 Jun 2011, David Doria wrote:

>>> boost::write_graphviz_dp(std::cout, g, dp, "style");
>>
>> The last argument here shouldn't be "style" -- it should be the default
>> "node_id" or something else that can be used to identify vertices in the
>> graph.
>>
>>> return 0;
>>> }
>>>
>>> However, I get an exception "dynamic property get cannot retrieve
>>> value for property: style."
>>
>> I'm not sure what the issue is for that -- try changing the call to
>> write_graphviz_dp and see if it recurs.
>>
>> -- Jeremiah Willcock
>> _______________________________________________
>> Boost-users mailing list
>> Boost...@lists.boost.org
>> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>>
>
> I tried both:
> boost::write_graphviz_dp(std::cout, g, dp, "node_id");
> and
> boost::write_graphviz_dp(std::cout, g, dp);
>
> and both produce the exception "dynamic property cannot retrieve value
> for property: "node_id".

You will also need dp.property("node_id", get(vertex_index, g)); before
the write_graphviz_dp call.

David Doria

unread,
Jun 21, 2011, 2:55:36 PM6/21/11
to boost...@lists.boost.org
> You will also need dp.property("node_id", get(vertex_index, g)); before the
> write_graphviz_dp call.

Awesome, works like a charm. Thanks for your help!

David

David Doria

unread,
Jun 21, 2011, 9:03:28 PM6/21/11
to boost...@lists.boost.org
On Tue, Jun 21, 2011 at 2:55 PM, David Doria <david...@gmail.com> wrote:
>> You will also need dp.property("node_id", get(vertex_index, g)); before the
>> write_graphviz_dp call.
>
> Awesome, works like a charm. Thanks for your help!
>
> David
>

Here is the full example for future readers:
http://programmingexamples.net/index.php?title=CPP/Boost/BGL/InvisibleEdges

Reply all
Reply to author
Forward
0 new messages