Question: For this computer assignment, you are to write a C++ program to implement several graph algorithms on simple graphs. These graphs are implemented with adjacency
For this computer assignment, you are to write a C++ program to implement several graph algorithms on simple graphs. These graphs are implemented with adjacency list representation.Fix the problems with this code
#include "assignment9.h" #include
using namespace std;
int graph::get_size() const { return size; }
graph::~graph() {}
graph::graph(const char* filename) {
ifstream input; input.open(filename);
if (!input.is_open()) { cerr << "filename invalid" << endl; exit(0); }
input >> size; char temp[100]; input.getline(temp, 1); input.getline(temp, 100); { string line(temp); string::iterator end = remove_if(line.begin(), line.end(), static_cast
while (input) { list
void graph::print() const { int i = 0; cout << "Number of verticies in graph: " << size << endl; cout << " ----------START GRAPH---------" << endl; for (list
int * num; string s; void graph::depth_first(int x) const { num[x]++; cout << labels[x] << " "; for (int i : adj_list[x]) if (num[i] == 0) { s = s + '(' + labels[x] + ',' + labels[i] + ')'; depth_first(i); } }
void graph::traverse() const { s = ""; cout << " --------traverse of graph--------" << endl; num = new int[size]; for (int i = 0; i< size; i++) num[i] = 0;
for (int i = 0; i < size; i++) if (num[i] == 0) depth_first(i); cout << s << endl;
delete num; cout << "----------end traverse-----------" << endl; }
int main(int argc, char** argv) { if (argc < 2) { cerr << "args: input-file-name "; return 1; }
graph g(argv[1]);
g.print();
g.traverse();
return 0;
}
#ifndef ASSIGNMENT9_H #define ASSIGNMENT9_H #include
class graph { private: int size; std::vector< std::list
};
#endif
//7 //A B C D E F G //A 0 1 0 0 0 0 1 //B 1 0 0 1 1 0 0 //C 0 0 0 0 0 1 0 //D 0 1 0 0 1 0 0 //E 0 1 0 1 0 0 0 //F 0 0 1 0 0 0 0 //G 1 0 0 0 0 0 0
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
