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

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 = {}
    dmax = sorted([d for n, d in G1.degree()], reverse=True)[0]
    for v in G1.nodes():
      for u in G1.nodes():
        Nu = set(G1.neighbors(u))
        Nv = set(G1.neighbors(v))
        if v > u and G1.has_edge(v, u) == False:
          
          # common neighbors
          # k = len(Nu.intersection(Nv))
          
          # jaccard
          # k = len(Nv.intersection(Nu))
          # k /= len(Nv.union(Nu))
          # k *= dmax
          
          # adamic-adar index
          # k = 0
          # for z in Nv.intersection(Nu):
          #   if len(list(G1.neighbors(z))) > 1:
          #     k += 1.0 / math.log(len(list(G1.neighbors(z))))
              
          # preferential attachment
          #  k = len(Nv) * len(Nu)
              
          if k > 0:
            commons[(v,u)] = int(k)
          if k > max_k:
            max_k = int(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()

