igraph-help
[Top][All Lists]
Advanced

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

Re: [igraph] how to "collapse" a network


From: jeremy.raw
Subject: Re: [igraph] how to "collapse" a network
Date: Tue, 6 Mar 2012 14:02:52 +0000

Assuming R is the platform of choice, the very simple way to make the collapsed graph is NOT to do it in igraph at all, but instead to use R’s “aggregate” function, operating directly on an edge list representation of the graph, with the vertex labels consisting either of company or industry ID’s (in the original poster’s problem formulation).

 

The edge list (call it “edges”) might start with vertices V1 and V2 (presumed to be company ID’s, but could be industry ID’s), and then be augmented with a Weight field initially filled with whatever weight is appropriate (just 1 if counting, or some other value perhaps related to company size, dollar sales, deal value, etc.).  If the source data starts as a table of edges with attributes, even better.

 

edgelist <- get.edgelist(graph,names=TRUE) # presuming the vertex ‘name’ attribute is a stable identifier

edges <- as.data.frame(edgelist) # default column names are V1 and V2

edges$Weight <- 1

 

weighted.edges <- aggregate( edges[“Weight”], by=list(V1=edges$V2,V2=edges$V2), FUN=sum )

 

collapsed.graph <- graph.data.frame( weighted.edges )

E(collapsed.graph)$Weight

 

If the vertices are industry attributes, the same one line operation will generate a weighted edge list of industry-industry connections.  It gets a little trickier if the graph is undirected and the “same” edges can have the vertices in either direction.  The simplest approach there is to swap the A and B labels so that the A label is always “less” than the B label using some arbitrary ordering function.

 

I hope that helps!

 

---
Jeremy Raw

 

 

From: igraph-help-bounces+address@hidden [mailto:igraph-help-bounces+address@hidden On Behalf Of Tamás Nepusz
Sent: Tuesday, March 06, 2012 4:37 AM
To: address@hidden; Help for igraph users
Subject: Re: [igraph] how to "collapse" a network

 

Hello Peter,

 

It happens frequently that the same companies engage in multiple deals

together, but each deal is represented by a line in my data. As a

result, you see that 'COMP 1' -- 'COMP 2' occurs twice.

I would like to be able to collapse this to a weighted edge list where

'COMP 1' -- 'COMP 2' occurs only once but with value 2, 'COMP 1' --

'COMP 3' occurs only once having value 3, et cetera.

There must be a very simple way of doing this, but how?

I assume that you are using igraph from R. The "simplify" function in igraph 0.6 will sum up numeric edge attributes when it eliminates multiple edges, so I guess the easiest for you is to upgrade to the development version of igraph (that is, 0.6). Since it has not been released officially yet, you can download it from the page of our nightly builds:

 

 

Download the latest "GNU R source package", and install it in R. If you happen to use Windows and need a pre-compiled package, let us know - Gabor can probably prepare one for you.

 

Unfortunately if you cannot upgrade to igraph 0.6 for any reason, then the only option is to use count.multiple to find the "multiplicity" of each edge, and then construct a new graph manually; e.g., like this (not sure if there is a simpler solution):

 

mul <- count.multiple(g)

df <- as.data.frame(get.edgelist(g))

df$weight <- cm

g <- graph.data.frame(unique(df))

 

Second, each company has several attributes, including industry. One of

the things I would like to do is to collapse the entire

company*company network into a weigted industry*industry network. This

would reduce the network from approx. 2000*2000 to 40*40.

How can I construct such a weighted network in igraph?

igraph 0.6 will also have a function called contract.vertices which lets you contract many vertices into a single one, so you could proceed by first contracting the vertices by industry using contract.vertices, followed by simplify.

 

-- 

T.


reply via email to

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