fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. class Graph{
  5. public:
  6. int n; int m;
  7. int maxEdges;
  8. bool isDirected;
  9.  
  10. vector<vector<int>> adj;
  11. // int **adj;
  12.  
  13. Graph(int nodes, bool isDiGraph) : adj(nodes, vector<int>(nodes)){
  14. n = nodes;
  15. m = 0;
  16. isDirected = isDiGraph;
  17. maxEdges = isDirected ? n*(n-1) : n*(n-1)/2;
  18.  
  19. // adj = new int*[n];
  20.  
  21. // for(int i = 0; i < n; ++i)
  22. // adj[i] = new int[n];
  23. }
  24.  
  25. void insertEdge(int u, int v){
  26.  
  27. if(u >= n || v >=n || u < 0 || v < 0){
  28. cout<<"Either of the vertices doesn't exists in the graph.\n";
  29. return;
  30. }
  31.  
  32. if(m == maxEdges){
  33. cout<<"Maximum allowed edges are already in the graph. Can't insert more.\n";
  34. return;
  35. }
  36.  
  37. adj[u][v] = 1; ++m;
  38.  
  39. if(!isDirected)
  40. adj[v][u] = 1;
  41. }
  42.  
  43. void deleteEdge(int u, int v){
  44.  
  45. if(u >= n || v >=n || u < 0 || v < 0){
  46. cout<<"Either of the vertices doesn't exists in the graph.\n";
  47. return;
  48. }
  49.  
  50. if(!adj[u][v]){
  51. cout<<"The edge doesn't exists in the graph.\n"; return;
  52. }
  53.  
  54. adj[u][v] = 0; --m;
  55.  
  56. if(!isDirected)
  57. adj[v][u] = 0;
  58.  
  59. }
  60.  
  61. void displayGraph(){
  62.  
  63. if(!m){
  64. cout<<"The graph is empty!!"; return;
  65. }
  66.  
  67. cout<<"Total # edges in the graph: "<<m<<"\n";
  68.  
  69. for(int i = 0; i < n; ++i){
  70. cout<<"The edges from vertex "<<i<<":";
  71.  
  72. for(int j = 0; j < n; ++j)
  73. if(adj[i][j])
  74. cout<<"->"<<j;
  75.  
  76. cout<<"\n";
  77. }
  78. }
  79.  
  80. };
  81.  
  82. int main() {
  83. int opt, isDir, src, dest;
  84.  
  85. cin>>opt;
  86. cout<<"Please enter # vertices in the graph: "<<opt<<"\n";
  87. cin>>isDir;
  88. cout<<"Is the graph directed(1/0)? "<<isDir<<"\n";
  89.  
  90. Graph g(opt, isDir);
  91.  
  92. cout<<"Operations available on graph:\n";
  93. cout<<"1. Insert an edge\n2. Delete an edge\n3. Print graph\n4. Exit\n";
  94. cout<<"****************************************************\n";
  95. while(true){
  96. cin>>opt;
  97. switch(opt){
  98. case 1:
  99. cin>>src>>dest;
  100. cout<<"Enter the source and destination to insert edge,\n"
  101. "Source: " <<src<<" Destination: "<<dest<<"\n"; g.insertEdge(src, dest);
  102. break;
  103. case 2:
  104. cin>>src>>dest;
  105. cout<<"Enter the source and destination to delete edge,\n"
  106. "Source: " <<src<<" Destination: "<<dest<<"\n"; g.deleteEdge(src, dest);
  107. break;
  108. case 3:
  109. g.displayGraph();
  110. break;
  111. case 4:
  112. exit(0);
  113. }
  114. cout<<"****************************************************\n";
  115. }
  116.  
  117. return 0;
  118. }
Success #stdin #stdout 0s 4276KB
stdin
4 1
1 0 1
1 0 2
1 0 3
1 1 3
1 1 0
1 1 2
1 2 0
1 2 3
1 2 1
1 3 0
1 3 2
1 3 1
1 3 1
1 0 2
3
1 4 1
2 0 2
2 3 1
2 1 2
3
2 3 1
3 4
stdout
Please enter # vertices in the graph: 4
Is the graph directed(1/0)? 1
Operations available on graph:
1. Insert an edge
2. Delete an edge
3. Print graph
4. Exit
****************************************************
Enter the source and destination to insert edge,
Source: 0 Destination: 1
****************************************************
Enter the source and destination to insert edge,
Source: 0 Destination: 2
****************************************************
Enter the source and destination to insert edge,
Source: 0 Destination: 3
****************************************************
Enter the source and destination to insert edge,
Source: 1 Destination: 3
****************************************************
Enter the source and destination to insert edge,
Source: 1 Destination: 0
****************************************************
Enter the source and destination to insert edge,
Source: 1 Destination: 2
****************************************************
Enter the source and destination to insert edge,
Source: 2 Destination: 0
****************************************************
Enter the source and destination to insert edge,
Source: 2 Destination: 3
****************************************************
Enter the source and destination to insert edge,
Source: 2 Destination: 1
****************************************************
Enter the source and destination to insert edge,
Source: 3 Destination: 0
****************************************************
Enter the source and destination to insert edge,
Source: 3 Destination: 2
****************************************************
Enter the source and destination to insert edge,
Source: 3 Destination: 1
****************************************************
Enter the source and destination to insert edge,
Source: 3 Destination: 1
Maximum allowed edges are already in the graph. Can't insert more.
****************************************************
Enter the source and destination to insert edge,
Source: 0 Destination: 2
Maximum allowed edges are already in the graph. Can't insert more.
****************************************************
Total # edges in the graph: 12
The edges from vertex 0:->1->2->3
The edges from vertex 1:->0->2->3
The edges from vertex 2:->0->1->3
The edges from vertex 3:->0->1->2
****************************************************
Enter the source and destination to insert edge,
Source: 4 Destination: 1
Either of the vertices doesn't exists in the graph.
****************************************************
Enter the source and destination to delete edge,
Source: 0 Destination: 2
****************************************************
Enter the source and destination to delete edge,
Source: 3 Destination: 1
****************************************************
Enter the source and destination to delete edge,
Source: 1 Destination: 2
****************************************************
Total # edges in the graph: 9
The edges from vertex 0:->1->3
The edges from vertex 1:->0->3
The edges from vertex 2:->0->1->3
The edges from vertex 3:->0->2
****************************************************
Enter the source and destination to delete edge,
Source: 3 Destination: 1
The edge doesn't exists in the graph.
****************************************************
Total # edges in the graph: 9
The edges from vertex 0:->1->3
The edges from vertex 1:->0->3
The edges from vertex 2:->0->1->3
The edges from vertex 3:->0->2
****************************************************