Page 1 of 1

Graph traversal is a very important algorithm to go over all vertices in a graph. In this homework, you need to implemen

Posted: Mon May 09, 2022 7:03 am
by answerhappygod
Graph Traversal Is A Very Important Algorithm To Go Over All Vertices In A Graph In This Homework You Need To Implemen 1
Graph Traversal Is A Very Important Algorithm To Go Over All Vertices In A Graph In This Homework You Need To Implemen 1 (75.35 KiB) Viewed 28 times
graph.h
Graph Traversal Is A Very Important Algorithm To Go Over All Vertices In A Graph In This Homework You Need To Implemen 2
Graph Traversal Is A Very Important Algorithm To Go Over All Vertices In A Graph In This Homework You Need To Implemen 2 (37.35 KiB) Viewed 28 times
Graph Traversal Is A Very Important Algorithm To Go Over All Vertices In A Graph In This Homework You Need To Implemen 3
Graph Traversal Is A Very Important Algorithm To Go Over All Vertices In A Graph In This Homework You Need To Implemen 3 (37.68 KiB) Viewed 28 times
graph.cpp
Graph Traversal Is A Very Important Algorithm To Go Over All Vertices In A Graph In This Homework You Need To Implemen 4
Graph Traversal Is A Very Important Algorithm To Go Over All Vertices In A Graph In This Homework You Need To Implemen 4 (37.96 KiB) Viewed 28 times
Graph Traversal Is A Very Important Algorithm To Go Over All Vertices In A Graph In This Homework You Need To Implemen 5
Graph Traversal Is A Very Important Algorithm To Go Over All Vertices In A Graph In This Homework You Need To Implemen 5 (32.14 KiB) Viewed 28 times
main.cpp
Graph Traversal Is A Very Important Algorithm To Go Over All Vertices In A Graph In This Homework You Need To Implemen 6
Graph Traversal Is A Very Important Algorithm To Go Over All Vertices In A Graph In This Homework You Need To Implemen 6 (48.11 KiB) Viewed 28 times
Graph traversal is a very important algorithm to go over all vertices in a graph. In this homework, you need to implement both the depth-first search (DFS) and the breadth-first search (BFS) algorithms, and output the order of these two traversals from any given graph. A header file is provided for the graph traversal class: graph.h. You need to download it from the ecourses, and implement all functions defined in the class using CH with a file named Graph.cpp. The testing main.cpp file is provided too. You need to test your algorithms with different graphs to check if these functions are working correctly. The expected output of your program should be the order of visiting the vertices in the graph as below. DFS: a, b, f, e, g, d, c, h, BFS: a, b, e, f, g, d, h, c, Your output format needs to be the same as the expected one, or you will not receive any credit for the homework. Submission policy: 1. You need to put everything (including your completed program source code and the execution results screenshots) into a report (.docx or .pdf file) and then submit the report AND your source code to eCourses before the due time. Do NOT change any of their names or upload other files to ecourses.
11- // // Homework graph.h // // Class declaration for the graph traversal algorithms // 11- #ifndef GRAPH_H #define GRAPH_H #include <iostream> #include <vector> using namespace std; struct Node { Node(char newName) {name = newName, visited = 0; edges.clear();} char name; int visited; vector<char> edges; }; class graph { public: graph(); void addVertex (const Node node); void addEdge(const char from, const char to); void print(); void DFS(); void BFS;
#ifndef GRAPH_H #define GRAPH_H #include <iostream> #include <vector> using namespace std; struct Node { Node(char newName) {name = newName, visited = 0; edges.clear();} char name; int visited; vector<char> edges; }; class graph { public: graph(); void addvertex(const Node node); void addEdge (const char from, const char to); void print(); void DFS(); void BFS(); void reset(); vector<Node>& getVertices() {return vertices;} private: vector<Node> vertices; void dfs (Node& node); void bfs (Node& node); int count; }; #endif
#include <iostream> #include "graph.h" using namespace std; graph::graph() { } void graph::addVertex (const Node node) { vertices.push_back(node); } void graph::addEdge(const char from, const char to) { vertices[from-'a').edges.push_back(to); vertices[to-'a').edges.push_back(from); } void graph::print() { for(int i=0; i<vertices.size(); i++) { cout << vertices.name « for (int j=0; j<vertices.edges.size(); j++) cout << vertices.edges[j] << ", cout << endl; } void graph::DFS { } void graph::dfs (Node& node)
vertices to-'a').edges.push_back(from); } void graph::print() { for(int i=0; i<vertices.size(); i++) { cout << vertices.name << ": "; for (int j=0; j<vertices.edges.size(); j++) cout << vertices.edges[j] << ", cout << endl; } } void graph::DFS() { } void graph::dfs (Node& node) { } void graph: :reset() { for(int i=0; i<vertices.size(); i++) vertices.visited = 0; count = 0; } void graph::BFS) { } void graph::bfs (Node& node) { }
#include <iostream> #include "graph.h" using namespace std; int main() { graph my Graph; int num = 8; for(int i=0; i<num; i++) { Node node('a'+i); myGraph.addVertex (node); } my Graph.addEdge('a', 'b'); myGraph.addEdge('a', 'e'); myGraph.addEdge('a', 'f'); myGraph.addEdge('b', 'f'); my Graph.addEdge('b', 'g'); myGraph.addEdge('c', 'd'); myGraph.addEdge('c', 'h'); my Graph.addEdge('d', 'g'); my Graph.addEdge('e', 'f'); my Graph.addEdge('g', 'h'); my Graph.print(); my Graph.DFS(); my Graph.BFS(); return 0; }