c++ - Detecting and printing cycle in undirected graph -
i have question similar - same - one. want know how not detect cycle print out vertices cycle contains. tried ways mentioned in question above, must have done wrong, why doesn't work my. program checks if 1 specific vertex makes cycle. code here:
#include<iostream> #include <list> using namespace std; class graph { int v; list<int> *adj; public: graph(int v); void addedge(int v, int w); bool graph::iscyclicutil(int v, bool visited[], int *cyclevertices, int parent, int index); }; graph::graph(int v) { this->v = v; adj = new list<int>[v]; } void graph::addedge(int v, int w) { adj[v].push_back(w); adj[w].push_back(v); } bool graph::iscyclicutil(int v, bool visited[], int *cyclevertices, int parent, int index) { visited[v] = true; list<int>::iterator i; (i = adj[v].begin(); != adj[v].end(); ++i) { if (!visited[*i]) { if (iscyclicutil(*i, visited, cyclevertices, v, index)) { if (index <= 1 || cyclevertices[0] != cyclevertices[index - 1]) cyclevertices[index++] = *i; return true; } } else if (*i != parent) { cyclevertices[index++] = *i; return true; } } return false; } int main() { bool *visited = new bool[5]; (int = 0; < 5; i++) visited[i] = false; int cyclevertices[5]; (int = 0; < 5; i++) cyclevertices[i] = -1; graph g1(5); g1.addedge(1, 0); g1.addedge(0, 2); g1.addedge(2, 1); g1.addedge(0, 3); g1.addedge(3, 4); g1.iscyclicutil(4, visited, cyclevertices, -1, 0) ? cout << "graph contains cycle\n" : cout << "graph doesn't contain cycle\n"; int x = 0; while (cyclevertices[x] != -1) cout << cyclevertices[x++] << " "; return 0; }
i've found solution. i've tried j_random_hacker's solution in post , didn't work. problem indexing in cyclevertices in code. variable index same. i've added new attribute index in class graph , works. here edited code:
#include<iostream> #include <list> #define finished -1 #define nocycle -2 using namespace std; class graph { int v; int index; list<int> *adj; public: graph(int v); void addedge(int v, int w); void set_index(); int graph::iscyclicutil(int v, bool visited[], int *cyclevertices, int parent); }; graph::graph(int v) { this->v = v; adj = new list<int>[v]; this->index = 0; } void graph::set_index() { this->index = 0; } void graph::addedge(int v, int w) { adj[v].push_back(w); adj[w].push_back(v); } int graph::iscyclicutil(int v, bool visited[], int *cyclevertices, int parent) { visited[v] = true; list<int>::iterator i; (i = adj[v].begin(); != adj[v].end(); ++i) { if (!visited[*i]) { int result = iscyclicutil(*i, visited, cyclevertices, v); if (result == finished) return finished; else if (result != nocycle) { cyclevertices[index++] = v; if (result == v) return finished; else return result; } } else if (*i != parent) { return *i; } } return nocycle; } int main() { bool *visited = new bool[4]; (int = 0; < 4; i++) visited[i] = false; int cyclevertices[4]; (int = 0; < 4; i++) cyclevertices[i] = -1; graph g1(4); g1.addedge(0, 1); g1.addedge(1, 2); g1.addedge(2, 3); g1.addedge(3, 0); g1.iscyclicutil(3, visited, cyclevertices, -1) ? cout << "graph contains cycle\n" : cout << "graph doesn't contain cycle\n"; int x = 0; while (cyclevertices[x] != -1) cout << cyclevertices[x++] << " "; return 0; }
Comments
Post a Comment