File: complement.py

package info (click to toggle)
python-igraph 0.11.8%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,480 kB
  • sloc: ansic: 24,545; python: 21,699; sh: 107; makefile: 35; sed: 2
file content (75 lines) | stat: -rw-r--r-- 1,912 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
"""
.. _tutorials-complement:

================
Complement
================

This example shows how to generate the `complement graph <https://en.wikipedia.org/wiki/Complement_graph>`_ of a graph (sometimes known as the anti-graph) using :meth:`igraph.GraphBase.complementer`.
"""
import igraph as ig
import matplotlib.pyplot as plt
import random

# %%
# First, we generate a random graph
random.seed(0)
g1 = ig.Graph.Erdos_Renyi(n=10, p=0.5)

# %%
# .. note::
#     We set the random seed to ensure the graph comes out exactly the same
#     each time in the gallery. You don't need to do that if you're exploring
#     really random graphs ;-)

# %%
# Then we generate the complement graph:
g2 = g1.complementer(loops=False)

# %%
# The union graph of the two is of course the full graph, i.e. a graph with
# edges connecting all vertices to all other vertices. Because we decided to
# ignore loops (aka self-edges) in the complementer, the full graph does not
# include loops either.
g_full = g1 | g2

# %%
# In case there was any doubt, the complement of the full graph is an
# empty graph, with the same vertices but no edges:
g_empty = g_full.complementer(loops=False)

# %%
# To demonstrate these concepts more clearly, here's a layout of each of the
# four graphs we discussed (input, complement, union/full, complement of
# union/empty):
fig, axs = plt.subplots(2, 2)
ig.plot(
    g1,
    target=axs[0, 0],
    layout="circle",
    vertex_color="black",
)
axs[0, 0].set_title('Original graph')
ig.plot(
    g2,
    target=axs[0, 1],
    layout="circle",
    vertex_color="black",
)
axs[0, 1].set_title('Complement graph')

ig.plot(
    g_full,
    target=axs[1, 0],
    layout="circle",
    vertex_color="black",
)
axs[1, 0].set_title('Union graph')
ig.plot(
    g_empty,
    target=axs[1, 1],
    layout="circle",
    vertex_color="black",
)
axs[1, 1].set_title('Complement of union graph')
plt.show()