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.