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 #include #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 (isspace)); line.erase(end, line.end()); for (int i = 0; i < line.size(); i++) labels.push_back('A' + i); }

while (input) { list values; input.getline(temp, 100); string line(temp); if (line.size() == 0) break; line.erase(remove_if(line.begin(), line.end(), static_cast(isspace)), line.end()); for (int i = 1; i < line.size(); i++) { if (line[i] == '1') values.push_back(i - 1); } adj_list.push_back(values); } }

void graph::print() const { int i = 0; cout << "Number of verticies in graph: " << size << endl; cout << " ----------START GRAPH---------" << endl; for (list l : adj_list) { cout << (char)('A' + i++) << ": "; for (int c : l) cout << labels[c] << ", "; cout << endl; } cout << "-----------END GRAPH----------" << endl; }

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 #include

class graph { private: int size; std::vector< std::list > adj_list; std::vector< char > labels; void depth_first(int) const; public: graph(const char* filename); ~graph(); int get_size() const; void traverse() const; void print() const;

};

#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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!