Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a program ( in main.cpp ) that: Prompts the user for a filename containing node data. Outputs the nodes of a graph in a

Write a program (in main.cpp) that:
Prompts the user for a filename containing node data.
Outputs the nodes of a graph in a depth first traversal.
info>Note: Files Ch20_Ex13Data.txt and Ch20_Ex1Data.txt contain node data that you may test your program with.
Here is my code:
main.cpp
#include
#include
#include
#include
using namespace std;
void depthFirstTraversal(const vector>& graph, int start){
vector visited(graph.size(), false);
stack s;
s.push(start);
while (!s.empty()){
int node = s.top();
s.pop();
if (!visited[node]){
cout node "";
visited[node]= true;
for (int neighbor : graph[node]){
if (!visited[neighbor]){
s.push(neighbor);
}
}
}
}
}
int main(){
string filename;
cout "Enter filename containing node data: ";
cin >> filename;
ifstream inputFile(filename);
if (!inputFile){
cerr "Error opening file." endl;
return 1;
}
int numNodes;
inputFile >> numNodes;
vector> graph(numNodes);
for (int i =0; i numNodes; ++i){
int node;
inputFile >> node;
while (node !=-999){
graph[i].push_back(node);
inputFile >> node;
}
}
inputFile.close();
cout "Depth First Traversal:" endl;
for (int i =0; i numNodes; ++i){
cout "Starting from node " i ": ";
depthFirstTraversal(graph, i);
cout endl;
}
return 0;
}
the code is working yet the parameter that is being asked is not being approved, please help.
image text in transcribed

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_2

Step: 3

blur-text-image_3

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

Upgrading Oracle Databases Oracle Database New Features

Authors: Charles Kim, Gary Gordhamer, Sean Scott

1st Edition

B0BL12WFP6, 979-8359657501

More Books

Students also viewed these Databases questions

Question

Provide examples of KPIs in Human Capital Management.

Answered: 1 week ago

Question

What are OLAP Cubes?

Answered: 1 week ago