Lucien S.
Lucien S.

Reputation: 5345

collapsing graphs according to a common parameter in iGraph [Python]

I have an igraph network g where all edges' weights are 1 and where all vertices belong to on of 5 groups (noted with a vertex parameter called "group").

Now I want to make a new graph G where the vertices of g are collapsed, ore contracted, according to the group they belong to. So that the number of vertices of G is reduced to the number of different groups in g, and the edges between these new collapsed vertices have a weight equal to the summed up number of edges between the same groups in g.

I believe I could use contract_vertices(). But the edges' weights wouldn't be added, I'm guessing... Anyway, is there a way to accomplish this in igraph?

Thanks!

Upvotes: 0

Views: 647

Answers (1)

Tamás
Tamás

Reputation: 48071

Use contract_vertices() followed with simplify(), which can collapse the edges and sum the weights:

g2 = g.copy()
g2.contract_vertices(membership)
g2.simplify(combine_edges={"weight": sum})

Upvotes: 2

Related Questions