import networkx as nx
import numpy as np
import matplotlib.pyplot as plt
import random
import operator

G = nx.read_weighted_edgelist("out.moreno_sheep_sheep", create_using=nx.Graph(), comments="%")

# Does the strength of local ties correlate with the number of common neighbors?
overlaps = {}
for e in G.edges():
	overlaps[e] = len(list(nx.common_neighbors(G, e[0], e[1]))) / (G.degree(e[0]) + G.degree(e[1]))

tie_strengths = sorted(G.edges(data=True), key=lambda t: t[2].get('weight', 1))

data = []
strength = 1.0
cur_sum = 0.0
cur_num = 0
for e in tie_strengths:
	if e[2]['weight'] != strength:
		if cur_num > 0:
			data.append((strength, cur_sum / cur_num))
		cur_sum = overlaps[(e[0],e[1])]
		cur_num = 1
		strength = e[2]['weight']
	else:
		cur_num += 1
		cur_sum += overlaps[(e[0],e[1])]

data.append((strength, cur_sum / cur_num))

# appears to be case
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot([k for (k,v) in data], [v for (k, v) in data])
plt.show()


# does removing weak ties disconnects the graph quicker?
tie_strengths = sorted(G.edges(data=True), key=lambda t: t[2].get('weight', 1), reverse=True)
tie_strengths = sorted(G.edges(data=True), key=lambda t: t[2].get('weight', 1), reverse=False)

counter = 0
for e in tie_strengths:
	G.remove_edge(e[0], e[1])
	counter += 1
	if nx.is_connected(G) == False:
		break 
	if counter % 10 == 0:
		nx.draw(G)
		plt.show()

print(counter)


# number of common neighbors is a strong predictor of link formation
G = nx.read_weighted_edgelist("out.dnc-temporalGraph", create_using=nx.MultiGraph(), comments="%")

edges = sorted(G.edges(data=True), key=lambda t: t[2].get('weight', 1))

G1 = nx.MultiGraph()
t_0 = G.size() / 20
counter = 0
max_k = 0
for e in edges:
	G1.add_edge(e[0], e[1])
	counter += 1
	if counter > t_0:
		t_0 = G.size() + 1
		commons = {}
		for v in G.nodes():
			for u in G.nodes():
				if v > u and G1.has_edge(v, u) == False:
					k = len(list(nx.common_neighbors(G, v, u)))
					if k > 0:
						commons[(v,u)] = k
					if k > max_k:
						max_k = k

total_counts = [0]*(max_k+1)
edge_counts = [0]*(max_k+1)
for c in commons:
	total_counts[commons[c]] += 1
	if G1.has_edge(c[0], c[1]):
		edge_counts[commons[c]] += 1

probs = [0]*(max_k+1)
for i in range(0, max_k+1):
	if total_counts[i] > 0:
		probs[i] = edge_counts[i] / total_counts[i]

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(probs)
plt.show()

