B Furtado
B Furtado

Reputation: 1500

add_edge in igraph resulting in self-loop instead of new edge

I have two different graphs: g1 and g2. I want do add them together and create some edges. Why this does not work?

import igraph

def edging_up(g1, g2):
    g = g1 + g2
    for v in g.vs:
        for w in g.vs:
            try:
                if v['name'].get_researcher_id() == w['name'].get_tutor_id():
                    g.add_edge(v, w)
            except:
                pass

    return g

I need the try/exception because one graph does not have the get_tutor_id() method. I also tried

def edging_up(g1, g2):
    g = g1 + g2
    for v in g1.vs:
        for w in g2.vs:
            if v['name'].get_researcher_id() == w['name'].get_tutor_id():
                    g.add_edge(v, w)
    return g

Then, it works, but instead of adding an edge from v to w, it adds a self-loop to v.

Upvotes: 0

Views: 678

Answers (1)

Tamás
Tamás

Reputation: 48051

The second example does not work because w is a vertex from g2, and you are trying to add an edge in g. The vertex objects that igraph returns are actually simply proxies to vertex indices , so when you run g.add_edge(v, w), igraph will simply retrieve the index attribute of v and w and then essentially run g.add_edge(v.index, w.index), which does not make sense in the context of g (because the indices refer to g1 and g2, respectively).

As for the first example, you need to provide more information; for instance, what does the name vertex attribute contain?

Upvotes: 1

Related Questions