예제 #1
0
    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')
예제 #2
0
        source_node = next(
            iter(sorted(nx.connected_components(G), key=len, reverse=True)[i]))
        dest_node = next(
            iter(sorted(nx.connected_components(G), key=len, reverse=True)[0]))
        print("SOURCE NODEE", source_node)
        print("DEST NODEE", dest_node)
        G.add_edge(int(source_node), int(dest_node), weight=randint(1, 1))

assert nx.is_connected(G)

print(sorted(sorted(nx.connected_components(G), key=len, reverse=True)))

# 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)))

# G.add_edge(0, 3, weight=4)
for e in G.edges:
    a = e[0]
    b = e[1]

    w = randint(1, 1)
    G.add_edge(int(e[0]), int(e[1]), weight=w)
# print (a)

# Drawing the graph
node_pos = nx.spring_layout(G)

edge_weight = nx.get_edge_attributes(G, 'weight')
예제 #3
0
#    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()
예제 #4
0
import sys

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