[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [igraph] disjoint_union and vertex attribute

From: Tamás Nepusz
Subject: Re: [igraph] disjoint_union and vertex attribute
Date: Mon, 16 Jun 2014 15:32:07 +0200

> Why graph_union does not have "owner" attribute?
Unfortunately the disjoint_union method does not handle attributes properly 
yet; you can work around this by exploiting the fact that the vertex and edge 
IDs of the first graph will be the same in the union graph as well:

def get_attrs_or_nones(seq, attr_name);
        return seq[attr_name]
    except KeyError:
        return [None] * len(seq)

def better_disjoint_union(g1, g2):
    g = g1+g2
    vertex_attributes = set(g.vertex_attributes() + g2.vertex_attributes())
    edge_attributes = set(g.edge_attributes() + g2.edge_attributes())
    for attr in vertex_attributes:
        g.vs[attr] = get_attrs_or_nones(g1.vs, attr) + 
get_attrs_or_nones(g2.vs, attr)
    for attr in edge_attributes:
        g.es[attr] = get_attrs_or_nones(g1.es, attr) + 
get_attrs_or_nones(g2.es, attr)
    return g

(Untested, but shows the general idea: if g1 has N vertices and M edges, then 
the first N vertices and the first M edges in the union will belong to g1 in 
the same order as in g1, and the rest belongs to g2 in the same order as in g2).

All the best,

reply via email to

[Prev in Thread] Current Thread [Next in Thread]