c++ - Why doesn't my code work? -
i'm implementing simple dfs traversal directed graph:
#include <iostream> #include <vector> #include <climits> #include <utility> #include <deque> #include <queue> #include <algorithm> #include <iomanip> #include <list> using namespace std; enum class color_type { black, white, gray }; struct vertex { char label; color_type color; int start; int finish; vertex *parent; vector<vertex> adjacents; vertex(char label) :label(label), start(0), finish(0), color(color_type::white) { } void add_neighbor(const vertex &v) { adjacents.push_back(v); } }; class digraph { private: vector<vertex> vertices; int count; public: digraph() :count(0) { vertex a('a'); vertex b('b'); vertex c('c'); add_edge(a, b); add_edge(b, c); add_edge(c, a); vertices.push_back(a); vertices.push_back(b); vertices.push_back(c); (int = 0; < vertices.size(); ++i) { vertices[i].color = color_type::white; vertices[i].parent = null; } } void add_edge(vertex &u, vertex &v) { u.add_neighbor(v); } void dfs() { dfs_visit(vertices[0]); } void dfs_visit(vertex &u) { count++; u.start = count; u.color = color_type::gray; cout << "??? visit = " << u.label << endl; cout << "# neighbors: " << u.adjacents.size() << '\n'; (int = 0; < u.adjacents.size(); ++i) { if (u.adjacents[i].color == color_type::white) { cout << "visit neighbor of [" << u.label << "] is: " << u.adjacents[i].label << endl; u.adjacents[i].parent = &u; dfs_visit(u.adjacents[i]); } } u.color = color_type::black; count++; u.finish = count; } public: friend ostream& operator <<(ostream& o, const digraph &dg) { (int = 0; < dg.vertices.size(); ++i) { o << dg.vertices[i].label << ":\n"; o << "\t start = " << dg.vertices[i].start << endl; o << "\t finish = " << dg.vertices[i].finish << endl; } return o; } }; int main() { digraph dg; dg.dfs(); cout << dg << endl; return 0; }
the problem call dfs_visit()
stop after visit 2nd vertex. pass vertex u
reference parameter of dfs_visit()
function, somehow number of neighbor of vertex b
becomes 0
odd.
seemed me vertices stored in vector vertices
not same ones passed dfs_visit
, don't see how case. i've been using java while , i'm rusty c++. shed me lights regarding issue?
edit
this closer you're looking for, using pointers neighbors. hope helps. difference by-pointer addressing of neighbors within primary vertex container, opposed copies being made in code.
note: add-construction sets node have "next" node in vertices collection neighbor, finishing last node getting first neighbor. seemed you're code trying accomplish.
#include <iostream> #include <vector> #include <climits> #include <utility> #include <deque> #include <queue> #include <algorithm> #include <iomanip> #include <list> using namespace std; enum class color_type { black, white, gray }; struct vertex { char label; color_type color; int start; int finish; vertex *parent; vector<vertex*> adjacents; vertex(char label) :label(label), start(0), finish(0), color(color_type::white) { } void add_neighbor(vertex &v) { adjacents.push_back(std::addressof(v)); } }; class digraph { private: vector<vertex> vertices; int count; public: digraph() :count(0) { vertices.push_back(vertex('a')); vertices.push_back(vertex('b')); vertices.push_back(vertex('c')); (size_t i=0; i<vertices.size(); ++i) { vertices[i].color = color_type::white; vertices[i].parent = null; vertices[i].add_neighbor(vertices[(i+1)%vertices.size()]); } } void dfs() { dfs_visit(vertices[0]); } void dfs_visit(vertex &u) { count++; u.start = count; u.color = color_type::gray; cout << "??? visit = " << u.label << endl; cout << "# neighbors: " << u.adjacents.size() << '\n'; (int = 0; < u.adjacents.size(); ++i) { if (u.adjacents[i]->color == color_type::white) { cout << "visit neighbor of [" << u.label << "] is: " << u.adjacents[i]->label << endl; u.adjacents[i]->parent = &u; dfs_visit(*(u.adjacents[i])); } } u.color = color_type::black; count++; u.finish = count; } public: friend ostream& operator <<(ostream& o, const digraph &dg) { (int = 0; < dg.vertices.size(); ++i) { o << dg.vertices[i].label << ":\n"; o << "\t start = " << dg.vertices[i].start << endl; o << "\t finish = " << dg.vertices[i].finish << endl; } return o; } }; int main() { digraph dg; dg.dfs(); cout << dg << endl; return 0; }
output
??? visit = # neighbors: 1 visit neighbor of [a] is: b ??? visit = b # neighbors: 1 visit neighbor of [b] is: c ??? visit = c # neighbors: 1 a: start = 1 finish = 6 b: start = 2 finish = 5 c: start = 3 finish = 4
Comments
Post a Comment