title: Creating random undirected graph in C++
body: <p>The issue is I need to create a random undirected graph to test the benchmark of <a href="https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm" rel="nofollow noreferrer">Dijkstra's algorithm</a> using an array and heap to store vertices. AFAIK a heap implementation shall be faster than an array when running on sparse and average graphs, however when it comes to dense graphs, the heap should became less efficient than an array.</p>
  
  <p>I tried to write code that will produce a graph based on the input - number of vertices and total number of edges (maximum number of edges in undirected graph is n(n-1)/2).</p>
  
  <p>On the entrance I divide the total number of edges by the number of vertices so that I have a const number of edges coming out from every single vertex. The graph is represented by an adjacency list. Here is what I came up with:</p>
  
  <pre class="lang-c++ prettyprint-override"><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;time.h&gt;
  #include &lt;list&gt;
  #include &lt;set&gt;
  
  #define MAX 1000
  #define MIN 1
  
  class Vertex
  {
  public:
      int Number;
      int Distance;
      Vertex(void);
      Vertex(int, int);
      ~Vertex(void);
  };
  
  Vertex::Vertex(void)
  {
      Number = 0;
      Distance = 0;
  }
  
  Vertex::Vertex(int C, int D)
  {
      Number = C;
      Distance = D;
  }
  
  Vertex::~Vertex(void)
  {
  }
  
  
  int main()
  {
      int VertexNumber, EdgeNumber;
  
      while(scanf("%d %d", &amp;VertexNumber, &amp;EdgeNumber) &gt; 0)
      {
          int EdgesFromVertex = (EdgeNumber/VertexNumber);
  
          std::list&lt;Vertex&gt;* Graph = new std::list&lt;Vertex&gt; [VertexNumber];
  
          srand(time(NULL));
  
          int Distance, Neighbour;
          bool Exist, First;
  
          std::set&lt;std::pair&lt;int, int&gt;&gt; Added;
  
          for(int i = 0; i &lt; VertexNumber; i++)
          {
              for(int j = 0; j &lt; EdgesFromVertex; j++)
              {
                  First = true;
                  Exist = true;
  
                  while(First || Exist)
                  {
                      Neighbour = rand() % (VertexNumber - 1) + 0;
  
                      if(!Added.count(std::pair&lt;int, int&gt;(i, Neighbour)))
                      {
                          Added.insert(std::pair&lt;int, int&gt;(i, Neighbour));
                          Exist = false;
                      }
                      First = false;
                  }
              }
  
              First = true;
              std::set&lt;std::pair&lt;int, int&gt;&gt;::iterator next = Added.begin();
  
              for(std::set&lt;std::pair&lt;int, int&gt;&gt;::iterator it = Added.begin(); it != Added.end();)
              {
                  if(!First)
                      Added.erase(next);
  
                  Distance = rand() % MAX + MIN;
  
                  Graph[it-&gt;first].push_back(Vertex(it-&gt;second, Distance));
                  Graph[it-&gt;second].push_back(Vertex(it-&gt;first, Distance));
  
                  std::set&lt;std::pair&lt;int, int&gt;&gt;::iterator next = it;
                  First = false;
              }
          }
  
          // Dijkstra's implementation
      }
  
      return 0;
  }
  </code></pre>
  
  <p>I get an error:</p>
  
  <blockquote>
    <p>set iterator not dereferencable" when trying to create graph from set data.</p>
  </blockquote>
  
  <p>I know it has something to do with erasing set elements on the fly, however I need to erase them asap to diminish memory usage.</p>
  
  <p>Maybe there's a better way to create some undirectioned graph? Mine is pretty raw, but that's the best I came up with. I was thinking about making a directed graph which is easier task, but it doesn't ensure that every two vertices will be connected.</p>
  
  <p>I would be grateful for any tips and solutions!</p>