views:

98

answers:

1

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.

The graph does not contain any self loops.

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.

void modify(vector<pair<int, int> > &f)
{
    // sorting elements of each pair
    for(vector<pair<int, int> >::iterator it = f.begin(); it != f.end(); it++)
    {
      if(it->first > it->second)
        swap(it->first, it->second);
    }

    // sorting elements of set f
    sort(f.begin(), f.end());
    reverse(f.begin(), f.end());

    for(vector<pair<int, int> >::iterator it = f.begin(); it != f.end(); it++)
    {
      int x, y;
      x = it->first;
      y = it->second;

      for(int i = 0; i < 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<bool> pos(n, false); // array of positions to be deleted

    for(vector<pair<int, int> >::iterator it = f.begin(); it != f.end(); it++)
      pos[it->second] = true;

    // deleting rows
    int count = 0;
    for(int i = 0; i < n; i++)
    {
            for(int j = 0; j < n; j++)
              graph[i-count][j] = graph[i][j];
            if(pos[i])
              count++;
    }

    // deleting columns
    count = 0;
    for(int j = 0; j < n; j++)
    {
            for(int i = 0; i < n; i++)
              graph[i][j-count] = graph[i][j];
            if(pos[j])
              count++;
    }

    // finally dimension of the matrix becomes n - count
    n = n - count;
}

Thanks in advance.

+1  A: 

Given the following example graph:

a       d
 \     /
  b---e
 /     \
c       f

Would "contradicting" the edge {b, e} result in the following?:

a   d
 \ /
  g
 / \
c   f

"Yes, it is exactly that" - amit. Thx, I wanted to get the specification right, before I answer Your question. Further assumptions are:

  • Graph is directed
  • Vertices are represented through integers.
  • The graph is stored in the two dimensional array graph, graph[x][y] being the weight of the edge (x, y); 0 indicating that there is no edge from x to y

Lets give it a try with some pseudocode:

void contradictEdge(int v1, int v2) {
    for (int i = 0; i < n; i++) {
        if (graph[v2][i] > 0) {
            graph[v1][i] = graph[v1][i] > 0 ? min(graph[v1][i], graph[v2][i]) :
                                              graph[v2][i];
            graph[v2][i] = 0;
        }
        if (graph[i][v2] > 0) {
            graph[i][v1] = graph[i][v1] > 0 ? min(graph[i][v1], graph[i][v2]) :
                                              graph[i][v2];
            graph[i][v2] = 0;
        }
        graph[v1][v2] = graph[v2][v1] = 0;
    }
}

The code works like this: Given the edge {v1, v2}, v1 becomes the "contradicted" vertex. That means, instead of inserting a new vertex ("g" in my first example), v1 remains in the graph and v2 is deleted from it (by assigning 0 weights on edges to all other vertices, the actual vertex count doesn't change). Further, all edges that contain v2 are bend to contain v1.

Now one can call this method for a set of edges. The runtime will be O(n * #edges_to_contradict). I hope this is the behavior You desired.

Important: If You use a different representation for Your edge weights, namely 0 meaning an edge with 0 weight and ∞(infinite) indicating the absence of an edge, then Your problem becomes trivial because all You have to do is:

graph[v1][v2] = graph[v2][v1] = 0

which effectively contradicts the edge {v1, v2}, since now it doesn't cost anything to travel between v1 and v2

Dave
Yes, it is exactly that.
amit.codename13
@amit.codename13 I updated my answer based on Your additional information. Let me know if it's what You were looking for.
Dave