Graph Adjacent List





#include <bits/stdc++.h>
using namespace std;
vector<int>edges[1000];
vector<int>cost[1000];
vector<int>outdegree;
vector<int>indegree[100];
vector<int>indeg;
int main()
{
    int i,j,n,e,u,v,k,c,l,m;
    cin>>n>>e;
    for(i=1;i<=e;i++)
    {
        cin>>u>>v;
        edges[u].push_back(v);
        indegree[v].push_back(u);    //1st u index then vector index ex: edges[1][0]=2, edges[1][1]=4
        cost[u].push_back(1);

    }
    cout<<endl;
    cout<<"Outdegree"<<endl;
    cout<<"......................................."<<endl;
    for(i=1;i<=n;i++)
    {
        cout<<i<<"==>>";
         for(j=0;j<edges[i].size();j++)
         {
             cout<<edges[i][j]<<" ";
         }
         cout<<endl;
         outdegree.push_back(edges[i].size());

    }
    cout<<endl;
    cout<<"Indegree"<<endl;
    cout<<"......................................."<<endl;
    for(i=1;i<=n;i++)
    {
        cout<<i<<"==>>";
         for(j=0;j<indegree[i].size();j++)
         {
             cout<<indegree[i][j]<<" ";
         }
         cout<<endl;
         indeg.push_back(indegree[i].size());

    }
    cout<<endl;
    cout<<"Count Of Outdegree Or Total Adjacent Nodes"<<endl;
    cout<<"......................................."<<endl;
    for(k=0;k<outdegree.size();k++)
    {
        cout<<"Outdegree Of "<<k+1<<"="<<outdegree[k]<<endl;
    }
    cout<<endl;
    cout<<"Count Of Indegree"<<endl;
    cout<<"......................................."<<endl;
    for(k=0;k<indeg.size();k++)
    {
        cout<<"indegree Of "<<k+1<<"="<<indeg[k]<<endl;
    }

    return 0;
}

Download Coding Interview Book and Get More Tutorials for Coding and Interview Solution: Click Here

Download System Design Interview Book and Get More Tutorials and Interview Solution: Click Here

Do you need more Guidance or Help? Then Book 1:1 Quick Call with Me: Click Here

Share on Google Plus

About Ashadullah Shawon

I am Ashadullah Shawon. I am a Software Engineer. I studied Computer Science and Engineering (CSE) at RUET. I Like To Share Knowledge. Learn More: Click Here
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment