|
Boost Users : |
From: John Reid (j.reid_at_[hidden])
Date: 2006-06-01 04:02:47
Hi,
I was trying to build a graph from an edge list and ran into some
problems. The following code throws an exception on write_graphviz(). I
think it is because there's no property map called node_id in the graph.
import boost.graph as bgl
edges = [ ( 'a', 'b' ), ( 'b', 'c' ), ( 'c', 'a' ) ]
g = bgl.Graph( edges )
g.write_graphviz( 'bind-copy.dot' )
I get the following:
Traceback (most recent call last):
File "<stdin>", line 1, in ?
KeyError: 'node_id'
I couldn't manage to generate the node_id property map automatically. So
after you have built the graph with the edge list constructor how do you
know which vertex is which?
I ended up using the following code to create the graph:
def build( edges ):
# Create a graph to build
g = bgl.Graph()
# Construct some property maps
node_ids = g.vertex_properties[ 'node_id' ] = g.vertex_property_map(
'string' )
color = g.vertex_properties[ 'color' ] = g.vertex_property_map( 'string' )
centrality = g.vertex_properties[ 'centrality' ] =
g.vertex_property_map( 'float' )
weight = g.edge_properties[ 'weight' ] = g.edge_property_map( 'float' )
length = g.edge_properties[ 'len' ] = g.edge_property_map( 'float' )
# create the edges and vertices
name_2_vertex = { }
for ( name1, name2 ) in edges:
if not name_2_vertex.has_key( name1 ):
v1 = name_2_vertex[ name1 ] = g.add_vertex()
node_ids[ v1 ] = name1
else:
v1 = name_2_vertex[ name1 ]
if not name_2_vertex.has_key( name2 ):
v2 = name_2_vertex[ name2 ] = g.add_vertex()
node_ids[ v2 ] = name2
else:
v2 = name_2_vertex[ name2 ]
e = g.add_edge( v1, v2 )
weight[ e ] = 0.1
length[ e ] = 4
# Color articulation points red
for v in bgl.biconnected_components( g ):
color[ v ] = 'red'
# Assign centrality values to property map
bgl.brandes_betweenness_centrality( g, centrality )
return g
This code seems to works fine. So I thought I'd post this just to see if
I was missing something obvious and/or in the hope someone else might
find it useful.
Best,
John.
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net