i don't know where is the problem or wha i'm missing here but i'm stuck in an infinite loop when i compile it keep desplaying these two cout

cout << "\ntotal weight of neighbors of  "<< *it <<" is " << " "<< W;




cout << "the selected node with argmax of degree/weight is = " << best_vertex;

here's my code

#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;
// fill hte weight vector
    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;
// get the adjancey matrix "the matrix of neighboors"
    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) << ", ";
    }
// filling the wlist
   for( int i=0; i < n; i++ ) {
       wlist.push_back(i);
   }
        list<int>::iterator theIterator;
// desplaying white list "it's just a test"
        cout << "white list elements are: \n" ;
   for( theIterator = wlist.begin(); theIterator != wlist.end(); theIterator++ ) {
     cout << *theIterator << " ";
   }

    int cd;
    int W ;
    int best_vertex = -1;
    double bval = -1.0;
    double nval;
    vector<int>::iterator row;
    vector<int>::iterator col;
    while (!wlist.empty()){
        bval = 0.0;
// loop to get the Weight of the white neighbors
    for(list<int>::iterator it = wlist.begin(); it != wlist.end(); it++)
    {
        W =0;
    for (row = adj.at(*it).begin(); row != adj.at(*it).end(); row++) {

        if(color.at(*row) == 'w')
        {   
            W = W +weight.at(*row);
        }
    cout << "\ntotal weight of neighbors of  "<< *it <<" is " << " "<< W;
   }
        nval = (double) W / (double) weight[*it];
        //cout << "nval =" << nval << "\n" ;
        if (best_vertex == -1)
        {
            bval = nval;
            best_vertex = *it;
        }
        else
            if(nval > bval)
            {
                bval = nval;
                best_vertex = *it;
            }
    }
     degree[best_vertex] = 0;
    cout << "the selected node with argmax of degree/weight is = " << best_vertex; 

//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++*/ )

    {
        int i;
        int x =0;
    for( i=0;i<adj.at(*it).size();i++){
        if(color.at(adj.at(*it).at(i)) == 'w')
         x++;
    }
        if(x == 0)
        {
            it = glist.erase(it);
        }
        else
        {
        ++it;
        }
        //cout << i;
    }
    // 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';

    }
    cout << "\n  the white list" <<"\n";
    for(list<int>::iterator theIterator = wlist.begin(); theIterator != wlist.end(); theIterator++ ) {
     cout << *theIterator << " ";
   }
    cout << "\n  the gray list" <<"\n";
       for(list<int>::iterator theIterator = glist.begin(); theIterator != glist.end(); theIterator++ ) {
     cout << *theIterator << " ";
   }
      cout << "\n  the black list" <<"\n";
    for(list<int>::iterator theIterator = blist.begin(); theIterator != blist.end(); theIterator++ ) {
     cout << *theIterator << " ";
   }

   cin.ignore();
    return 0;
}

with the same value of *it W and best_vertex
please i really need help in this

Recommended Answers

All 2 Replies

Since those output lines are in a loop that will run forever if wlist never empties...

never empties means here the bval never changes but i return the bval value to 0.0 after each iteration

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.