Advertisement
Sunjaree

Find Indegree and Outdegree of a Directed Graph

Oct 22nd, 2020
1,877
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.32 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using  namespace  std;
  3.  
  4. #define endl     "\n"
  5. #define ll       long long
  6. #define PI       acos(-1.0)
  7. #define test     cout<<"\n****\n"
  8. #define LCM(a,b) ((a/__gcd(a,b))*b)
  9. #define precise  fixed(cout);cout<<setprecision(12)
  10. #define fast     ios_base :: sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
  11.  
  12. #define MAX 1000
  13. vector<int> graph[MAX];
  14. int in[MAX],out[MAX];
  15. int main() {
  16.  
  17.     int NumNodes,NumEdges;
  18.     cout<<"Enter the number of nodes: "<<endl;
  19.     cin>>NumNodes;
  20.     cout<<"Enter the number of edges: "<<endl;
  21.     cin>>NumEdges;
  22.  
  23.     cout<<"Now Enter Graph Connections: "<<endl;
  24.  
  25.     for(int i=1;i<=NumEdges;i++){
  26.         int node,edge;
  27.         cin>>node>>edge;
  28.         graph[node].push_back(edge);
  29.     }
  30.  
  31.     cout<<"Nodes are joined among: "<<endl;
  32.     for(int i=1;i<=NumNodes;i++){
  33.         cout<<i<<">>";
  34.         if(graph[i].size()==0){
  35.             cout<<" NULL\n";
  36.         } else {
  37.             for (int j = 0; j < graph[i].size(); j++) {
  38.                 out[i]++;
  39.                 in[graph[i][j]]++;
  40.                 cout << " " << graph[i][j];
  41.             }
  42.             cout << endl;
  43.         }
  44.     }
  45.  
  46.     cout<<"Indegree and Outdegree:"<<endl;
  47.     for(int i=1;i<=NumNodes;i++){
  48.         cout<<i<<"-> In:"<<in[i]<<" Out:"<<out[i]<<endl;
  49.     }
  50.  
  51.     return 0;
  52. }
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement