The following two examples use data in the both cited papers mentioned in the index page.


In [1]:
import networkx as nx
nx.__version__


Out[1]:
'2.0'

In [2]:
from modularity_maximization import partition
from modularity_maximization.utils import get_modularity

Undirected Network: Karate


In [3]:
karate = nx.Graph(nx.read_pajek("data/karate.net"))

In [4]:
print(nx.info(karate))


Name: 
Type: Graph
Number of nodes: 34
Number of edges: 78
Average degree:   4.5882

In [5]:
comm_dict = partition(karate)

In [6]:
for comm in set(comm_dict.values()):
    print("Community %d"%comm)
    print(', '.join([node for node in comm_dict if comm_dict[node] == comm]))


Community 3
24, 25, 26, 28, 29, 32
Community 4
9, 15, 16, 19, 21, 23, 27, 30, 31, 33, 34
Community 5
1, 2, 3, 4, 8, 10, 12, 13, 14, 18, 20, 22
Community 6
5, 6, 7, 11, 17

In [7]:
print('Modularity of such partition for karate is %.3f' % get_modularity(karate, comm_dict))


Calculating modularity for undirected graph
Modularity of such partition for karate is 0.419

Jazz Network


In [8]:
jazz = nx.Graph(nx.read_pajek("data/jazz.net"))

In [9]:
print(nx.info(jazz))


Name: 
Type: Graph
Number of nodes: 198
Number of edges: 2742
Average degree:  27.6970

In [10]:
comm_dict = partition(jazz)

In [11]:
for comm in set(comm_dict.values()):
    print("Community %d"%comm)
    print(', '.join([node for node in comm_dict if comm_dict[node] == comm]))


Community 3
3, 4, 5, 6, 13, 18, 21, 25, 26, 27, 28, 29, 37, 39, 41, 45, 47, 51, 73, 75, 76, 77, 79, 85, 86, 88, 90, 92, 96, 97, 102, 115, 124, 126, 128, 133, 136, 138, 140, 144, 145, 147, 148, 149, 152, 153, 155, 156, 157, 160, 163, 167, 169, 172, 173, 176, 180, 181, 184, 189, 191, 198
Community 4
2, 7, 10, 11, 12, 14, 19, 30, 31, 34, 36, 49, 52, 53, 54, 55, 56, 57, 61, 67, 69, 70, 71, 72, 74, 83, 87, 89, 103, 112, 113, 114, 127, 129, 130, 141, 142, 150, 151, 158, 161, 164, 165, 174, 178, 185, 186, 192, 194, 195, 196, 197
Community 5
1, 8, 15, 16, 17, 20, 22, 23, 24, 32, 33, 35, 38, 40, 42, 43, 44, 46, 48, 50, 58, 60, 62, 63, 64, 65, 66, 68, 78, 80, 81, 91, 95, 98, 99, 100, 101, 104, 105, 106, 107, 108, 109, 110, 119, 120, 122, 123, 131, 132, 134, 135, 139, 154, 159, 162, 166, 168, 170, 171, 179, 182, 187, 188
Community 6
9, 59, 82, 84, 93, 94, 111, 116, 117, 118, 121, 125, 137, 143, 146, 175, 177, 183, 190, 193

In [12]:
print('Modularity of such partition for jazz is %.3f' % get_modularity(jazz, comm_dict))


Calculating modularity for undirected graph
Modularity of such partition for jazz is 0.442

Directed Network: Big 10 Football Season 2005


In [13]:
big_10_football = nx.read_gml("data/big_10_football_directed.gml")

In [14]:
print(nx.info(big_10_football))


Name: 
Type: DiGraph
Number of nodes: 11
Number of edges: 44
Average in degree:   4.0000
Average out degree:   4.0000

In [15]:
comm_dict = partition(big_10_football)

In [16]:
for comm in set(comm_dict.values()):
    print("Community %d"%comm)
    print(', '.join([node for node in comm_dict if comm_dict[node] == comm]))


Community 1
Purdue, Illinois, MichiganState, Indiana
Community 2
Northwestern, Wisconsin, Iowa, PennState, OhioState, Michigan, Minnesota

In [17]:
print('Modularity of such partition for karate is %.3f' %\
      get_modularity(big_10_football, comm_dict))


Calculating modularity for directed graph
Modularity of such partition for karate is 0.112