igraph-help
[Top][All Lists]

## Re: [igraph] Collapsing/Folding graph based on vertex attributes

 From: Gábor Csárdi Subject: Re: [igraph] Collapsing/Folding graph based on vertex attributes Date: Tue, 11 Sep 2012 12:35:40 -0400

```Hi Ali,

contract.vertices() just takes a vector that gives the group id for
each vertex. E.g. if you have nine vertices and three groups each,
then 'mapping' can be something like this: c(1,1,2,2,3,1,2,3,3). So if
you just want to contract the vertices according to ethnic groups,
then you'll need to code the ethnic group using integer numbers, and
supply that as 'mapping'. An easy way to do this is to convert it to a
factor. This should work:

contracted_g <- contract.vertices(g,
mapping=as.integer(as.factor(V(g)\$Ethnic.Group)),
vertex.attr.comb=list(Ethnic.Group=toString, "ignore"))

Best,
Gabor

On Tue, Sep 11, 2012 at 12:27 PM, S. M. Ali Abbas <address@hidden> wrote:
> Hi Tamas,
> Thanks a lot for your help. I highly appreciate it. This contract function,
> it seems has been introduced in Igraph 0.6.
> I am struggling to apply the same on my graph. Specifically, I couldn't
> understand the mapping parameter in the function. The help page says that it
> should correspond to the elements of vertices. In my case, for instance,
> there are 57 unique ethnicities. That means the contracted graph, based on
> ethnicity vertex attribute, should have just 57 vertices. Do I have to know
> vertex ID within each ethnicity, and then supply it here? This is how I
> (incorrectly) did it:
>
> contracted_g <- contract.vertices(g, 1:length(unique(V(g)\$Ethnic.Group)),
> vertex.attr.comb=list(Ethnic.Group=toString, "ignore")) # just consider
> Ethnic group attribute, and ignore the rest for now.
>
> As expected, I am getting this error:
>
> At structural_properties.c:6915 : Invalid mapping vector length, Invalid
> value
>
> Could you please shed more light how to do this mapping?
>
> Thanks!
>
> Cheers,
> Ali
>
>
> I unfortunately could not make it work for my graph. I am sorry but I am
> struggling to use this on my graph. The mapping parameter is
>
>
> On 11/09/2012 15:05, Tamás Nepusz wrote:
>>
>> Hi,
>>
>> Use the contract.vertices() function first to contract the vertices, then
>> use simplify() to collapse multiple edges between the now-contracted
>> vertices into a single one. These functions let you specify what to do with
>> the attributes of the original vertices and edges. For instance, if you want
>> the new (contracted) vertices to have a "count" attribute that contains the
>> number of vertices they were contracted from, this can be achieved by adding
>> a "count" attribute to every vertex of the original graph with a value of 1
>> and then use vertex.attr.comb=list(count="sum") to specify that you want to
>> sum the "count" attributes while collapsing. Similarly, you can assign a
>> "weight" of 1 to every edge before calling simplify() and then sum the
>> weight of the collapsed edges.
>>
>
>
> _______________________________________________
> igraph-help mailing list