Check if two vertices are connected in iGraph
Asked Answered
J

3

8

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.

Jordanjordana answered 13/12, 2012 at 8:15 Comment(2)
I've not heard of igraph; the standard graph library for Python IME is networkx. It has connected_componentsVase
CONNECTION IS DIFFERENT FROM ADJACENCYHoar
K
13

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.

Kathie answered 13/12, 2012 at 8:41 Comment(6)
@phant0m, why is it so terrible?Kathie
This worked perfectly for what I wanted to do. Mersi Andrei. This is the solution I was looking for.Jordanjordana
@Kathie It's just unusual for a Python API to return error codes.Dashtilut
@phant0m: if error=False is not given, get_eid raises an exception as expected. error=False explicitly requests get_eid to return -1 instead of raising an exception.Enclitic
@Scintillate Interesting... In that case, I'd prefer False or better yet None over -1 though.Dashtilut
@phant0m: the function returns -1 because most of igraph is written in C and the C code behind get_eid (i.e. igraph_get_eid) returns -1. I admit that False or None would be more natural.Enclitic
S
15

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
Scintillate answered 13/12, 2012 at 10:17 Comment(1)
in igraph 0.7.1.post6 this function tells whether the vertices are adjacent, not connected. Just saying that maybe a more proper name could be are_adjacentHoar
K
13

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.

Kathie answered 13/12, 2012 at 8:41 Comment(6)
@phant0m, why is it so terrible?Kathie
This worked perfectly for what I wanted to do. Mersi Andrei. This is the solution I was looking for.Jordanjordana
@Kathie It's just unusual for a Python API to return error codes.Dashtilut
@phant0m: if error=False is not given, get_eid raises an exception as expected. error=False explicitly requests get_eid to return -1 instead of raising an exception.Enclitic
@Scintillate Interesting... In that case, I'd prefer False or better yet None over -1 though.Dashtilut
@phant0m: the function returns -1 because most of igraph is written in C and the C code behind get_eid (i.e. igraph_get_eid) returns -1. I admit that False or None would be more natural.Enclitic
A
0

I have never heard about that module but, anyway, it seems like it is an import problem, try to import that function from the module, i.e.:

from igraph import are_connected

Otherwise, python will not recognize it. Another possibility is that the function must be called from a graph object you have declared first:

from module import MyGraphObject
...
MyGraphObject.are_connected(...)
Albedo answered 13/12, 2012 at 8:52 Comment(1)
I will keep in mind the {from module import MyGraphObject ...} solution because for sure I will run in to many similar problems during my project, but first I did go with the idea of Andrei and it worked easy for me. Thank you anyway!Jordanjordana

© 2022 - 2024 — McMap. All rights reserved.