fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. // initialize a 51x51 vector with cell value of 0 for each cell
  5. vector<vector<int>> adjacencyMatrix(51, vector<int>(51, 0));
  6.  
  7. // initialize a vector with 51 empty rows
  8. vector<vector<int>> adjacencyList(51);
  9.  
  10. // Declare file stream so all functions can access it
  11. ifstream file("input.txt");
  12. int u, v;
  13.  
  14. void inputMatrix(int &nodes, int &edges);
  15. void inputList(int &nodes, int &edges);
  16. void printMatrix(int nodes);
  17. void printList(int nodes);
  18.  
  19. int main(){
  20. int input, nodes, edges;
  21.  
  22. cout << "Enter 1 or 2 based on your preference." << endl;
  23. cout << "Option 1: If you want to store the graph to adjacency matrix." << endl;
  24. cout << "Option 2: If you want to store the graph to adjacency list." << endl;
  25. cout << "Enter your option (1/2): ";
  26.  
  27. while (cin >> input){
  28. if (input != 1 && input != 2){
  29. cout << "Invalid Input!!! try again." << endl;
  30. cout << "Enter your option (1/2): ";
  31. continue;
  32. }
  33. break;
  34. }
  35. // Check if file can be opened
  36. if (!file){
  37. cout << "Error opening file!" << endl;
  38. return 0;
  39. }
  40. // Read total number of nodes and edges from file
  41. file >> nodes >> edges;
  42.  
  43. if (input == 1){
  44. inputMatrix(nodes, edges);
  45. printMatrix(nodes);
  46. }
  47. else{
  48. inputList(nodes, edges);
  49. printList(nodes);
  50. }
  51. file.close();
  52. return 0;
  53. }
  54.  
  55. void inputMatrix(int &nodes, int &edges){
  56. int count = 0;
  57. // Read each edge (u, v) and update adjacency matrix
  58. while (count < edges && file >> u >> v)
  59. {
  60. adjacencyMatrix[u][v] = 1;// edge from u to v
  61. adjacencyMatrix[v][u] = 1;// edge from v to u
  62. count++;
  63. }
  64. }
  65.  
  66. void inputList(int &nodes, int &edges){
  67. int count = 0;
  68. // Read each edge (u, v) and store in adjacency list
  69. while (count < edges && file >> u >> v){
  70. adjacencyList[u].push_back(v);
  71. adjacencyList[v].push_back(u);
  72. count++;
  73. }
  74. }
  75. // Displays the adjacency matrix
  76. void printMatrix(int nodes){
  77. cout << "\nAdjacency Matrix :\n";
  78. for(auto rowIt= adjacencyMatrix.begin()+1;rowIt!= adjacencyMatrix.begin()+1+nodes;++rowIt){
  79. for(auto colIt= rowIt->begin()+1;colIt!= rowIt->begin()+1+nodes;++colIt){
  80. cout << *colIt << " ";
  81. }
  82. cout << endl;
  83. }
  84. }
  85. // Displays the adjacency list
  86. void printList(int nodes){
  87. cout << "\nAdjacency List:\n";
  88. // Loop through each node and print its connections
  89. for (int i = 1; i <= nodes; i++){
  90. cout << "Node"<<" " << i<<" " << "connected to : ";
  91. if (adjacencyList[i].empty()){
  92. cout << "No connections";
  93. }
  94. else{
  95. bool a = true; // for proper separator
  96. for (int e : adjacencyList[i]){
  97. if (!a){
  98. cout << " , "; }// separator
  99. cout << e;
  100. a = false;
  101. }
  102. }
  103. cout << endl;
  104. }
  105. }
  106.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Enter 1 or 2 based on your preference.
Option 1: If you want to store the graph to adjacency matrix.
Option 2: If you want to store the graph to adjacency list.
Enter your option (1/2): Error opening file!