Hi folks,
I would be glad for any suggestion about how to build an iterator, which yields pairs of adjacent (connected) nodes? The same as it’s printed in summary(graph)
Hi folks,
I would be glad for any suggestion about how to build an iterator, which yields pairs of adjacent (connected) nodes? The same as it’s printed in summary(graph)
You can simply iterate over the connected pairs of vertices by iterating over the es
attribute of a Graph
object:
for edge in graph.es:
print(edge.source, edge.target)
Use help(EdgeSeq)
to find out more about the edge sequence object and help(Edge)
to read more about the individual properties of edges.
Thank you @tamas. Could you please say how complex graph.es.select(_source, _target) or graph.es.select(between=) query?
_source
, _target
and _between
use the incident()
method of the graph to query the edges incident on a particular vertex. Due to the indexed edge list data structure that the igraph library uses internally, a single call to incident()
should run in constant time, therefore the time needed to execute a _source
, _target
or _between
query should be proportional to the number of vertices involved, plus the complexity of the set intersection or union operations, which rely on standard Python data structures so their time complexity is ultimately up to the Python implementation that you use (but they should be heavily optimized).
However, if you are looking for a single particular edge between two vertices, you are even better off with using graph.get_eid(source, target)
, which returns the numeric edge ID, and then you can use that to index into graph.es
:
graph.es[graph.get_eid(source, target)]
I found that the fastest way to collect edges of graph is following:
import numpy as np
index = np.fromiter(chain(*graph.get_edgelist()), np.dtype('i'), count=graph.ecount())
edges = index.reshape(-1, 2) # or index.reshape(-1, 2).tolist()
It is equivalent of np.array([n.tuple for n in graph.es])
or np.array([(edge.source, edge.target) for n in graph.es])
You can find more info in my draft, diagram number 4.