Adding edge weights and scaling the length of elongated edges in the graph

I am using the graph-tool and I cannot find a way to determine the weights for the edges. How to add weight to the graph?

In addition, I would like that when using graph_draw graph was plotted with boundary distances in accordance with the weight. How can i do this?

+5
source share
1 answer

You are looking for Property Maps . From the docs:

Property maps are a way of associating additional information with vertices, edges, or the graph itself. Thus, there are three types of property maps: vertex, edge, and graph. All of them are handled by the same PropertyMap class.

Property maps are much more flexible than simple weights, because you can attach arbitrary objects to the vertices and edges of the graph, but using them to snap edges to floating point or integer values ​​can achieve the same effect.

Example code from the documentation:

 from itertools import izip from numpy.random import randint g = Graph() g.add_vertex(100) # insert some random links for s,t in izip(randint(0, 100, 100), randint(0, 100, 100)): g.add_edge(g.vertex(s), g.vertex(t)) vprop_double = g.new_vertex_property("double") # Double-precision floating point vprop_double[g.vertex(10)] = 3.1416 vprop_vint = g.new_vertex_property("vector<int>") # Vector of ints vprop_vint[g.vertex(40)] = [1, 3, 42, 54] eprop_dict = g.new_edge_property("object") # Arbitrary python object. eprop_dict[g.edges().next()] = {"foo": "bar", "gnu": 42} # In this case, a dict. gprop_bool = g.new_edge_property("bool") # Boolean gprop_bool[g] = True 

For the second part of your question, here is a graphical utility for drawing and layout documentation , which contains several different algorithms that you can use to display a graph instead of of graph_draw . Take a look at algorithms that take an edge property map as an input argument. I have not used them before, but it seems that with the correct layout of the layout make up the weight map, when you create the layout object, you need to take care of scaling the edge length for you.

+7
source

Source: https://habr.com/ru/post/1215704/


All Articles