do you need this additional intermediate data structure "edges" ? Seems to me that if you initialized 'g' right upfront, you could do addition of newly generated edges to g on the fly as edges are being randomly created.