Simd
Simd

Reputation: 21353

Simple python + igraph usage

I am using python + igraph 0.6 for the first time and have some basic questions.

First, I would like to make a random graph and then insert a clique into it somewhere.

from igraph import *
g = Graph.Erdos_Renyi(50,0.1)
h = Graph.Full(5)

How do I make a new graph g2 with h connected by two edges to the graph g?

Second, having done this I would like to find maximal cliques and show in a picture where they are. Call the combined graph g2.

mcliques = g2.maximal_cliques(4, 7)

How can I now plot the cliques we have just found either separately or in the graph g2?

Upvotes: 0

Views: 1334

Answers (1)

Tamás
Tamás

Reputation: 48101

Answer to your first question:

g2 = g + h
g2.add_edges([(0, 50), (1, 51)])

or even simpler:

g2 = g + h + [(0, 50), (1, 51)]

Here we make use of the fact that the + operator between two graphs creates a disjoint union of the two graphs, and since igraph always uses a consecutive vertex ID range, we can simply know that vertices 0-49 of g2 will span g and vertices 50-54 will span h.

Regarding your second question, you did not specify what exactly you mean by plotting the cliques, but a possible solution is as follows:

group_markers = [(clique, "gray") for clique in mcliques]
plot(g2, mark_groups=group_markers)

See the documentation of Graph.__plot__ for more information about the mark_groups parameter; basically it is either a dictionary mapping colors to groups of vertices (vertex ids), or a list yielding pairs of vertex ids and colors. The vertex groups are then enclosed by a shaded area with the given color as background.

Upvotes: 7

Related Questions