hello there
let me explain my work
i have a file i read from it the data
after that i try to get the best vertex that it's max(degree/weight)
when i find it i put it's neighbors into the glist and i put it into blist and delete both of it and
the neighbors from the wlist
here's the code i added comments to make it understandable
but when i compile it keeps displaying the same value of the best vertex with non stop
i really need help
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <vector>
#include <list>
#include <stdlib.h>
using namespace std;
int main(int argc, char **argv) {
//////////////////// reading file///////////////////////
ifstream inFile;
inFile.open("Problem1.dat_50_50_0",std::ifstream::in);
if ( !inFile ) {
cout << "file could not be opened !" << endl;
exit(-1);
}
string line;
int n, value;
vector<int> weight;
inFile >> n;
for ( int i = 0; i < n; ++i)
if ( inFile >> value )
weight.push_back(value);
vector<vector<int> > matrix(n , vector<int>(n));
vector<vector<int> > adj(n);
vector<int> degree;
vector<char> color (n , 'w');
list<int> wlist;
list<int> glist;
list<int> blist;
cout << "color elements test: \n";
for (int i = 0; i < n; ++i) {
cout << color.at(i) << ", ";
}
cout << "the weights are: \n";
for (int i = 0; i < n; ++i) {
cout << weight.at(i) << ", ";
}
cout << endl;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
inFile >> value;
matrix.at(i).at(j) = value;
if (i == j)
matrix[i][j] = 0;
if (value == 1)
adj.at(i).push_back(j);
}
degree.push_back(adj[i].size());
}
cout << "the neighbor of each elemnt are: \n";
for (int i = 0; i < adj.size(); ++i) {
cout << "[" << i << "]: ";
for (int j = 0; j < adj.at(i).size(); ++j) {
cout << adj.at(i).at(j) << ",";
}
cout << endl;
}
cout << "the degree are: \n";
for (int i = 0; i < n; ++i) {
cout << degree.at(i) << ", ";
}
for( int i=0; i < n; i++ ) {
wlist.push_back(i);
}
cout << "\n white list elements are: \n" ;
for(list<int>::iterator theIterator = wlist.begin(); theIterator != wlist.end(); theIterator++ ) {
cout << *theIterator << " ";
}
//////////////////// greedy heuristic algorithm ///////////////////////
cout << " \n i'm here\n" ;
int cd;
int best_vertex = -1;
double bval;
double nval;
while(! wlist.empty()){
for(list<int>::iterator it = wlist.begin(); it != wlist.end(); it++)
{
cd = degree[*it];
nval = (double) cd / (double) weight[*it];
if (best_vertex == -1)
{
bval = nval;
best_vertex = *it;
}
else
if(nval > bval)
{
bval = nval;
best_vertex = *it;
}
}
// check if the best vertex is in the glist
for(list<int>::iterator it = glist.begin(); it != glist.end(); it++)
{
cd = degree[*it];
nval = (double) cd / (double) weight[*it];
if(nval > bval)
{
bval = nval;
best_vertex = *it;
}
}
degree[best_vertex] = 0; // while the best vertex is chosen change it dgree to 0
cout <<"the chosen vertex in this iteration is :" << best_vertex << "\n" ;
//add neighbors to glist
for(vector<int>::iterator it = adj[best_vertex].begin(); it != adj[best_vertex].end(); it++)
{
if(color.at(*it) == 'w')
{
wlist.remove(*it);
color.at(*it) = 'g';
glist.push_back(*it);
}
}
//delete gray vertices that hv no white neighbors
for(list<int>::iterator it = glist.begin(); it != glist.end(); it++)
{
if (degree.at(*it) == 0)
{
it = glist.erase(it);
}
}
// add best_vertex to blist
if (color.at(best_vertex) == 'w')
wlist.remove(best_vertex);
else
glist.remove(best_vertex);
blist.push_back(best_vertex);
color.at(best_vertex) = 'b';
}
cin.ignore();
return 0;
}