1

Im writing a program for breadth first traversal of a graph, it takes input from a file and calculates time taken for traversal and disconnected graphs. I get the error deque iterator not dereferenceable and dont really know what that means, ive tried reading the answers posted to questions before but couldn't make any sense of them. Please help me... Here is my code

#include <iostream>
#include <fstream>
#include <vector>
#include <ctime>
#include <string>
#include <iomanip>
#include <sstream>
#include <queue>


using std::cout;
using std::cin;
using std::endl;
using std::setw;
using std::ofstream;
using std::ifstream;
using std::fstream;
using std::string;
using std::ios;
using std::istringstream;
using std::stringstream;
using std::vector;
using std::queue;

bool allVisited(vector<bool> );

static const int N=32;
int main(){
    bool found;
    int count=0;
    clock_t start;
    clock_t stop;
    double time;
    vector<vector<int> > matrix ( N, vector<int> ( N ) );
    vector<bool> traversed;
    for(int i=0;i<N;i++)
        traversed.push_back(false);

    queue<int> breadth_first;

    ifstream myFile;
    string line;
    int offset; 
    myFile.open ("g32.data");

    if(myFile.is_open()){
        while(!myFile.eof()){
            int p=0, q=0;
            getline(myFile,line);

            istringstream tokenizer(line);
            string token,token1;
            getline(tokenizer, token, ' ');
            getline(tokenizer, token1, ' ');

            istringstream first(token);
            first >>p;

            istringstream second(token1);
            second >>q;

            if(p==0||q==0)
                continue;
            else
            matrix[p-1][q-1]=1;
        }
    }

    start=clock();
    int root=0;
    traversed[root]=true;
    breadth_first.push(root);

    while(!breadth_first.empty() && !allVisited(traversed)){

            for (int i=0; i<traversed.size(); i++){
                if(matrix[root][i]!=0 && !traversed[i]){
                cout<<"here";
                traversed[i]=true;
                breadth_first.push(i);
                }
            }

            breadth_first.pop();
            root=breadth_first.front();

    } 

    stop=clock();
    time=double(stop-start)/CLOCKS_PER_SEC;

    for(int i=0; i<traversed.size(); i++)
    if(traversed[i]==1)
        count++;

    cout<<"The Execution Time is :"<<time<<" Seconds"<<endl;
    if(count==N)
    cout<<"There were no Disconnected Graphs"<<endl;
    system("PAUSE");
}

bool allVisited(vector<bool> v) {
  for(int i=0; i<v.size(); i++)
    if(v[i]==false)
      return false;

  return true;
}
  • It seems the same issue: http://stackoverflow.com/questions/1259099/stl-queue-iteration – VladimirM Feb 21 '14 at 01:13
  • And _that's_ why having a knee-jerk reaction that `using namespace std;` is bad is bad. – Nicu Stiurca Feb 21 '14 at 01:14
  • 1
    @SchighSchagh I don't know about anyone else, but I've no problem with `using namespace insertyourfavoritenamehere;`, including `std`, so long as it isn't in a *header* file. Maybe its a preference thing. Dunno. – WhozCraig Feb 21 '14 at 01:29
  • And for the OP, unrelated, but this [`while(!myFile.eof())` is wrong](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). You should fix that sooner than later. – WhozCraig Feb 21 '14 at 01:31
  • @WhozCraig I think that's what SchighSchagh was saying, if you read carefully. – paddy Feb 21 '14 at 01:34
  • Please paste the compiling error message,this may help. – prehistoricpenguin Feb 21 '14 at 02:04

0 Answers0