A more simple way to filter vertices of degree 1 an the property (weight) of their edge

Hello,

Is there an obvious way to collect vertices of degree 1 for reading the
property of their edge?

The following code succeed in doing the job on a tiny scale, but I am sure
this not the way to do:

/import graph_tool.all as gt
#from graph_tool import topology

## Make a graph

T = gt.Graph(directed = False)
edge_weights = T.new_edge_property('double')
T.properties[("e","weight")] = edge_weights

T.add_vertex(n=5)

e_1 = T.add_edge(0,1)
e_2 = T.add_edge(1,2)
e_3 = T.add_edge(0,0)
e_4 = T.add_edge(1,3)

edge_weights[e_1]= 2
edge_weights[e_2]= 6
edge_weights[e_3]= 1
edge_weights[e_4]= 3

## Get the vertices of degree 1 in a dictionary (keys= vertices degree,
values=vertices)

from collections import defaultdict
deg_dic = defaultdict(list)

for v in T.vertices():
    degree = v.out_degree()
    deg_dic[degree].append(v)

## Read the weight of the bounded edge

print 'vertices of degree 1:'
for d1 in deg_dic[1]:
    weight_of_bound_edge = [edge_weights[e] for e in d1.out_edges()]
    print 'vertex',d1,' weight of out edge:', weight_of_bound_edge/

By the way, the objective is to count the vertices of degree 1 as a function
of the pruning steps. Is there sucha a function in graph-tool?

Best regards

Jean-Patrick

Hi Jean-Patrick,
I think a faster way could be using the degree_property_map associated to
the graph:

kp = T.degree_property_map('out')

and filter it using your favorite function (the first that comes to my mind
is. np.equal(kp.a, np.ones(T.num_vertices(),dtype=np.int)) ).
You can then use the resulting array to select the vertices to inspect. I
expect an equality between N integers to be faster than N append.

Best,
Giuseppe

attachment.html (3.24 KB)

Thanks a lot.