I am building an Erdos-Renyi graph from a set of nodes, which are class objects of different types. The class is taken from [blob example] (https://pythonprogramming.net/many-blob-objects-intermediate-python-tutorial/)
I start with an empty graph, create nodes that are the red and blob objects, but to have an Erdos-Renyi graph, I want these nodes to be connected with probability p. Using the Networkx syntax for such a graph creates it from scratch.
I found some similar posts here [complete graph] (Networkx: Creating a complete graph for a given set of nodes), but they did not help me with this random graph.
import pygame
import random
import networkx
from matplotlib import pyplot as plt
STARTING_BLUE_BLOBS = 10
STARTING_RED_BLOBS = 3
WIDTH = 800
HEIGHT = 600
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
RED = (255, 0, 0
class Blob:
def __init__(self, color):
self.x = random.randrange(0, WIDTH)
self.y = random.randrange(0, HEIGHT)
self.size = random.randrange(4,8)
self.color = color
def main():
blue_blobs = dict(enumerate([Blob(BLUE) for i in
range(STARTING_BLUE_BLOBS)]))
red_blobs = dict(enumerate([Blob(RED) for i in range(STARTING_RED_BLOBS)]))
Gb = nx.Graph()
for i in range(10):
Gb.add_node(blue_blobs[i])
for i in range(3):
Gb.add_node(red_blobs[i])
Gb = nx.erdos_renyi_graph(13,0.5)
nx.draw(Gb, with_labels=True)
plt.draw()
plt.show()
if __name__ == '__main__':
main()
How can I keep my nodes and have such a random graph using them? Thank you very much,