Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6616685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:36:45+00:00 2026-05-25T20:36:45+00:00

I am trying to implement the BFS algorithm described in CLRS. And have the

  • 0

I am trying to implement the BFS algorithm described in CLRS. And have the following:

#include <iostream>
#include <list>
#include <queue>
#include <string>
#include <sstream>
using namespace std;
struct Node{
    char colour;
    int numNbr;
    Node* parent;
    int distance;
    int* neighbours;
    int* costs;
    int name;
    Node(int _numNbr,int _name){
        name = _name;
        colour = 'w';
        parent = 0;
        distance = -1;
        neighbours = new int[_numNbr];
        costs      = new int[_numNbr];
        numNbr = _numNbr;
    }
};

list<Node*> bfs(Node** &nodes,int numNodes,int startNode) {
    cout << "performing BFS\n";
    for(int i = 0; i < numNodes;i++) {
        nodes[i]->colour = 'w';
        nodes[i]->parent = 0;
    }
    cout << "All nodes painted white" <<endl;
    queue<Node*> q; // segfault occurs here
    cout << "initialised a queue" << endl;
    list<Node*> l;
    cout << "initialised a list" << endl;
    nodes[startNode]->colour = 'g';
    nodes[startNode]->distance = 0;
    q.push(nodes[startNode]);
    Node* u;
    Node* v;
    while(!q.empty()){
        u = q.front();
        for(int i = 0;i < u->numNbr; i++) {
            v = nodes[u->neighbours[i]];
            if(v->colour == 'w'){
                v->colour = 'g';
                v->distance = (u->distance)+1;
                v->parent = u;
                q.push(v);
            }
        }
        l.push_front(u);
        u->colour = 'b';
        q.pop();
    }
    return l;
}

int main(){
    int nodeCount;
    cin >> nodeCount;
    cin.ignore();
    Node** nodes = new Node*[nodeCount+1];
    for(int i = 0; i < nodeCount; i++){
        .... // build up the nodes in the adjacency list
    }
    list<Node*> l = bfs(nodes,nodeCount,1);
    cout << "BFS of nodes\n";
    for(list<Node*>::iterator it = l.begin();it != l.end();it++){
        cout << (*it)->name << " ";
    }
    cout << endl;
    return 0;
}

When I run this however, I only get the following output followed by a segfault when the queue is initialised:

jonathan@debian:~/Code/cpp/dijkstra$ ./dijkstra 
3
1 2 1 3 1
2 3 1
3 1 1

performing bfs
All nodes painted white
Segmentation fault

I am compiling with the following command:

g++ -Wall  -o dijkstra dijkstra.cpp
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T20:36:46+00:00Added an answer on May 25, 2026 at 8:36 pm
    list<Node*> bfs(...
    

    while you return:

    return l;
    

    also, no need for reference here:

    Node** &nodes
    

    And segfault didn’t occur where you pointed. I/O buffers were not fulshed because of it and it loooks like this

    cout << "initialised a queue" << endl;
    list<Node*> l;
    cout << "initialised a list" << endl;
    

    wasn’t executed

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I trying to implement synchronization algorithm in C#, without success. Why isn't the following
Im trying to implement pagination using multiple searching criteria. Supposed I Have student table.
Trying to implement the following behavior on an iPad. I have a map-centric application
trying to implement a dialog-box style behaviour using a separate div section with all
I am trying to implement string unescaping with Python regex and backreferences, and it
Trying to implement what I thought was a simple concept. I have a user
Trying to implement the following structure from c to use NSArray in objective-c: In
iam trying to implement an Searchmachine into my site. Iam using Zend_Search_Lucene for this.
I'm trying to implement the Ford Fulkerson Algorithm in C++. However, I'm having trouble
I'm trying implement a bracket in my program (using C#/.NET MVC) and I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.