Skip to content

Latest commit

 

History

32 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Computer Science & Software Engineering Algorithms

A structured repository dedicated to implementing, analyzing, and documenting foundational computer science algorithms and data structures using Python. This project serves as an educational sandbox to study runtime complexities, data architecture, and verification methodologies.

Project Structure

  • src/: Core Python implementations categorized by algorithmic domain.
  • service/: Transport-agnostic wrappers exposing the algorithms via an MCP stdio server and a FastAPI HTTP API.
  • tests/: Automated unit tests mirroring the codebase layout to validate edge cases and performance boundaries.
  • docs/adr/: Architectural Decision Records tracking the design choices for each algorithm.
  • docs/CONTRIBUTING.md: Step-by-step branching, testing, and PR/merge workflow guide.

Getting Started

Prerequisites

  • Python 3.10 or higher
  • pip (Python package installer)

Installation

  1. Clone the repository:

    git clone https://github.com
    cd csc-algorithms
  2. Initialize a local virtual environment:

    python -m venv venv
    source venv/bin/activate  # On Windows use: venv\Scripts\activate
  3. Install required development and testing dependencies:

    pip install -r requirements.txt

Execution and Testing

The repository uses pytest for codebase verification. Run the test suite globally using the following command:

pytest tests/

To run syntax and style validation checks using flake8 or black:

black --check src/ tests/

For the full test and coverage gate, run make verify. Local commits use the tracked .githooks/pre-commit hook for fast staged-area tests by default; use TEST_SCOPE=full git commit to run the complete 100% coverage gate before committing.

Running the Service Layer

Every algorithm is also exposed via a stateless MCP server and a REST API, both backed by the same service/tools.py wrapper functions.

MCP server (stdio transport) — for use with MCP-aware agents/chat clients (Claude Desktop, VS Code, etc.):

python -m service.mcp_server

Register it with your MCP client by pointing it at this command; consult your client's documentation for its mcp.json/config format.

HTTP API — for any other programmatic caller:

uvicorn service.http_app:app --reload

Each endpoint mirrors an MCP tool, e.g. POST /sorting/quicksort, POST /graphs/dijkstra, POST /machine-learning/kmeans. Interactive OpenAPI docs are available at http://127.0.0.1:8000/docs once the server is running.

Architectural Decision Records (ADRs)

The architectural choices, trade-offs, and design patterns for each algorithm are fully documented below:

Algorithm Catalog

Browse the algorithm catalog for concise definitions, complexity notes, implementation links, tests, and guidance on choosing an algorithm or data structure.


Code Quality and Design Guidelines

Naming Conventions & Code Style

To ensure uniformity, this repository follows strict standards derived from PEP 8:

  • Functions & Variables: Lowercase word blocks separated by underscores (snake_case).
  • Protected Components: Preceded by a single leading underscore (e.g., _partition).
  • Constants: Full uppercase strings separated by underscores (UPPER_SNAKE_CASE).
  • Type Hinting: Mandatory on all public functions via the typing module framework.

Security, Stability & Privacy Considerations

  1. Input Integrity & Memory Protection: Sorting algorithms construct a explicit local list() copy of tracking variables to prevent input reference mutation bugs.
  2. Denial of Service (DoS) Boundaries: Quicksort worst-case scaling behavior is $O(n^2)$. For safety-critical systems sorting untrusted or adversarial user inputs, randomizing the pivot selection or utilizing heap-sort/merge-sort derivatives should be considered.
  3. Graph Payload Resilience: The Dijkstra parser explicitly references data isolation using explicit float("inf") typing arrays. Node configurations must strictly pass hashable unique strings to mitigate graph processing collision events.
  4. Data Isolation: This package operates entirely locally on internal operational states. No logging pipelines, web tracing, or environment data tracking hooks are implemented, ensuring maximum data privacy.
  5. Negative-Weight Cycle Guarding: The Bellman-Ford implementation runs an explicit final relaxation pass to detect reachable negative-weight cycles and raises a ValueError rather than allowing an untrusted graph payload to loop indefinitely.
  6. Heuristic Input Validation: The A* implementation validates that source, target, and coordinate metadata exist before search begins, and rejects negative edge weights, preventing malformed spatial graphs from corrupting the heuristic scoring.
  7. Balanced Depth Guarantee: The AVL Tree rebalances on every insert and delete, preventing adversarial sorted-input sequences from degrading traversal operations to linear time.
  8. Codebook Integrity Validation: The Huffman decoder rejects malformed or duplicate-code codebooks and dangling/invalid bitstreams with an explicit ValueError, rather than silently returning corrupted or truncated text.
  9. Cycle & Referential Integrity Guards: Topological Sort validates that every edge references a declared node and raises a ValueError when a cycle prevents a complete ordering, rather than silently returning a partial or misleading sequence.
  10. Bounded Memory Allocation: The Sieve of Eratosthenes allocates its boolean tracking array based on the caller-supplied boundary; callers should validate untrusted boundary inputs against a sane upper limit before use to avoid excessive memory allocation.
  11. Service Layer Input Validation: The HTTP API validates request bodies via Pydantic schemas and translates algorithm-level ValueErrors into HTTP 400 responses rather than leaking stack traces; both the MCP server and HTTP API are stateless per call, so no client-supplied data persists across requests.
  12. Fixed Element Universe: Union-Find validates every find()/union() call against its initial element set and raises a ValueError for unknown elements, preventing silent creation of untracked entries.
  13. Worst-Case DoS Mitigation: Merge Sort guarantees $O(n \log n)$ even on adversarial input, making it the safer default over Quicksort when sorting untrusted, attacker-influenced data where worst-case scaling matters.
  14. Bounded Traversal Footprint: The Singly Linked List's search/delete/reverse operations are strictly O(n) iterative walks with no recursion, preventing stack-depth exhaustion on very large untrusted input lists.
  15. Iterative DP, No Recursion Limits: The 0/1 Knapsack solver uses bottom-up tabulation rather than top-down recursion, avoiding Python's RecursionError on large item counts.
  16. Quadratic Complexity Awareness: LCS runs in $O(n \times m)$ time and space; callers should bound input string lengths when comparing untrusted, attacker-controlled text to avoid excessive memory allocation on very large inputs.
  17. In-Place Worst-Case Guarantee: Heap Sort provides the same $O(n \log n)$ worst-case guarantee as Merge Sort but with $O(1)$ auxiliary space, useful when both adversarial-input resilience and memory constraints matter simultaneously.
  18. Precondition Responsibility: Binary Search assumes sorted input and does not validate it; callers must guarantee sortedness themselves, since verifying it would negate the algorithm's logarithmic performance advantage.
  19. Traversal Input Integrity: BFS and DFS validate the source and every adjacency reference before traversal, preventing malformed graph payloads from producing partial results; both use iterative state to avoid recursion-depth exhaustion.
  20. Minimum-Spanning-Tree Integrity: Kruskal validates vertex and edge references, skips cycle-forming edges with Union-Find, and rejects disconnected graphs instead of returning a partial spanning tree.

About

A structured repository dedicated to implementing, analyzing, and documenting foundational computer science algorithms and data structures using Python. This project serves as an educational sandbox to study runtime complexities, data architecture, and verification methodologies.

Topics

Resources

Contributing

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages