RaresDumitrica

Graph print

Jan 17th, 2019
527
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.85 KB | None | 0 0
  1. using namespace std;
  2.  
  3. // A utility function to add an edge in an
  4. // undirected graph.
  5. void addEdge(vector<int> adj[], int u, int v)
  6. {
  7.     adj[u].push_back(v);
  8.     adj[v].push_back(u);
  9. }
  10.  
  11. // A utility function to print the adjacency list
  12. // representation of graph
  13. void printGraph(vector<int> adj[], int V)
  14. {
  15.     for (int v = 0; v < V; ++v)
  16.     {
  17.         cout << "\n Adjacency list of vertex "
  18.              << v << "\n head ";
  19.         for (auto x : adj[v])
  20.            cout << "-> " << x;
  21.         printf("\n");
  22.     }
  23. }
  24.  
  25. // Driver code
  26. int main()
  27. {
  28.     int V = 5;
  29.     vector<int> adj[V];
  30.     addEdge(adj, 0, 1);
  31.     addEdge(adj, 0, 4);
  32.     addEdge(adj, 1, 2);
  33.     addEdge(adj, 1, 3);
  34.     addEdge(adj, 1, 4);
  35.     addEdge(adj, 2, 3);
  36.     addEdge(adj, 3, 4);
  37.     printGraph(adj, V);
  38.     return 0;
  39. }
Add Comment
Please, Sign In to add comment