|
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! --- From: igraph-help-bounces+address@hidden [mailto:igraph-help-bounces+address@hidden
On Behalf Of Tamás Nepusz Hello Peter,
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))
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. |
[Prev in Thread] | Current Thread | [Next in Thread] |