akashtadwai

Untitled

Dec 5th, 2019
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.58 KB | None | 0 0
  1. // C++ program to print connected components in
  2. // an undirected graph
  3. #include<bits/stdc++.h>
  4. #include <list>
  5. using namespace std;
  6. #define ver 100005
  7. // Graph class represents a undirected graph
  8. // using adjacency list representation
  9. typedef pair<int, int> iPair;
  10. class Graph
  11. {
  12.     int V; // No. of vertices
  13.  
  14.     // Pointer to an array containing adjacency lists
  15.     list<iPair>*adj;
  16.  
  17.     // A function used by DFS  
  18. public:
  19.     Graph(int V); // Constructor
  20.   void BFS(int v, bool visited[]);
  21.    void addEdge(list <iPair> adj[], int u,
  22.                                      int v, int wt);
  23.     void connectedComponents();
  24. };
  25.  
  26. // Method to print connected components in an
  27. // undirected graph
  28. void Graph::connectedComponents()
  29. {
  30.     // Mark all the vertices as not visited
  31.     bool *visited = new bool[V];
  32.     int cnt=0;
  33.     for(int v = 0; v < V; v++)
  34.         visited[v] = false;
  35.  
  36.     for (int v=0; v<V; v++)
  37.     {
  38.         if (visited[v] == false and adj[v].size()!=0)
  39.         {
  40.             // print all reachable vertices
  41.             // from v
  42.             BFS(v, visited);
  43.             cnt++;
  44.             cout << "\n";
  45.         }
  46.     }
  47.   cout<<cnt<<endl;
  48. }
  49. void Graph::BFS(int v, bool visited[])
  50. {
  51.   list<int> queue;
  52.   visited[v] = true;
  53.   queue.push_back(v);
  54.   //list<int>::iterator i;
  55.   while (!queue.empty()) {
  56.     v = queue.front();
  57.     cout<<v<<" ";
  58.     queue.pop_front();
  59.     for (auto i = adj[v].begin(); i != adj[v].end(); ++i) {
  60.       if (!visited[i->first]) {
  61.         visited[i->first] = true;
  62.         queue.push_back(i->first);
  63.       }
  64.     }
  65.   }
  66. }
  67.  
  68.  
  69. Graph::Graph(int V)
  70. {
  71.     this->V = V;
  72.     adj= new list<iPair>[V];
  73. }
  74.  
  75. // method to add an undirected edge
  76. void Graph:: addEdge(list <iPair> *adj, int u,
  77.                                      int v, int wt)
  78. {
  79.     adj[u].push_back(make_pair(v, wt));
  80.     adj[v].push_back(make_pair(u, wt));
  81. }
  82.  
  83. // Drive program to test above
  84. int main()
  85. {
  86.     // Create a graph given in the above diagram
  87.    int V = 9;
  88.   Graph g(V);
  89.     // making above shown graph
  90.     g.addEdge(adj, 0, 1, 4);
  91.     g.addEdge(adj, 0, 7, 8);
  92.     g.addEdge(adj, 1, 2, 8);
  93.     g.addEdge(adj, 1, 7, 11);
  94.     g.addEdge(adj, 2, 3, 7);
  95.     g.addEdge(adj, 2, 8, 2);
  96.     g.addEdge(adj, 2, 5, 4);
  97.     g.addEdge(adj, 3, 4, 9);
  98.     g.addEdge(adj, 3, 5, 14);
  99.     g.addEdge(adj, 4, 5, 10);
  100.     g.addEdge(adj, 5, 6, 2);
  101.     g.addEdge(adj, 6, 7, 1);
  102.     g.addEdge(adj, 6, 8, 6);
  103.     g.addEdge(adj, 7, 8, 7);
  104.     cout << "Following are connected components \n";
  105.     g.connectedComponents();
  106.     return 0;
  107. }
Add Comment
Please, Sign In to add comment