jeudi 22 février 2018

Generate random graphs with different topologies and a variable number of vertices preserving that topology

I'm a newbie at using networkX package. So l would like to generate random graphs as illustrated in the following figure :

such that for each topology let's take Ring :

1)l can generate 5 variants of ring by addding more nodes (1 ,3 ,5 ..) and it preserves topology of ring. (same for other topologies)

2) Assign to each vertice a random vector of 200 values

3) Assign to each vertice a different color (randomly : red, green or blue).

enter image description here

Here is what l tried :

import matplotlib.pyplot as plt
import networkx as nx
import random as random
from random import randint
import numpy as np
from itertools import chain

def colors(n): # generate n random colors (n is the number of vertices)

        ret = []
        color = []
        r = int(random.random() * 256)
        g = int(random.random() * 256)
        b = int(random.random() * 256)
        step = 256 / n
        for i in np.arange(n):
            r += step
            g += step
            b += step
            r = int(r) % 256
            g = int(g) % 256
            b = int(b) % 256
            get_max = np.argmax((r, g, b))
            if get_max == 0:
                ret.append((r, g, b))
                color.append('red')
            elif get_max == 1:
                ret.append((r, g, b))
                color.append('green')
            else:
                ret.append((r, g, b))
                color.append('blue')
        return ret, color


def main(p):
    n = p  #number of vertices
    k = np.arange(n)
    list_node = list(k)
    #G= nx.star_graph(n) # l don't get enough variability, when l increase the number of vertices l lose star topology
    #G=nx.generators.wheel_graph(n) # l don't get enough variability, when l increase the number of vertices l lose wheel topology topology
    #G = nx.random_geometric_graph(n,radius=3)

    G.add_nodes_from(list_node)
    list_edges = []
    for i in np.arange(30): # generate random edges 
        (x, y) = randint(1, n), randint(1, n)
        list_edges.append((x, y))
    for j in k: # loop to be sure that the graph is connected and each vertice has at least one edge
        if j not in list(chain.from_iterable(list_edges)):
            r = randint(1, n)
            if r == j:
                while r == j:
                    r = randint(1, n)
                p, z = (j, r)
                list_edges.append((p, z))
            else:
                list_edges.append((j, r))
    # print(list_edges)
    G.add_edges_from(list_edges)

    rgb, color = colors(n + 1)
    x = 0
    node_color = color

    for v in G.nodes():
        print('inde node ', v)
        print(rgb[v])
        G.node[v]['value'] = rgb[v]
        print('graph')
        print(G.nodes(v))
        x += 10
    #print(G.node[1]['value'])
    nx.draw(G, node_color=node_color, with_labels=True)
    plt.title('Star graph with '+str(p+1)+ ' nodes')
    plt.show()

My purpose is to get for instance line graph with 5 vertices, 10 vertices, 50 vertices but the topology is preserved. The same for the remaining topologies

Hope it's more clear.

Thank you for your help




Aucun commentaire:

Enregistrer un commentaire