summaryrefslogtreecommitdiffstats
path: root/ic_experiments/old_stuff/distances.py
diff options
context:
space:
mode:
authorThibaut Horel <thibaut.horel@gmail.com>2015-09-14 23:08:02 -0400
committerThibaut Horel <thibaut.horel@gmail.com>2015-09-14 23:08:02 -0400
commitab0b1f3cefedb35327a19ec1b6afd560bfdf802d (patch)
treeb777f3e2c0ac0e712d8c5faab5107b1d236e2c3a /ic_experiments/old_stuff/distances.py
parent960676226862d2d68c7a9c04c56d4f8157803025 (diff)
downloadcriminal_cascades-ab0b1f3cefedb35327a19ec1b6afd560bfdf802d.tar.gz
Import supplements and repo reorganization
Diffstat (limited to 'ic_experiments/old_stuff/distances.py')
-rw-r--r--ic_experiments/old_stuff/distances.py42
1 files changed, 42 insertions, 0 deletions
diff --git a/ic_experiments/old_stuff/distances.py b/ic_experiments/old_stuff/distances.py
new file mode 100644
index 0000000..1834d36
--- /dev/null
+++ b/ic_experiments/old_stuff/distances.py
@@ -0,0 +1,42 @@
+import networkx as nx
+import sys
+from collections import Counter
+
+
+def build_graph(filename):
+ g = nx.Graph()
+ with open(filename) as fh:
+ fh.readline()
+ for line in fh:
+ i, j = map(lambda x: int(float(x)), line.strip().split(",")[1:])
+ g.add_edge(i, j)
+ return g
+
+
+def distances(g):
+ victims = set()
+ with open("vics.csv") as fh:
+ fh.readline()
+ for line in fh:
+ victims.add(int(line.strip().split(",")[1]))
+ level = 0
+ seen = {}
+ nextlevel = {v: 1 for v in victims}
+ while level <= 3:
+ thislevel = nextlevel
+ nextlevel = {}
+ for v in thislevel:
+ if v not in seen:
+ seen[v] = level
+ nextlevel.update(g[v])
+ level += 1
+ return seen
+
+
+if __name__ == "__main__":
+ g = build_graph(sys.argv[1])
+ print g.number_of_nodes(), g.number_of_edges()
+ cnt = Counter()
+ for v, d in distances(g).iteritems():
+ cnt[d] += 1
+ print cnt.most_common()