Advertisement
Guest User

Untitled

a guest
Mar 11th, 2020
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <cassert>
  4. #include <vector>
  5. #include <algorithm>using namespace std;
  6. typedef vector<bool> matrix_row;
  7. typedef vector<matrix_row> matrix;matrix adjacency_to_incidence(const matrix &);
  8. void print_matrix(const matrix &);
  9. int main(){
  10. setlocale(0, "");
  11. const int n=6;
  12. bool koef[15];
  13. for(int i = 0;i<15;i++){
  14. cout << "Введiть " << i + 1 << " значення: ";
  15. cin >> koef[i];
  16. }
  17. int adjacencyArr[n][n];
  18. int k = 0;// ADJ = 0
  19. for (int i = 0; i < n; i++)
  20. for (int j = 0; j < n; j++)
  21. adjacencyArr[i][j] = 0;
  22. for (int i = 0; i < n - 1; i++)
  23. {
  24. for (int j = i + 1; j < n; j++)
  25. if (i != j){adjacencyArr[i][j] = koef[k++];
  26. adjacencyArr[j][i] = adjacencyArr[i][j];
  27. }
  28. }
  29. matrix adj;
  30. for (int i = 0; i < n; i++)
  31. {matrix_row tmp;for (int j = 0; j < n; j++)tmp.push_back(adjacencyArr[i][j]);adj.push_back(tmp);
  32. }
  33. cout << "Матриця сумiжностi" << endl;print_matrix(adj);
  34. matrix inc = adjacency_to_incidence(adj);
  35. cout << "Матриця Iнцидентностi" << endl;
  36. print_matrix(inc);vector<int> adjHrom(n);
  37. vector<int> incHrom(n);for (int i = 0; i < n; i++)
  38. {
  39. adjHrom[i] = 0;incHrom[i] = 0;
  40. }
  41. for (int i = 0; i < n; i++)
  42. {
  43. for (int j = 0; j < n; j++)
  44. {
  45. if (adj.at(i).at(j) == 1)adjHrom[i]++;
  46. }
  47. }
  48. for (int i = 0; i < n; i++)
  49. {
  50. for (int j = 0; j < n; j++)
  51. {
  52. if (inc.at(i).at(j) == 1)incHrom[i]++;
  53. }
  54. }
  55. int maxAdjHrom = *max_element(adjHrom.begin(), adjHrom.end()) + 1, maxIncHrom =
  56. *max_element(incHrom.begin(),
  57. incHrom.end()) * 2 + 1;
  58. cout << "Хроматичне число " << endl;
  59. cout << maxAdjHrom << "\t" << maxIncHrom;_getch();
  60. return 0;
  61. matrix adjacency_to_incidence(const matrix &adj)
  62. {
  63. int cols = adj.size();
  64. assert(cols > 0);
  65. int rows = adj[0].size();
  66. assert(rows > 0);
  67. assert(rows == cols);
  68. int edge = 0;
  69. matrix incidence;
  70. for (int col = 0; col < cols; ++col)
  71. {
  72. for (int row = 0; row <= col; ++row) {
  73. if (adj[col][row])
  74. {
  75. incidence.push_back(matrix_row(cols, 0));
  76. incidence[edge][row] = incidence[edge][col] = 1;++edge;}
  77. }
  78. }
  79. return incidence;
  80. }
  81. void print_matrix(const matrix &m)
  82. {
  83. int cols = m.size();
  84. if (cols == 0) return;
  85. int rows = m[0].size();
  86. if (rows == 0) return;
  87. for (int c = 0; c < cols; ++c) {for (int r = 0; r < rows; ++r)
  88. {cout << m[c][r] << " ";
  89. }
  90. cout << std::endl;}cout << std::endl;
  91. }
  92.  
  93. cout << std::endl;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement