def findCommunities(G): """ Partition network with the Infomap algorithm. Annotates nodes with 'community' id and return number of communities found. """ conf = infomap.init("--two-level"); # Input data network = infomap.Network(conf); # Output data tree = infomap.HierarchicalNetwork(conf) print "Building network..." for e in G.edges_iter(): network.addLink(*e) network.finalizeAndCheckNetwork(True, nx.number_of_nodes(G)); # Cluster network infomap.run(network, tree); codelength = tree.codelength() print "Codelength:", codelength communities = {} for leaf in tree.leafIter(): communities[leaf.originalLeafIndex] = leaf.parentNode.parentIndex nx.set_node_attributes(G, 'community', communities) return tree.numTopModules()
def findCommunities(G): """ Partition network with the Infomap algorithm. Annotates nodes with 'community' id and return number of communities found. """ conf = infomap.init("--two-level"); # Input data network = infomap.Network(conf); # Output data tree = infomap.HierarchicalNetwork(conf) print "Building network..." for e in G.edges_iter(): network.addLink(*e) network.finalizeAndCheckNetwork(True, nx.number_of_nodes(G)); # Cluster network infomap.run(network, tree); print "Found %d top modules with codelength: %f" % (tree.numTopModules(), tree.codelength()) communities = {} clusterIndexLevel = 1 # 1, 2, ... or -1 for top, second, ... or lowest cluster level for node in tree.leafIter(clusterIndexLevel): communities[node.originalLeafIndex] = node.clusterIndex() nx.set_node_attributes(G, 'community', communities) return tree.numTopModules()
print "Creating network..." network = infomap.Network(conf) network.addLink(0, 1) network.addLink(0, 2) network.addLink(0, 3) network.addLink(1, 0) network.addLink(1, 2) network.addLink(2, 1) network.addLink(2, 0) network.addLink(3, 0) network.addLink(3, 4) network.addLink(3, 5) network.addLink(4, 3) network.addLink(4, 5) network.addLink(5, 4) network.addLink(5, 3) print "Num links:", network.numLinks() network.finalizeAndCheckNetwork() tree = infomap.HierarchicalNetwork(conf) infomap.run(network, tree) codelength = tree.codelength() print "Codelength:", codelength