fork(1) download
  1. #include<bits/stdc++.h>
  2. #include <stdio.h>
  3. using namespace std;
  4.  
  5. void swap(vector<int> *a, vector<int> *b) {
  6. vector<int> temp;
  7. temp = *a;
  8. *a = *b;
  9. *b = temp;
  10. }
  11.  
  12.  
  13. void sortGraph(vector<int> * adj, size_t count)
  14. {
  15. int size = count;
  16.  
  17. cout<< endl<< "antes" << endl;
  18. for(std::size_t j = 0; j < size-1; j++)
  19. {
  20. cout << "\n \n Index:" << j << "Degree:" << adj[j].size() ;
  21. }
  22.  
  23.  
  24. //sort by degree, number of children
  25. for (int j = 0; j < size - 1; j++) {
  26. int min_idx = j;
  27. for (int i = j + 1; i < size; i++) {
  28. if (adj[i].size() < adj[min_idx].size())
  29. min_idx = i;
  30. }
  31. swap(&adj[min_idx], &adj[j]);
  32. }
  33.  
  34. cout<< endl<< "Depois" << endl;
  35. for(std::size_t j = 0; j < size-1; j++)
  36. {
  37. cout << "\n \n Index:" << j << "Degree:" << adj[j].size() ;
  38. }
  39.  
  40.  
  41. }
  42.  
  43.  
  44. void printGraph(std::vector<int> const* adj, size_t count)
  45. {
  46. std::vector<size_t> indices;
  47. for (size_t i = 0; i != count; ++i)
  48. {
  49. indices.push_back(i);
  50. }
  51.  
  52. for (auto index : indices)
  53. {
  54. std::cout << "Vertex " << index << ", degree " << adj[index].size() << '\n';
  55. }
  56. }
  57.  
  58. void addEdge(vector<int> adj[], int u, int v)
  59. {
  60. adj[u].push_back(v);
  61. adj[v].push_back(u);
  62. }
  63.  
  64.  
  65. int main()
  66. {
  67. int V = 5;
  68. vector<int> adj[V];
  69. addEdge(adj, 0, 2);
  70. addEdge(adj, 0, 1);
  71. addEdge(adj, 0, 3);
  72. addEdge(adj, 0, 4);
  73. addEdge(adj, 2, 1);
  74. addEdge(adj, 4, 1);
  75. //printGraph(adj, V);
  76. sortGraph(adj, V);
  77. return 0;
  78. }
  79.  
Success #stdin #stdout 0s 5488KB
stdin
Standard input is empty
stdout
antes

 
 Index:0Degree:4
 
 Index:1Degree:3
 
 Index:2Degree:2
 
 Index:3Degree:1
Depois

 
 Index:0Degree:1
 
 Index:1Degree:2
 
 Index:2Degree:2
 
 Index:3Degree:3