hadiuzzaman65

finding indegree

Apr 12th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <list>
  3. #include <iterator>
  4. #include <vector>
  5. using namespace std;
  6. class graph
  7. {
  8.     int v;
  9.     list <int> *adj;
  10. public:
  11.  
  12.     graph(int v)
  13.     {
  14.         this->v=v;
  15.         adj=new list<int>[v];
  16.     }
  17.     void add_adj(int v,int w)
  18.     {
  19.         adj[v].push_back(w);
  20.     }
  21.     void find_ind()
  22.     {
  23.         vector <int>indegree(v,0);
  24.         vector<int>::iterator it2;
  25.         list<int>::iterator it;
  26.         for(int i=0; i<v; i++)
  27.         {
  28.             int count=0;
  29.             for(int j=0; j<v; j++)
  30.             {
  31.  
  32.                 for(it=adj[j].begin(); it!=adj[j].end(); it++)
  33.                 {
  34.                     if( *it==i)
  35.                         count++;
  36.                 }
  37.             }
  38.             cout<<"indegree "<<i<< ":   "<< count<<endl;
  39.         }
  40.     }
  41. };
  42. int main()
  43. {
  44.     graph g(7);
  45.     g.add_adj(0, 2);
  46.     g.add_adj(3, 2);
  47.     g.add_adj(1, 3);
  48.     g.add_adj(4, 2);
  49.     g.add_adj(1, 5);
  50.     g.add_adj(6, 5);
  51.     g.add_adj(6,0);
  52.     g.find_ind();
  53.  
  54.     return 0;
  55. }
Add Comment
Please, Sign In to add comment