Advertisement
vaibhav1906

Graph construction

Jul 26th, 2021
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.57 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int main() {
  4. int v;
  5. int e;
  6. cout<<"Enter vertex and edges \n";
  7. cin>>v;
  8. cin>>e;
  9. vector<vector<int> >graph(v);
  10.  
  11. for(int i=0; i<e; i++){
  12. int a,b;
  13. cout<<"Enter Two nodes which are connected \n";
  14. cin>>a;
  15. cin>>b;
  16. graph[a].push_back(b);
  17. graph[b].push_back(a);//Remove this if you want directed
  18. }
  19.  
  20. for(int i = 0; i<v; i++){
  21. for(int j = 0; j<graph[i].size(); j++){
  22. cout<<graph[i][j]<<" ";
  23. }
  24. cout<<endl;
  25. }
  26.  
  27. }
  28.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement