Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if two vertices are connected in iGraph

Is there a very short expression in iGraph 0.6 for python 2.7 to see if two vertices specified by index are connected by an edge or not?

I found somewhere:

are_connected(v1, v2)  

but in python I would get an error message: "NameError: global name 'are_connected' is not defined"

The above expression could be for R or just totally wrong. I don't know. R is not enough for what I'm trying to do with my project.

My graph is undirected and has many sequences of vertices and edges (vs and es) described in this tutorial: http://hal.elte.hu/~nepusz/development/igraph/tutorial/tutorial.html

Update: I've found http://packages.python.org/python-igraph/igraph.GraphBase-class.html#is_multiple is_multiple and is_mutual and I think each of them could do the trick, yet I still get the error: "NameError: global name 'are_mutual' is not defined".

On the internet I couldn't find an example of how to implement it correctly. I'm still looking.

like image 521
Laci Avatar asked Dec 13 '12 08:12

Laci


2 Answers

For the record: are_connected (and also is_mutual and is_multiple that the poster has mentioned) are methods of the graph itself and not functions on their own, so the correct way to use them is as follows:

>>> g = Graph.GRG(100, 0.2)
>>> g.are_connected(0, 2)
False
like image 139
Tamás Avatar answered Sep 28 '22 03:09

Tamás


GraphBase class has function get_eid(v1, v2, directed=True, error=True) that returns arbitrary edge between vertices specified by their indices. In you call it like this:

g.get_eid(v1, v2, directed=False, error=False)

it will return -1 if the vertices are disconnected, and some edge otherwise.

like image 39
Andrei Avatar answered Sep 28 '22 03:09

Andrei