I am looping through the edges of a graph with:
for es in graph.es:
....
# v = []
# v = es.vertices()?
...
What method can I use to get the source and the target vertices for each edge?
First, you will need to install python-igraph if you do not have it already installed. You can use pip. You will also need to install cairocffi to plot the graphs. If you are using a Python package manager such as Anaconda or Miniconda, you can install python-igraph using the conda install command.
Start from a random source vertex, and then calculate the minimum weight from that vertex to all others. Then, one by one go to every vertex that the current vertex isn't connect to, and connect them with the minimum weight you calculated. Repeat this same process for all the other nodes.
These are the very basic functionalities of igraph, described here thoroughly.
If you iterate the <EdgeSeq>
object (graph.es
), you will go through all <Edge>
objects (here edge
). <Edge>
has properties source
and target
. These are vertex ids, simply integers. You can get the corresponding <Vertex>
object by graph.vs[]
:
for edge in graph.es:
source_vertex_id = edge.source
target_vertex_id = edge.target
source_vertex = graph.vs[source_vertex_id]
target_vertex = graph.vs[target_vertex_id]
# using get_eid() you can do the opposite:
same_edge_id = graph.get_eid(source_vertex_id, target_vertex_id)
same_edge = graph.es[same_edge_id]
# by .index you get the id from the Vertex or Edge object:
source_vertex.index == source_vertex_id
# True
edge.index == same_edge_id
# True
Be aware if you have directed graph, otherwise source and target are simply two equivalent endpoints. With directed graphs you may use error = False
with get_eid()
, which then returns -1
in case there is no edge in the given direction between the vertices.
Here is one simple way to get edge information from an igraph object using built-in function get.data.frame()
in R (I did not notice the question was asking about python, sorry):
edges_data_frame <- get.data.frame(your_igraph, what = "edges")
The first two columns of edges_data_frame
will be "from" and "to". The following columns will be other attributes of the edges if there are.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With