Today we are going to share a **Python Program to count islands in boolean 2D matrix**. If you are a python beginner and want to start learning the python programming, then keep your close attention in this tutorial as I am going to share a Python Program to count islands in boolean 2D matrix.

To increase your Python knowledge, practice all Python programs, here is a **collection of 100+ Python problems with solutions.**

Copy the below python program and execute it with the help of **python compiler**.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | class Graph: def __init__(self, row, col, g): self.ROW = row self.COL = col self.graph = g # A function to check if a given cell # (row, col) can be included in DFS def isSafe(self, i, j, visited): # row number is in range, column number # is in range and value is 1 # and not yet 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]) # A utility function to do DFS for a 2D # boolean matrix. It only considers # the 8 neighbours as adjacent vertices def DFS(self, i, j, visited): # These arrays are used to get row and # column numbers of 8 neighbours # of a given cell rowNbr = [-1, -1, -1, 0, 0, 1, 1, 1]; colNbr = [-1, 0, 1, -1, 1, -1, 0, 1]; # Mark this cell as visited visited[i][j] = True # Recur for all connected neighbours for k in range(8): if self.isSafe(i + rowNbr[k], j + colNbr[k], visited): self.DFS(i + rowNbr[k], j + colNbr[k], visited) # The main function that returns # count of islands in a given boolean # 2D matrix def countIslands(self): # Make a bool array to mark visited cells. # Initially all cells are unvisited visited = [[False for j in range(self.COL)]for i in range(self.ROW)] # Initialize count as 0 and travese # through the all cells of # given matrix count = 0 for i in range(self.ROW): for j in range(self.COL): # If a cell with value 1 is not visited yet, # then new island found if visited[i][j] == False and self.graph[i][j] ==1: # Visit all cells in this island # and increment island count self.DFS(i, j, visited) count += 1 return count graph = [[1, 1, 0, 0, 0], [0, 1, 0, 0, 1], [1, 0, 0, 1, 1], [0, 0, 0, 0, 0], [1, 0, 1, 0, 1]] row = len(graph) col = len(graph[0]) g= Graph(row, col, graph) print "Number of islands is:" print g.countIslands() |

Number of islands is: 5

If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.

- Python File Methods
- Python program to add digits of a number
- Python program for subset sum problem
- Generating strong password using Python
- Download Google Images using Python Script
- Python examples on simple mathematical problems
- Program for adding two binary numbers
- Python Program to Solve n-Queen Problem with Recursion
- Python Program to Solve the Celebrity Problem
- Python Program to Solve n-Queen Problem without Recursion

We use cookies to ensure that we give you the best experience on our website. We and our partners are using technologies like cookies and process personal data like the IP-address or browser information in order to personalize the advertising that you see. This helps us to show you more relevant ads and improves your internet experience. Read more about our Privacy Policy.Got it!