from degree_distribution_for_graphs import normalized_in_degree_distribution def er(n, p): """ Implmentation of Erdos-Renyi algorithm to generate random directed graph. Returns: A directed graph. """ digraph = {key: set() for key in xrange(n)} for u in xrange(n): for v in xrange(n): if u != v: if random.random() < p: digraph[u].add(v) return digraph digraph = er(1000, 0.2) distribution = normalized_in_degree_distribution(digraph) plt.title("Normalized in-degree distribution (Point graph)") plt.xlabel("In-degrees (log)") plt.ylabel("Normalized Values (log)") plt.loglog(distribution.keys(), distribution.values(), "ro") plt.show()
# compute the neighbors for the newly-created node new_node_neighbors = set() for dummy_idx in range(num_nodes): new_node_neighbors.add(random.choice(self._node_numbers)) # update the list of node numbers so that each node number # appears in the correct ratio self._node_numbers.append(self._num_nodes) self._node_numbers.extend(list(new_node_neighbors)) #update the number of nodes self._num_nodes += 1 return new_node_neighbors def DPA(nodes, m): dpa_graph = make_complete_graph(m) dpa = DPATrial(m) for i in range(nodes-m): v_dash = dpa.run_trial(m) dpa_graph[i+m] = v_dash return dpa_graph normalized_digraph = normalized_in_degree_distribution(DPA(27770, 12)) plt.title("DPA Algorithm") plt.xlabel("DPA Graph In-degrees (log)") plt.ylabel("Probability (log)") plt.loglog(normalized_digraph.keys(), normalized_digraph.values(), 'ro') plt.show()