Improve vertex addition logic in adjacency matrix graph #183
+65
−42
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.
-This pull request refactors the vertex addition logic in the adjacency-matrix graph implementation to improve readability, correctness, and maintainability, without changing the external behavior of the code.
Fixes #8
Detailed Changes
1.Reordered adjacency matrix initialization logic
.The creation of a new zero-initialized row for the adjacency matrix is now clearly separated from vertex registration.
.This makes the flow easier to understand:
1.Add vertex
2.Resize matrix
3.Assign index
.Initialization logic is now explicit and follows a logical sequence.
.The refactor makes it easier for new contributors to understand how the adjacency matrix grows when a vertex is added.
3.Converted class variables to instance variables
.Moved vertices, edges, and edge_indices into init.
.This prevents shared state between multiple Graph instances and aligns with Python best practices.
4.No functional changes
.Graph behavior, edge addition, and output remain unchanged.
.The refactor is purely structural and readability-focused.
Why this change is important
..Improves comprehension for beginners and reviewers
..Prevents hidden bugs caused by shared class state
..Aligns implementation with standard data structure design patterns
..Makes the project more maintainable and contributor-friendly
🧾 Example “Before vs After” (conceptual)
Before
..Vertex addition
..Matrix expansion
..Row initialization mixed together
After
..Vertex registration
..Matrix expansion
..Explicit zero-initialization