Skip to content

Commit 8877e9b

Browse files
author
Adwaita Jadhav
committed
feat: Add high priority algorithms - backtracking, advanced graph, string, and sorting algorithms
- Add new backtracking module with N-Queens, Sudoku solver, maze solver, and permutations - Extend pathfinding with Bellman-Ford, Floyd-Warshall, and Prim's algorithms - Add advanced string algorithms: KMP search and edit distance - Add Bingo sort algorithm optimized for datasets with duplicates - Include comprehensive test suite with 10/10 tests passing - Maintain consistent API patterns and educational focus - All algorithms authored by Adwaita Jadhav Closes: Enhancement request for missing fundamental algorithms
1 parent be35813 commit 8877e9b

17 files changed

Lines changed: 2819 additions & 5 deletions

ALGORITHM_ADDITIONS.md

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
# High Priority Algorithm Additions to Pygorithm
2+
3+
This document summarizes the high priority algorithms that have been successfully added to the Pygorithm library.
4+
5+
## Summary of Additions
6+
7+
### 1. **Backtracking Algorithms** (New Category)
8+
**Location:** `pygorithm/backtracking/`
9+
10+
#### N-Queens Problem (`n_queens.py`)
11+
- **Author:** Adwaita Jadhav
12+
- **Functions:**
13+
- `solve_n_queens(n)` - Find all solutions
14+
- `solve_n_queens_first_solution(n)` - Find first solution
15+
- `print_board(solution)` - Display solution
16+
- `count_solutions(n)` - Count total solutions
17+
- **Time Complexity:** O(N!)
18+
- **Features:** Complete backtracking implementation with board visualization
19+
20+
#### Sudoku Solver (`sudoku_solver.py`)
21+
- **Author:** Adwaita Jadhav
22+
- **Functions:**
23+
- `solve_sudoku(board)` - Solve 9x9 Sudoku puzzle
24+
- `is_valid_sudoku(board)` - Validate Sudoku board
25+
- `print_board(board)` - Display board with formatting
26+
- `create_empty_board()` - Generate empty board
27+
- **Time Complexity:** O(9^(n*n))
28+
- **Features:** Full constraint satisfaction with validation
29+
30+
#### Maze Solver (`maze_solver.py`)
31+
- **Author:** Adwaita Jadhav
32+
- **Functions:**
33+
- `solve_maze(maze, start, end)` - Find path through maze
34+
- `solve_maze_all_paths(maze, start, end)` - Find all possible paths
35+
- `print_maze_with_path(maze, path)` - Visualize solution
36+
- `create_sample_maze()` - Generate test maze
37+
- **Time Complexity:** O(4^(n*m))
38+
- **Features:** Path finding with visualization and multiple path support
39+
40+
#### Permutations Generator (`permutations.py`)
41+
- **Author:** Adwaita Jadhav
42+
- **Functions:**
43+
- `generate_permutations(arr)` - All permutations
44+
- `generate_unique_permutations(arr)` - Handle duplicates
45+
- `generate_k_permutations(arr, k)` - K-length permutations
46+
- `next_permutation(arr)` - Lexicographic next permutation
47+
- **Time Complexity:** O(n! * n)
48+
- **Features:** Multiple permutation algorithms with duplicate handling
49+
50+
### 2. **Advanced Graph Algorithms** (Extended Pathfinding)
51+
**Location:** `pygorithm/pathfinding/`
52+
53+
#### Bellman-Ford Algorithm (`bellman_ford.py`)
54+
- **Author:** Adwaita Jadhav
55+
- **Functions:**
56+
- `bellman_ford(graph, source)` - Single source shortest paths
57+
- `bellman_ford_with_path(graph, source, target)` - Path reconstruction
58+
- `detect_negative_cycle(graph)` - Negative cycle detection
59+
- **Time Complexity:** O(V * E)
60+
- **Features:** Handles negative weights, detects negative cycles
61+
62+
#### Floyd-Warshall Algorithm (`floyd_warshall.py`)
63+
- **Author:** Adwaita Jadhav
64+
- **Functions:**
65+
- `floyd_warshall(graph)` - All-pairs shortest paths
66+
- `get_shortest_path(source, target, next_matrix)` - Path reconstruction
67+
- `print_distance_matrix(distance_matrix)` - Formatted output
68+
- `find_diameter(distance_matrix)` - Graph diameter
69+
- **Time Complexity:** O(V^3)
70+
- **Features:** Complete all-pairs solution with path reconstruction
71+
72+
#### Prim's Algorithm (`prims_algorithm.py`)
73+
- **Author:** Adwaita Jadhav
74+
- **Functions:**
75+
- `prims_mst(graph, start_vertex)` - Minimum spanning tree
76+
- `prims_mst_adjacency_matrix(adj_matrix)` - Matrix-based version
77+
- `validate_mst(graph, mst_edges)` - MST validation
78+
- `is_connected_graph(graph)` - Connectivity check
79+
- **Time Complexity:** O(E log V)
80+
- **Features:** Priority queue implementation with validation
81+
82+
### 3. **Advanced String Algorithms** (Extended Strings)
83+
**Location:** `pygorithm/strings/`
84+
85+
#### KMP String Search (`kmp_search.py`)
86+
- **Author:** Adwaita Jadhav
87+
- **Functions:**
88+
- `kmp_search(text, pattern)` - Find all occurrences
89+
- `build_lps_array(pattern)` - Failure function construction
90+
- `kmp_search_overlapping(text, pattern)` - Overlapping matches
91+
- `kmp_replace(text, pattern, replacement)` - Pattern replacement
92+
- **Time Complexity:** O(n + m)
93+
- **Features:** Optimal string matching with LPS array visualization
94+
95+
#### Edit Distance (`edit_distance.py`)
96+
- **Author:** Adwaita Jadhav
97+
- **Functions:**
98+
- `edit_distance(str1, str2)` - Levenshtein distance
99+
- `edit_distance_with_operations(str1, str2)` - Operation sequence
100+
- `similarity_ratio(str1, str2)` - Similarity percentage
101+
- `is_one_edit_away(str1, str2)` - Single edit check
102+
- **Time Complexity:** O(m * n)
103+
- **Features:** Complete edit distance with operation tracking
104+
105+
### 4. **Advanced Sorting Algorithm** (Extended Sorting)
106+
**Location:** `pygorithm/sorting/`
107+
108+
#### Bingo Sort (`bingo_sort.py`)
109+
- **Author:** Adwaita Jadhav
110+
- **Functions:**
111+
- `sort(_list)` - Main bingo sort implementation
112+
- `bingo_sort_optimized(_list)` - Optimized version
113+
- `is_suitable_for_bingo_sort(_list)` - Suitability analysis
114+
- `analyze_efficiency(_list)` - Performance analysis
115+
- **Time Complexity:** O(n + k) best case, O(n^2) worst case
116+
- **Features:** Efficient for datasets with many duplicates
117+
118+
## Testing
119+
120+
All algorithms include comprehensive test coverage in `tests/test_backtracking.py`:
121+
- ✅ 10/10 tests passing
122+
- Unit tests for all major functions
123+
- Edge case handling
124+
- Performance validation
125+
126+
## Integration
127+
128+
All new algorithms are properly integrated:
129+
- Updated module `__init__.py` files
130+
- Added to main `pygorithm/__init__.py`
131+
- Consistent API patterns maintained
132+
- Documentation strings included
133+
- Time complexity information provided
134+
135+
## Usage Examples
136+
137+
### Backtracking
138+
```python
139+
from pygorithm.backtracking import n_queens, sudoku_solver
140+
141+
# Solve 8-Queens
142+
solutions = n_queens.solve_n_queens(8)
143+
print(f"Found {len(solutions)} solutions")
144+
145+
# Solve Sudoku
146+
board = [[5,3,0,0,7,0,0,0,0], ...] # 9x9 puzzle
147+
sudoku_solver.solve_sudoku(board)
148+
```
149+
150+
### Graph Algorithms
151+
```python
152+
from pygorithm.pathfinding import bellman_ford, floyd_warshall
153+
154+
# Single source shortest paths
155+
graph = {'A': [('B', -1), ('C', 4)], ...}
156+
distances, predecessors = bellman_ford.bellman_ford(graph, 'A')
157+
158+
# All pairs shortest paths
159+
distance_matrix, next_matrix = floyd_warshall.floyd_warshall(graph)
160+
```
161+
162+
### String Algorithms
163+
```python
164+
from pygorithm.strings import kmp_search, edit_distance
165+
166+
# Pattern matching
167+
matches = kmp_search.kmp_search("ABABCABABA", "ABAB")
168+
169+
# Edit distance
170+
distance = edit_distance.edit_distance("kitten", "sitting") # Returns 3
171+
```
172+
173+
## Educational Value
174+
175+
These additions significantly enhance Pygorithm's educational value by providing:
176+
- **Constraint Satisfaction:** Backtracking algorithms for complex problems
177+
- **Advanced Graph Theory:** Shortest paths and minimum spanning trees
178+
- **String Processing:** Efficient pattern matching and text analysis
179+
- **Specialized Sorting:** Algorithms optimized for specific data patterns
180+
181+
All implementations maintain the library's focus on education with clear documentation, time complexity analysis, and practical examples.

pygorithm/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@
7171
'searching',
7272
'sorting',
7373
'strings',
74-
'pathfinding'
74+
'pathfinding',
7575
'geometry',
76-
'greedy_algorithm'
76+
'greedy_algorithm',
77+
'backtracking'
7778
]

pygorithm/backtracking/__init__.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""
2+
Collection of backtracking algorithms
3+
"""
4+
from . import n_queens
5+
from . import sudoku_solver
6+
from . import maze_solver
7+
from . import permutations
8+
9+
__all__ = [
10+
'n_queens',
11+
'sudoku_solver',
12+
'maze_solver',
13+
'permutations'
14+
]

0 commit comments

Comments
 (0)