Skip to content

Commit

Permalink
Add bfs count islands (keon#612)
Browse files Browse the repository at this point in the history
* [Add] Create count_islands.py

Add count_islands problem in bfs section.

* [Edit] Add description in count_islands problem

Add description in count_islands problem in bfs section.

* [Edit] Add examples in count_islands problem

Add two examples in count_islands problem in bfs section.

* [Edit] Add counting_islands method

Add counting_islands method in count_islands.py in bfs section.

* [Edit] Modify count_islands problem in bfs

Add needed variables.

* [Edit] Implement counting_islands method

Complete counting_islands method.

* [Edit] Add examples

Add test cases and testing code.

* [Edit] Delete testing code

* [Edit] Change method's name

Change counting_islands to count_islands in count_islands problem.

* [Edit] Add unittest

Implement unittest func. of count_islands problem in bfs section.

* [Edit] Edit count_islands method

While func. is shortened.

* [Delete] Delete unittest of count_islands problem

This reverts commit 8cd0a66.

* [Edit] Add unittest

Implement unittest func. of count_islands problem and edit init file.
  • Loading branch information
guswh11 authored and keon committed Dec 13, 2019
1 parent c675beb commit 595d107
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
1 change: 1 addition & 0 deletions algorithms/bfs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .count_islands import *
from .maze_search import *
from .shortest_distance_from_all_buildings import *
from .word_ladder import *
65 changes: 65 additions & 0 deletions algorithms/bfs/count_islands.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
"""
This is a bfs-version of counting-islands problem in dfs section.
Given a 2d grid map of '1's (land) and '0's (water),
count the number of islands.
An island is surrounded by water and is formed by
connecting adjacent lands horizontally or vertically.
You may assume all four edges of the grid are all surrounded by water.
Example 1:
11110
11010
11000
00000
Answer: 1
Example 2:
11000
11000
00100
00011
Answer: 3
Example 3:
111000
110000
100001
001101
001100
Answer: 3
Example 4:
110011
001100
000001
111100
Answer: 5
"""


def count_islands(grid):
row = len(grid)
col = len(grid[0])

num_islands = 0
visited = [[0] * col for i in range(row)]
directions = [[-1, 0], [1, 0], [0, -1], [0, 1]]
queue = []

for i in range(row):
for j, num in enumerate(grid[i]):
if num == 1 and visited[i][j] != 1:
visited[i][j] = 1
queue.append((i, j))
while queue:
x, y = queue.pop(0)
for k in range(len(directions)):
nx_x = x + directions[k][0]
nx_y = y + directions[k][1]
if nx_x >= 0 and nx_y >= 0 and nx_x < row and nx_y < col:
if visited[nx_x][nx_y] != 1 and grid[nx_x][nx_y] == 1:
queue.append((nx_x, nx_y))
visited[nx_x][nx_y] = 1
num_islands += 1

return num_islands
16 changes: 15 additions & 1 deletion tests/test_bfs.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from algorithms.bfs import (
count_islands,
maze_search,
shortest_distance_from_all_buildings,
ladder_length
Expand All @@ -7,8 +8,21 @@
import unittest


class TestCountIslands(unittest.TestCase):

def test_count_islands(self):
grid_1 = [[1,1,1,1,0], [1,1,0,1,0], [1,1,0,0,0], [0,0,0,0,0]]
self.assertEqual(1, count_islands(grid_1))
grid_2 = [[1,1,0,0,0], [1,1,0,0,0], [0,0,1,0,0], [0,0,0,1,1]]
self.assertEqual(3, count_islands(grid_2))
grid_3 = [[1,1,1,0,0,0], [1,1,0,0,0,0], [1,0,0,0,0,1], [0,0,1,1,0,1], [0,0,1,1,0,0]]
self.assertEqual(3, count_islands(grid_3))
grid_4 = [[1,1,0,0,1,1], [0,0,1,1,0,0], [0,0,0,0,0,1], [1,1,1,1,0,0]]
self.assertEqual(5, count_islands(grid_4))


class TestMazeSearch(unittest.TestCase):

def test_maze_search(self):
grid_1 = [[1,0,1,1,1,1],[1,0,1,0,1,0],[1,0,1,0,1,1],[1,1,1,0,1,1]]
self.assertEqual(14, maze_search(grid_1))
Expand Down

0 comments on commit 595d107

Please sign in to comment.