Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C++ program called BFS.cpp that implements the Breadth-First Search (BFS

ID: 3832648 • Letter: W

Question

Write a C++ program called BFS.cpp that implements the Breadth-First Search (BFS) algorithm. Your program should read an input file name and a starting node from a user. After that, your program should display the list of nodes visited. In the problem, you can assume that the number of vertices in the input file is less than or equal to 25. When you write the program, don’t forget to include “Title”, “Abstract”, “ID (A four-digit number)”, “Name”, and “Date”.

Input file format: This is a sample input file called t1.txt.

The first line (= 3 in the example) indicates that there are three vertices in the graph. For the homework, we can assume that the first vertex starts from the number 0. Thus, t1.txt describes a graph like below:

One blank space is used to delimiter the data. Note that there’s no blank space at the end of each line. Your program must read the file correctly.

The following presents a sample run of the program. Your program has to run exactly like this.

Enter filename: C:\tmp\t1.txt

Enter a start vertex: 0

BFS order: 0 -> 1 -> 2

In the program, your program has to follow our convention (= ascending order).

101 010 101 3010

Explanation / Answer

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <conio.h>
#include <deque>

using namespace std;

struct coords
{

int x;
int y;
char ch;
int score;
int cost;
coords* parent;
};

bool search ( deque <coords*> &templist, int x, int y)
{
deque <coords*>::iterator m;
m = templist.begin();
while( m != templist.end())
{
if ((*m)->x == x && (*m)->y == y)
{
return true;
}
m++;
}
return false;
}


int main()
{
deque <coords*> openlist;
deque <coords*> closedlist;
deque <coords*>::iterator p;
deque <coords*>::iterator k;

coords* newstate = new(coords);
coords* start = new(coords);
coords* current = new(coords);
coords* goal = new(coords);

vector<string> grid;

int row = 10;
int col = 10;

ifstream myfile( "example.txt" );

string line;
while(getline(input, line))
{
grid.push_back(line);

}


while( input && row <= 100 )
{
input >> grid[ row ][ col ];

if( input )
{
if( ++col == 10 )
{   
++row;
col = 0;
}
}

}

for(size_t i=0 ; i < grid.size(); ++i)
{
cout <<grid[i] << ' ';
}

input.close();


goal-> x = 3;
goal-> y = 8;
start->x = 2;
start->y = 2;

openlist.push_back(start);

while (complete != true)
{

current = openlist.front();
openlist.pop_front();
if (current-> x == goal->x && current->y == goal->y)
{
complete = true;
}
closedlist.push_back(current);
  

int count =5;

for( int n = 1; n <count; n++)
{


if( n == 1)
{
newstate = new(coords);
newstate ->x = current-> x;
newstate ->y = current-> y += 1;
if(search(openlist, newstate->x, newstate->y) == false && search(closedlist, newstate->x, newstate->y))
{
openlist.push_back(newstate);
}

}

if( n == 2)
{
newstate = new(coords);
newstate ->x = current->x += 1;
newstate ->y = current->y;
if(search(openlist, newstate->x, newstate->y) == false && search(closedlist, newstate->x, newstate->y))
{
openlist.push_back(newstate);
}

}

if( n == 3)
{
newstate = new(coords);
newstate ->x = current->x;
newstate ->y = current->y -= 1;
if(search(openlist, newstate->x, newstate->y) == false && search(closedlist, newstate->x, newstate->y))
{
openlist.push_back(newstate);
}
}

if( n == 4)
{
newstate = new(coords);
newstate ->x = current->x -= 1;
newstate ->y = current-> y;
if(search(openlist, newstate->x, newstate->y) == false && search(closedlist, newstate->x, newstate->y))
{
openlist.push_back(newstate);
}

cout << "Current: " << current->x << ", " << current->y << endl;

p = openlist.begin();
cout<<"openlist:"<< endl;
while( p != openlist.end() )
{

cout << (*p)->x << " , "<< (*p)->y << endl;
p++;
}

k = closedlist.begin();
cout<<"closedlist:"<< endl;
while( k != closedlist.end() )
{

cout << (*k)->x << " , "<< (*k)->y<< endl;
k++;
}

}

}
cin.get();

}
cout << " GOAL FOUND ";


system("pause");
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote