Quick actions

cmd+k|ctrl+k

Navigation

Languages

Implementation of Graph in C++ using list 

Snippet info

Language

Cpp

Visibility

public

Author

amangoyal1727

Created

2021-06-13T10:09:38.641976Z

Updated

2021-06-13T10:09:38.641976Z

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

class Graph {
    
    int V;
    list<pair<int,int>> *l;
    int weight;
    
    
    public:    
        Graph(int V){
            this->V = V;
            l = new list<pair<int,int>>[V];
        }
        
        void addEdge(int x , int y , int weight){
            l[x].push_back(make_pair(y , weight));
            l[y].push_back(make_pair(x , weight));
        }
        
        void print(){
            
            for(int i=0 ; i<V ; i++){
                
                for(auto it = l[i].begin() ; it!= l[i].end(); it++){
                    cout<<i<<"->"<<it->first<<"("<<"Weight: "<<it->second<<")"<<endl;
                }
            }
        }
};

int main() {

    Graph g(4);
    g.addEdge(0,1 , 10);
    g.addEdge(0,2, 20);
    g.addEdge(1,2, 21);
    g.print();
    
    return 0;
    
    
}
INFO