0

I am new to programming and I'm trying to solve a problem which says "find the average number of edges that an empty undirected graph needs to become connected"....it also asks to "keep track of isolated components along the way"...

I can add nodes :

from random import random, choice

import networkx as nx

G = nx.Graph()
i = int(input("enter the number of desired nodes : "))
for node in range(0, i):
    G.add_node(i)  # creates a graph with desired number of nodes
    i = i - 1

the output is like 10 9 ... 1 then I try to select two random nodes and connect them with an edge like this :

first_node = choice(G.nodes())
second_node = choice(G.nodes())
print(first_node, second_node) # print which nodes are selected

but it's not working...any idea?

  • So, what exactly is not working then? It's not selecting nodes? I would use `G.add_node(node)` instead of manipulation `i` in the for loop by the way. – Ronald May 24 '20 at 13:22
  • Where exactly are you connecting the nodes? Looks more like you are printing two random integers. – Jernej May 24 '20 at 13:28
  • @RonaldEvers . yes it does not select the nodes....I wanted the user to insert the number of nodes because the question asks for the average number of edges that makes the graph with n = 100, 200, ....., 1000 nodes connected – Samin Shapour May 24 '20 at 13:31
  • @Jernej I wanted to see which random nodes it selected..that print thing is just for me to see, it is unnecessary. I just need to add random edges to it until it becomes one component – Samin Shapour May 24 '20 at 13:35

1 Answers1

1

You need to be more specific about what kind of simulation you are trying to accomplish as there are different random processes for graph generation. Ler me know more specifically and I can edit the answer.

As an example the following code generates a graph with N vertices and iteratively adds random edges (without repetition). The process stops when the resulting graph is connected and returns the number of edges required to make it connected.

 from itertools import combinations
 from random import choice
 def simulate(n): 
     G = nx.Graph() 
     G.add_nodes_from(range(n)) 
     E = list(combinations(range(n), 2)) 

     while True: 
         e = choice(E) 
         E.remove(e) 
         G.add_edge(e[0], e[1]) 
         if nx.is_connected(G): 
             return G.size() 

We can see from here that, on average, we need to add 32 (random) edges to a graph with 20 vertices, for it to become connected.

In [24]: L = [simulate(20) for _ in range(1000)]                                

In [25]: sum(L)/len(L)                                                          
Out[25]: 32.739
Jernej
  • 342
  • 5
  • 18
  • thanks a lot...could you please tell me what was my mistake? – Samin Shapour May 24 '20 at 14:17
  • Hard to say since it was so different. As a start you were randomly picking two vertices which could result in picking equal vertices ( creating a loop) or previously chosen edges ( creating multiedges). In general its not that its wrong but you didn't specify what exactly is expected by your assignment, – Jernej May 24 '20 at 16:38