def plot_graph(graph, title='', xlabel='', ylabel=''): """ Function uses matplotlib to create a log/log plot of the in-degree distribution of a graph. Args: graph: a dictionary representation of a directed graph where the keys are node names and the values are sets of edges. Returns: None and plots a log/log plot of the graph. """ # Calculate the in-degree distribution of the graph in_degree_dist = g.in_degree_distribution(graph) # Normalize the in-degree distribution to sum up to 1 normalized_distribution = g.normalize_distribution(in_degree_dist) # Plot the normalized distribution on a graph #plt.plot(normalized_distribution.keys(), normalized_distribution.values(), 'ro') plt.loglog(normalized_distribution.keys(), normalized_distribution.values(), 'bo', basex=10, basey=10) # Add attributes to the plot plt.title(title) plt.xlabel(xlabel) plt.ylabel(ylabel) plt.show()
def plot_citation_graph(): """ Function for plotting the in-degree distribution of a specific citation graph. """ # Load the external graph of citation information citation_graph = g.load_graph(CITATION_URL) # Calculate the in-degree distribution of the citation graph in_degree_dist = g.in_degree_distribution(citation_graph) # Normalize the in-degree distribution to sum up to 1 normalized_distribution = g.normalize_distribution(in_degree_dist) # Plot the normalized distribution on a log/log graph plt.loglog(normalized_distribution.keys(), normalized_distribution.values(), 'ro', basex=10, basey=10) plt.title( 'Log/log plot of the normalized distribution of a citation graph') plt.ylabel('Number of citations - base 10') plt.xlabel('Papers - base 10') plt.show()
def plot_citation_graph(): """ Function for plotting the in-degree distribution of a specific citation graph. """ # Load the external graph of citation information citation_graph = g.load_graph(CITATION_URL) # Calculate the in-degree distribution of the citation graph in_degree_dist = g.in_degree_distribution(citation_graph) # Normalize the in-degree distribution to sum up to 1 normalized_distribution = g.normalize_distribution(in_degree_dist) # Plot the normalized distribution on a log/log graph plt.loglog(normalized_distribution.keys(), normalized_distribution.values(), 'ro', basex=10, basey=10) plt.title('Log/log plot of the normalized distribution of a citation graph') plt.ylabel('Number of citations - base 10') plt.xlabel('Papers - base 10') plt.show()
import graph def er(n, p): outgraph = dict(enumerate(range(n))) for key in outgraph.keys(): outgraph[key] = set([]) nodes = set([item for item in outgraph.keys() if item != key]) for node in nodes: rand = random.random() if rand < p: outgraph[key].add(node) return outgraph percentage = 0.01 num_nodes = 27770 in_degrees = graph.in_degree_distribution(er(num_nodes, percentage)) def divide(x): return x / float(num_nodes) normalized = map(divide, in_degrees.values()) pyplot.loglog(in_degrees.keys(), normalized, 'bo') pyplot.xlabel('degree') pyplot.ylabel('normalized frequency') pyplot.title('ER graph - normalized degree distribution') pyplot.show()
def dpa(n, m): outgraph = graph.make_complete_graph(m) dpat = DPATrial.DPATrial(m) i = m while i < n: outgraph[i] = dpat.run_trial(m) i += 1 return outgraph num_nodes = 100 avg_out_degree = 13 dpaGraph = dpa(num_nodes, avg_out_degree) in_degrees = graph.in_degree_distribution(dpaGraph) # def divide(x): # return x / float(num_nodes) # # normalized = map(divide, in_degrees.values()) # # # # pyplot.loglog(in_degrees.keys(), normalized, 'bo') # pyplot.xlabel('degree') # pyplot.ylabel('normalized frequency') # pyplot.title('DPA graph - normalized degree distribution') # pyplot.show()
# -*- coding: utf-8 -*- """ Created on Tue Aug 26 21:00:06 2014 @author: Administrator """ import graph import random import matplotlib.pyplot as plt def upa(n,m): result_graph = graph.make_complete_graph(m) for i in range(m,n): edeg = [] for count in range(m): edeg.append(random.choice(result_graph.keys())) result_graph[i] = set(edeg) return result_graph if __name__ == "__main__": g = upa(27770,13) edeg_num = 0 for key in g.keys(): edeg_num += len(g[key]) distribution = graph.in_degree_distribution(g) for dummy_key in distribution.keys(): distribution[dummy_key] = (float)(distribution[dummy_key]) / 27770 plt.loglog(distribution.keys(),distribution.values())
node = int(neighbors[0]) answer_graph[node] = set([]) for neighbor in neighbors[1 : -1]: answer_graph[node].add(int(neighbor)) return answer_graph citation_graph = load_graph(CITATION_URL) total = float(27770) def divide(x): """ division method """ return x / total; in_degrees = graph.in_degree_distribution(citation_graph) print(in_degrees) print graph.avg_in_degree(in_degrees) # normalized = map(divide, in_degrees.values()) # print(len(normalized), "length") # # def add(x,y): return x + y # # print(reduce(add, normalized)) # # print normalized # # pyplot.loglog(in_degrees.keys(), normalized, 'bo') # pyplot.xlabel('degree')
def dpa(n, m): outgraph = graph.make_complete_graph(m) dpat = DPATrial.DPATrial(m) i = m while i < n: outgraph[i] = dpat.run_trial(m) i += 1 return outgraph num_nodes = 100 avg_out_degree = 13 dpaGraph= dpa(num_nodes, avg_out_degree) in_degrees = graph.in_degree_distribution(dpaGraph) # def divide(x): # return x / float(num_nodes) # # normalized = map(divide, in_degrees.values()) # # # # pyplot.loglog(in_degrees.keys(), normalized, 'bo') # pyplot.xlabel('degree') # pyplot.ylabel('normalized frequency') # pyplot.title('DPA graph - normalized degree distribution') # pyplot.show()