# Re: [graph-tool] Find minimum value of edge property for all edges connected to a given node

On 16.09.2016 16:22, Alexandre Hannud Abdo wrote:
> Don't think there's a boxed method for this task, [...]

In this particular case, in fact there is:

g.vp.year = incident_edges_op(g, "out", "min", g.ep.year)

Here are the docs:

https://graph-tool.skewed.de/static/doc/graph_tool.html#graph_tool.incident_edges_op

> Two possible ways to improve it:
>
> * you could use the fact that the min() function in Python takes an iterable
> as argument, and pass it something like the generator below:
>
> # chain must be imported from itertools
> for v in g.vertices():
>     g.vp.year[v] = min( g.ep.year[e] for e in chain(v.out_edges(),
> v.in_edges()) )

I think it would be better to use v.all_edges() instead of chain(), but
chain() is a good general tip to have in mind.

> * you could iterate only once over g.edges() and then, update the g.vp.year
> of source and target for each edge.
>
> I would go with something like this:
>
> # this assumes g.vp.year was initlized to a large value by passing 'val' to
> new_vertex_property()
> for e in g.edges():
>     for v in (e.source(), e.target()):
>         if g.vp.year[v] > g.ep.year[e]:
>             g.vp.year[v] = g.ep.year[e]

Definitely the most elegant. The only reason why "incident_edges_op()"
is not implemented like this in C++ is because doing it like your first
option can be parallelized. But this is a moot point in Python.

Best,
Tiago

--
Tiago de Paula Peixoto <ti...@skewed.de>
_______________________________________________
graph-tool mailing list
graph-tool@skewed.de
https://lists.skewed.de/mailman/listinfo/graph-tool