J'essaie de faire une contraction de bord sur un graphique. n est le nombre de sommets dans le graphique. Le vecteur f contenant des paires de sommets représentant une arête contient les arêtes à contracter.Exécution d'une contraction de bord sur un graphique
Le graphique ne contient aucune auto-boucle.
Veuillez indiquer les éventuelles erreurs logiques dans le code. Si la méthode est entièrement fausse alors s'il vous plaît laissez-moi savoir l'algorithme correct.
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;
}
Merci d'avance.
Je peux être mal interprété, mais où est défini? –
En fait n et le graphe sont définis globalement.Il n'y a pas de bonne raison de les rendre globaux, mais j'ai posté mon code uniquement pour savoir si mon algorithme est correct. – amitkarmakar