c++ - How I can find max connected component of graph? -
not found answers.... mea culpea.
this program find connected components of undirected graph. can done using depth first search use benchmark develop , complete yours.
// implementation of kosaraju's algorithm print sccs #include <iostream> #include <list> #include <stack> using namespace std; class graph{ int v; // no. of vertices list<int> *adj; // array of adjacency lists // fills stack vertices (in increasing order of finishing times) // top element of stack has maximum finishing time void fillorder(int v, bool visited[], stack<int> &stack); // recursive function print dfs starting v void dfsutil(int v, bool visited[]); public: graph(int v); void addedge(int v, int w); // main function finds , prints connected components int printsccs(); // function returns reverse (or transpose) of graph graph gettranspose(); }; graph::graph(int v){ this->v = v; adj = new list<int> [v]; } // recursive function print dfs starting v void graph::dfsutil(int v, bool visited[]){ // mark current node visited , print visited[v] = true; cout << v << " "; // recur vertices adjacent vertex list<int>::iterator i; (i = adj[v].begin(); != adj[v].end(); ++i) if (!visited[*i]) dfsutil(*i, visited); } graph graph::gettranspose(){ graph g(v); (int v = 0; v < v; v++){ // recur vertices adjacent vertex list<int>::iterator i; (i = adj[v].begin(); != adj[v].end(); ++i){ g.adj[*i].push_back(v); } } return g; } void graph::addedge(int v, int w){ adj[v].push_back(w); // add w v’s list. } void graph::fillorder(int v, bool visited[], stack<int> &stack){ // mark current node visited , print visited[v] = true; // recur vertices adjacent vertex list<int>::iterator i; (i = adj[v].begin(); != adj[v].end(); ++i) if (!visited[*i]) fillorder(*i, visited, stack); // vertices reachable v processed now, push v stack stack.push(v); } // main function finds , prints connected components int graph::printsccs(){ stack<int> stack; // mark vertices not visited (for first dfs) bool *visited = new bool[v]; (int = 0; < v; i++) visited[i] = false; // fill vertices in stack according finishing times (int = 0; < v; i++) if (visited[i] == false) fillorder(i, visited, stack); // create reversed graph graph gr = gettranspose(); // mark vertices not visited (for second dfs) (int = 0; < v; i++) visited[i] = false; int count = 0; // process vertices in order defined stack while (stack.empty() == false){ // pop vertex stack int v = stack.top(); stack.pop(); // print connected component of popped vertex if (visited[v] == false){ gr.dfsutil(v, visited); cout << endl; } count++; } return count; } //----------------------------------------------------------------------------------------------- // test above functions int main(){ // create graph given in above diagram graph g(5); g.addedge(1, 0); g.addedge(0, 2); g.addedge(2, 1); g.addedge(0, 3); g.addedge(3, 4); cout << "following connected components in given graph \n"; if (g.printsccs() > 1){ cout << "graph weakly connected."; }else{ cout << "graph connected."; } return 0; }
Comments
Post a Comment