AlgoZen is a comprehensive Python package providing efficient implementations of data structures, algorithms, design patterns, and common programming problems. Perfect for learning, coding interviews, competitive programming, and production use.
- Heaps: MinHeap, MaxHeap with O(log n) operations
- Linear: Stack, Queue, LinkedList with full functionality
- Trees: BinaryTree, BinarySearchTree, AVLTree, Trie
- Advanced: SegmentTree, FenwickTree, UnionFind
- Graphs: Comprehensive graph representation with algorithms
- Sorting: Bubble, Selection, Insertion, Merge, Quick, Heap, Counting, Radix
- Searching: Linear, Binary, Interpolation, Exponential, Jump
- String: KMP, Rabin-Karp, Z-Algorithm, Edit Distance
- Graph: DFS, BFS, Dijkstra, Bellman-Ford, Floyd-Warshall
- Tree: Traversals, LCA, Path algorithms
- Mathematical: GCD, LCM, Prime algorithms, Number theory
- Bit Manipulation: Count bits, XOR operations, bit tricks
- Sliding Window: Maximum sum, substring problems
- Two Pointers: Pair finding, array manipulation
- Fast/Slow Pointers: Cycle detection, middle finding
- Merge Intervals: Overlapping intervals, scheduling
- Dynamic Programming: Advanced DP patterns and optimizations
- Creational: Factory, Builder patterns
- Behavioral: Observer, Strategy, Command, State
- Structural: Decorator, Adapter patterns
- Arrays: Two sum, maximum subarray, product problems
- Strings: Palindromes, anagrams, pattern matching
- Trees: Traversals, path problems, validation
- Graphs: Shortest paths, connectivity, topological sort
- Dynamic Programming: Classic DP problems and variations
# Install from PyPI
pip install algozen
# Or install the latest version
pip install --upgrade algozen# Import the package
import algozen
# Use data structures
from algozen.data_structures import MinHeap, BinarySearchTree, Graph
# Create and use a MinHeap
heap = MinHeap()
heap.insert(5)
heap.insert(3)
heap.insert(8)
print(heap.extract_min()) # Output: 3
# Use algorithms
from algozen.algorithms import quick_sort, binary_search
# Sort an array
arr = [64, 34, 25, 12, 22, 11, 90]
sorted_arr = quick_sort(arr)
print(sorted_arr) # Output: [11, 12, 22, 25, 34, 64, 90]
# Search in sorted array
index = binary_search(sorted_arr, 25)
print(index) # Output: 3
# Use patterns
from algozen.patterns.sliding_window import max_sum_subarray_of_size_k
from algozen.patterns.two_pointers import pair_with_target_sum
# Sliding window pattern
max_sum = max_sum_subarray_of_size_k([2, 1, 5, 1, 3, 2], 3)
print(max_sum) # Output: 9
# Two pointers pattern
indices = pair_with_target_sum([1, 2, 3, 4, 6], 6)
print(indices) # Output: [1, 3]
# Use design patterns
from algozen.design_patterns.factory import AnimalFactory, AnimalType
factory = AnimalFactory()
dog = factory.create_animal(AnimalType.DOG, "Buddy")
print(dog.make_sound()) # Output: Woof!
# Solve problems
from algozen.problems.arrays import two_sum, max_subarray_sum
# Two sum problem
result = two_sum([2, 7, 11, 15], 9)
print(result) # Output: [0, 1]
# Maximum subarray sum (Kadane's algorithm)
max_sum = max_subarray_sum([-2, 1, -3, 4, -1, 2, 1, -5, 4])
print(max_sum) # Output: 6from algozen.data_structures import MinHeap, MaxHeap
# MinHeap operations
heap = MinHeap()
heap.insert(10) # Insert element
heap.insert(5)
min_val = heap.extract_min() # Remove and return minimum
top = heap.peek() # View minimum without removing
size = heap.size() # Get number of elements
empty = heap.is_empty() # Check if emptyfrom algozen.data_structures import BinarySearchTree
bst = BinarySearchTree()
bst.insert(50) # Insert key
bst.insert(30)
bst.insert(70)
found = bst.search(30) # Search for key
bst.delete(30) # Delete key
min_key = bst.min() # Find minimum key
max_key = bst.max() # Find maximum keyfrom algozen.data_structures import Graph
graph = Graph()
graph.add_vertex("A") # Add vertex
graph.add_vertex("B")
graph.add_edge("A", "B", 5) # Add weighted edge
neighbors = graph.get_neighbors("A") # Get adjacent verticesfrom algozen.algorithms.sorting import quick_sort, merge_sort, heap_sort
arr = [64, 34, 25, 12, 22, 11, 90]
# All sorting functions modify and return the array
sorted_arr = quick_sort(arr.copy()) # O(n log n) average
sorted_arr = merge_sort(arr.copy()) # O(n log n) guaranteed
sorted_arr = heap_sort(arr.copy()) # O(n log n) in-placefrom algozen.algorithms.string_algorithms import kmp_search, rabin_karp_search
text = "ababcababa"
pattern = "aba"
# Find all occurrences of pattern in text
matches = kmp_search(text, pattern) # KMP algorithm
matches = rabin_karp_search(text, pattern) # Rabin-Karp algorithmfrom algozen.patterns.sliding_window import (
max_sum_subarray_of_size_k,
longest_substring_with_k_distinct,
longest_substring_without_repeating_chars
)
# Maximum sum of subarray of size k
max_sum = max_sum_subarray_of_size_k([2, 1, 5, 1, 3, 2], 3)
# Longest substring with k distinct characters
length = longest_substring_with_k_distinct("araaci", 2)
# Longest substring without repeating characters
length = longest_substring_without_repeating_chars("abcabcbb")from algozen.patterns.two_pointers import (
pair_with_target_sum,
remove_duplicates,
search_triplets
)
# Find pair with target sum in sorted array
indices = pair_with_target_sum([1, 2, 3, 4, 6], 6)
# Remove duplicates from sorted array in-place
arr = [2, 3, 3, 3, 6, 9, 9]
new_length = remove_duplicates(arr)
# Find all triplets that sum to target
triplets = search_triplets([-3, 0, 1, 2, -1, 1, -2], 0)from algozen.design_patterns.factory import AnimalFactory, AnimalType
factory = AnimalFactory()
dog = factory.create_animal(AnimalType.DOG, "Buddy")
cat = factory.create_animal(AnimalType.CAT, "Whiskers")
print(dog.make_sound()) # Woof!
print(cat.make_sound()) # Meow!from algozen.design_patterns.observer import Subject, Observer
# Create subject and observers
subject = Subject()
observer1 = Observer("Observer1")
observer2 = Observer("Observer2")
# Attach observers
subject.attach(observer1)
subject.attach(observer2)
# Notify all observers
subject.notify("data_changed", {"new_value": 42})AlgoZen comes with comprehensive test coverage ensuring all functionality works correctly:
# All modules are thoroughly tested
import algozen
# Test data structures
from algozen.data_structures import MinHeap
heap = MinHeap()
heap.insert(5)
assert heap.extract_min() is not None
# Test algorithms
from algozen.algorithms.sorting import quick_sort
result = quick_sort([3, 1, 4, 1, 5])
assert result == [1, 1, 3, 4, 5]| Data Structure | Insert | Delete | Search | Space |
|---|---|---|---|---|
| MinHeap/MaxHeap | O(log n) | O(log n) | O(n) | O(n) |
| Stack | O(1) | O(1) | O(n) | O(n) |
| Queue | O(1) | O(1) | O(n) | O(n) |
| BST | O(log n)* | O(log n)* | O(log n)* | O(n) |
| Graph | O(1) | O(V) | O(V) | O(V+E) |
*Average case. Worst case is O(n) for unbalanced trees.
| Algorithm | Best | Average | Worst | Space |
|---|---|---|---|---|
| Quick Sort | O(n log n) | O(n log n) | O(nΒ²) | O(log n) |
| Merge Sort | O(n log n) | O(n log n) | O(n log n) | O(n) |
| Binary Search | O(1) | O(log n) | O(log n) | O(1) |
| KMP Search | O(n+m) | O(n+m) | O(n+m) | O(m) |
algozen/
βββ data_structures/ # Core data structure implementations
β βββ heap.py # MinHeap, MaxHeap
β βββ stack.py # Stack implementation
β βββ queue.py # Queue implementation
β βββ linked_list.py # LinkedList with Node
β βββ binary_search_tree.py # BST implementation
β βββ graph.py # Graph with Edge
β βββ trie.py # Trie with TrieNode
β βββ ... # Other data structures
βββ algorithms/ # Algorithm implementations
β βββ sorting.py # All sorting algorithms
β βββ searching.py # All searching algorithms
β βββ string_algorithms.py # String processing
β βββ ... # Other algorithm categories
βββ patterns/ # DSA patterns and techniques
β βββ bit_manipulation.py # Bit manipulation patterns
β βββ sliding_window.py # Sliding window technique
β βββ two_pointers.py # Two pointers technique
β βββ ... # Other patterns
βββ design_patterns/ # Software design patterns
β βββ factory.py # Factory pattern
β βββ observer.py # Observer pattern
β βββ strategy.py # Strategy pattern
β βββ ... # Other design patterns
βββ problems/ # Common DSA problems
β βββ arrays.py # Array problems
β βββ strings.py # String problems
β βββ trees.py # Tree problems
β βββ ... # Other problem categories
βββ test_comprehensive.py # Complete test suite
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
- Follow PEP 8 style guidelines
- Add comprehensive docstrings with time/space complexity
- Include examples in docstrings
- Ensure backward compatibility
This project is licensed under the MIT License - see the LICENSE file for details.
- Inspired by classic algorithms and data structures textbooks
- Built with modern Python best practices
- Designed for both learning and production use
- 55 Python modules with comprehensive implementations
- 900+ functions and classes covering all major CS concepts
- Zero dependencies for core functionality
- Production-ready code with proper error handling
- Extensive documentation with examples and complexity analysis
- MIT Licensed - free for commercial and personal use
- Complete reorganization with perfect module structure
- Added comprehensive design patterns module
- Enhanced all data structures with full functionality
- Improved algorithm implementations with better performance
- Added 200+ problem solutions across 9 categories
- Zero duplicate code - clean, maintainable codebase
- Full test coverage ensuring reliability
Made with β€οΈ by moah0911
AlgoZen - Where algorithms meet zen-like simplicity π§ββοΈ