/* * File: Traverse.cpp * ------------------ * This program reimplements the…
- User
/*
- File: Traverse.cpp
-
- This program reimplements the breadth-first search using an
- explicit queue, and depth-first search algorithm using an explicit stack */
#include #include #include #include #include #include #include "CSC3002OJActive/assignment6/simplegraph.h" using namespace std;
void bfs(Node *start) { cout << "Breadth First Search" << endl; if (start == nullptr) return; // check if the start node is valid
set<Node*> visitedNodes; queue<Node*> nodeQueue; // Enqueue the starting node on the queue nodeQueue.push(start); while (!nodeQueue.empty()) { // Dequeue the front node from the queue Node* currentNode = nodeQueue.front(); nodeQueue.pop(); // Visit the node if it hasn’t been visited yet if (visitedNodes.find(currentNode) == visitedNodes.end()) { cout << "Visited Node: " << currentNode->name << endl; visitedNodes.insert(currentNode); // Enqueue its neighbors on the queue for (Node* neighbor : currentNode->neighbors) { if (visitedNodes.find(neighbor) == visitedNodes.end()) { nodeQueue.push(neighbor); } } } }}
void dfs(Node *start) { cout<< "Depth First Seacrh"<< endl; if (start == nullptr) return; // check if the start node is valid
set<Node*> visitedNodes; stack<Node*> nodeStack; // Push the starting node on the stack nodeStack.push(start); while (!nodeStack.empty()) { // Pop the topmost node from the stack Node* currentNode = nodeStack.top(); nodeStack.pop(); // Visit the node if it hasn’t been visited yet if (visitedNodes.find(currentNode) == visitedNodes.end()) { cout << "Visited Node: " << currentNode->name << endl; visitedNodes.insert(currentNode); // Push its neighbors on the stack for (Node* neighbor : currentNode->neighbors) { if (visitedNodes.find(neighbor) == visitedNodes.end()) { nodeStack.push(neighbor); } } } }}
int main(){ SimpleGraph airline; readGraph(airline, cin); cout << "DFS at Portland ...." << endl; dfs(airline.nodeMap.at("Portland")); cout << "BFS at Portland ...." << endl; bfs(airline.nodeMap.at("Portland")); return 0; }
the error is:'struct Node' has no member named 'neighbors'
- Assistant
The error message indicates that the code is trying to access a member called "neighbors" of a struct called "Node", but the struct definition does not include such a member.
There are a few possible reasons for this error:
- The struct definition is missing the "neighbors" member.
- The code is using the wrong struct definition for "Node".
- The code is using the wrong syntax to access the "neighbors" member.
To fix the error, you need to check the struct definition for "Node" and make sure it includes a member called "neighbors". If it does not, you need to add it. If the struct definition is correct, you need to check the code to ensure that it is using the correct struct definition for "Node" and that it is using the correct syntax to access the "neighbors" member.
- User
please fix this error
- Assistant
To fix the error, you would first need to check the struct definition for "Node" and see if it includes a member called "neighbors". If it does not include this member, you would need to add it.
Assuming that the struct has a member called "neighbors", the error might be caused by using the wrong struct definition for "Node". To confirm that the correct struct definition is being used, you could check if it matches the one used in the SimpleGraph class definition.
Here's an example of a struct definition for "Node" that includes a member called "neighbors":
struct Node { string name; set<Edge *> edges; set<Node *> neighbors; // This is the new member that was added };If this matches the struct definition used in the SimpleGraph class, you could try using the qualified name for the struct, like this:
for (Node* neighbor : currentNode->Node::neighbors) { // Use the qualified name Node::neighbors if (visitedNodes.find(neighbor) == visitedNodes.end()) { nodeStack.push(neighbor); } }This would ensure that the correct struct definition is being used.