Post

Infomap Algorithm and the Map Equation

A concise introduction to Infomap and the map equation, with a worked network example.

Infomap Algorithm and the Map Equation

Introduction

Large real-world networks can be difficult to understand by looking at every node and connection. Community-detection methods help reveal groups of nodes, but the most useful method depends on the network and the question being asked.

Infomap is a flow-based community-detection method introduced by Martin Rosvall and Carl T. Bergstrom in 2008. It models movement through a network with a random walker, then uses the map equation to measure how compactly that movement can be described. Read the original paper.

The intuition is simple: if a random walker tends to stay within a group of nodes before moving elsewhere, that group may form a module. Infomap searches for a partition that gives a short description of the walk.

The map equation uses Shannon entropy, a measure from information theory, to calculate the expected description length. Entropy does not remove randomness. Instead, the random walk models flow, and the map equation measures how efficiently that flow can be encoded.

The Map Equation

For a partition (M) of a network into (m) modules, the map equation is:

\[L(M) = q_{\curvearrowright} H(\mathcal{Q}) + \sum_{i=1}^{m} p_{i\circlearrowright} H(\mathcal{P}_i)\]

(L(M)) is the average number of bits per step needed to describe the random walk under partition (M). The first term is the cost of describing module changes with an index codebook. The sum represents the cost of describing node visits and module exits with a separate module codebook for each module.

In the notation below, (\(q_{i\curvearrowright}\)) is the rate at which the walker exits module (i). For a stationary walk, a module’s entry and exit rates are equal.

Meaning of the terms

The total rate of module changes is:

\[q_{\curvearrowright} = \sum_{i=1}^{m} q_{i\curvearrowright}\]

The entropy of the module-exit rates is:

\[H(\mathcal{Q}) = -\sum_{i=1}^{m} \frac{q_{i\curvearrowright}}{q_{\curvearrowright}} \log_2\left(\frac{q_{i\curvearrowright}}{q_{\curvearrowright}}\right)\]

The rate at which module (i)’s codebook is used is the sum of node visits in the module and exits from it:

\[p_{i\circlearrowright} = \sum_{\alpha \in i} p_\alpha + q_{i\curvearrowright}\]

The entropy of the events in module (i)’s codebook is:

\[H(\mathcal{P}_i) = -\frac{q_{i\curvearrowright}}{p_{i\circlearrowright}} \log_2\left(\frac{q_{i\curvearrowright}}{p_{i\circlearrowright}}\right) -\sum_{\alpha \in i} \frac{p_\alpha}{p_{i\circlearrowright}} \log_2\left(\frac{p_\alpha}{p_{i\circlearrowright}}\right)\]

Those events are either exiting the module or visiting one of its nodes.

Worked Example: Two Clusters Connected by a Bridge

1. Define the network

Consider six nodes arranged in two fully connected clusters, with one bridge between them:

  • Left cluster: (A, B, C)
  • Right cluster: (D, E, F)
  • Bridge: (C) is connected to (F)

Graph representation

Treat each undirected edge as two directed links, one in each direction. There are seven undirected edges, or (14) directed links in total. Assume all links have equal weight.

For this undirected, equal-weight network, the stationary visit rate of a node is its degree divided by the total degree, (14). Nodes (A, B, D, E) each have degree (2); nodes (C, F) each have degree (3).

NodesVisit rate
(A, B, D, E)(2/14) each
(C, F)(3/14) each

The visit rates sum to (1).

2. One-module partition (M_1)

First, put all six nodes in one module. The walker never changes modules, so (\(q_{\curvearrowright}=0\)) and the index-codebook term is zero. The module codebook is used at rate (1).

Its entropy is:

\[\begin{aligned} H(\mathcal{P}_1) &= -\left[ 4\left(\frac{2}{14}\log_2\frac{2}{14}\right) +2\left(\frac{3}{14}\log_2\frac{3}{14}\right) \right] \ \approx 2.557 \text{ bits} \end{aligned}\]

Therefore:

\[L(M_1) = 1 \times 2.557 \approx 2.557 \text{ bits per step}\]

3. Two-module partition (M_2)

Note: that here for the simplicity of the demo we showed directly the last community division but this should not be the second iteration but depending on the random walk the secoond step should be moving one node to a module (So here we should get Module i: ({A, B} or {B, c} or {A, C} or {D, E} or {E, F} or {D, F}) plus the rest of the modules).

Now divide the network into its two clusters:

  • Module 1: ({A, B, C})
  • Module 2: ({D, E, F})

One directed link exits each module: (C \to F) and (F \to C). Each link carries flow (1/14), so:

\[q_{1\curvearrowright} = q_{2\curvearrowright} = \frac{1}{14}, \qquad q_{\curvearrowright} = \frac{2}{14}\]

Index codebook

The exit rates are equal, so the relative rates are ((1/2, 1/2)). Their entropy is one bit:

\[H(\mathcal{Q}) = -\left( \frac{1}{2}\log_2\frac{1}{2} +\frac{1}{2}\log_2\frac{1}{2} \right) = 1\]

The weighted index-codebook cost is:

\[q_{\curvearrowright}H(\mathcal{Q}) = \frac{2}{14}\times 1 \approx 0.143 \text{ bits per step}\]

Module codebooks

In Module 1, the codebook events are visits to (A), (B), or (C), and an exit from the module. Its total use rate is:

\[p_{1\circlearrowright} = \frac{2}{14}+\frac{2}{14}+\frac{3}{14}+\frac{1}{14} = \frac{8}{14}\]

The relative event rates are ((1/8, 2/8, 2/8, 3/8)). Therefore:

\[\begin{aligned} H(\mathcal{P}_1) = -\bigg[ &\frac{1}{8}\log_2\frac{1}{8} +\frac{2}{8}\log_2\frac{2}{8} \ +\frac{2}{8}\log_2\frac{2}{8} +\frac{3}{8}\log_2\frac{3}{8} \bigg] \approx 1.906 \text{ bits} \end{aligned}\]

Module 2 is symmetric, so it has the same use rate and entropy. The weighted cost for each module is:

\[\frac{8}{14}\times 1.906 \approx 1.089 \text{ bits per step}\]

Adding the index and module-codebook costs gives:

\[\begin{aligned} L(M_2) &= \frac{2}{14}(1) + 2\left(\frac{8}{14}\times 1.906\right) \\ &\approx 2.321 \text{ bits per step} \end{aligned}\]

4. Compare the partitions

\[L(M_1) \approx 2.557 \qquad\text{>}\qquad L(M_2) \approx 2.321\]

For these two candidate partitions, the two-module partition has a shorter description length by about (0.236) bits per step. The local codebooks save more bits than the index codebook uses to describe moves between modules.

Infomap in Python

The following example runs Infomap on the same graph using python-igraph. The random seed makes the run reproducible.

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
import random
import igraph as ig

edges = [
    ("A", "B"), ("A", "C"), ("B", "C"),
    ("C", "F"),
    ("D", "E"), ("D", "F"), ("E", "F"),
]

graph = ig.Graph.TupleList(edges, directed=False, vertex_name_attr="name")

# Set a seed for reproducibility
ig.set_random_number_generator(random.Random(42))

result = graph.community_infomap(trials=10)
membership = result.membership

print(f"Number of communities: {len(set(membership))}")
print(f"Map equation codelength: {result.codelength:.4f} bits")

for community_id in sorted(set(membership)):
    nodes = [
        graph.vs[index]["name"]
        for index, assigned_id in enumerate(membership)
        if assigned_id == community_id
    ]
    print(f"Community {community_id + 1}: {', '.join(nodes)}")

A run should find the two clusters, although the order in which the communities or their nodes are printed may differ.

Algorithm overview

The diagram below summarizes the Infomap algorithm.

Flowchart of the Infomap algorithm

Conclusion

Infomap identifies communities by finding a partition that gives a short description of network flow. In this example, the two-cluster partition has a lower map-equation value than the single-module partition, so it is preferred among the two partitions compared.

There is not always one universally correct partition of a network. The useful result depends on the network, the flow being modeled, and the question being asked. Treat the detected modules as an interpretation of the network’s flow, and check that they make sense for your analysis.

References

This post is licensed under CC BY 4.0 by the author.