Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Realization #10

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions app/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,31 @@
#include "add.h"
#include "building.h"
#include <iostream>

int main() {
std::cout << "2 + 2 = " << add(2, 2) << std::endl;
std::vector < std::pair < int, int > > enemies;

std::cout << "Task: built minimal number of groups for {1, 2 .. 5}." << std::endl;

std::cout << std::endl << "Enemies:" << std::endl;
enemies.push_back(std::make_pair(0, 1));
enemies.push_back(std::make_pair(1, 2));
enemies.push_back(std::make_pair(0, 2));
enemies.push_back(std::make_pair(3, 4));
enemies.push_back(std::make_pair(4, 2));
int size = enemies.size();
for (int i = 0; i < size; i++) {
std::cout << enemies[i].first + 1 << " and " << enemies[i].second + 1;
std::cout << ";" << std::endl;
}

std::cout << std::endl << "Result: " << "(";
std::vector < int > result = built_groups(enemies, 5);
int i;
int res_size = result.size();
for (i = 0; i < res_size - 1; i++) {
std::cout << result[i] + 1 << ", ";
}
std::cout << result[i] + 1 << ")" << std::endl;

return 0;
}
6 changes: 0 additions & 6 deletions include/add.h

This file was deleted.

21 changes: 21 additions & 0 deletions include/building.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef INCLUDE_BUILDING_H_
#define INCLUDE_BUILDING_H_
#include <vector>
#include <stdexcept>

class Disjoint_Sets {
public:
int size;
int *parent;
int *rank;

explicit Disjoint_Sets(int _size);
~Disjoint_Sets();
void make_set(int x);
void union_sets(int x, int y);
int find_set(int x);
};

std::vector < int > built_groups(std::vector <std::pair <int, int> > enemies, int n);

#endif // INCLUDE_BUILDING_H_
3 changes: 0 additions & 3 deletions src/add.cpp

This file was deleted.

92 changes: 92 additions & 0 deletions src/building.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#include "building.h"

/* ����������� */
Disjoint_Sets::Disjoint_Sets(int _size) {
if (_size < 0)
throw std::logic_error("Input Error: wrong size. Size must be > 0.\n");
size = _size;
parent = new int[size];
rank = new int[size];
for (int i = 0; i < size; i++) {
parent[i] = i;
rank[i] = 1;
}
}

/* ���������� */
Disjoint_Sets::~Disjoint_Sets() {
delete[]parent;
delete[]rank;
}

/* �������� ��� ���� ��������� ������� 1 �� ������ ���� */
void Disjoint_Sets::make_set(int x) {
if (x < 0 || (x-1) > size)
throw std::logic_error("Input Error: wrong parameter.\n");
if (parent[x] != -1)
throw std::logic_error("Input Error: wrong parameter.\n");
parent[x] = x;
rank[x] = 0;
}

/* ����� ���������, �������� ����������� ������� */
int Disjoint_Sets::find_set(int x) {
if (x < 0 || (x - 1) > size) {
throw std::logic_error("Input Error: wrong parameter.\n");
} else {
x = parent[x];
while (parent[x] != x) {
x = parent[x];
}
return x;
}
}

/* ����������� �������� */
void Disjoint_Sets::union_sets(int x, int y) {
if (x < 0 || (x - 1) > size || y < 0 || (y - 1) > size) {
throw std::logic_error("Input Error: wrong parameter.\n");
} else {
int _x, _y;
_x = find_set(x);
_y = find_set(y);
if (rank[_x] > rank[_y]) {
++rank[_x];
parent[_y] = parent[_x];
} else {
++rank[_y];
parent[_x] = parent[_y];
}
}
}

/* ���������� ����� � ������ ������ */
std::vector < int > built_groups(std::vector <std::pair <int, int> > enemies, int n) {
int size = enemies.size();
Disjoint_Sets groups(n);
int x, y;
bool they_are_friends;

for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
x = groups.find_set(i);
y = groups.find_set(j);
they_are_friends = true;
for (int k = 0; k < size; k++) {
if (((groups.find_set(enemies[k].first) == x) &&
(groups.find_set(enemies[k].second) == y))
|| ((groups.find_set(enemies[k].first) == y) &&
(groups.find_set(enemies[k].second) == x))) {
they_are_friends = false;
break;
}
}
if (they_are_friends)
groups.union_sets(x, y);
}
}
std::vector < int > result;
for (int i = 0; i < n; i++)
result.push_back(groups.find_set(i));
return result;
}
80 changes: 80 additions & 0 deletions test/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include <gtest/gtest.h>
#include <vector>
#include "building.h"

TEST(built_groups, example_1) {
std::vector < std::pair < int, int > > enemies;
enemies.push_back(std::make_pair(0, 1));
enemies.push_back(std::make_pair(1, 2));
enemies.push_back(std::make_pair(0, 2));
std::vector < int > result = built_groups(enemies, 3);

std::vector < int > expect;
expect.push_back(0);
expect.push_back(1);
expect.push_back(2);

EXPECT_EQ(result, expect);
}

TEST(built_groups, example_2) {
std::vector < std::pair < int, int > > enemies;
enemies.push_back(std::make_pair(0, 1));
enemies.push_back(std::make_pair(1, 2));
enemies.push_back(std::make_pair(0, 2));
enemies.push_back(std::make_pair(3, 4));
enemies.push_back(std::make_pair(4, 2));
std::vector < int > result = built_groups(enemies, 5);

std::vector < int > expect;
expect.push_back(0);
expect.push_back(1);
expect.push_back(2);
expect.push_back(0);
expect.push_back(1);

EXPECT_EQ(result, expect);
}

TEST(built_groups, example_3) {
std::vector < std::pair < int, int > > enemies;
enemies.push_back(std::make_pair(0, 1));
enemies.push_back(std::make_pair(1, 2));
enemies.push_back(std::make_pair(2, 3));
enemies.push_back(std::make_pair(3, 4));
enemies.push_back(std::make_pair(4, 5));
std::vector < int > result = built_groups(enemies, 6);

std::vector < int > expect;
expect.push_back(0);
expect.push_back(1);
expect.push_back(0);
expect.push_back(1);
expect.push_back(0);
expect.push_back(1);

EXPECT_EQ(result, expect);
}

TEST(built_groups, example_4) {
std::vector < std::pair < int, int > > enemies;
std::vector < int > result = built_groups(enemies, 6);

std::vector < int > expect;
expect.push_back(0);
expect.push_back(0);
expect.push_back(0);
expect.push_back(0);
expect.push_back(0);
expect.push_back(0);

EXPECT_EQ(result, expect);
}

TEST(built_groups, example_5) {
std::vector < std::pair < int, int > > enemies;
std::vector < int > result = built_groups(enemies, 0);
std::vector < int > expect;

EXPECT_EQ(result, expect);
}
7 changes: 0 additions & 7 deletions test/test_add.cpp

This file was deleted.