igraph-help
[Top][All Lists]

## RE: [igraph] union of graphs

 From: venura.2.mendis Subject: RE: [igraph] union of graphs Date: Mon, 13 Oct 2008 17:13:22 +0100

```Hi Marco,

If I understand you correctly you are trying to do something similar to what I
was asking about earlier. There is an igraph.union() method but this doesn't
let you take union based on node attributes.
The other guys on the list can probably tell you more efficient ways of doing
this but thought it might be helpful if I sent you some of my code as I think
ive just got it working!
I am taking the union of graphs based on the 'label' attribute on the nodes (I
gues you could consider that my selection rule). i.e I consider a node equal if
they have same label. Also I'm adding the 'weight' attributes of the edges.
Hopefully you should be ok modifying this to use the selection rule you refer
too.

@staticmethod
def mergeGraphs(graph, graph1):
nodeList1 = set(node['label'] for node in graph.vs)
nodeList2 = set(node['label'] for node in graph1.vs)

mergedNodeList = nodeList2.union(nodeList1)
mergedEdgeDict=dict()

for nodeLabel in mergedNodeList:
if nodeLabel in graph.vs['label']:
for edge in edges:
if graph.es[edge].source> graph.es[edge].target:
edgeTuple =
(graph.vs[graph.es[edge].target]['label'],graph.vs[graph.es[edge].source]['label'])
else:
edgeTuple
=(graph.vs[graph.es[edge].source]['label'],graph.vs[graph.es[edge].target]['label'])

if(edgeTuple not in mergedEdgeDict.keys()):

mergedEdgeDict.setdefault(edgeTuple,graph.es[edge]['weight'])
else:
mergedEdgeDict[edgeTuple] =
mergedEdgeDict[edgeTuple]+graph.es[edge]['weight']

if nodeLabel in graph1.vs['label']:
for edge in edges:
if graph1.es[edge].source> graph1.es[edge].target:
edgeTuple
=(graph1.vs[graph1.es[edge].target]['label'],graph1.vs[graph1.es[edge].source]['label'])
else:
edgeTuple
=(graph1.vs[graph1.es[edge].source]['label'],graph1.vs[graph1.es[edge].target]['label'])

if(edgeTuple not in mergedEdgeDict.keys()):

mergedEdgeDict.setdefault(edgeTuple,graph1.es[edge]['weight'])
else:
mergedEdgeDict[edgeTuple] =
mergedEdgeDict[edgeTuple]+graph1.es[edge]['weight']

graph3 = igraph.Graph(len(mergedNodeList),directed=False)
for ind, nodeLabel in enumerate(mergedNodeList):
graph3.vs[ind]['label'] = str(nodeLabel)

for indx, edge in enumerate(mergedEdgeDict):

)
graph3.es[indx]['weight'] = mergedEdgeDict[edge]/2

return graph3

Hope this is useful.
Thanks,
Venura

-----Original Message-----
Sent: 13 October 2008 16:59
To: Help for igraph users
Subject: [igraph] union of graphs

Hi,

let's suppose i have n graphs, which are randomly generated, with
igraph.Graph.Erdos_Renyi.
Now, let's suppose i want to make an union of m of those graphs, with
some selection rule.
Basically i want to take a node in the graph 1 and connect it to a
node in the graph 2, then again for every couple of graphs in set of n
graphs.

Suggestions on how i can do it with igraph (i am using the python frontend)?

Thanks in advance for you help,

marco

--

è il gioco della vita,
la dobbiamo preparare
che non ci sfugga dalle dita
come la sabbia in riva al mare.

Lucio Dalla

_______________________________________________
igraph-help mailing list