def __init__(self, operationCount, peerCount, graphInput): print("Inside Simulation init") self._op_count = int(operationCount) self._peer_count = int(peerCount) plt.axis('off') graph = nx.read_weighted_edgelist(graphInput) print(graph) # some properties print("node degree clustering") for v in nx.nodes(graph): print('%s %d %f' % (v, nx.degree(graph, v), nx.clustering(graph, v))) # print the adjacency list to terminal try: nx.write_adjlist(graph, sys.stdout) except TypeError: nx.write_adjlist(graph, sys.stdout.buffer) # node_pos = nx.spring_layout(graph) # # edge_weight = nx.get_edge_attributes(graph, 'weight') # # Draw the nodes # nx.draw_networkx(graph, node_pos, node_color='grey', node_size=100) # # Draw the edges # nx.draw_networkx_edges(graph, node_pos, edge_color='black') # # Draw the edge labels # nx.draw_networkx_edge_labels(graph, node_pos, edge_color='red', edge_labels=edge_weight) # plt.show() paths_for_diameter = nx.shortest_path_length(graph, weight='weight') ecc = nx.eccentricity(graph, sp=dict(paths_for_diameter)) self._diameter = nx.diameter(graph, e=ecc) print('The graph diameter is ', self._diameter) self._height_of_cluster = math.ceil(math.log(self._diameter, 2)) + 1 if not os.path.exists(graphInput+'_network'): print("CREATING NEW NETWORK") self._network = Network(graph) self._setup_peers(graph) # self._build_clusters_exactly_logn_membership(graph) self._build_clusters_at_least_one_and_at_most_logn(graph) # self._build_clusters_no_overlapping(graph) self._build_tree(graph) self.save_network(graphInput + '_network') exit(0) else: print("LOADING NETWORK FROM INPUT FILE") # load network self.load_network(graphInput + '_network')
import sys import numpy as np import matplotlib.pyplot as plt from networkx import nx n = 10 # 10 nodes p = .2 # 20 edges p2 = 2 * np.log(n) / n G = nx.gnp_random_graph(n, p2) # some properties print("node degree clustering") for v in nx.nodes(G): print('%s %d %f' % (v, nx.degree(G, v), nx.clustering(G, v))) # print the adjacency list to terminal try: nx.write_adjlist(G, sys.stdout) except TypeError: # Python 3.x nx.write_adjlist(G, sys.stdout.buffer) nx.draw(G) plt.show()
# Copyright (C) 2004-2017 by # Aric Hagberg <*****@*****.**> # Dan Schult <*****@*****.**> # Pieter Swart <*****@*****.**> # All rights reserved. # BSD license. import sys import matplotlib.pyplot as plt from networkx import nx n = 10 # 10 nodes m = 20 # 20 edges G = nx.gnm_random_graph(n, m) # some properties print("node degree clustering") for v in nx.nodes(G): print('%s %d %f' % (v, nx.degree(G, v), nx.clustering(G, v))) # print the adjacency list to terminal try: nx.write_adjlist(G, sys.stdout) except TypeError: # Python 3.x nx.write_adjlist(G, sys.stdout.buffer) nx.draw(G) plt.show()
import matplotlib.pyplot as plt from networkx import nx, json_graph plt.axis('off') # graph = nx.read_gml("test_gml") graph = nx.read_weighted_edgelist("8_0.01diamter3_newtest.weighted.edgelist") # some properties print("node degree clustering") for v in nx.nodes(graph): print('%s %d %f' % (v, nx.degree(graph, v), nx.clustering(graph, v))) # print the adjacency list to terminal try: nx.write_adjlist(graph, sys.stdout) except TypeError: nx.write_adjlist(graph, sys.stdout.buffer) node_pos = nx.spring_layout(graph) edge_weight = nx.get_edge_attributes(graph, 'weight') # Draw the nodes nx.draw_networkx(graph, node_pos, node_color='grey', node_size=100) # Draw the edges nx.draw_networkx_edges(graph, node_pos, edge_color='black') # Draw the edge labels nx.draw_networkx_edge_labels(graph, node_pos,