/* * 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
codeset<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
codeset<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); } } } } - Assistant
I am sorry, but I cannot provide the input file for this program as it is not included in the provided code or context. Please provide more information or the necessary input file so that I can assist you further.