/* 从任务的前面开始扫描如果两个任务a[i],a[j](i>j)满足如下两个条件,需要交换a[i],a[j]: 1 a[i],a[j] 都为早任务,且a[i].d<a[j].d (a[i]的截止日期大于a[j]),则交换a[i],a[j] 2 a[i]为早任务,a[j]为迟任务,则交换a[i],a[j] 由于已经按限制日期排过序(在init()函数中冒泡排序,并存于orderd_task中),所以没有在重复左交换 */ int taskOrder() { int i,j; int count=0; print(my_task,ALLTASK); //从i=1开始,依次把my_task[i]进行测试,看加入my_task[i]是否能保持独立集。 for(i=1;i<=num_of_task;i++) { //todo 暂且把my_test[i]当作早任务,测试是否是独立集 indepSet(my_task[i].id);//不满足独立集,把设成迟任务 } //打印早任务序号 print(orderd_task,EARLYTASK); //打印迟任务序号 print(orderd_task,LATETASK); //计算总共罚款值 for(i=1;i<=num_of_task;i++) if(!orderd_task[i].is_early) count+=orderd_task[i].w; printf("\n\n总罚款最小为:%d\n",count); }
int Graph::color() { //***************************************************************** //Greedy Choice: Instead of looking for the absolute best coloring, //pick a vertex and iterate through the graph only coloring when //necessary. //Optimal substructure: Choose the vertex with the least edges, //Then create subgraphs for every vertex that branches off in a graph //that is unconnected to the other graphs. If there are other graphs //with the same # of edges as your root vertex, repeat the process for them. //When done apply the greedy algorithm for each of them. //Worst possible graph would be a complete bipartite graph, because the //optimal substructure would then pick every graph as a relative "root", //At this point since they are all the same it would simply iterate through //picking a new color for every vertex. //The greedy coloring algorithm first sets every vertex color to -1, then sorts the //vertices by their degree and then place them into a sorted array. Iterate through //this array and apply a color for each node that isn't taken by its neighbors. This //is best done as an adjacency matrix. string liststring; liststring=modprint(); char *gstring=(char*)liststring.c_str(); int gsize=liststring.length(); int colorsize; vector<vector<int> > tempvec; vector<int> rowvec; char *pch; pch=strtok(gstring," "); int i=0; //Put adjacency list in adjacency matrix //strtok spaces + new lines while (pch != NULL) { if (pch[1]=='!') { //moded graph gives a "! " at every new vertex tempvec.push_back(rowvec); rowvec.clear(); n++; } else { rowvec.push_back(atoi(pch)); } pch=strtok(NULL," "); } printf("size: %d\n",n); map<int,int> neighbor_number; int *vert_ord = new int[n]; //initialize the adjacency matrix // bool adjmat[n][n]; bool **adjmat = new bool*[n]; for (int i=0; i<n; i++) adjmat[i] = new bool[n]; for (int i=0; i<n; i++) for (int j=0; j<n; j++) adjmat[i][j]=0; //Fill the unordered adjacency matrix. for (int i=0; i<tempvec.size(); i++) { for (int j=0; j<tempvec.at(i).size(); j++) { adjmat[i][tempvec.at(i).at(j)]=1; adjmat[tempvec.at(i).at(j)][i]=1; } } for (int i=0; i<n; i++) { for (int j=0; j<n; j++) { if (adjmat[i][j]==1) neighbor_number[i]++; } } int less_then=0; for (int i=0; i<(n-1); i++) { less_then=0; for (int j=(i+1); j<n; j++) { if (neighbor_number[i]>=n) cout << "poo"; } } vector<pair<int,int> > vertvec; vertvec = mapsort(neighbor_number); vector<pair<int,int> >::iterator it; for (it = vertvec.begin(); it != vertvec.end(); it++) cout << "test[" << (*it).first << "]=" << (*it).second << "\n"; vector<int> independentSet; it=vertvec.begin(); independentSet=indepSet((*it).first, adjmat); return colorsize; }
int Graph::color() { //******************************************************************************** //Greedy Graph Coloring: Basic idea is that you go through the graph, finding //the largest independent set of vertices possible at any given iteration. Each of //these vertices is colored a new color. At this point remove the independent set //from the graph and then repeat the process until you have all vertices colored. //Optimal substructure: The optimal substructure is the largest possible set of //indepented vertices. This can be found by locating the lowest ordered vertex and //then placing this in the set and removing the vertex and its neighbors from the graph. //Continue until the entire graph is empty. Number of independent sets required to empty //the graph is the color # of the graph. //Worst possible graph would be an incomplete bipartite graph with at least one vertex //having fewer edges (not less then or equal) then the others. Since the algorithm only //considers the lowest ordered vertex when first picking in a set, it will pick one //of the lower ordered vertices first. This ends up with at least one vertex not getting //colored the first time around. It'll require way more iterations this way then necessary. string liststring; liststring=modprint(); char *gstring=(char*)liststring.c_str(); int gsize=liststring.length(); int colorsize; vector<vector<int> > tempvec; vector<int> rowvec; char *pch; pch=strtok(gstring," "); int i=0; //Put adjacency list in adjacency matrix //strtok spaces + new lines while (pch != NULL) { if (pch[1]=='!') { //moded graph gives a "! " at every new vertex tempvec.push_back(rowvec); rowvec.clear(); n++; } else { rowvec.push_back(atoi(pch)); } pch=strtok(NULL," "); } printf("size: %d\n",n); map<int,int> neighbor_number; int *vert_ord = new int[n]; //initialize the adjacency matrix bool **adjmat = new bool*[n]; for (int i=0; i<n; i++) adjmat[i] = new bool[n+1]; for (int i=0; i<n; i++) for (int j=0; j<n; j++) adjmat[i][j+1]=0; for (int i=0; i<n; i++) adjmat[i][0]=1; //Still a valid vertex! The first index is a flag variable, //which shows whether or not a node is in a vertex bool **tmpmat = new bool*[n+1]; for (int i=0; i<n; i++) tmpmat[i] = new bool[n]; //Fill the unordered adjacency matrix. for (int i=0; i<tempvec.size(); i++) { for (int j=0; j<tempvec.at(i).size(); j++) { adjmat[i][tempvec.at(i).at(j)+1]=1; adjmat[tempvec.at(i).at(j)][i+1]=1; } } for (int i=0; i<n; i++) { for (int j=0; j<n; j++) { if (adjmat[i][j]==1) neighbor_number[i]++; } } printf("Unordered Incidence Matrix: \n"); for (int i=0; i<n; i++) { for (int j=0; j<n+1; j++) printf("%d ",adjmat[i][j]); printf("\n"); } for (int i=0; i<n; i++) for (int j=0; j<n+1; j++) tmpmat[i][j]=adjmat[i][j]; tmpmat=adjmat; int total=n; int colorNum=0; while (total) { vector<int> inSet=indepSet(tmpmat); vector<int>::iterator it = inSet.begin(); printf("Independent set: "); for (it = inSet.begin(); it != inSet.end(); it++) printf("%d, ",*it); printf("\n"); total-=(int)inSet.size(); for (it = inSet.begin(); it != inSet.end(); it++) removeVertFromAdjacency(*it, tmpmat); colorsize++; } return colorsize; }