因此,我需要创建一个函数,该函数将返回图的色数。该图通过函数使用文件名找到的邻接矩阵给出。我有一个理论上应该可以正常运行的函数,编译器对此没有任何问题,但是当我运行它时,它只是打印出一个空行并结束程序。
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
int Find_Chromatic_Number (vector <vector <int>> matg, int matc[], int n) {
if (n == 0) {
return 0;
}
int result, i, j;
result = 0;
for (i = 0; i < n; i++) {
for (j = i; j < n; j++) {
if (matg[i][j] == 1) {
if (matc[i] == matc[j]) {
matc[j]++;
}
}
}
}
for (i = 0; i < n; i++) {
if (result < matc[i]) {
result = matc[i];
}
}
return result;
}
int main() {
string file;
int n, i, j, m;
cout << "unesite ime datoteke: " << endl;
cin >> file;
ifstream reader;
reader.open(file.c_str());
reader >> n;
vector<vector<int>> matg(n, vector<int>(0));
int matc[n];
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
reader >> matg[i][j];
}
matc[i] = 1;
}
int result = Find_Chromatic_Number(matg, matc, n);
cout << result << endl;
return 0;
}
该程序应该使用freader将文件转换为表示邻接矩阵(matg)的2D向量。我还制作了一个数组(matc),该数组代表每个顶点的值,并具有与不同颜色对应的不同数字。 该函数应该遍历向量,并且每当两个顶点之间有一条边时,该函数应检查其在matc中的颜色值是否相同。如果是,则将第二个值(j)上移一个。函数通过向量后,matc数组应包含n个不同的数字,其中最大的数字是我要查找的色数。 我希望我已经对我要完成的工作做了足够的解释,即使不是仅仅询问,我也会补充任何进一步的解释。
答案 0 :(得分:0)
尝试使它像这样。
不要为您的向量选择大小
vector<vector<int> > matg;
而不是使用reader >> matg[i][j];
使用:
int tmp;
reader >> tmp;
matg[i].push_back(tmp);