Finding island problem for given boolean 2D matrix without recursion










0















I have the code to find count of islands in binary 2d matrix.

It ended with error " RecursionError: maximum recursion depth exceeded in comparison" if i go for complex 2d matrix. So i need implementation of DFS without recursion.



# Program to count islands in boolean 2D matrix 
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









share|improve this question




























    0















    I have the code to find count of islands in binary 2d matrix.

    It ended with error " RecursionError: maximum recursion depth exceeded in comparison" if i go for complex 2d matrix. So i need implementation of DFS without recursion.



    # Program to count islands in boolean 2D matrix 
    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









    share|improve this question


























      0












      0








      0








      I have the code to find count of islands in binary 2d matrix.

      It ended with error " RecursionError: maximum recursion depth exceeded in comparison" if i go for complex 2d matrix. So i need implementation of DFS without recursion.



      # Program to count islands in boolean 2D matrix 
      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









      share|improve this question
















      I have the code to find count of islands in binary 2d matrix.

      It ended with error " RecursionError: maximum recursion depth exceeded in comparison" if i go for complex 2d matrix. So i need implementation of DFS without recursion.



      # Program to count islands in boolean 2D matrix 
      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






      python-3.x while-loop iteration stack-overflow depth-first-search






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 1 '18 at 10:18









      c0der

      8,28251744




      8,28251744










      asked Nov 13 '18 at 6:08









      krishnakrishna

      11




      11






















          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53274815%2ffinding-island-problem-for-given-boolean-2d-matrix-without-recursion%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53274815%2ffinding-island-problem-for-given-boolean-2d-matrix-without-recursion%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          How to read a connectionString WITH PROVIDER in .NET Core?

          Node.js Script on GitHub Pages or Amazon S3

          Museum of Modern and Contemporary Art of Trento and Rovereto