Removing constraint from a model in gurobi c++ - c++

I have one constraint set
after some modification I have to remove this constraint:model.addConstr(LHS10_2 <= LHS10_1)set from the model. model.remove() is not working. How can I do it?
Thank for your help.
model.remove(LHS10_2 <= LHS10_1) can not work.
for (i = 1; i <= ULD; i++)
{
for (j = 1; j <= station; j++)
{
GRBLinExpr LHS10_1 = 0;//自載飛航節線
GRBLinExpr LHS10_2 = 0;//他航載飛航節線
for (k = 2; k <= load; k++)
{
if (k == 2 || k == 3 || k == 7)
{
for (l = 1; l <= (node - 2); l++)
{
for (m = 1; m <= Num_FAn[j][k][l]; m++)
{
LHS10_1 += p*X[i][j][k][l][FSAn[j][k][l][m]][FLAn[j][k][l][m]][FNAn[j][k][l][m]];
}
}
}
if (k == 4 || k == 5)
{
for (l = 1; l <= (node - 2); l++)
{
for (m = 1; m <= Num_FAn[j][k][l]; m++)
{
LHS10_2 += X[i][j][k][l][FSAn[j][k][l][m]][FLAn[j][k][l][m]][FNAn[j][k][l][m]];
}
}
}
}
model.addConstr(LHS10_2 <= LHS10_1);
}
}

The method GRBModel::addConstr() returns an GRBConstr object that you should save in a local variable. Then at a later point in time, you can use the GRBModel::remove() method to delete that particular constraint from the model again, i.e., you could do something like
// Array to hold added constraint objects
GRBConstr* c = new GRBConstr[nConstr];
for (int k = 0; k < nConstr, ++k) {
// Create expressions LHS10_2 and LHS10_1 as needed
// [...]
// Add k-th constraint, grap object for later removal from model
c[k] = model.addConstr(LHS10_2 <= LHS10_1);
}
// Do some stuff, optimize, etc.
// [...]
// now delete unwanted constraints from model
for (int k = 0; k < nConstr, ++k) {
model.remove(c[k]);
}

Related

Contagion program, Matrices and Syntax issue

I am trying to do the following program:
Write a function onstepcontagion : bool array array -> bool array array = that given a rectangular bool matrix, where true represents an infected square and false represent non-infected square it calculates the next step of infection. Infected squares remain infected indefinitely, non-infected squares become infected if the they are vertically/horizontally adjacent to at least two other infected squares
My code so far:
let printmat matrix =
let n = Array.length matrix in
let n1 = Array.length matrix.(0) in
for i = 0 to n - 1 do
for j = 0 to n1 - 1 do
if matrix.(i).(j) == true then print_string"1"
else print_string"0";
done;
print_string "\n";
done;;
let onstepcontagion matrix =
let n = Array.length matrix in
let n1 = Array.length matrix.(0) in
for i = 0 to n - 1 do
for j = 0 to n1 - 1 do
if (j < n1-1) then
let right = if matrix.(i).(j+1) == true then 1 else 0 in
if (j > 0) then
let left = if matrix.(i).(j-1) == true then 1 else 0 in
if (i < n-1) then
let up = if matrix.(i-1).(j) == true then 1 else 0 in
if (i > 0) then
let down = if matrix.(i+1).(j) == true then 1 else 0 in
let sum = right + left + up + down in
if sum > 1 then matrix.(i).(j) = true
done;
print_string "\n";
done;;
printmat matrix
**Error: Error: This expression has type bool but an expression was expected of type
unit
Characters 1618-1639:
if sum > 1 then matrix.(i).(j) = true **
I have made a C version of the program I want to implement:
#include <stdio.h>
#include <stdlib.h>
void print_mat (int n, int m, int arr[n][m])
{
int i,j;
for (i = 0; i < n; i++){
for (j = 0; j < m; j++)
printf("%d ",arr[i][j]);
printf("\n");
}
printf("\n\n");
}
int main()
{
int n, m, i, j, tmp, changes = 1;
printf("input Mat len\n");
scanf("%d%d",&n,&m);
int arr[n][m];
int cpy[n][m];
for (i = 0; i < n; i++)
for (j = 0; j < m; j++){
scanf("%d",&tmp);
if (tmp == 0)
arr[i][j] = tmp;
else
arr[i][j] = 1;
}
while (changes){
changes = 0;
for (i = 0; i < n; i++){
for (j = 0; j < m; j++)
{
tmp = 0;
if (arr[i][j] != 1){
if (j < m-1)
if (arr[i][j+1] == 1)
tmp++;
if (j > 0)
if (arr[i][j-1] == 1)
tmp++;
if (i < n-1)
if (arr[i+1][j] == 1)
tmp++;
if (i > 0)
if (arr[i-1][j] == 1)
tmp++;
if (tmp > 1){
cpy[i][j] = 1;
changes = 1;
}
}
}
}
if (changes == 1){
for (i = 0; i < n; i++)
for (j = 0; j < m; j++)
if (arr[i][j] == 1 || cpy[i][j] == 1)
arr[i][j] = 1;
printf("\n");
print_mat(n,m, arr);
}
}
return 0;
}
= is the structural equality operator. To set an array element you need to use <-.
Also, you should almost never use == because it tests for physical equality, i.e. that references point to the same address in memory. For comparing bools it doesn't strictly matter, because they're not pointers, but you should get into the habit of using = instead to avoid surprises in the future.

how to optimize a 'for loop' for 3d complex<double> arrays to improve speed in C++

all the arrays in this code are complex type in this code and the running time for this for loop is about 1 min. Ktemp is an array with size 141*1202*141. could anyone help me to optimize this code and save the running time?
complex<double> ***P1;
P1 = new complex<double>**[141];
for (i = 0; i < num_y; i++)
{
P1[i] = new complex<double> *[1202];
for (j = 0; j < tsize; j++)
{
P1[i][j] = new complex<double>[141];
}
}
for (int zz = 1; zz < 20; zz++)//in z direction
{
for (i = 0; i < 141; i++)
{
for (j = 0; j < 1202; j++)
{
for (k = 0; k < 141; k++)
{
if (Ktemp[i][j][k].real() <= 0)
{
P1[i][j][k] = 0;
}
else
{
P1[i][j][k] = excit_pfft[i][j][k] * expn[i][j][k];
}
}
}
}
excit_pfft = P1;
}
my second question is about rewriting matlab function 'fftshift' with C++. I have finished the code, but it seems not that efficient. could anyone help me rewrite this code? my code is attached below:
complex<double> ***fftw_shift(complex<double> ***te, int a, int b, int c)
{
complex<double> ***tempa;
tempa = new complex<double> **[a];
for (i = 0; i < a; i++)
{
tempa[i] = new complex<double> *[b];
for (j = 0; j < b; j++)
{
tempa[i][j] = new complex<double>[c];
}
}
/*for the row*/
if (c % 2 == 1)
{
for (i = 0; i < a; i++)
{
for (j = 0; j < b; j++)
{
for (k = 0; k < c / 2; k++)
{
tempa[i][j][k] = te[i][j][k + c / 2 + 1];
tempa[i][j][k + c / 2] = te[i][j][k];
tempa[i][j][c - 1] = te[i][j][c / 2];
}
}
}
}
else
{
for (i = 0; i < a; i++)
{
for (j = 0; j < b; j++)
{
for (k = 0; k < c / 2; k++)
{
tempa[i][j][k] = te[i][j][k + c / 2];
tempa[i][j][k + c / 2] = te[i][j][k];
}
}
}
}
for (i = 0; i < a; i++)
{
for (j = 0; j < b; j++)
{
for (k = 0; k < c; k++)
{
te[i][j][k] = tempa[i][j][k];
}
}
}
/*for the column*/
if (b % 2 == 1)
{
for (i = 0; i < a; i++)
{
for (j = 0; j < b / 2; j++)
{
for (k = 0; k < c; k++)
{
tempa[i][j][k] = te[i][j + b / 2 + 1][k];
tempa[i][j + b / 2][k] = te[i][j][k];
tempa[i][b - 1][k] = te[i][b / 2][k];
}
}
}
}
else
{
for (i = 0; i < a; i++)
{
for (j = 0; j < b / 2; j++)
{
for (k = 0; k < c; k++)
{
tempa[i][j][k] = te[i][j + b / 2][k];
tempa[i][j + b / 2][k] = te[i][j][k];
}
}
}
}
for (i = 0; i < a; i++)
{
for (j = 0; j < b; j++)
{
for (k = 0; k < c; k++)
{
te[i][j][k] = tempa[i][j][k];
}
}
}
/*for the third dimension*/
if (a % 2 == 1)
{
for (i = 0; i < a / 2; i++)
{
for (j = 0; j < b; j++)
{
for (k = 0; k < c; k++)
{
tempa[i][j][k] = te[i + a / 2 + 1][j][k];
tempa[i + a / 2][j][k] = te[i][j][k];
tempa[a - 1][j][k] = te[a / 2][j][k];
}
}
}
}
else
{
for (i = 0; i < a / 2; i++)
{
for (j = 0; j < b; j++)
{
for (k = 0; k < c; k++)
{
tempa[i][j][k] = te[i + a / 2][j][k];
tempa[i + a / 2][j][k] = te[i][j][k];
}
}
}
}
for (i = 0; i < a; i++)
{
for (j = 0; j < b; j++)
{
for (k = 0; k < c; k++)
{
te[i][j][k] = tempa[i][j][k];
}
}
}
return (te);
}
Since you are repeatedly multiplying by the values in expn (i.e. calculating an exponent) you can do this more efficiently using the pow function and get rid of the zz loop:
for (i = 0; i < 141; i++)
{
for (j = 0; j < 1202; j++)
{
for (k = 0; k < 141; k++)
{
if (Ktemp[i][j][k].real() <= 0)
{
excit_pfft[i][j][k] = 0;
}
else
{
excit_pfft[i][j][k] = excit_pfft[i][j][k] * pow(expn[i][j][k], 20);
}
}
}
}
Your code also seems to have a memory leak because you assign P1 to excit_pfft, but never free the previous contents of excit_pfft. You don't need to have the P1 temporary array in any case once you get rid of the outer loop.
I'm not sure of the internals of the complex pow() function, but you can calculate the (scalar) exponent of a complex number geometrically by converting it to polar co-ordinates (angle + distance scalar), then multiplying the angle by the power and raising the distance to the power, then converting back. So it's a lot faster than repeated multiplication.
First (will probably give you a big performance boost), get rid of the pointer arrays if you know beforehand the size of your arrays and simply allocate them in the stack:
complex<double> P1[141][1202][141];
Instead of :
complex<double> ***P1;
P1 = new complex<double>**[141];
for (i = 0; i < num_y; i++)
{
P1[i] = new complex<double> *[1202];
for (j = 0; j < tsize; j++)
{
P1[i][j] = new complex<double>[141];
}
}
And since I don't know exactly what this does, I'm assuming this:
for (int zz = 1; zz < 20; zz++)//in z direction
{
for (i = 0; i < 141; i++)
{
for (j = 0; j < 1202; j++)
{
for (k = 0; k < 141; k++)
{
if (Ktemp[i][j][k].real() <= 0)
{
P1[i][j][k] = 0;
}
else
{
P1[i][j][k] = excit_pfft[i][j][k] * expn[i][j][k];
}
}
}
}
excit_pfft = P1;
}
Could become this:
for (int zz = 1; zz < 20; zz++)//in z direction
{
for (i = 0; i < 141; i++)
{
for (j = 0; j < 1202; j++)
{
for (k = 0; k < 141; k++)
{
if (Ktemp[i][j][k].real() <= 0)
{
P1[i][j][k] = 0;
}
else
{
P1[i][j][k] = P1[i][j][k] * expn[i][j][k];
}
}
}
}
}
If this cannot be done than I'll need a more broad chunk of this code to analyze excit_pfft, etc.
A huge performance boost you could have is to use Worker Threads and run this last code multithreaded.
The same goes for our second question, Worker Threads should do it.
EDIT:
On second though, the stack won't be able to handle that much variables.
I'd recommend using vector<vector<vector<complex<double> > > > instead.

C ignoring incrementation

I tried this but my compiler(Visual Studio 2013) keeps messing up things.
I have a 9 by 9 matrix indexed from 1. It is 0 at the beginig. And starting from element 1:1 I start incrementing the value in the matrix or incrementing x,y, basically moving to the next matrix element.
However, the program ignores my incrementation and the fact that x,y are initially set to 1.
Also it ignores a function call.
Code is commented below.
I am sure this is the source I am compiling!
Restarted laptop and Visual Studio but still doesn't work.
Opened new project, same thing.
Thanks in advance.
#include<stdio.h>
#include<stdlib.h>
unsigned int Matrix[10][10], x, y;
// Ignore this..
int checkLine()
{
unsigned int i, j, k;
for (k = 1; k <= 9; k++){
if (Matrix[k][1] == 0) break;
for (i = 1; i <= 9; i++)
for (j = 1; j <= 9; j++)
if (Matrix[k][i] == Matrix[k][j] && i!=j)
return 0;
}
return 1;
}
//Ignore this..
int checkColumn()
{
unsigned int i, j, k;
for (k = 1; k <= 9; k++){
if (Matrix[1][k] == 0) break;
for (i = 1; i <= 9; i++)
for (j = 1; j <= 9; j++)
if (Matrix[i][k] == Matrix[j][k] && i!=j)
return 0;
}
return 1;
}
//Ignore this..
int checkSquare()
{
unsigned int i, j, k,l,m,n;
for (m = 1; m <= 7; m = m + 3)
for (n = 1; n <= 7; n = n + 3)
for (k = m; k <= m + 2; k++)
for (l = n; l <= n + 2; l++)
for (i = m; i <= m + 2; i++)
for (j = n; j <= n + 2; j++)
if (Matrix[k][l] == Matrix[i][j] && !(k==i && l==j))
return 0;
return 1;
}
void increment()
{
if (y == 9)
{
x++;
y = 1;
}
else y++;
}
void decrement()
{
if (y == 1)
{
x--;
y = 9;
}
else
y--;
}
void print_Matrix(){
unsigned int i, j;
for (i = 1; i <= 9; i++){
for (j = 1; j <= 9; j++)
printf("%u ", Matrix[i][j]);
printf("\n");
}
}
//
// MAIN. PROBLEM BELOW
//**
void main()
{
unsigned int i, j;
for (i = 1; i <= 9;i++)
for (j = 1; j <= 9; j++)
Matrix[i][j] = 0;
print_Matrix(); // Function call is ignored here. Don't know why.***
x = 1;
y = 1;
// X and Y are OBVIOUSLY 1***
while (x < 10) //Condition OBVIOUSLY true***
{
printf("%u, %u", x, y); //Keeps printing 0,3 and eventually 0,2***
printf("\n");
Matrix[x][y]++; //Incrementation...***
print_Matrix(); // Always prints a blank Matrix consisting of only 0's***
if (checkLine() && checkColumn() && checkSquare())
{
increment();
}
if (Matrix[x][y] == 10){
Matrix[x][y] = 0;
decrement();
}
}
print_Matrix();
}
You feel that the increment is ignored because the checkSquare function is buggy. It never returned 1 and hence the increment function was never called.
What happens is that you're incrementing the position marked by x and y, that's position [1][1]. Until it reaches 10 nothing interesting happens, you can actually see the top left corner of the matrix increasign to 10, but then the condition to decrement becomes true and you decrement.
See for yourself (prints),
while (x < 10) //Condition OBVIOUSLY true***
{
printf("%u, %u", x, y); //Keeps printing 0,3 and eventually 0,2***
printf("\n");
Matrix[x][y]++; //Incrementation...***
print_Matrix(); // Always prints a blank Matrix consisting of only 0's***
if (checkLine() && checkColumn() && checkSquare())
{
increment();
}
if (Matrix[x][y] == 10){
Matrix[x][y] = 0;
decrement();
}
printf( "Enter to continue\n" );
getchar();
}
It turns Y = 9, X = 0 and the [1][1] position becomes 0, so you see only zeros because you're not printing the zero indexes.
This process repeats until Y = 1 and X = 0, you increase the position until 10 so that the decrement works again.
When Y = 1, X = 0 and position [0][1] is 10, the decrement call will do x--. Since X is an unsigned int, it will underflow and become 4.2 billion something, which is greater than 10 and the loop ends.
What are you trying to achieve here?
Edit: Something even more amazing happens when you make x and y ints instead of unsigned ints.
Instead of x underflowing it will become -1. Matrix[-1][9]++ strangely increased x by 1 when I ran the code, so x went back to 0. Which means the program loops forever at this point.
The increment function was never called.
It shows matrix and increment when tested online, here are results
1) for C compiler
http://ideone.com/KRLO8w
#include<stdio.h>
#include<stdlib.h>
unsigned int Matrix[10][10], x, y;
// Ignore this..
int checkLine()
{
unsigned int i, j, k;
for (k = 1; k <= 9; k++){
if (Matrix[k][1] == 0) break;
for (i = 1; i <= 9; i++)
for (j = 1; j <= 9; j++)
if (Matrix[k][i] == Matrix[k][j] && i!=j)
return 0;
}
return 1;
}
//Ignore this..
int checkColumn()
{
unsigned int i, j, k;
for (k = 1; k <= 9; k++){
if (Matrix[1][k] == 0) break;
for (i = 1; i <= 9; i++)
for (j = 1; j <= 9; j++)
if (Matrix[i][k] == Matrix[j][k] && i!=j)
return 0;
}
return 1;
}
//Ignore this..
int checkSquare()
{
unsigned int i, j, k,l,m,n;
for (m = 1; m <= 7; m = m + 3)
for (n = 1; n <= 7; n = n + 3)
for (k = m; k <= m + 2; k++)
for (l = n; l <= n + 2; l++)
for (i = m; i <= m + 2; i++)
for (j = n; j <= n + 2; j++)
if (Matrix[k][l] == Matrix[i][j] && !(k==i && l==j))
return 0;
return 1;
}
void increment()
{
if (y == 9)
{
x++;
y = 1;
}
else y++;
}
void decrement()
{
if (y == 1)
{
x--;
y = 9;
}
else
y--;
}
void print_Matrix(){
unsigned int i, j;
for (i = 1; i <= 9; i++){
for (j = 1; j <= 9; j++)
printf("%u ", Matrix[i][j]);
printf("\n");
}
}
//
// MAIN. PROBLEM BELOW
//**
void main()
{
unsigned int i, j;
for (i = 1; i <= 9;i++)
for (j = 1; j <= 9; j++)
Matrix[i][j] = 0;
print_Matrix(); // Function call is ignored here. Don't know why.***
x = 1;
y = 1;
// X and Y are OBVIOUSLY 1***
while (x < 10) //Condition OBVIOUSLY true***
{
printf("%u, %u", x, y); //Keeps printing 0,3 and eventually 0,2***
printf("\n");
Matrix[x][y]++; //Incrementation...***
print_Matrix(); // Always prints a blank Matrix consisting of only 0's***
if (checkLine() && checkColumn() && checkSquare())
{
increment();
}
if (Matrix[x][y] == 10){
Matrix[x][y] = 0;
decrement();
}
}
print_Matrix();
}
2) for c++ compiler C++ 4.9.2 (changed return type of main to int)
http://ideone.com/Ey5nG1
In your image starting value of 0, 3 is due to buffer limit of command prompt. As the program never ends, so it terminates abruptly and latest few bytes are stored in buffer and is only shown that much. To see complete output redirect it to a file and open it.
Your output is a bit confusing since the output of the line
printf("%u, %u", x, y);
runs into the output of
print_Matrix();
By adding a newline to the output of the first line, i.e. by using
printf("%u, %u\n", x, y);
you will notice that at some point x gets decremented to 0 and never gets incremented again. Since you never print Matrix[0][y], you never see the non-zero values.
In addition to the change to above printf, if you change print_Matrix to:
void print_Matrix(){
unsigned int i, j;
for (i = 0; i <= 9; i++){
// ^^^ Use 0 instead of 1
for (j = 0; j <= 9; j++)
// ^^^ Use 0 instead of 1
printf("%u ", Matrix[i][j]);
printf("\n");
}
}
you will see the non-zero values.
See working code at http://ideone.com/HlQ4xp.

Matrix multiplication - C++ [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
void power(int a[][21], int n, int d) {
int e[21][21], k, i, j, l;
if (d == 1) {
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++)
cout << a[i][j] << " ";
cout << endl;
}
} else {
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
e[i][j] = 0;
for (l = 1; l < d; l++)
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
for (k = 1; k <= n; k++)
e[i][j] = e[i][j] + (a[i][k] * a[k][j]);
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++)
cout << e[i][j] << " ";
cout << endl;
}
}
}
I am trying to build a function that calculates the power of a matrix. The program works for the 1st and 2nd power of the matrix but if I want to calculate the 3rd power the function will return incorrect values. I think the problem is at retaining the previous results but I can't figure out on how to fix it.
This block of code
for (l = 1; l < d; l++)
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
for (k = 1; k <= n; k++)
e[i][j] = e[i][j] + (a[i][k] * a[k][j]);
sets e to be a*a no matter what d is.
You need to have a temporary matrix to make things work.
Bootstrap:
e = a;
In the loop:
temp = e;
e = temp * a;
temp = e;
e = temp * a;
... etc.
Something along the lines of:
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
e[i][j] = a[i][j];
}
}
for (l = 1; l < d; l++)
{
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
temp[i][j] = e[i][j];
}
}
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
e[i][j] = 0;
for (k = 0; k < n; k++)
{
e[i][j] += (temp[i][k] * a[k][j]);
}
}
}
}
PS I have used 0-based indexing to access the matrices. I am not sure why you have used 1-based indexing.

Dynamic approach to the TSP

I'm having trouble recognizing why this algorithm doesn't return the shortest path for the TSP.
vector<int> tsp(int n, vector< vector<float> >& cost)
{
long nsub = 1 << n;
vector< vector<float> > opt(nsub, vector<float>(n));
for (long s = 1; s < nsub; s += 2)
for (int i = 1; i < n; ++i) {
vector<int> subset;
for (int u = 0; u < n; ++u)
if (s & (1 << u))
subset.push_back(u);
if (subset.size() == 2)
opt[s][i] = cost[0][i];
else if (subset.size() > 2) {
float min_subpath = FLT_MAX;
long t = s & ~(1 << i);
for (vector<int>::iterator j = subset.begin(); j != subset.end(); ++j)
if (*j != i && opt[t][*j] + cost[*j][i] < min_subpath)
min_subpath = opt[t][*j] + cost[*j][i];
opt[s][i] = min_subpath;
}
}
vector<int> tour;
tour.push_back(0);
bool selected[n];
fill(selected, selected + n, false);
selected[0] = true;
long s = nsub - 1;
for (int i = 0; i < n - 1; ++i) {
int j = tour.back();
float min_subpath = FLT_MAX;
int best_k;
for (int k = 0; k < n; ++k)
if (!selected[k] && opt[s][k] + cost[k][j] < min_subpath) {
min_subpath = opt[s][k] + cost[k][j];
best_k = k;
}
tour.push_back(best_k);
selected[best_k] = true;
s -= 1 << best_k;
}
tour.push_back(0);
return tour;
}
For example, on a distance cost matrix of just 5 points (5 different nodes in the graph), the algorithm returns a path that's less than optimal. Any help in recognizing a blatant or small error would be appreciated. Or any helpful tips as to what's going wrong.
One thing that looks odd is that the main for loop does things even if i is not part of the subset s.
In other words, opt[17][8] will be set to cost[0][8]. opt[17][8] represents the state of being at node 8, and having visited nodes 0 and 4 (because 5=2^0+2^4).
This should be marked as being impossible because if we are at node 8, we must certainly have visited node 8!
I would suggest preventing these cases from occuring by changing:
for (int i = 1; i < n; ++i) {
vector<int> subset;
to
for (int i = 1; i < n; ++i) {
vector<int> subset;
if ((s&(1<<i))==0) {
opt[s][i]=FLT_MAX;
continue;
}
Nested loop for(j= iterates over all nodes in subset, including the starting node. This results in using uninitialized values opt[t][0] and therefore in incorrect optimal path length calculation.
The easiest fix would be to exclude starting node from subset:
for (int u = 1; u < n; ++u)
...
if (subset.size() == 1)
...
else if (subset.size() > 1)