Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ParallelTask1.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
- //например, в графе из 10 вершин удаляются 10 ребер
- #include <iostream>
- #include <thread>
- #include <ctime>
- #include <chrono>
- #include <vector>
- using namespace std;
- using namespace std::chrono;
- const int N = 10;
- using Matrix = int[N][N];
- void matrixCreate(Matrix& matrix) { //генерация полного графа
- for (int i = 0; i < N; i++)
- for (int j = 0; j < N; j++) {
- if (i != j)
- matrix[i][j] = 1;
- if (i==j)
- matrix[i][j] = 0;
- }
- }
- void matrixCreateZero(Matrix& trashMatrix) { //генерация полного графа
- for (int i = 0; i < N; i++)
- for (int j = 0; j < N; j++) {
- trashMatrix[i][j] = 0;
- }
- }
- void deleteEdge(Matrix& matrix) {
- int counter = 0; int e1, e2;
- while (counter != N) {
- e1 = rand() % N;
- e2 = rand() % N;
- if (matrix[e1][e2] == 1 && matrix[e2][e1] == 1) {
- matrix[e1][e2] = 0;
- matrix[e2][e1] = 0;
- counter++;
- }
- }
- }
- void createTrashMatrix(Matrix& trashMatrix, Matrix matrix) {
- for (int i=0; i<N; i++)
- for (int j=0; j<N; j++)
- if (i != j) {
- if (matrix[i][j] == 0 && matrix[j][i] == 0) {
- trashMatrix[i][j] = 1;
- trashMatrix[j][i] = 1;
- }
- }
- }
- int checkTriangles(Matrix& matrix) {
- int check = 0;
- for (int i = 0; i < N; i++)
- for (int j = i + 1; j < N; j++)
- for (int k = j + 1; k < N; k++)
- if (matrix[i][j] == matrix[j][k] && matrix[j][k] == matrix[k][i] && matrix[k][i] == 1) check++;
- return check;
- }
- int main()
- {
- Matrix matrix; Matrix trashMatrix;
- srand(time(0));
- matrixCreateZero(trashMatrix);
- matrixCreate(matrix);
- deleteEdge(matrix);
- createTrashMatrix(trashMatrix, matrix);
- int check = checkTriangles(trashMatrix);
- int result = (N*(N - 1)*(N - 2) / 6) - check;
- cout << endl;
- checkTriangles(matrix); //проверка за кубическое время
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement