fork download
  1. //
  2. // main.cpp
  3. // Adjacency List
  4. //
  5. // Created by Himanshu on 26/11/22.
  6. //
  7.  
  8. #include <iostream>
  9. #include <vector>
  10. using namespace std;
  11. #define N 5
  12.  
  13. //N = number of nodes in graph
  14. void printAdjacencyList (vector<int> graph[N+1]) {
  15.  
  16. for (int i=1; i<=N; i++) {
  17. cout<<i<<": ";
  18. for (int j=0; j<graph[i].size(); j++) {
  19. cout<<graph[i][j]<<" ";
  20. }
  21. cout<<endl;
  22. }
  23. }
  24.  
  25. int main() {
  26. vector<int> graph[N+1];
  27.  
  28. graph[1].push_back(2);
  29. graph[2].push_back(1);
  30. graph[2].push_back(4);
  31. graph[3].push_back(4);
  32. graph[3].push_back(5);
  33. graph[4].push_back(2);
  34. graph[4].push_back(3);
  35. graph[4].push_back(5);
  36. graph[5].push_back(3);
  37. graph[5].push_back(4);
  38.  
  39. cout<<"Graph G (Adjacency List):"<<endl;
  40. printAdjacencyList(graph);
  41.  
  42. return 0;
  43. }
Success #stdin #stdout 0.01s 5516KB
stdin
Standard input is empty
stdout
Graph G (Adjacency List):
1: 2 
2: 1 4 
3: 4 5 
4: 2 3 5 
5: 3 4