隣接ノードのリストからグラフの隣接行列を作成し、ファイル(重みは不要)にC++のビットセット配列に格納
するタスクがあります。ファイルから隣接ノードを正常に読み取りましたが、ビットセット配列に格納しようとすると、結果が正しくありません。私の機能は次のとおりです。
bitset<N>* read_graph(string filepath)
{
FILE *fp;
char line[100];
bitset<N> bs[N];
fp = fopen(filepath.c_str(), "r");
if(fp != NULL)
{
while(!feof(fp))
{
fgets(line, 100, fp);
//cout << line << endl;
if(line[0] == 'a')
{
string str = "";
int i(0);
for(i = 2; line[i] != ' '; i++)
{
if(line[i] != ' ')
{
str += line[i];
}
}
int fi = atoi(str.c_str());
i++;
str = "";
for(int j = i; line[j] != ' '; j++)
{
if(line[j] != ' ')
{
str += line[j];
}
}
int si = atoi(str.c_str());
si--;
fi--;
//cout << "string: " << str << endl;
cout << "fi: " << fi;
//cout << "string: " << str << endl;
cout << " si: " << si << endl;
bs[fi][si]= 1;
}
}
}
fclose(fp);
return bs;
}
結果は次のようになります(fiは最初のインデックスを表し、siは2番目のインデックスを表します)。
sample.gr
fi:0 si:1
fi:0 si:2
fi:1 si:3
fi:2 si:4
fi:3 si:2
fi:3 si:5
fi:4 si:1
fi:4 si: 5
fi:4 si:5000000
000001
011000
001000
000000
000000
The indexes are right, I've checked them, but the matrix should be the following (it is mirrored because of the bitset's right side positioning):
000000
010001
001001
000010
000100
011000
I guess the error is somewhere around the bitset element accessing but I cannot find out what exactly is wrong.
I appreciate any help. Thanks.