Quick actions

cmd+k|ctrl+k

Navigation

Languages

Graph Implementation

Snippet info

Language

Cpp

Visibility

public

Author

amangoyal1727

Created

2021-06-15T01:24:59.335386Z

Updated

2021-06-15T01:24:59.335386Z

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

class Edge {
  public:
  int y;
  int weight;
  
  Edge(int y, int weight){
      this->y = y;
      this->weight = weight;
  }
    
};

class Graph{
  public: 
  int V;
  list<Edge> *l;
  
  Graph(int V){
      this->V = V;
      l = new list<Edge>[V];
      
  }
  
  void addEdge(int x ,int y, int weight){
      Edge v(y, weight);
      l[x].push_back(v);
      Edge v2(x,weight);
      l[y].push_back(v2);
  }
  
  void print() {
      for(int i=0; i<V; i++){
          for(auto x: l[i]){
              cout<<i<<"-->"<<x.y<<" Weight: "<<x.weight<<endl;
          }
      }
  }   
    
    
};

int main() {
    
    
    Graph g(3);
    g.addEdge(0,1,10);
    g.addEdge(0,2,20);
    g.addEdge(1,2,21);
    g.print();
    
    
}
INFO