Advertisement
mamamaria

GraphNotOpti

Apr 10th, 2021
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.36 KB | None | 0 0
  1. // ParallelTask1.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
  2. //например, в графе из 10 вершин удаляются 10 ребер
  3.  
  4. #include <iostream>
  5. #include <thread>
  6. #include <ctime>
  7. #include <chrono>
  8. #include <vector>
  9. using namespace std;
  10. using namespace std::chrono;
  11.  
  12.  
  13.  
  14. const int N = 10;
  15.  
  16. using Matrix = int[N][N];
  17.  
  18.  
  19.  
  20. void matrixCreate(Matrix& matrix) { //генерация полного графа
  21.     for (int i = 0; i < N; i++)
  22.         for (int j = 0; j < N; j++) {
  23.             if (i != j)
  24.                 matrix[i][j] = 1;
  25.             if (i==j)
  26.                 matrix[i][j] = 0;
  27.         }
  28. }
  29.  
  30. void matrixCreateZero(Matrix& trashMatrix) { //генерация полного графа
  31.     for (int i = 0; i < N; i++)
  32.         for (int j = 0; j < N; j++) {
  33.             trashMatrix[i][j] = 0;
  34.         }
  35. }
  36.  
  37.  
  38. void deleteEdge(Matrix& matrix) {
  39.     int counter = 0; int e1, e2;
  40.     while (counter != N) {
  41.         e1 = rand() % N;
  42.         e2 = rand() % N;
  43.         if (matrix[e1][e2] == 1 && matrix[e2][e1] == 1) {
  44.             matrix[e1][e2] = 0;
  45.             matrix[e2][e1] = 0;
  46.             counter++;
  47.         }
  48.        
  49.     }
  50. }
  51.  
  52. void createTrashMatrix(Matrix& trashMatrix, Matrix matrix) {
  53.     for (int i=0; i<N; i++)
  54.         for (int j=0; j<N; j++)
  55.             if (i != j) {
  56.                 if (matrix[i][j] == 0 && matrix[j][i] == 0) {
  57.                     trashMatrix[i][j] = 1;
  58.                     trashMatrix[j][i] = 1;
  59.                 }
  60.                
  61.             }
  62. }
  63.  
  64. int checkTriangles(Matrix& matrix) {
  65.     int check = 0;
  66.     for (int i = 0; i < N; i++)
  67.         for (int j = i + 1; j < N; j++)
  68.             for (int k = j + 1; k < N; k++)
  69.                 if (matrix[i][j] == matrix[j][k] && matrix[j][k] == matrix[k][i] && matrix[k][i] == 1) check++;
  70.     return check;
  71. }
  72.  
  73.  
  74. int main()
  75. {
  76.     Matrix matrix; Matrix trashMatrix;
  77.     srand(time(0));
  78.     matrixCreateZero(trashMatrix);
  79.     matrixCreate(matrix);
  80.     deleteEdge(matrix);
  81.    
  82.     createTrashMatrix(trashMatrix, matrix);
  83.     int check = checkTriangles(trashMatrix);
  84.     int result = (N*(N - 1)*(N - 2) / 6) - check;
  85.     cout << endl;
  86.     checkTriangles(matrix); //проверка за кубическое время
  87.  
  88.  
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement