0

I've got a graph with edge weights. I looked around and found that I can use edge_properties to represent an edge weight. I do it like this:

edge_weight = g.new_edge_property("double")

for i in range(10):
    e = g.add_edge(i, i+1)
    edge_weight[e] = i

Now I want to print a graph from this with the given edge weights on the edges. Do you have any ideas how to do this? The only thing that I could come up is this:

edge_weight = g.new_edge_property("double")
edge_str_weight = g.new_edge_property("string")

for i in range(10):
    e = g.add_edge(i, i+1)
    edge_weight[e] = i
    edge_str_weight[e] = str(i)

graph_draw(g, edge_text=edge_str_weight, output="out.png")

It works, but it's quite redundant. Also if it's suggested to store the edge weight in an other structure or something, feel free to comment :)

Bence Gedai
  • 1,461
  • 2
  • 13
  • 25

2 Answers2

1

Maybe it's a typo but the assignment to edge_str_weight should reference the edge e you are currently working with:

edge_str_weight[e] = str(i)

Other than that, working with property maps is generally the best option with graph-tool. If for some reason you want to use a one-time property-map just for plotting purposes, you will again need to create one:

edge_alt_str_weights = g.new_edge_property("string")
for edge in g.edges():
    edge_alt_str_weights[edge] = str(edge_weight[edge])

You might also want to define the property maps you plan to keep around as internal in case you want to use them persistently.

Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253
  • Yes, it was just a typo. I didn't think of building up edge_str_weight in a way like you suggested, but it looks cool. I'll mark your answer as accepted if no one else can help. – Bence Gedai Sep 24 '15 at 21:03
  • If for some reason you **def** don't want to keep the string version of your weights around, just create a property map on the fly ***and don't internalize it***, you should internalize the rest though. At least, this is how I would do it :-) – Dimitris Fasarakis Hilliard Sep 24 '15 at 21:09
1

In principle, there is no need create a different property, since a conversion to string will be made inside graph_draw(). However, graph-tool uses hexadecimal float notation by default, because it allows for a perfect representation. This is ideal for storing the values in a file, but not for displaying them. Therefore your approach is correct. You can perhaps do it more succinctly and efficiently using map_property_values():

label = g.new_edge_property()
map_property_values(edge_weight, label, lambda w: str(w))

graph_draw(g, edge_text=label, output="out.png"))
Tiago Peixoto
  • 5,149
  • 2
  • 28
  • 28