Last active
August 31, 2015 18:34
-
-
Save nzw0301/fd0aaa27c1389ead17cc to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import fileinput | |
import networkx as nx | |
g = nx.Graph() # 無向グラフ | |
file = "sample.txt" | |
# file = "facebook_combined.txt" | |
for line in fileinput.input(file): | |
edge = list(map(int, line.split())) | |
g.add_edge(edge[0], edge[1]) | |
cutoff = 2 | |
pair = nx.all_pairs_shortest_path_length(g, cutoff) | |
node_dic = {} | |
size = len(g) | |
nodes = list(pair.keys()) | |
for i in range(size): | |
doc = [] | |
for node,distance in pair[nodes[i]].items(): | |
if distance == cutoff: | |
doc.append(node) | |
else: | |
doc.extend([node]*cutoff) | |
node_dic[nodes[i]] = doc | |
for k,v in node_dic.items(): | |
print(k, ",".join(map(str, v))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment