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

################################################################################
################################################################################
# Part 1: Link prediction on DNC email dataset
################################################################################
################################################################################
# Read in dataset and sort edges in temporal order. Use that to create graph on
# the first 25% of edges
G = nx.read_weighted_edgelist("out.dnc-temporalGraph.data", create_using=nx.MultiGraph(), comments="%")


################################################################################
# Calculate values for common neighbors, adamic/adar, preferential attachment,
# strong triadic closure, personalized PR, and katz


################################################################################
# First, compute common neighbors, adamic/adar, preferential attachment, and
# strong triadic closure


################################################################################
# Next compute personalized PageRanks for all (u->v) via the 
# linear algebraic formulation. The final value used for prediction will be the
# sum of (u->v) and (v->u) PPRs


################################################################################
# Now compute Katz


################################################################################
# take top 100 for each as our predictions


################################################################################
# compare the predicted links to the full dataset and output precision

precision_common_neighbors = 0.0
precision_adamic_adar = 0.0
precision_pref_attachment = 0.0
precision_triadic_closure = 0.0
precision_pagerank = 0.0
precision_katz = 0.0


print("Precision common neighbors:", round(precision_common_neighbors, 2))
print("Precision Adamic/Adar:", round(precision_adamic_adar, 2))
print("Precision preferential attachment:", round(precision_pref_attachment, 2))
print("Precision triadic closure:", round(precision_triadic_closure, 2))
print("Precision personalized pagerank:", round(precision_pagerank, 2))
print("Precision katz:", round(precision_katz, 2))


################################################################################
################################################################################
# Part 2: Centrality and Connectivity on the Congress Twitter dataset
################################################################################
################################################################################
G = nx.read_weighted_edgelist("congress.data")


################################################################################
# Initialize all of the centrality measures


################################################################################
# Sort the centrality values and associated nodes in order


################################################################################
# Create new graph to delete these nodes from


################################################################################
# Track how many of the top centrality nodes is required for removal from G
# in order to disconnect G into more than 1 component.
num_removed_DC = 0
num_removed_CC = 0
num_removed_BC = 0
num_removed_EC = 0
num_removed_PR = 0

# And output the results
print("Degree centrality number to disconnect:", num_removed_DC)
print("Closeness centrality number to disconnect:", num_removed_CC)
print("Betweenness centrality number to disconnect:", num_removed_BC)
print("Eigenvector centrality number to disconnect:", num_removed_EC)
print("Pagerank centrality number to disconnect:", num_removed_PR)

