Advertisement
vaibhav1906

Adj List

Dec 30th, 2021
960
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.66 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. void addEdge(vector<int> adj[], int u, int v)
  5. {
  6.     adj[u].push_back(v);
  7.     adj[v].push_back(u);
  8. }
  9. void printGraph(vector<int> adj[], int V)
  10. {
  11.     for (int v = 0; v < V; ++v)
  12.     {
  13.         cout << "Adjacency list of vertex "
  14.              << v << "\n";
  15.         for (auto x : adj[v])
  16.            cout << x <<" ";
  17.         cout<<"\n";
  18.     }
  19. }
  20. int main()
  21. {
  22.     int V = 5;
  23.     vector<int> adj[V];
  24.     addEdge(adj, 0, 1);
  25.     addEdge(adj, 0, 4);
  26.     addEdge(adj, 1, 2);
  27.     addEdge(adj, 1, 3);
  28.     addEdge(adj, 1, 4);
  29.     addEdge(adj, 2, 3);
  30.     addEdge(adj, 3, 4);
  31.     printGraph(adj, V);
  32.     return 0;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement