Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

See UPDATE below. Hi! I'm supposed to write this breadthFirstSearch algorithm in C++, but I'm not quite sure how to do it since we're only

See UPDATE below.

Hi! I'm supposed to write this breadthFirstSearch algorithm in C++, but I'm not quite sure how to do it since we're only supposed to take in a start value. The skeleton code is below...can anyone help me? Thanks!

#include #include #include

using namespace std;

class My_Graph { const static int MAXNUMVERTICES = 100; int theGraph[MAXNUMVERTICES][MAXNUMVERTICES]; bool undirected; public:

My_Graph() { undirected = true; }

void insertEdge(int to, int from, int weight) { theGraph[to][from] = weight; theGraph[from][to] = weight; } void breadthFirstSearch(int start) { //insert code here };

int main() { My_Graph *theGraph = new My_Graph(); int numEdges, inVert, outVert; int lowestVert=999; cin >> numEdges; for (int i=0; i { cin >> inVert; cin >> outVert; theGraph->insertEdge(inVert, outVert, 1); if (inVert < lowestVert) lowestVert = inVert; if (outVert < lowestVert) lowestVert = outVert; } theGraph->breadthFirstSearch(lowestVert); }

UPDATE: Nevermind! Here is the solution:

#include #include #include

using namespace std;

class My_Graph { const static int MAXNUMVERTICES = 100; int theGraph[MAXNUMVERTICES][MAXNUMVERTICES]; bool undirected; public:

My_Graph() { undirected = true; }

void insertEdge(int to, int from, int weight) { theGraph[to][from] = weight; theGraph[from][to] = weight; } void breadthFirstSearch(int start) { //your code here int visited[MAXNUMVERTICES]; queue q; int i; for(i=start;i<=MAXNUMVERTICES;i++) //initialize visited array visited[i]=0;

// make visited starting vertex visited[start] = 1; //push the starting vertex into queue q.push(start); while(!q.empty()) { int x = q.front(); q.pop(); cout<< x <<" "; int j; //for all vertex j adjacent to x for(j=start;j<=MAXNUMVERTICES;j++) { //if j is not visited if(theGraph[x][j] == 1 && visited[j] == 0) { //make visited[j] =1 and push into stack visited[j] = 1; q.push(j); } } } while(!q.empty()) { cout<< q.front() <<" "; q.pop(); } } };

int main() { My_Graph *theGraph = new My_Graph(); int numEdges, inVert, outVert; int lowestVert=999; cin >> numEdges; for (int i=0; i { cin >> inVert; cin >> outVert; theGraph->insertEdge(inVert, outVert, 1); if (inVert < lowestVert) lowestVert = inVert; if (outVert < lowestVert) lowestVert = outVert; } theGraph->breadthFirstSearch(lowestVert); }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Management System MCQs Multiple Choice Questions And Answers

Authors: Arshad Iqbal

1st Edition

1073328554, 978-1073328550

More Books

Students also viewed these Databases questions

Question

Has the priority order been provided by someone else?

Answered: 1 week ago

Question

Compare the current team to the ideal team.

Answered: 1 week ago

Question

Are the rules readily available?

Answered: 1 week ago