Skip to content

Instantly share code, notes, and snippets.

@nzw0301
Last active August 31, 2015 18:34
Show Gist options
  • Save nzw0301/fd0aaa27c1389ead17cc to your computer and use it in GitHub Desktop.
Save nzw0301/fd0aaa27c1389ead17cc to your computer and use it in GitHub Desktop.
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