#include "stdc++.h"
#include "iostream"
#include "string"
#include "cstdio"
#include "cstring"
#include "algorithm"
#include "ctime"
#include "fstream"
#include "sstream"
#include "vector"
#include "stdlib.h"
using namespace std;
//int n;//表示图的顶点数
//double p[999];
//double q[999];//
//int l[999];//注意更改//
//int m[101][101];//有向图的邻接矩阵
int x[101];//当前团的解
int bestx[101];//最优解
int cn;//当前团的大小
int bestnn;//当前最优值
char filename1[256] = "testdataSS02.csv";
template<typename T>
vector<int> sort_indexes(const vector<T>& v, bool reverse = false) {
// initialize original index locations
vector<int> idx(v.size());
for (int i = 0; i != idx.size(); ++i) idx[i] = i;
// sort indexes based on comparing values in v
if (reverse)
{
sort(idx.begin(), idx.end(),
[&v](int i1, int i2) {return v[i1] > v[i2]; });
}
else {
sort(idx.begin(), idx.end(),
[&v](int i1, int i2) {return v[i1] < v[i2]; });
}
return idx;
}
int BronKerbosch(int i,int n,int m[][101])///自定义B-K算法void BronKerbosch函数用来求解最大团
{
//int x[101] = {0};//当前团的解
//int bestx[101] = {0};//最优解
//int cn=0;//当前团的大小
//int bestnn=0;//当前最优值
if (i > n) {//递归出口,到根节点时,更新最优值和最优解,返回
bestnn = cn;//更新最优值
for (int j = 1; j <= n; j++)
bestx[j] = x[j];
return bestnn;//返回
}
x[i] = 1;//先假定x[i]=0;
for (int j = 1; j < i; j++) {
if (x[j] == 1 && m[i][j] == 0) {
x[i] = 0;//如果该点与已知解中的点无边相邻
break;//则不遍历左子树
}
}
if (x[i] == 1) {//当且仅当x[i]==1时,遍历左子树
cn++;//该点加入当前解
BronKerbosch(i + 1,n, m);//递归调用
cn--;//还原当前解
}
x[i] = 0;//假定x[i]==0
if (cn + n - i > bestnn)
{//如果当前值+右子树可能选择的节点<当前最优解,不遍历左子树
x[i] = 0;
BronKerbosch(i + 1,n,m);
}
return bestnn;
}
void xiugai(string& a, int lie1, string xinneirong)
{
//cout << "开始进行修改操作!" << endl;
string tempp;
string zuizhong;//最终得出的一个字符串,来覆盖原有的字符串的字符串!
int cont = 0;
int flag = 0;
int neirongshu = 0;//一共有多少内容,来判断有几个逗号!
vector<string>aa;
char b[100];
char c[100];
strcpy(b, a.c_str());
char delims[] = ",";
char* result = NULL;
char* shizhi = NULL;
shizhi = strtok(b, delims); strtok:https://blog.csdn.net/qq_43478260/article/details/109298608
while (shizhi != NULL)
{
neirongshu++;
shizhi = strtok(NULL, delims);
}
strcpy(c, a.c_str());
result = strtok(c, delims);
//cout<<"请输入新数据!"<<endl;
while (result != NULL)
{
cont++;
tempp = result;
if (cont < neirongshu)
{
tempp += ',';
}
aa.push_back(tempp);//将每列的内容加上逗号压入vector
result = strtok(NULL, delims);
}
//cout << "将修改数据写入CSV中!" << endl;
aa[lie1] = xinneirong + ',';//因为数组是从0开始的
for (int ii = 0; ii < aa.size(); ii++)
{
zuizhong += aa[ii];
}
a = zuizhong;//使传进来的string变成修改后的string
return;
}
void xierucsv(char filename[256], int hang, int lie, double xinneirong1)
{
//int hang;//需要进行修改的行数
//int lie;//需要进行修改的列数
ifstream infile(filename);
/*string tempmp;*/
string line;
string xinneirong = to_string(xinneirong1);
int flag = 0;
vector<string>neirong;//创建动态string数组来储存文件里的数据
while (infile.good())//文件是否正确打开
{
getline(infile, line);
flag++;//记录行数
neirong.push_back(line);
//cout<<"文件正确打开"<< endl;
}
infile.close();
cout << "请输入你要修改的行数和列数,以回车作为分隔!" << endl;
cin >> hang >> lie;
while (1)//考虑剪枝
{
if (hang <= flag && hang > 0)
{
break;
}
if (hang > flag)
{
cout << "请输入正确的行数!" << endl;
cin >> hang;
}
}//考虑剪枝
int hang1 = hang - 1;
int douhao = 0;//记录逗号数 //考虑剪枝
for (int i = 0; i <= neirong[hang1].size(); i++)//考虑剪枝
{
if (neirong[hang1][i] == ',')
{
douhao++;
}
}
cout << "逗号数为" << douhao << endl; //考虑剪枝
while (1)//考虑剪枝
{
if (lie <= douhao + 1 && lie > 0)
{
break;
}
if (lie > douhao + 1)
{
cout << "请输入正确的列数!" << endl;
cin >> lie;
}
} //考虑剪枝
xiugai(neirong[hang1], lie-1, xinneirong);//向xiugai函数传入string(即需要修改的行的内容)和需要修改的列数
ofstream onfile(filename);
//cout << "现在进行写入csv文件操作!" << endl;
for (int l = 0; l < neirong.size(); l++)
{
onfile << neirong[l] << endl;
}
//cout << "写入完毕!" << endl;
onfile.close();
//cout << "关闭写入流!" << endl;
return;
}
int main()
{
time_t begin, end; //计算程序运行时长
double ret;
begin = clock(); //计时开始
bool agg = true;
while (agg)
{
//for (int i=0;i<5;i++)
//{
cout << "将读取CSV文件的数据保存在数组n1中" << endl;
//读取文件中的数据
ifstream ifs; //创建流对象
ifs.open("testdataSS02.csv", ios::in); //打开文件
if (!ifs.is_open()) //判断文件是否打开
{
cout << "打开文件失败!!!";
return 0;
}
vector<string> item; //用于存放文件中的一行数据
string temp1; //临时存储文件中的一行数据
int count = 0;
while (getline(ifs, temp1)) //利用 getline()读取文件中的每一行,并放入到 item 中
{
if (temp1 == "")
break;
item.push_back(temp1);
count++;
}
ifs.close();
int ii = 0; int jj = 0; //int cc = 0; //
//double* p = new double[999];///
//double* q = new double[999];//
//int* l = new int[999];//注意更改//
double p[999] = {0};
double q[999] = {0};//
int l[999] = {0};//注意更改//
//for (auto it = item.begin(); it != item.end(); it++)//遍历文件中的每一行数据
for(int cc=0;cc<count;cc++)
{
string str;
string it = item[cc];
//其作用是把字符串分解为单词(在此处就是把一行数据分为单个数据)
istringstream istr(it);
//将字符串流 istr 中的字符读入到 str 字符串中,读取方式是以逗号为分隔符
getline(istr, str, ',');
p[ii] = atof(str.c_str());
ii++;
//getline(istr, str, ',');
//cout << str << "\t"; // str 对应第二列数据
//getline(istr, str, ',');
//cout << str << "\t"; // str 对应第三列数据
//getline(istr, str, ',');
//cout << str << "\t"; // str 对应第四列数据
getline(istr, str, ','); // str 对应第五列数据
q[jj] = atof(str.c_str());
jj++;// str 对应第六列数据
getline(istr, str, ',');
l[cc] = atoi(str.c_str());
//cc++;// str 对应第六列数据
}
//ifs.close();
cout << "输出威胁度系数l" << endl;
for (int i = 0; i < 51; i++)
{
cout << l[i] << " ";
}cout << endl;
double n1[51][2] = { 0 };
for (int i = 0; i < 51; i++)
{
n1[i][0] = p[i];
}
for (int i = 0; i < 51; i++)
{
n1[i][1] = q[i];
}
int num = sizeof(n1) / sizeof(n1[0]);
printf("输出矩阵n1\n");
for (int i = 0; i < num; i++)
{
for (int j = 0; j < 2; j++)
{
cout << n1[i][j] << " ";
};
cout << endl;
}
cout << endl;
int m1[101][101] = { 0 };///
cout << "输入图的顶点数n:" << num << endl;
for (int i = 0; i < num; i++)//有逻辑问题得到改正
{
for (int j = i + 1; j < num; j++)
if (((n1[j][0] <= n1[i][0]) && (n1[i][0] < n1[j][1])) || ((n1[j][0] < n1[i][1]) && (n1[i][1] <= n1[j][1]))
|| ((n1[j][0] <= n1[i][0]) && (n1[i][1] <= n1[j][1])) || ((n1[i][0] <= n1[j][0]) && (n1[j][1] <= n1[i][1])))
//if (((n1[j][0] < n1[i][0]) && (n1[i][0] < n1[j][1])) || ((n1[j][0] < n1[i][1]) && (n1[i][1] < n1[j][1])) ||
// ((n1[j][0] < n1[i][0]) && (n1[i][1] < n1[j][1])) || ((n1[i][0] < n1[j][0]) && (n1[j][1] < n1[i][1])) ||
// ((n1[j][0] = n1[i][0]) && (n1[i][1] = n1[j][1])))
{
m1[i + 1][j + 1] = 1;
m1[j + 1][i + 1] = 1;
};
} cout << endl;
cout << "输出冲突矩阵M\n" << endl;
for (int a = 1; a < num + 1; a++)
{
for (int b = 1; b < num + 1; b++)
{
cout << m1[a][b] << " ";
};
cout << endl;
}cout << endl;
int bestn = 0;
cn = 0;
x[101] = {0};//当前团的解
bestx[101] = {0};//最优解
bestnn = 0;//当前最优值
bestn = BronKerbosch(1, num, m1);//求最优解
cout << "输出SS链序号构成最大团的数目 bestn:" << bestn << endl;//输出最优值
int mm[25] = { 0 };//mm矩阵用于装输出构成最大团的SS链序号
int cmm = 0;
printf("输出构成最大团的SS链序号:");
for (int i = 1; i <= num; i++)//输出
{
if (bestx[i])
{
cout << i - 1 << " ";//输出最优解
mm[cmm] = i - 1;
cmm++;
}
}cout << endl;
cout << "mm输出SS链序号构成最大团元素" << endl;
for (int i = 0; i < bestn; i++)
{
cout << mm[i] << " ";
}cout << endl;
cout << "输出SS链序号构成最大团的数目 bestn:" << bestn << endl;//输出最优值
if (bestn > 8)
{
agg = true;
printf("共享WQSS链数目为超过最大通道容量,该WQ发生了资源冲突");
cout << "sizeof(mm) / sizeof(mm[0])" << sizeof(mm) / sizeof(mm[0]) << "bestn" << bestn << endl;
// 根据mm输出SS链序号构成最大团元素对应的威胁度系数mm1
cout << "输出SS链序号构成最大团元素对应的威胁度系数mm1" << endl;
int mm1[sizeof(mm) / sizeof(mm[0])] = { 0 };
for (int i = 0; i < bestn; i++)//sizeof(mm) / sizeof(mm[0])->bestn
{
mm1[i] = l[mm[i]];
}
for (int i = 0; i < bestn; i++)//sizeof(mm) / sizeof(mm[0])->bestn
{
cout << mm1[i] << " ";
}
cout << endl;
// 消解算法第一步
cout << "sizeof(mm1) / sizeof(mm1[0]):" << bestn << endl;
vector<int> ll(mm1, mm1 + bestn);
vector<int> sorted_indx;
sorted_indx = sort_indexes(ll);
sorted_indx = sort_indexes(ll, true);
int tempw[sizeof(mm) / sizeof(mm[0])] = { 0 };//tempwSS链序号的索引
cout << "sorted_indx.size():" << sorted_indx.size() << endl;
cout << "descending sorted: ";
for (int i = 0; i < sorted_indx.size(); i++)
{
cout << sorted_indx[i] << " ";
tempw[i] = sorted_indx[i];
}
cout << endl;
cout << "输出需要消解的SS链序号的索引: ";
for (int i = 8; i < bestn; i++)//sizeof(mm) / sizeof(mm[0])->bestn
{
cout << tempw[i] << " ";
}
cout << endl;
// 消解算法第二步
cout << "sizeof(mm) / sizeof(mm[0]) - 8 " << sizeof(mm) / sizeof(mm[0]) - 8 << endl;;
int tempwx[sizeof(mm) / sizeof(mm[0]) - 8] = { 0 };//除了前8:tempwSS链序号的索引tempwx
for (int i = 0; i < bestn - 8; i++)//sizeof(mm) / sizeof(mm[0])->bestn
{
tempwx[i] = tempw[i + 8];
}
cout << "除了前8的tempwSS链序号的索引tempwx: ";
for (int i = 0; i < bestn - 8; i++)//sizeof(mm) / sizeof(mm[0])->bestn
{
cout << tempwx[i] << " ";
}cout << endl;
cout << "输出需要消解的SS链序号: ";
for (int i = 0; i < bestn - 8; i++)//sizeof(mm) / sizeof(mm[0])->bestn
{
cout << mm[tempwx[i]] << " ";
}cout << endl;
// 消解算法第三步
计算调整量;进行调整;
double nn2[sizeof(mm) / sizeof(mm[0])] = { 0 };
double nn3[sizeof(mm) / sizeof(mm[0])] = { 0 };
for (int i = 0; i < bestn; i++)
{
nn2[i] = n1[mm[i]][0];
nn3[i] = n1[mm[i]][1];
}
cout << "nn2[i]&nn3[i]" << bestn << endl;
for (int i = 0; i < bestn; i++)
{
cout << nn2[i] << " " << nn3[i] << endl;
}
double max_num = 0;
double min_num = 0;
max_num = *max_element(nn2, nn2 + bestn); //sizeof(mm) / sizeof(mm[0])->bestn
min_num = *min_element(nn3, nn3 + bestn);//sizeof(mm) / sizeof(mm[0])->bestn
cout << "max_num" << max_num << endl;
cout << "min_num" << min_num << endl;
cout << "bestn-8:" << bestn - 8 << endl;
cout << "sizeof(tempwx) / sizeof(tempwx[0]):" << sizeof(tempwx) / sizeof(tempwx[0]) << endl;
for (int i = 0; i < bestn - 8; i++)
{
xierucsv(filename1, mm[tempwx[i]] + 1, 1, double(min_num));
xierucsv(filename1, mm[tempwx[i]] + 1, 2, double(min_num + 10));//计算调整量
}
}
else
{
printf("该WQ无资源冲突");
agg = false;
}
//delete[] p;
//delete[] q;
//delete[] l;
}
end = clock(); 计时结束
ret = double(end - begin) / CLOCKS_PER_SEC;//计算程序运行时长
cout << "程序运行时间为:" << ret << "s" << endl;
return 0;
}
C++(师兄努力版本)
最新推荐文章于 2024-09-17 00:14:16 发布