Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Breadth First Search question C++

This is my first time programming C++ and I've been asked to code a breadth first search where given this class

class route {

  friend ostream& operator<<(ostream& os, const route& p);

 public:

  route(const string& startPlayer);
  int getLength() const { return links.size(); };
  void addConnect(const sport& s, const string& player);
  void removeConnect();
  const string& getLastPlayer() const;

 private:

  struct Connect {
    sport s;
    string player;
    Connect() {}
    Connect(const sport& s, const string& player) : s(s), player(player) {}
  };

  string startPlayer;
  vector<Connect> links;
};

sport is a struct consisting of string name and int players. Could someone explain to me how I'd go about making the BFS?

Thanks in advance!

EDIT:

I understand the algorithm for BFS, but since I've only ever programmed C, understanding OO programming is quite confusing to me, given that interface, where do I start with this BFS, do I make a new function which makes the BFS comparing, the start string with the target string

namespace {

string promptForSPlayer(const string& prompt, const spdb& db)
{
  string response;
  while (true) {
    cout << prompt << " [or <enter> to quit]: ";
    getline(cin, response);
    if (response == "") return "";
    vector<sport> splist;
    if (db.getsplist(response, splist)) return response;
    cout << "It's not here: \"" << response << "\" in the sports database. "
     << "Please try again." << endl;
  }
}

}

int main(int argc, char *argv[])
{
  if (argc != 2) {
    cerr << "Usage: sports" << endl;
    return 1;
  }

  spdb db(argv[1]);

  if (!db.good()) {
    cout << "Failed to properly initialize the spdb database." << endl;
    cout << "Please check to make sure the start files exist and that you have permission to read them." << endl;
    exit(1);
  }

  while (true) {
    string start = promptForSplayer("Player", db);
    if (start == "") break;
    string target = promptForSplayer("Another Player", db);
    if (target == "") break;
    if (start == target) {
      cout << "Good one.  This is only interesting if you specify two different people." << endl;
    } else {
      // replace the following line by a call to your generateShortestPath routine... 
      cout << endl << "No path between those two people could be found." << endl << endl;
    }
  }
  return 0;
}
like image 299
FHr Avatar asked Apr 16 '26 09:04

FHr


1 Answers

Breadth First search is all about asking 2 questions

  1. What state am I at right now?
  2. What states can I get to from here?

The idea is to have an initial state and continuously ask yourself these 2 questions until

  1. No more states left.
  2. I have reached the destination state.

BFS usually uses a Queue to which you simply add any new states you find and simply pop from the front of the queue whenever you want to process a new state and add any new states to the end of the queue.

like image 161
Jesus Ramos Avatar answered Apr 18 '26 22:04

Jesus Ramos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!