1

I am attempting to reproduce algorithm 5 from the paper: http://vlado.fmf.uni-lj.si/pub/networks/doc/ms/rndgen.pdf

My code is the following:

import random
import numpy as np
from random import randrange
from random import randint
from networkx import empty_graph
def fast_generate_scale_free(n, d):
  #input: number of vertices: n, int
  #minimum degree d, int
  #output: scale-free multigraph 


  G=empty_graph(n)
  M=np.zeros(2 *n *d)
  if d >=1:
    for v in range(0,n):
      for i in range(0, d):
        v= M[int(2*(v*d+i))]
        #draw r uniformly at random
        r=randint(0, 2*(v*d+i))
      
       
        M[int(2*(v*d+i)+1)]=M[r]
        
    for i in range(0, (n*d)):
    
      a=M[int(2*i)]
      b=M[int(2*i+1)]
      G.add_edge(a,b)
     
  return G

Essentially I get an output of (0,0) for any choice of n and d, which I suspect is caused by my initialization of the M array.

I would appreciate any help and this is purely for my own understanding.

Rock910
  • 41
  • 2
  • Where do you get this output? This code doesn't create any output. – mkrieger1 Feb 15 '22 at 01:28
  • @mkrieger1: Apologies, I should have clarified, I printed G.edges() in my console. G1=fast_generate_scale_free(5,3), then G1.edges() gives me (0,0) for all choices of n and d – Rock910 Feb 15 '22 at 01:51
  • What do you find when you use a debugger? https://stackoverflow.com/questions/4929251/how-to-step-through-python-code-to-help-debug-issues – mkrieger1 Feb 15 '22 at 08:14
  • @mkrieger1 When I used a debugger, I noticed that going through the first and second for loops, basically when v=1(because of in range(0,n)) but v is set to zero after the second for loop, and this process repeats, giving only zeros at the end. I'm not sure why this happens but I think either having the initialization of the array to be all zeros, or might be some issue with v. – Rock910 Feb 16 '22 at 14:07
  • What did you intend `v= M[int(2*(v*d+i))]` to mean, given that `M` is initialized with `np.zeros`? – mkrieger1 Feb 16 '22 at 14:33
  • @mkrieger1 I think that I really want to initialize an array of length 2*n*d, in the paper it's mentioned that it's an edge list which can be initialized from a seed graph, so I guess my real confusion is how to properly initialize M so that the code works. I'm not entirely sure how to proceed. – Rock910 Feb 17 '22 at 02:27
  • Even if you initialized `M` correctly, you are still assigning to `v` both a value from `range(0,n)` and from `M`. That doesn't seem right, shouldn't this be two separate variables? – mkrieger1 Feb 17 '22 at 13:14

0 Answers0