igraph-help
[Top][All Lists]

## Re: [igraph] connecting disparate graphs

 From: Tamas Nepusz Subject: Re: [igraph] connecting disparate graphs Date: Tue, 27 May 2008 15:43:39 +0200

```Hi Kurt,

```
for example. i want to merge G1 and G2 into one big graph. However, the web crawls are seperated by time, so the edges incident to G1.vs[0] /= G2.vs[49] necessarily. So i figure i just want to include all connections for both graphs into one new node
```
Anyway, is there any igraph functions that might help????
```
Graph.union is able to take the union of two (or more) graphs, assuming that the vertices being united have the same IDs:
```
In [1]: g1=Graph([(0,1), (1,2), (2,3), (3,0)])
In [2]: g2=Graph([(0,2), (1,3)])
In [3]: g3=g1.union(g2)
In [4]: g3.get_edgelist()
Out[4]: [(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)]

Since this does not hold for your graph, I suggest a different approach:

```
1. Get the uids of the first and the second graph and store them in two lists:
```uids1, uids2 = g1.vs["uid"], g2.vs["uid"]

```
2. Create a dict that maps vertex IDs in g2 to vertex IDs in g1 based on the uids:
```mapping = {}
for i, uid in enumerate(uids2): mapping[i] = uids1.index(uid)
```
(this assumes that every uid in uids2 is to be found in uids1; if not, you have to add an exception handler around uids1.index and add the missing vertices to g1 as well)
```
```
3. Get the edge list of g2 and remap it according to the mapping obtained in step 2: el_remapped = [(mapping[v1], mapping[v2]) for v1, v2 in g2.get_edgelist()]
```
4. Add the remapped edges to g1:
g1 += el_remapped

```
I hope it works, I did not try it, but I think the basic idea is correct. If your edges also carry attributes, you'll have to add them to the new edges in g1 afterwards.
```
--
Tamas

```

reply via email to