File: connected_components.py

package info (click to toggle)
python-igraph 0.11.9%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 3,480 kB
  • sloc: ansic: 24,611; python: 21,748; sh: 111; makefile: 35; sed: 2
file content (44 lines) | stat: -rw-r--r-- 1,310 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
"""
.. _tutorials-connected-components:

=====================
Connected Components
=====================

This example demonstrates how to visualise the connected components in a graph using :meth:`igraph.GraphBase.connected_components`.
"""
import igraph as ig
import matplotlib.pyplot as plt
import random

# %%
# First, we generate a randomized geometric graph with random vertex sizes. The
# seed is set to the example is reproducible in our manual: you don't really
# need it to understand the concepts.
random.seed(0)
g = ig.Graph.GRG(50, 0.15)

# %%
# Now we can cluster the graph into weakly connected components, i.e. subgraphs
# that have no edges connecting them to one another:
components = g.connected_components(mode='weak')

# %%
# Finally, we can visualize the distinct connected components of the graph:
fig, ax = plt.subplots()
ig.plot(
    components,
    target=ax,
    palette=ig.RainbowPalette(),
    vertex_size=7,
    vertex_color=list(map(int, ig.rescale(components.membership, (0, 200), clamp=True))),
    edge_width=0.7
)
plt.show()

# %%
# .. note::
#
#     We use the integers from 0 to 200 instead of 0 to 255 in our vertex
#     colors, since 255 in the :class:`igraph.drawing.colors.RainbowPalette`
#     corresponds to looping back to red. This gives us nicely distinct hues.