Created
September 5, 2014 07:42
-
-
Save wanirepo/e16e0f1b6af6bae17595 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from os import listdir | |
from os.path import isfile, join | |
import multiprocessing | |
import networkx as nx | |
from sklearn.utils.graph import single_source_shortest_path_length as sssp | |
fb_folder = "just8" | |
def func_sssp(component): | |
am = nx.adjacency_matrix(component) | |
length = len(component.nodes()) | |
geodesic_vals = [] | |
for idx in range(length): | |
sssp_dicts = sssp(am, int(idx)) | |
filtered = filter(lambda a: a != 0, sssp_dicts.values()) | |
geodesic_vals.extend(filtered) | |
#geodesic_vals.extend(sssp_dicts.values()) | |
return geodesic_vals | |
def get_max_component(g): | |
components = nx.connected_component_subgraphs(g) | |
return components.next() | |
def do(fb_file): | |
g = nx.read_edgelist(join(fb_folder,fb_file)) | |
giant_component = get_max_component(g) | |
geodesic_vals = func_sssp(giant_component) | |
return geodesic_vals | |
fb_files = [ f for f in listdir(fb_folder) if isfile(join(fb_folder,f)) ] | |
p = multiprocessing.Pool(multiprocessing.cpu_count()) | |
output_list = p.map(do, fb_files) | |
grand_total = 0 | |
total_num = 0 | |
max_val = 0 | |
for output in output_list: | |
grand_total += sum(output) | |
total_num += len(output) | |
max_output = max(output) | |
max_val = max(max_val, max_output) | |
mean = grand_total / total_num | |
print "MEAN:", mean | |
print "MAX :", max_val |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I will run this code on my machine asap and I will share the result. Thanks!