Skip to content

fix(matrix): make is_safe() return bool and prove count_islands' improper results - #15363

Open
priya-sundaram-dev wants to merge 1 commit into
TheAlgorithms:masterfrom
priya-sundaram-dev:fix/is-safe-bool-count-islands
Open

priya-sundaram-dev wants to merge 1 commit into
TheAlgorithms:masterfrom
priya-sundaram-dev:fix/is-safe-bool-count-islands

Conversation

@priya-sundaram-dev

@priya-sundaram-dev priya-sundaram-dev commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

Describe your change:

Follow-up to the discussion on #12691, as requested by @cclauss — a focused PR that (1) makes Matrix.is_safe() -> bool actually return a bool, and (2) adds count_islands() test cases that pin down where the algorithm was giving improper results.

The bug. is_safe ended in and self.graph[i][j], so on a safe cell it returned the raw cell value (an int), not a bool — hence the old doctests asserted 1/0. More than cosmetic: is_safe used a truthy test while count_islands seeds islands only on cells == 1. The two disagree on any cell whose value isn't 0/1.

The fix. End with and self.graph[i][j] == 1. This always returns a bool and aligns is_safe with count_islands' seeding rule.

Proof of improper results (new doctests). For [[1, 2, 1]] the two 1-islands are bridged by a non-island 2. The old truthy is_safe absorbed the 2 and merged them, returning 1; the corrected version returns 2. Also cover out-of-bounds, diagonal-only adjacency, and a lone 2.

Checklist:

  • Add an algorithm? No — bug fix + tests.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues then the description above includes the issue number(s).

Contributes to #9943

@akiels

…ng rule

is_safe was returning the raw cell value (int) from its final
`and self.graph[i][j]` clause, violating its `-> bool` annotation.
Switch to `self.graph[i][j] == 1` so it always returns a bool and,
critically, so is_safe uses the same 'is this cell part of an island'
rule (value == 1) that count_islands uses to seed islands.

Add doctests that prove the previous improper results: a matrix like
[[1, 2, 1]] has two 1-islands bridged by a non-island 2; the old truthy
check absorbed the 2 and returned 1, now it correctly returns 2.

Contributes to TheAlgorithms#9943
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant