fix(matrix): make is_safe() return bool and prove count_islands' improper results - #15363
Open
priya-sundaram-dev wants to merge 1 commit into
Open
priya-sundaram-dev wants to merge 1 commit into
priya-sundaram-dev wants to merge 1 commit into
Conversation
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Describe your change:
Follow-up to the discussion on #12691, as requested by @cclauss — a focused PR that (1) makes
Matrix.is_safe() -> boolactually return abool, and (2) addscount_islands()test cases that pin down where the algorithm was giving improper results.The bug.
is_safeended inand self.graph[i][j], so on a safe cell it returned the raw cell value (anint), not abool— hence the old doctests asserted1/0. More than cosmetic:is_safeused a truthy test whilecount_islandsseeds islands only on cells== 1. The two disagree on any cell whose value isn't0/1.The fix. End with
and self.graph[i][j] == 1. This always returns abooland alignsis_safewithcount_islands' seeding rule.Proof of improper results (new doctests). For
[[1, 2, 1]]the two1-islands are bridged by a non-island2. The old truthyis_safeabsorbed the2and merged them, returning1; the corrected version returns2. Also cover out-of-bounds, diagonal-only adjacency, and a lone2.Checklist:
Contributes to #9943
@akiels