Skip to content

Q.88 incorrect answer - existing answer does not to compute borders #238

@Hemanth21k

Description

@Hemanth21k

Question 88: How to implement the Game of Life using numpy arrays?

Rules considered for the Game of Life according to Wikipedia article.
Wikipedia article: https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life
Leetcode reference: https://leetcode.com/problems/game-of-life/description/

Each cell has 8 neighbors, vertical 2, horizontal 2, diagonal 4.

  1. Any live cell with fewer than 2 live neighbors dies.
  2. Any live cell with 2 or 3 live neighbors, survives.
  3. Any live cell with more than 3 live neighbors dies.
  4. Any dead cell with exactly 3 live neighbors, becomes live.

Considering the above rules, I have tested the given answer for the following input and compared with hand calculated outputs after one iteration.

Case 1:

Input board state:
[[0, 1, 0],
[0, 0, 1],
[1, 1, 1]

Neighbors count:
[[1 1 2]
[3 5 3]
[1 3 2]]

Expected state after 1 iteration:
[[0, 0, 0],
[1, 0, 1],
[0, 1, 1]]

Observed next state by the existing algorithm:
[[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]

Case 2:

Input board state:
[[0, 0, 0, 1, 0],
[0, 1, 0, 0, 1],
[0, 1, 0, 0, 0],
[1, 1, 0, 0, 0],
[1, 1, 1, 1, 1]]

Neighbors count:
[[1 1 2 1 2]
[2 1 3 2 1]
[4 3 3 1 1]
[4 5 5 3 2]
[3 4 3 2 1]]

Expected next state:
[[0, 0, 0, 0, 0],
[0, 0, 1, 0, 0],
[0, 1, 1, 0, 0],
[0, 0, 0, 1, 0],
[1, 0, 1, 1, 0]]

Observed next state by the existing algorithm:
[[0, 0, 0, 0, 0],
[0, 0, 1, 0, 0],
[0, 1, 1, 0, 0],
[0, 0, 0, 1, 0],
[0, 0, 0, 0, 0]]

The author's solution seem to be based on considering cells that certainly contain 8 neighbors, thus only counting from index [1,1] to [-1,-1] which fails to compute the border values.

Irrespective of the borders being ignored intentionally or not, this results in incorrect answer because the following states gets affected and provide incorrect final state after several iterations.

Considering case 1 as example:

Hand calculated solution:

Input state -------------- Expected state 1 -------------- Expected state 2 -------------- Expected state 3
[[0, 1, 0], -------------------- [[0, 0, 0], -------------------- [[0, 0, 0], -------------------- [[0, 0, 0],
[0, 0, 1], -------- => -------- [1, 0, 1], -------- => -------- [0, 0, 1], -------- => -------- [0, 1, 1],
[1, 1, 1] ---------------------- [0, 1, 1]] ---------------------- [0, 1, 1]] ---------------------- [0, 1, 1]]

Current algorithm:

Input state -------------- Observed state 1 -------------- Observed state 2 -------------- Observed state 3
[[0, 1, 0], -------------------- [[0, 0, 0], -------------------- [[0, 0, 0], -------------------- [[0, 0, 0],
[0, 0, 1], -------- => -------- [0, 0, 0], -------- => -------- [0, 0, 0], -------- => -------- [0, 0, 0],
[1, 1, 1] ---------------------- [0, 0, 0]] -------------------- [0, 0, 0]] -------------------- [0, 0, 0]]

As we can see above, even if the borders are ignored to determine the validity of the algorithm, after few iterations, the border values start to affect the internal values and ultimately result in an incorrect final state. If borders ignored, we are expected to see 1 in state 3 while we observed 0 in state 3,

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions