def get_color(i, r_off=1, g_off=1, b_off=1):
    n = 16
    low, high = 0.1, 0.9
    span = high - low
    r = low + span * (((i + r_off) * 4) % n) / (n - 1)
    g = low + span * (((i + g_off) * 8) % n) / (n - 1)
    b = low + span * (((i + b_off) * 12) % n) / (n - 1)
    return (r, g, b)


G = nx.read_gexf('../../dataset/graph-small/graph.gexf')

# Remove social media accounts and isolates from the dataset
G = remove_unnecessary_data(G)

communities = girvan_newman(G)

node_groups = []
for community in next(communities):
    node_groups.append(list(community))

node_colors = []
for ng in range(len(node_groups)):
    for node in G:
        if node in node_groups[ng]:
            node_colors.append(get_color(ng))

nx.draw(G, node_color=node_colors)
plt.show()
# print(adjmat)

# Construct a networkx graph from the adjacency matrix
# (Singleton nodes are excluded from the graph)
G = make_graph(adjmat, labels=Config.labels)
# nx.draw(G, with_labels=True)
"""---


# ##Community detection using Girvan-Newman
# """

from networkx.algorithms.community.centrality import girvan_newman
# from networkx.algorithms.community import k_clique_communities

comp = girvan_newman(G)

max_shown = 1
shown_count = 1
possibilities = []
for communities in itertools.islice(comp, max_shown):
    # print(communities) # a tuple
    # print("Possibility", shown_count, ": ", end='')
    # print(communities)
    possibilities.append(communities)
    color_map = ["" for x in range(len(G))]
    color = 0
    for c in communities:
        indices = [i for i, x in enumerate(G.nodes) if x in c]
        for i in indices:
            color_map[i] = Config.colors[color]
Exemple #3
0
def main():

  # Load data
  nodes = pd.read_csv("../data/nodes.csv", sep='\t', index_col=0)

  # Data in nice form
  headers = list(nodes.columns)
  nodes = np.asarray(nodes)

  # Load social network accordingly
  edges = pd.read_csv("../data/edges.csv", sep='\t', index_col=0)
  edges = np.asarray(edges).astype(int)
  G = nx.Graph()
  G.add_nodes_from(range(nodes.shape[0]))
  G.add_edges_from(list(map(tuple, edges)))

  #first compute the best partition
  print("Computing Girvan Newman Algorithm")
  start = timeit.default_timer()
  comp = girvan_newman(G)
  print(tuple(sorted(c) for c in next(comp)))
  '''
  max_iterations = 100
  for i in range(max_iterations):
    print(gir[i])
  '''
  stop = timeit.default_timer()

  # Computing modularity
  '''
  num_cmtys = len(set(partition.values()))
  num_edges = edges.shape[0]
  cmtys = [[] for _ in range(num_cmtys)]
  for node in partition.keys():
    cmtys[partition[node]].append(node)

  # Load social network accordingly
  if path.exists("../data/youtube.graph"):
    FIn = snap.TFIn("../data/youtube.graph")
    social_network = snap.TNGraph.Load(FIn)
  else:
    social_network = data2dag(edges, nodes.shape[0])
  '''

  '''
  modularity = 0
  for cmty in cmtys:
    Nodes = snap.TIntV()
    for elem in cmty:
      Nodes.Add(int(elem))
    modularity += snap.GetModularity(social_network, Nodes, num_edges)
  '''
  '''
  print("Calculating Modularity")
  assert(is_partition(G, cmtys))
  modul = modularity(G, cmtys)
  print("Results from Louvain:")
  print("Modularity:",modul)
  print("Number of clusters:",num_cmtys)
  print("Time elapsed:",stop - start)
  '''


  #drawing
  '''
    def get_network_partitions(self, matrix, num_groups, threshold=0.5):
        first_array = matrix.nonzero()[0]
        second_array = matrix.nonzero()[1]
        # print("num edges before filter: " + str(len(first_array)))
        graph = nx.Graph()
        nodes = set()

        weights_dict = {}
        # print(len(first_array))
        for i in range(len(first_array)):
            if first_array[i] < second_array[i] and matrix[first_array[i], second_array[i]] > threshold:
                weights_dict[i] = matrix[first_array[i], second_array[i]]

        num_edges = 0
        sorted_weights = [i for i in sorted(weights_dict, key=weights_dict.get, reverse=True)]

        limit = min(len(sorted_weights), 1000)
        for i in sorted_weights[0: limit]:
            nodes.add(first_array[i])
            nodes.add(second_array[i])
            graph.add_edge(first_array[i], second_array[i])
            num_edges += 1

        #print("num nodes after filter: " + str(len(nodes)))
        graph.add_nodes_from(list(nodes))

        nodes = list(nodes)
        nodes_to_remove = []

        for sub_graph in connected_components(graph):
            if len(sub_graph) < 4:
                for node_id in sub_graph:
                    nodes_to_remove.append(node_id)
                    del nodes[nodes.index(node_id)]

        # print("num nodes after cliques: " + str(len(nodes)))

        for node_id in nodes_to_remove:
            graph.remove_node(node_id)

        comp = girvan_newman(graph)
        partition = ()

        for communities in comp:
            partition = tuple(sorted(c) for c in communities)
            if len(partition) >= num_groups:
                break
            elif len(partition) > num_groups:
                partition = self.reduce_partition(partition, num_groups)
                break
            else:
                continue

        parts = [-1] * len(nodes)
        for group_id, group in enumerate(partition):
            for index in group:
                parts[nodes.index(index)] = group_id

        modified_parts = []
        modified_nodes = []

        for i, part in enumerate(parts):
            if part >= 0:
                modified_parts.append(part)
                modified_nodes.append(nodes[i])

        return modified_nodes, modified_parts, graph
Exemple #5
0
import networkx as nx
import pandas as pd
import matplotlib.pyplot as plt
import itertools

from networkx.algorithms.community.centrality import girvan_newman

B = pd.read_csv('dm-graphtask-adj.txt', header=None)

G = nx.from_numpy_matrix(B.values)

comp = girvan_newman(G, most_valuable_edge=None)

k = 4
counter = 0
clusters = []

for communities in itertools.islice(comp, k):
    if counter == 3:
        for c in communities:
            nodelist = list(c)
            clusters.append(nodelist)
            print (nodelist)

    counter += 1

pos = nx.spring_layout(G)

# nodes
colors = ['r', 'g', 'b', 'y', 'm']
Exemple #6
0
    elif (partition[node] == 3):
        color_list.append("Blue")
    elif (partition[node] == 4):
        color_list.append("Purple")
    elif (partition[node] == 5):
        color_list.append("Green")
    elif (partition[node] == 6):
        color_list.append("Brown")

drawGraph(G, color_list, False)
'''
#######################################################################
Step 10 output: Girvan-Newman
a) Output the number of communities, the size of largest community, size of smallest community, and modularity of this partitioning
#######################################################################'''
components = c.girvan_newman(G)
i = 0
for row in components:
    if (i == 0):
        finalResult = row
    # print(row)
    i += 1

minCommunity = len(list(G.nodes)) + 1
maxCommunity = 0
ctr = 0
partitions = dict()
L = list(finalResult)
p = 0
for comp in L:
    for entry in comp:
Exemple #7
0
    # and add some random noise.
    centrality = {e: c / max_cent for e, c in centrality.items()}
    # Add some random noise.
    centrality = {e: c + random() for e, c in centrality.items()}
    return max(centrality, key=centrality.get)
    G = nx.path_graph(10)


if len(sys.argv) < 2:
    sys.stderr.write("Usage: %s <input graph>\n" % (argv[0], ))
    sys.exit(1)
graph_fn = sys.argv[1]
G = nx.Graph()  #let's create the graph first
buildG(G, graph_fn, '\t')

comp = girvan_newman(G, most_valuable_edge=most_central_edge)
com = 0
thisdict = {}

# Populating the items of the dictionary
for c in next(comp):
    list = sorted(c)
    for i in range(len(list)):
        if list[i] in thisdict:
            print('already found')
        else:
            thisdict.update({list[i]: com})
        i += 1
    com += 1

dict_nodes_girvan = {}