Skip to content

Count #2084

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

Merged
merged 9 commits into from
Jun 16, 2020
35 changes: 35 additions & 0 deletions matrix/count_islands_in_matrix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# An island in matrix is a group of linked areas, all having the same value.
# This code couts number of islands in a given matrix, with including diagonal connections.

class matrix: # Public class to implement a graph

def __init__(self, row, col, g): # Class constructor
self.ROW = row
self.COL = col
self.graph = g

def is_safe(self, i, j, visited):
return (i >= 0 and i < self.ROW and j >= 0 and j < self.COL and not visited[i][j] and self.graph[i][j])

def diffs(self, i, j, visited): # Checking all 8 elements surrounding nth element

rowNbr = [-1, -1, -1, 0, 0, 1, 1, 1]; # Coordinate order
colNbr = [-1, 0, 1, -1, 1, -1, 0, 1];

visited[i][j] = True # Make those cells visited

for k in range(8):
if self.is_safe(i + rowNbr[k], j + colNbr[k], visited):
self.diffs(i + rowNbr[k], j + colNbr[k], visited)


def count_islands(self): # And finally, count all islandas.
visited = [[False for j in range(self.COL)]for i in range(self.ROW)]
count = 0
for i in range(self.ROW):
for j in range(self.COL):
if visited[i][j] == False and self.graph[i][j] == 1:
self.diffs(i, j, visited)
count += 1
return count