def find_partitions():
    G = nx.read_edgelist('temp_graph.edges')
    file = open('temp_nb_of_tanks')
    for fulline in file:
        # Network Components
        values = fulline.split()
    nb_of_tanks = int(values[0])
    print(nb_of_tanks)
    while len(all_partitions) < 2 ^ nb_of_tanks:
        partitions = kernighan_lin_bisection(G,
                                             partition=None,
                                             max_iter=10,
                                             weight='weight')
        all_partitions.append(partitions[0])
        all_partitions.append(partitions[1])
        G_1 = nx.Graph()
        G_2 = nx.Graph()
        for edge in G.edges():
            if edge[0] and edge[1] in partitions[0]:
                G_1.add_edge(edge[0], edge[1])
            elif edge[0] and edge[1] in partitions[1]:
                G_2.add_edge(edge[0], edge[1])
        partitions = kernighan_lin_bisection(G_1,
                                             partition=None,
                                             max_iter=10,
                                             weight='weight')
        all_partitions.append(partitions[0])
        all_partitions.append(partitions[1])
        partitions = kernighan_lin_bisection(G_2,
                                             partition=None,
                                             max_iter=10,
                                             weight='weight')
        all_partitions.append(partitions[0])
        all_partitions.append(partitions[1])
def recursive_kl(graph: Graph, numgrp=2) -> List[Set[int]]:
    """
    Recursively use Kernighan-Lin algorithm to create a k-way graph partition
    """
    power = log(numgrp, 2)
    if power != int(power) or power < 1:
        raise ValueError("numgrp must be a power of 2 and at least 2.")
    if numgrp == 2:
        # Base case for recursion: use Kernighan-Lin to create 2 groups
        return list(kernighan_lin_bisection(graph))
    next_numgrp = numgrp / 2
    groups = []
    for subset in kernighan_lin_bisection(graph):
        subgraph = graph.subgraph(subset)
        groups.extend(recursive_kl(subgraph, numgrp=next_numgrp))
    return groups
def test_multigraph():
    G = nx.cycle_graph(4)
    M = nx.MultiGraph(G.edges())
    M.add_edges_from(G.edges())
    M.remove_edge(1, 2)
    A, B = kernighan_lin_bisection(M)
    assert_partition_equal([A, B], [{0, 1}, {2, 3}])
def recursive_partition(G, all_partitions, levels):
    if len(all_partitions) > 0 and math.log(len(all_partitions), 2) >= levels:
        return all_partitions
    print("nodes in original", len(G.nodes()))
    partitions = kernighan_lin_bisection(G,
                                         partition=None,
                                         max_iter=10,
                                         weight='weight')
    pos = nx.nx_pydot.graphviz_layout(G, prog='neato')
    nx.draw(G, pos, with_labels=False, node_size=10)
    plt.show()
    print("nb_of_partitions", len(partitions))
    print("nodes in 1", len(partitions[0]))
    print("nodes in 2", len(partitions[1]))
    all_partitions.append(partitions[0])
    all_partitions.append(partitions[1])
    G_1 = nx.Graph()
    G_2 = nx.Graph()
    for edge in G.edges():
        if edge[0] and edge[1] in partitions[0]:
            G_1.add_edge(edge[0], edge[1])
        else:
            G_2.add_edge(edge[0], edge[1])
    if (len(G_1.edges()) > 10):
        all_partitions = recursive_partition(G_1,
                                             all_partitions=all_partitions,
                                             levels=levels)
    print(len(all_partitions))
    if (len(G_2.edges()) > 10):
        all_partitions = recursive_partition(G_2,
                                             all_partitions=all_partitions,
                                             levels=levels)
    print(len(all_partitions))
    return all_partitions
Exemple #5
0
 def kl(self, G, scores=[], depth=0):
     U, V = community.kernighan_lin_bisection(G)
     lU = set([self.communities.get(u, 'NA') for u in U]) - {'NA'}
     lV = set([self.communities.get(v, 'NA') for v in V]) - {'NA'}
     shared = lU & lV
     match = 0
     mismatch = 0
     if len(shared) > 0:
         for label in shared:
             mem = set(self.community(label))
             ku = len(mem & U)  # one side
             kv = len(mem & V)  # the other
             mismatch += ku * kv
             match += ku * ku + kv * kv
     for label in lU | lV - shared:  # the ones that were not split
         mem = set(self.community(label))
         k = len(mem)
         match += k * k
     agreement = match / (match + mismatch)
     scores.append(agreement**(1 / (depth + 1)))
     if len(lU) > 1:
         scores = self.kl(G.subgraph(U), scores, depth + 1)
     if len(lV) > 1:
         scores = self.kl(G.subgraph(V), scores, depth + 1)
     return scores
Exemple #6
0
def test_multigraph():
    G = nx.cycle_graph(4)
    M = nx.MultiGraph(G.edges())
    M.add_edges_from(G.edges())
    M.remove_edge(1, 2)
    for labels in permutations(range(4)):
        mapping = dict(zip(M, labels))
        A, B = kernighan_lin_bisection(nx.relabel_nodes(M, mapping), seed=0)
        assert_partition_equal(
            [A, B], [{mapping[0], mapping[1]}, {mapping[2], mapping[3]}])
Exemple #7
0
def recursive_kl(graph: Graph, numgrp=2) -> List[Set[int]]:
    """
    Recursively use the Kernighan-Lin algorithm to create a k-way graph partition.
    This function will either return two groups or more than two depending on the
    value of numgrp. Each group generated is different from the previous.
    """
    power = log(numgrp, 2)
    if power != int(power) or power < 1:
        raise ValueError("numgrp must be a power of 2 and at least 2.")
    # For a group of two bisect it and return two groups
    if numgrp == 2:
        # Base case for recursion: use Kernighan-Lin to create 2 groups
        return list(kernighan_lin_bisection(graph))
    # For the next group of two divide numgrp by 2
    next_numgrp = numgrp / 2
    groups = []
    for subset in kernighan_lin_bisection(graph):
        subgraph = graph.subgraph(subset)
        groups.extend(recursive_kl(subgraph, numgrp=next_numgrp))
    return groups
Exemple #8
0
def get_twitter_data(filename, w=None, save=False):
    '''
    reads twitter data, makes bipartition and assign group memebership
    with constant weights of infection
    '''
    f = None
    DG = None
    try:
        f = open(filename + '.txt', 'r')
        print("loaded: " + filename)
        DG = nx.DiGraph()
        n, m = map(int, f.readline().split())
        for i, line in enumerate(f):
            if i < n:
                node_str = line.split()
                u = int(node_str[0])
                color = node_str[1]
                DG.add_node(u, color=color)
            else:
                edges_str = line.split()
                u = int(edges_str[0])
                v = int(edges_str[1])
                weight = float(edges_str[2])
                if w is not None:
                    DG.add_edge(u, v, weight=w)
                else:
                    DG.add_edge(u, v, weight=weight)
        f.close()

    except FileNotFoundError:
        #
        print(" Making graph ")
        f = open('twitter/twitter_combined.txt', 'r')
        DG = nx.DiGraph()

        for line in f:
            node_a, node_b = line.split()
            DG.add_nodes_from([node_a, node_b])
            DG.add_edges_from([(node_a, node_b)])

            DG[node_a][node_b]['weight'] = w

        print("done with edges and weights ")

        G_a, G_b = community.kernighan_lin_bisection(DG.to_undirected())
        for n in G_a:
            DG.nodes[n]['color'] = 'red'
        for n in G_b:
            DG.nodes[n]['color'] = 'blue'

        save_graph(filename, DG)

    return DG
Exemple #9
0
def create_two_clusters(G: nx.Graph):
    clusters = []
    partition = community.kernighan_lin_bisection(G,
                                                  None,
                                                  500,
                                                  weight='weight')

    i = 0
    for p in partition:
        clusters.append(cluster("kernighan_lin - " + str(i), p))
        i = i + 1

    return clusters
Exemple #10
0
def test_max_iter_argument():
    G = nx.Graph([
        ("A", "B", {
            "weight": 1
        }),
        ("A", "C", {
            "weight": 2
        }),
        ("A", "D", {
            "weight": 3
        }),
        ("A", "E", {
            "weight": 2
        }),
        ("A", "F", {
            "weight": 4
        }),
        ("B", "C", {
            "weight": 1
        }),
        ("B", "D", {
            "weight": 4
        }),
        ("B", "E", {
            "weight": 2
        }),
        ("B", "F", {
            "weight": 1
        }),
        ("C", "D", {
            "weight": 3
        }),
        ("C", "E", {
            "weight": 2
        }),
        ("C", "F", {
            "weight": 1
        }),
        ("D", "E", {
            "weight": 4
        }),
        ("D", "F", {
            "weight": 3
        }),
        ("E", "F", {
            "weight": 2
        }),
    ])
    partition = ({"A", "B", "C"}, {"D", "E", "F"})
    C = kernighan_lin_bisection(G, partition, max_iter=1)
    assert_partition_equal(C, ({"A", "F", "C"}, {"D", "E", "B"}))
Exemple #11
0
def networkx_manipulator(graphmatrix):

    #new graph object
    G = nx.Graph()

    #adding nodes
    G.add_nodes_from([x for x in range(len(graphmatrix))])

    #adding edges
    for x in range(len(graphmatrix)):
        for y in range(len(graphmatrix)):
            if graphmatrix[x][y] == 1:
                G.add_edge(x, y)

    return kernighan_lin_bisection(G)
Exemple #12
0
    def get_community(self, k):

        print("Finding communities using: {}".format(self.algorithm.upper()))

        communities: Dict[str, list] = dict()

        if self.algorithm.upper() == Algorithm.KERNIGHAN_LIN.value:
            partitions = community.kernighan_lin_bisection(self.graph)
            for p in range(len(partitions)):
                communities[str(p)] = list(partitions[p])

        elif self.algorithm.upper() == Algorithm.FLUIDC.value:
            partitions = community.asyn_fluidc(self.graph, k, seed=10)
            for p in range(k):
                communities[str(p)] = list(next(partitions))

        print("{} communities found".format(len(communities)))
        return communities
Exemple #13
0
def get_facebook_data(filename, w=None, save=False):
    '''
    reads twitter data, makes bipartition and assign group memebership
    with constant weights of infection
    '''
    f = None
    G = None
    try:
        f = open(filename + '_with_communities.txt', 'r')
        print("loaded: " + filename)
        G = nx.Graph()
        n, m = map(int, f.readline().split())
        for i, line in enumerate(f):
            if i < n:
                node_str = line.split()
                u = int(node_str[0])
                color = node_str[1]
                G.add_node(u, color=color)
            else:
                edges_str = line.split()
                u = int(edges_str[0])
                v = int(edges_str[1])
                weight = float(edges_str[2])
                if w is not None:
                    G.add_edge(u, v, weight=w)
                else:
                    G.add_edge(u, v, weight=weight)
        f.close()

    except FileNotFoundError:
        #
        print(" Making graph ")

        G = facebook_circles_graph(filename, w)

        G_a, G_b = community.kernighan_lin_bisection(G.to_undirected())
        for n in G_a:
            G.nodes[n]['color'] = 'red'
        for n in G_b:
            G.nodes[n]['color'] = 'blue'

        save_graph(filename, G)

    return G
Exemple #14
0
    def measgp(model, measure, *args, **kwargs):
        #kwargs.setdefault('groupby','niche')
        niches = model.find_data(role='niche').matrix
        names = sorted(set(niches))
        bioms = [
            np.sum(model.results['n'][-1][niches == n])
            for n in sorted(set(niches))
        ]
        names = {n: b for n, b in zip(names, np.argsort(bioms))}
        #from datatools import code_debugger
        # code_debugger()
        orderedniches = np.array([names[n] for n in niches])

        kwargs.setdefault('groupby', orderedniches)
        measure_groups(model, measure, *args, **kwargs)
        measure_groups_cavity(model, measure, *args, **kwargs)
        from networkx.algorithms import community
        Aij = model.find_data(role='interactions').matrix.copy()

        ords = []
        for i in range(5):
            parts = community.kernighan_lin_bisection(nx.Graph(Aij))
            pbioms = [
                np.sum(model.results['n'][-1][list(part)]) for part in parts
            ]
            pnames = np.argsort(pbioms)
            orderedparts = np.zeros(niches.shape)
            for i, p in enumerate(parts):
                orderedparts[list(p)] = pnames[i]
            ords.append(orderedparts)

        sbm = SBM(Aij)
        measure['partition_compare'] = np.mean([
            max(np.mean(ord == orderedniches),
                np.mean(1 - ord == orderedniches)) for ord in ords
        ])
        measure['SBM_compare'] = max(np.mean(sbm == orderedniches),
                                     np.mean(1 - sbm == orderedniches))
def test_seed_argument():
    G = nx.barbell_graph(3, 0)
    C = kernighan_lin_bisection(G, seed=1)
    assert_partition_equal(C, [{0, 1, 2}, {3, 4, 5}])
Exemple #16
0
def test_defined_partition_int_nodes():
    G = nx.Graph([(1, 2), (1, 3), (2, 3), (3, 4)])
    partition = ({1, 2}, {3, 4})
    kernighan_lin_bisection(G, partition)
        def eval_bisection(graph):
            """this evaluates the main function and cach it for speed up."""
            communities = list(kernighan_lin_bisection(graph))
            communities.sort(key=len, reverse=True)

            return communities
Exemple #18
0
def test_seed_argument():
    G = nx.barbell_graph(3, 0)
    C = kernighan_lin_bisection(G, seed=1)
    assert_partition_equal(C, [set([0, 1, 2]), set([3, 4, 5])])
Exemple #19
0
def test_defined_partition_str_nodes():
    G = nx.Graph([("A", "B"), ("A", "C"), ("B", "C"), ("C", "D")])
    partition = ({"A", "B"}, {"C", "D"})
    kernighan_lin_bisection(G, partition)
Exemple #20
0
def test_partition_argument_non_integer_nodes():
    G = nx.Graph([("A", "B"), ("A", "C"), ("B", "C"), ("C", "D")])
    partition = ({"A", "B"}, {"C", "D"})
    C = kernighan_lin_bisection(G, partition)
    assert_partition_equal(C, partition)
Exemple #21
0
def test_too_many_blocks():
    with pytest.raises(nx.NetworkXError):
        G = nx.barbell_graph(3, 0)
        partition = ({0, 1}, {2}, {3, 4, 5})
        kernighan_lin_bisection(G, partition)
def test_too_many_blocks():
    G = nx.barbell_graph(3, 0)
    partition = ({0, 1}, {2}, {3, 4, 5})
    kernighan_lin_bisection(G, partition)
def test_partition():
    G = nx.barbell_graph(3, 0)
    C = kernighan_lin_bisection(G)
    assert_partition_equal(C, [{0, 1, 2}, {3, 4, 5}])
Exemple #24
0
def test_non_disjoint_partition():
    G = nx.barbell_graph(3, 0)
    partition = (set([0, 1, 2]), set([2, 3, 4, 5]))
    kernighan_lin_bisection(G, partition)
Exemple #25
0
def test_too_many_blocks():
    G = nx.barbell_graph(3, 0)
    partition = (set([0, 1]), set([2]), set([3, 4, 5]))
    kernighan_lin_bisection(G, partition)
# -*- coding: utf-8 -*-
import networkx as nx
from networkx.algorithms import community

G = nx.barbell_graph(5, 1)
# print G.nodes
# print G.edges
# kernighan_lin_bisection 用于计算Kernighan-Lin二部算法的函数
res = community.kernighan_lin_bisection(G)
print res

# 利用渗透法在图中寻找k族群落
res = community.k_clique_communities(G, 2)
print res

# 使用Clauest Newman-Moore贪婪的模块化最大化在图中寻找社区
res = community.greedy_modularity_communities(G)
print res
def test_non_disjoint_partition():
    G = nx.barbell_graph(3, 0)
    partition = ({0, 1, 2}, {2, 3, 4, 5})
    kernighan_lin_bisection(G, partition)
Exemple #28
0
def test_partition_argument():
    G = nx.barbell_graph(3, 0)
    partition = [{0, 1, 2}, {3, 4, 5}]
    C = kernighan_lin_bisection(G, partition)
    assert_partition_equal(C, partition)
print('Graph H: Original graph')
H = nx.read_graphml("problem1-ZacharysKarateClub.graphml")
my_module.print_graph_info(H)
my_module.draw_node_size_by_deg(H)
print('============')

# partition the network into clusters using kernighan_lin_bisection
'''
The input to the algorithm is an undirected graph G = (V,E) with vertex set V, edge set E, and (optionally) 
numerical weights on the edges in E. The goal of the algorithm is to partition V into two disjoint subsets 
A and B of equal (or nearly equal) size, in a way that minimizes the sum T of the weights of the subset of 
edges that cross from A to B. If the graph is unweighted, then instead the goal is to minimize the number 
of crossing edges; this is equivalent to assigning weight one to each edge.
'''
(set1, set2) = community.kernighan_lin_bisection(H,
                                                 partition=None,
                                                 max_iter=40,
                                                 weight='weight')
H1 = H.subgraph(set1)  # create a subgraph with notes in set1
H2 = H.subgraph(set2)  # create a subgraph with notes in set2

print('set1')
print(set1)
my_module.print_graph_info(H1)
my_module.draw_node_size_by_deg(H1)
print('============')

print('set2')
print(set2)
my_module.print_graph_info(H2)
my_module.draw_node_size_by_deg(H2)
Exemple #30
0
def test_non_disjoint_partition():
    with pytest.raises(nx.NetworkXError):
        G = nx.barbell_graph(3, 0)
        partition = ({0, 1, 2}, {2, 3, 4, 5})
        kernighan_lin_bisection(G, partition)