Skip to content
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
80 changes: 80 additions & 0 deletions deliaioanac/assignment4/Islands.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include <vector>
#include <iostream>

// Function that return the 'head' of the island patch i belongs to
int find(int i, std::vector<int> &parent) {
int head = i, aux;

while (head != parent[head]) {
head = parent[head];
}

// Path compression (make the parent of every node along
// the way to be the head of the whole island)
while (i != parent[i]) {
aux = parent[i];
parent[i] = head;
i = aux;
}

return head;
}

// Unite two islands
void unite(int i, int j, std::vector<int> &parent, std::vector<int> &rank) {
if (rank[i] > rank[j]) {
parent[j] = i;
} else {
parent[i] = j;
}

if (rank[i] == rank[j]){
rank[j] ++;
}
}

int countingIslands(const std::vector<std::vector<bool> > &map) {
// If there is no map, there can be no islands
if (map.empty()) {
std::cout << "The map is empty!" << std::endl;
return 0;
}

// The initial number of distinct possible patches of land
int numberOfIslands = map.size() * map[0].size();
std::vector<int> parent(numberOfIslands), rank(numberOfIslands);

for (int i = 0; i < map.size(); i ++) {
for (int j = 0; j < map[i].size(); j ++) {
if (map[i][j]) {
parent[i * map.size() + j] = i * map.size() + j;
rank[i * map.size() + j] = 0;

// Check if these is a patch of land above this one and,
// if there is, unite its island to this patch of land
if (i > 0 && map[i - 1][j]) {
unite(find(i * map.size() + j, parent),
find((i - 1) * map.size() + j, parent), parent, rank);
numberOfIslands --;
}

// Check if there is a patch of land to the left of this
// one and, if there is and it does not belong to the
// same island as a patch of land above, unite their islands
if (j > 0 && map[i][j - 1] &&
(i == 0 || !map[i - 1][j] ||
find((i - 1) * map.size() + j, parent) !=
find(i * map.size() + (j - 1), parent))) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This if statement is very hard to parse mentally. Prefer to hide 'ugly' code like that behind helper functions:

if (j > 0 && map[i][j - 1] && AnotherCheck(...)) { ..}

Whoever needs to read this in 5 months time will thank you :)

unite(find(i * map.size() + j, parent),
find(i * map.size() + (j - 1), parent), parent, rank);
numberOfIslands --;
}
} else {
// If it is not land, it cannot be an island
numberOfIslands --;
}
}
}

return numberOfIslands;
}
56 changes: 56 additions & 0 deletions deliaioanac/assignment4/IslandsUnitTests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include "Islands.cpp"
#include <gtest/gtest.h>

std::vector<std::vector<bool> > map;

TEST(CountingIslandsTest, EmptyMap) {
EXPECT_EQ(0, countingIslands(map));
}

TEST(CountingIslandsTest, ExampleMap1) {
map = {{false, true, false, true},
{true, true, false, false},
{false, false, true, false},
{false, false, true, false}};

EXPECT_EQ(3, countingIslands(map));
}

TEST(CountingIslandsTest, ExampleMap2) {
map = {{false, true, false, true},
{true, true, true, false},
{false, false, true, false},
{true, false, true, false}};

EXPECT_EQ(3, countingIslands(map));
}

TEST(CountingIslandsTest, ExampleMap3) {
map = {{true, true, false, true},
{false, false, false, false},
{false, false, false, false},
{true, false, false, true}};

EXPECT_EQ(4, countingIslands(map));
}

TEST(CountingIslandsTest, NoIslands) {
map = {{false, false, false, false},
{false, false, false, false},
{false, false, false, false},
{false, false, false, false}};

EXPECT_EQ(0, countingIslands(map));
}

TEST(CountingIslandsTest, NoWaterFullIsland) {
map = {{true, true},
{true, true}};

EXPECT_EQ(1, countingIslands(map));
}

int main(int argc, char **argv) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}