title: Performing an edge contraction on a graph
body: <p>I am trying to do edge contraction on a graph.
  n is the number of vertices in the graph.
  The vector f containing pairs of vertices representing an edge contains the edges which are to be contracted.</p>
  
  <p>The graph does not contain any self loops.</p>
  
  <p>Please point out any logical mistakes if any in the code. 
  If the method is entirely wrong then please let me know the correct algorithm.</p>
  
  <pre><code>void modify(vector&lt;pair&lt;int, int&gt; &gt; &amp;f)
  {
      // sorting elements of each pair
      for(vector&lt;pair&lt;int, int&gt; &gt;::iterator it = f.begin(); it != f.end(); it++)
      {
        if(it-&gt;first &gt; it-&gt;second)
          swap(it-&gt;first, it-&gt;second);
      }
  
      // sorting elements of set f
      sort(f.begin(), f.end());
      reverse(f.begin(), f.end());
  
      for(vector&lt;pair&lt;int, int&gt; &gt;::iterator it = f.begin(); it != f.end(); it++)
      {
        int x, y;
        x = it-&gt;first;
        y = it-&gt;second;
  
        for(int i = 0; i &lt; n; i++)
          if(x != i)
          {
            graph[x][i] = graph[y][i] = graph[x][i] + graph[y][i];
            graph[i][x] = graph[i][y] = graph[i][x] + graph[i][y];
          }
      }
  
  
      vector&lt;bool&gt; pos(n, false); // array of positions to be deleted
  
      for(vector&lt;pair&lt;int, int&gt; &gt;::iterator it = f.begin(); it != f.end(); it++)
        pos[it-&gt;second] = true;
  
      // deleting rows
      int count = 0;
      for(int i = 0; i &lt; n; i++)
      {
              for(int j = 0; j &lt; n; j++)
                graph[i-count][j] = graph[i][j];
              if(pos[i])
                count++;
      }
  
      // deleting columns
      count = 0;
      for(int j = 0; j &lt; n; j++)
      {
              for(int i = 0; i &lt; n; i++)
                graph[i][j-count] = graph[i][j];
              if(pos[j])
                count++;
      }
  
      // finally dimension of the matrix becomes n - count
      n = n - count;
  }
  </code></pre>
  
  <p>Thanks in advance.</p>