[Top][All Lists]

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

Re: [igraph] Check for duplicate nodes when adding new nodes

From: Tamás Nepusz
Subject: Re: [igraph] Check for duplicate nodes when adding new nodes
Date: Mon, 2 Jun 2014 21:40:19 +0200


igraph does not allow duplicate nodes either, the difference is that igraph 
nodes are identified by a numeric ID and not by a string label ;) (Otherwise it 
would be impossible to import graphs from GraphML files containing two nodes 
with the same name). But anyway, g.vs.find("X") tells you whether there exists 
a node with name="X" or not (if not, it throws an exception).

However, depending on what you really want to do, this might not be the best 
solution at all. You mentioned that you are "building a graph". igraph's data 
structures are optimized for static graphs, so add_vertex() and add_edge() is a 
relatively expensive operation (since igraph rebuilds a bunch of internal 
indexes after each addition). In 99% of the cases you are better off with 
building an edgelist in advance and then invoking the Graph() constructor:

iterable = 
idgen = UniqueIdGenerator()
edgelist = [(idgen[source], idgen[target]) for source, target in iterable]
g = Graph(edgelist)
g.vs["name"] = idgen.values()

Alternatively, you can use the Graph.DictList() method:

g = Graph.DictList(edges=(dict(source=source, target=target) for source, target 
in iterable), vertices=None)


From: Krishna Sandeep Reddy Dubba address@hidden
Reply: Help for igraph users address@hidden
Date: 2 June 2014 at 16:12:12
To: Help for igraph users address@hidden
Subject:  [igraph] Check for duplicate nodes when adding new nodes

> Hi all,
> Recently I was trying to use igraph for building a graph. I first started
> with networkx where I was adding nodes which have a name associated with
> them. networkx nicely avoids duplicate nodes but igraph does not.
> # Session with networkx graph
> import networkx as nx
> graph = nx.DiGraph()
> graph.add_node('A')
> graph.nodes()
> Out[5]: ['A']
> graph.add_node('A')
> graph.nodes()
> Out[7]: ['A']
> So networkx does not allow duplicate nodes. This is not the case with
> igraph (igraph uses the attribute 'name' to store the node label, but does
> not do duplicate checking on this attribute). Because of this in my case,
> igraph is slower than networkx! Is there a way I can do the same thing in
> igraph as networkx without maintaining a separate datastructure checking in
> it if I have added it to the igraph before adding a node?
> Thanks,
> Krishna Dubba
> _______________________________________________
> igraph-help mailing list
> address@hidden
> https://lists.nongnu.org/mailman/listinfo/igraph-help

reply via email to

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