-
Notifications
You must be signed in to change notification settings - Fork 2
feat(algorithms, graphs): min cost to supply water #134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
📝 WalkthroughWalkthroughThis PR relocates graph algorithm entries from puzzles to algorithms, updates image path references by removing leading "./" from README files, and introduces the "Min Cost To Supply Water" algorithm with complete implementation, tests, and documentation using Union-Find and Kruskal's approach. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (1)
DIRECTORY.md (1)
87-114: Graphs section entries and links look correct; MD007 warning is optional to addressThe new Algorithms → Graphs bullets (Evaluate Division, Keys And Rooms, Knight On Chess Board, Maxareaofisland, Min Cost To Supply, Nearest Exit…, Number Of Islands (+ Union Find), Number Of Provinces, Reorder Routes, Rotting Oranges, Valid Path) correctly reference the
algorithms/graphs/...test files, including the new Min Cost To Supply tests.Indentation for this block matches the surrounding list structure; the MD007 (ul-indent) warnings from markdownlint are stylistic. If you want a perfectly clean lint run, you’d need to adjust indentation consistently across this whole file, not just this new block.
📜 Review details
Configuration used: defaults
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (24)
algorithms/graphs/knight_on_chess_board/knight_on_chess_board.jpgis excluded by!**/*.jpgalgorithms/graphs/maxareaofisland/maxarea-grid.jpgis excluded by!**/*.jpgalgorithms/graphs/min_cost_to_supply/images/examples/min_cost_to_supply_example_1.pngis excluded by!**/*.pngalgorithms/graphs/min_cost_to_supply/images/examples/min_cost_to_supply_example_2.pngis excluded by!**/*.pngalgorithms/graphs/min_cost_to_supply/images/examples/min_cost_to_supply_example_3.pngis excluded by!**/*.pngalgorithms/graphs/min_cost_to_supply/images/examples/min_cost_to_supply_example_4.pngis excluded by!**/*.pngalgorithms/graphs/min_cost_to_supply/images/examples/min_cost_to_supply_example_5.pngis excluded by!**/*.pngalgorithms/graphs/min_cost_to_supply/images/solutions/min_cost_to_supply_solution_1.pngis excluded by!**/*.pngalgorithms/graphs/min_cost_to_supply/images/solutions/min_cost_to_supply_solution_2.pngis excluded by!**/*.pngalgorithms/graphs/min_cost_to_supply/images/solutions/min_cost_to_supply_solution_3.pngis excluded by!**/*.pngalgorithms/graphs/min_cost_to_supply/images/solutions/min_cost_to_supply_solution_4.pngis excluded by!**/*.pngalgorithms/graphs/min_cost_to_supply/images/solutions/min_cost_to_supply_solution_5.pngis excluded by!**/*.pngalgorithms/graphs/min_cost_to_supply/images/solutions/min_cost_to_supply_solution_6.pngis excluded by!**/*.pngalgorithms/graphs/min_cost_to_supply/images/solutions/min_cost_to_supply_solution_7.pngis excluded by!**/*.pngalgorithms/graphs/min_cost_to_supply/images/solutions/min_cost_to_supply_solution_8.pngis excluded by!**/*.pngalgorithms/graphs/min_cost_to_supply/images/solutions/min_cost_to_supply_solution_9.pngis excluded by!**/*.pngalgorithms/graphs/nearest_exit_from_entrance_in_maze/nearest1-grid.jpgis excluded by!**/*.jpgalgorithms/graphs/nearest_exit_from_entrance_in_maze/nearest2-grid.jpgis excluded by!**/*.jpgalgorithms/graphs/nearest_exit_from_entrance_in_maze/nearest3-grid.jpgis excluded by!**/*.jpgalgorithms/graphs/number_of_provinces/no_of_provinces_1.jpgis excluded by!**/*.jpgalgorithms/graphs/number_of_provinces/no_of_provinces_2.jpgis excluded by!**/*.jpgalgorithms/graphs/reorder_routes/reorder_routes_example_1.pngis excluded by!**/*.pngalgorithms/graphs/reorder_routes/reorder_routes_example_2.pngis excluded by!**/*.pngalgorithms/graphs/rotting_oranges/rotting_oranges.pngis excluded by!**/*.png
📒 Files selected for processing (38)
DIRECTORY.mdalgorithms/graphs/evaluate_division/README.mdalgorithms/graphs/evaluate_division/__init__.pyalgorithms/graphs/evaluate_division/test_evaluate_division.pyalgorithms/graphs/keys_and_rooms/README.mdalgorithms/graphs/keys_and_rooms/__init__.pyalgorithms/graphs/keys_and_rooms/test_can_visit_all_rooms.pyalgorithms/graphs/knight_on_chess_board/README.mdalgorithms/graphs/knight_on_chess_board/__init__.pyalgorithms/graphs/knight_on_chess_board/test_knight_on_chess_board.pyalgorithms/graphs/maxareaofisland/README.mdalgorithms/graphs/maxareaofisland/__init__.pyalgorithms/graphs/maxareaofisland/test_max_area_of_island.pyalgorithms/graphs/mesh_message/README.mdalgorithms/graphs/mesh_message/__init__.pyalgorithms/graphs/min_cost_to_supply/README.mdalgorithms/graphs/min_cost_to_supply/__init__.pyalgorithms/graphs/min_cost_to_supply/test_min_cost_to_supply.pyalgorithms/graphs/nearest_exit_from_entrance_in_maze/README.mdalgorithms/graphs/nearest_exit_from_entrance_in_maze/__init__.pyalgorithms/graphs/nearest_exit_from_entrance_in_maze/test_nearest_exit_from_entrance.pyalgorithms/graphs/number_of_islands/README.mdalgorithms/graphs/number_of_islands/__init__.pyalgorithms/graphs/number_of_islands/test_number_of_islands.pyalgorithms/graphs/number_of_islands/union_find.pyalgorithms/graphs/number_of_provinces/README.mdalgorithms/graphs/number_of_provinces/__init__.pyalgorithms/graphs/number_of_provinces/test_number_of_provinces.pyalgorithms/graphs/reorder_routes/README.mdalgorithms/graphs/reorder_routes/__init__.pyalgorithms/graphs/reorder_routes/test_reorder_routes.pyalgorithms/graphs/rotting_oranges/README.mdalgorithms/graphs/rotting_oranges/__init__.pyalgorithms/graphs/rotting_oranges/test_rotting_oranges.pyalgorithms/graphs/valid_path/README.mdalgorithms/graphs/valid_path/__init__.pyalgorithms/graphs/valid_path/test_valid_path.pypuzzles/graphs/__init__.py
🧰 Additional context used
🧬 Code graph analysis (4)
algorithms/graphs/number_of_islands/test_number_of_islands.py (1)
algorithms/graphs/number_of_islands/__init__.py (2)
num_of_islands(5-43)num_islands_union_find(46-65)
algorithms/graphs/min_cost_to_supply/__init__.py (2)
algorithms/graphs/number_of_islands/union_find.py (2)
UnionFind(1-40)union(25-36)algorithms/graphs/number_of_provinces/__init__.py (2)
UnionFind(4-29)union(16-29)
algorithms/graphs/min_cost_to_supply/test_min_cost_to_supply.py (1)
algorithms/graphs/min_cost_to_supply/__init__.py (2)
min_cost_to_supply_water(5-38)min_cost_to_supply_water_2(41-51)
algorithms/graphs/number_of_islands/__init__.py (1)
algorithms/graphs/number_of_islands/union_find.py (1)
UnionFind(1-40)
🪛 LanguageTool
algorithms/graphs/min_cost_to_supply/README.md
[style] ~3-~3: Consider a more expressive alternative.
Context: ...want to supply water to every house. To do this, we have two options: 1. Build a w...
(DO_ACHIEVE)
🪛 markdownlint-cli2 (0.18.1)
DIRECTORY.md
90-90: Unordered list indentation
Expected: 2; Actual: 4
(MD007, ul-indent)
91-91: Unordered list indentation
Expected: 4; Actual: 6
(MD007, ul-indent)
92-92: Unordered list indentation
Expected: 2; Actual: 4
(MD007, ul-indent)
93-93: Unordered list indentation
Expected: 4; Actual: 6
(MD007, ul-indent)
94-94: Unordered list indentation
Expected: 2; Actual: 4
(MD007, ul-indent)
95-95: Unordered list indentation
Expected: 4; Actual: 6
(MD007, ul-indent)
96-96: Unordered list indentation
Expected: 2; Actual: 4
(MD007, ul-indent)
97-97: Unordered list indentation
Expected: 4; Actual: 6
(MD007, ul-indent)
98-98: Unordered list indentation
Expected: 2; Actual: 4
(MD007, ul-indent)
99-99: Unordered list indentation
Expected: 4; Actual: 6
(MD007, ul-indent)
100-100: Unordered list indentation
Expected: 2; Actual: 4
(MD007, ul-indent)
101-101: Unordered list indentation
Expected: 4; Actual: 6
(MD007, ul-indent)
102-102: Unordered list indentation
Expected: 2; Actual: 4
(MD007, ul-indent)
103-103: Unordered list indentation
Expected: 4; Actual: 6
(MD007, ul-indent)
104-104: Unordered list indentation
Expected: 2; Actual: 4
(MD007, ul-indent)
105-105: Unordered list indentation
Expected: 4; Actual: 6
(MD007, ul-indent)
106-106: Unordered list indentation
Expected: 4; Actual: 6
(MD007, ul-indent)
107-107: Unordered list indentation
Expected: 2; Actual: 4
(MD007, ul-indent)
108-108: Unordered list indentation
Expected: 4; Actual: 6
(MD007, ul-indent)
109-109: Unordered list indentation
Expected: 2; Actual: 4
(MD007, ul-indent)
110-110: Unordered list indentation
Expected: 4; Actual: 6
(MD007, ul-indent)
111-111: Unordered list indentation
Expected: 2; Actual: 4
(MD007, ul-indent)
112-112: Unordered list indentation
Expected: 4; Actual: 6
(MD007, ul-indent)
113-113: Unordered list indentation
Expected: 2; Actual: 4
(MD007, ul-indent)
114-114: Unordered list indentation
Expected: 4; Actual: 6
(MD007, ul-indent)
🔇 Additional comments (13)
algorithms/graphs/number_of_islands/test_number_of_islands.py (1)
4-4: Import path alignment with algorithms package looks correctThe updated import to
algorithms.graphs.number_of_islandsmatches the relocated implementation and exposed symbols; tests remain otherwise unchanged.algorithms/graphs/rotting_oranges/README.md (1)
15-15: Image path tweak is consistent and safeSwitching from
./rotting_oranges.pngtorotting_oranges.pngkeeps the relative reference valid and aligns with other graph README updates.algorithms/graphs/knight_on_chess_board/README.md (1)
6-6: Image reference normalization looks goodRemoving the leading
"./"from the knight image path is harmless and consistent with the rest of the graph READMEs.algorithms/graphs/reorder_routes/README.md (1)
18-27: Reorder Routes image paths updated consistentlyBoth example images now use
reorder_routes_example_X.pngwithout"./", matching the pattern adopted elsewhere in this PR.algorithms/graphs/nearest_exit_from_entrance_in_maze/README.md (1)
15-43: Nearest-exit maze image references look fineAll three image paths are normalized (no
"./") and still correctly target files in the same directory.algorithms/graphs/number_of_islands/__init__.py (1)
2-2: UnionFind import path update aligns with new module layoutImporting
UnionFindfromalgorithms.graphs.number_of_islands.union_findmatches the reorganized location of that helper and keepsnum_islands_union_findbehavior intact.algorithms/graphs/min_cost_to_supply/__init__.py (1)
1-51: No changes needed; theUnionFind.unionmethod correctly returns a booleanThe imported
UnionFindfromdatastructures.sets.union_findexplicitly returnsbool—Truewhen a merge occurs andFalsewhen elements are already in the same set. The patternif uf.union(house1, house2):is correct, andtotal_costwill properly accumulate. Both functions implement the MST/Kruskal approach correctly.algorithms/graphs/min_cost_to_supply/test_min_cost_to_supply.py (3)
1-18: LGTM! Well-structured test setup.The imports are correct, and the test cases provide good coverage across various scenarios including edge cases with different graph sizes, well costs, and pipe configurations.
21-27: LGTM! Test method is correctly implemented.The parameterized test is properly structured with accurate type hints and assertion logic.
29-38: LGTM! Second test method and CLI entry point are correct.The test for the alternative implementation shares the same test cases appropriately, and the CLI entry point follows Python conventions.
algorithms/graphs/min_cost_to_supply/README.md (3)
1-26: LGTM! Clear and comprehensive problem description.The problem statement effectively explains the water distribution challenge with wells and pipes, and the constraints are properly documented. The static analysis style hint about "To do this" can be safely ignored—the phrasing is clear and appropriate for technical documentation.
80-91: LGTM! Accurate complexity analysis.The time and space complexity analysis is correct and well-explained. The identification of the sorting step as the dominant factor and the mention of the inverse Ackermann function α(n) for Union-Find operations demonstrates thorough understanding.
29-67: LGTM! Excellent solution explanation with clear algorithm steps.The solution effectively describes the virtual node approach combined with Union-Find/Kruskal's algorithm. The step-by-step breakdown is clear and actionable. All referenced example images are present and properly linked.
Describe your change:
Min Cost to supply water
Checklist:
Fixes: #{$ISSUE_NO}.Summary by CodeRabbit
Release Notes
New Features
Documentation
Tests
✏️ Tip: You can customize this high-level summary in your review settings.