def compute_greedy_newman(n_vertex, edge_list): from agglomcluster import NewmanGreedy graph = tf.compute_networkx_form(n_vertex, edge_list) t = time.time() clst = NewmanGreedy(graph) clusters = clst.get_clusters() exectime = time.time() - t labels = tf.compute_labels_from_clusters(n_vertex, clusters) return labels, clusters, exectime
def extract_biggest_component(filename): from load_data import download_graph as dd n_vertex, edge_list = dd(filename) import networkx as nx from transform_functions import compute_networkx_form graph = compute_networkx_form(n_vertex, edge_list) components = list(nx.connected_component_subgraphs(graph)) if len(components) > 1: components.sort(key = len, reverse = True) newgraph = components[0] n_vertex_new = len(newgraph.nodes()) new_vertex_indices = {} for i in xrange(n_vertex_new): new_vertex_indices[newgraph.nodes()[i]] = i+1 f = open(filename[:-4]+'_new.txt', "w") f.write(str(n_vertex_new)+' '+str(len(newgraph.edges()))+'\n') for i in xrange(len(edge_list)): if edge_list[i][0] in newgraph.nodes(): if len(edge_list[0]) == 2: f.write(str(new_vertex_indices[edge_list[i][0]])+' '+str(new_vertex_indices[edge_list[i][1]])+'\n') else: f.write(str(new_vertex_indices[edge_list[i][0]])+' '+str(new_vertex_indices[edge_list[i][1]])+' '+str(edge_list[i][2])+'\n') f.close() import os if os.path.isfile(filename[:-4]+'_labels.txt'): f = open(filename[:-4]+'_labels.txt', "r") f1 = open(filename[:-4]+'new_labels.txt', "w") for i in xrange(n_vertex): s = f.readline() if i in newgraph.nodes(): f1.write(s) f.close() f1.close()