Quick actions

cmd+k|ctrl+k

Navigation

Languages

Adjacency List Implementation of the Graph

Snippet info

Language

Cpp

Visibility

public

Author

amangoyal1727

Created

2021-02-12T21:03:35Z

Updated

2021-02-12T21:03:35Z

#include <iostream>
using namespace std;
#include <vector>;

int main() {
    
    int nodes;
    int edges;
    cin>>nodes>>edges;
    int x,y;
    vector<int> A[10];
    
    
    //Loop for making the edges 
    for(int i=0; i<edges; ++i){
        cin>>x>>y;
        A[x].push_back(y);
    }
    
    
    //For printing the graph in form of edges and nodes
    for(int i=1; i<=nodes; ++i){
        
        cout<<"Adjacency List Of the graph is: "<<i<<": ";
        
        for(int j=0; j<A[i].size();++j){
            
            if(j==A[i].size()-1){
                cout<<A[i][j]<<endl;
            }
            else
                cout<<A[i][j]<<" --> ";
        }
        
    }
    
}
INFO