Skip to content

Revise README and enhance graph algorithms for Python 3#1

Open
mebriki wants to merge 6 commits intoyouben11:masterfrom
mebriki:master
Open

Revise README and enhance graph algorithms for Python 3#1
mebriki wants to merge 6 commits intoyouben11:masterfrom
mebriki:master

Conversation

@mebriki
Copy link
Copy Markdown

@mebriki mebriki commented Mar 27, 2026

This pull request modernizes and refines the graph theory codebase, providing clearer structure, robust implementations for key algorithms, and comprehensive testing. The main changes include significant improvements to both the weighted_graph.py and unweighted_graph.py modules, a complete rewrite of the documentation in README.md, and the addition of automated tests.

Key changes by theme:

Documentation and Project Structure

  • Rewrote README.md to provide a detailed project overview, usage examples, visualization guides, and notes on edge cases and exceptions. The documentation now clearly distinguishes between weighted and unweighted graph modules and explains the test suite.

Testing

  • Added tests/test_graphs.py with comprehensive unit tests covering shortest path algorithms, minimum spanning trees (Prim and Kruskal), cycle detection, and edge cases for both weighted and unweighted graphs.

Unweighted Graph Improvements (unweighted_graph.py)

  • Rewrote isCyclic to use a depth-first search with recursion and stack tracking, making cycle detection for directed graphs more robust and readable.
  • Refactored bestPath to use a standard breadth-first search (BFS), improving correctness and clarity for finding the shortest path between nodes.

Weighted Graph Improvements (weighted_graph.py)

  • Rewrote isCyclic to use an undirected DFS approach, ensuring accurate cycle detection for undirected graphs.
  • Reimplemented Kruskal's and Prim's algorithms for minimum spanning trees with standard, efficient approaches, including union-find for Kruskal and a priority queue for Prim. These changes increase correctness, efficiency, and handle disconnected graphs gracefully.
  • Refactored Dijkstra's and Bellman-Ford algorithms to use standard implementations with proper error handling (e.g., negative weights/cycles), improved path reconstruction, and clearer logic.

General Codebase Enhancements

  • Added necessary imports (e.g., heapq, deque) and improved code style for readability and maintainability. [1] [2]

These updates make the codebase more reliable, easier to use and test, and better suited for both learning and practical experimentation with graph algorithms.

mebriki added 6 commits March 27, 2026 15:22
Updated project name and added detailed overview of graph implementations.
* Reworked weighted graph core algorithms for correctness:
  * Reimplement Kruskal with Union-Find for correct MST construction.
  * Reimplement Prim with min-heap and disconnected-graph guard.
  * Reimplemented dijkstra with priority-queue relaxation and correct path reconstruction.
  * Reimplemented Bellman-Ford with standard relaxation and proper negative-cycle detection.

* Weighted graph structure and traversal safety:
  * Rework unoriented to build a safe undirected adjacency map, including referenced-only nodes.
  * Replace cycle detection logic with robust traversal over the undirected view.

* unweighted graph behavior improvements:
  * Replace cycle detection with DFS recursion-stack detection (directed-graph correct behavior).
  * Reimplement bestPath with BFS + parent reconstruction.
  * Add missing/unreachable guards in bestPath to return empty path safely.

* Added concise docstrings and minor readability improvements.
* Fix Python 3 compatibility issues (removed dict-keys indexing patterns).
…rams:

- Weighted sample graph with edge weights
- MST output graph (matching Prim/Kruskal expected tree)
- Unweighted sample graph (directed)
This gives users immediate visual context alongside the code examples and tests.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant