A comprehensive collection of Data Structures and Algorithms implemented in Python, designed for learning, interview preparation, and competitive programming.
- Overview
- Repository Structure
- Data Structures
- Algorithms
- Getting Started
- Usage Examples
- Contributing
- Resources
This repository contains clean, well-documented implementations of fundamental data structures and algorithms in Python. Each implementation includes:
- Clear explanations of concepts
- Time and space complexity analysis
- Visual representations where applicable
- Real-world use cases and examples
- Test cases and sample inputs/outputs
Perfect for:
- 📖 Learning DSA concepts
- 💼 Technical interview preparation
- 🏆 Competitive programming practice
- 🎓 Computer science students
- 👨💻 Software engineering interviews
DSA-with-Python/
├── Algorithms/
│ ├── fundamentals/
│ │ ├── heap/
│ │ │ └── min_heap_with_lib.py
│ │ ├── sorting/
│ │ ├── searching/
│ │ └── graph/
│ ├── dynamic_programming/
│ ├── greedy/
│ └── divide_conquer/
├── Data_Structures/
│ ├── linear/
│ │ ├── arrays/
│ │ ├── linked_lists/
│ │ ├── stacks/
│ │ └── queues/
│ ├── trees/
│ │ ├── binary_tree/
│ │ ├── bst/
│ │ └── avl_tree/
│ ├── graphs/
│ └── heaps/
├── Problems/
│ ├── leetcode/
│ ├── hackerrank/
│ └── practice/
├── ADVANCED_DATA_STRUCTURES.md
└── README.md
- Arrays & Lists - Dynamic arrays, operations, and applications
- Linked Lists - Singly, doubly, and circular linked lists
- Stacks - LIFO operations, expression evaluation
- Queues - FIFO operations, BFS implementation
- Deques - Double-ended queues
- Binary Trees - Tree traversals, operations
- Binary Search Trees - Search, insert, delete operations
- AVL Trees - Self-balancing binary search trees
- Heaps - Min/Max heaps, priority queues
- Graphs - Adjacency list/matrix, traversals
- Tries - Prefix trees for string operations
- Segment Trees - Range queries and updates
- Fenwick Trees - Binary indexed trees
- Disjoint Set Union - Union-find data structure
- Comparison-based: Quick Sort, Merge Sort, Heap Sort
- Non-comparison: Counting Sort, Radix Sort, Bucket Sort
- Hybrid: Tim Sort, Intro Sort
- Linear Search - Sequential search
- Binary Search - Divide and conquer search
- Interpolation Search - Improved binary search
- Exponential Search - Unbounded search
- Traversals: DFS, BFS
- Shortest Path: Dijkstra, Bellman-Ford, Floyd-Warshall
- Minimum Spanning Tree: Kruskal, Prim
- Topological Sort: DFS-based, Kahn's algorithm
- Classic Problems: Fibonacci, Knapsack, LCS, LIS
- Advanced: Matrix Chain Multiplication, Edit Distance
- Optimization: Memoization vs Tabulation
- Pattern Matching: KMP, Rabin-Karp, Boyer-Moore
- String Processing: Manacher's, Z-algorithm
- Python 3.7 or higher
- Basic understanding of Python programming
- Familiarity with object-oriented programming concepts
-
Clone the repository
git clone https://github.com/yourusername/DSA-with-Python.git cd DSA-with-Python -
Set up virtual environment (optional but recommended)
python -m venv dsa_env source dsa_env/bin/activate # On Windows: dsa_env\Scripts\activate
-
Install dependencies
pip install -r requirements.txt # If requirements file exists
# Run a specific data structure implementation
python Algorithms/fundamentals/heap/min_heap_with_lib.py
# Run sorting algorithms
python Algorithms/sorting/quick_sort.py
# Run graph algorithms
python Algorithms/graph/dijkstra.pyfrom Algorithms.fundamentals.heap.min_heap_with_lib import MinHeap
# Create a min heap
heap = MinHeap()
# Add elements
numbers = [5, 2, 8, 1, 9, 3]
for num in numbers:
heap.add(num)
print(heap) # Min Heap: [1, 2, 3, 5, 9, 8]
# Extract minimum
min_val = heap.print_and_delete()
print(f"Minimum value: {min_val}") # Minimum value: 1from Data_Structures.trees.bst import BST
# Create BST and insert values
bst = BST()
values = [50, 30, 70, 20, 40, 60, 80]
for val in values:
bst.insert(val)
# Search for a value
result = bst.search(40)
print(f"Found 40: {result}") # Found 40: Truefrom Algorithms.graph.dfs_bfs import Graph
# Create graph and add edges
graph = Graph()
graph.add_edge('A', 'B')
graph.add_edge('A', 'C')
graph.add_edge('B', 'D')
# Perform DFS
print("DFS traversal:")
graph.dfs('A')Contributions are welcome! Here's how you can help:
- Fork the repository
- Create a feature branch
git checkout -b feature/new-algorithm
- Add your implementation
- Follow the existing code style
- Include docstrings and comments
- Add test cases
- Update documentation if needed
- Commit your changes
git commit -m "Add: Implement [Algorithm/Data Structure name]" - Push to your branch
git push origin feature/new-algorithm
- Create a Pull Request
- Code Quality: Write clean, readable, and well-documented code
- Testing: Include test cases for your implementations
- Documentation: Add clear explanations and complexity analysis
- Naming: Use descriptive variable and function names
- Style: Follow PEP 8 Python style guidelines
- "Introduction to Algorithms" by Cormen, Leiserson, Rivest, and Stein
- "Algorithms" by Robert Sedgewick and Kevin Wayne
- "Data Structures and Algorithms in Python" by Goodrich, Tamassia, and Goldwasser
- LeetCode - Coding interview preparation
- HackerRank - Programming challenges
- GeeksforGeeks - CS fundamentals
- Visualgo - Algorithm visualizations
| Data Structure | Access | Search | Insertion | Deletion |
|---|---|---|---|---|
| Array | O(1) | O(n) | O(n) (shifting elements) | O(n) (shifting elements) |
| Singly Linked List | O(n) | O(n) | O(1)* (given a node or at head) | O(1)* (given the node to delete) |
| Stack (LIFO) | O(n) | O(n) | O(1) (push) | O(1) (pop) |
| Queue (FIFO) | O(n) | O(n) | O(1) (enqueue) | O(1) (dequeue) |
| Binary Tree | O(n) | O(n) | O(1)* (if you know where to insert) / O(n) to find spot | O(1)* (if you know the node) / O(n) to find it |
| BST (average) | O(log n) | O(log n) | O(log n) | O(log n) |
| BST (worst-case) | O(n) | O(n) | O(n) | O(n) |
| Hash Table (avg.) | O(1) | O(1) | O(1) | O(1) |
| Hash Table (worst) | O(n) | O(n) | O(n) | O(n) |
| Heap (min or max) | O(1) (peek root) | O(n) (arbitrary) | O(log n) | O(log n) (remove root) |
* Truly O(1) for a linked list or general binary tree only if you already have a direct reference to the node (or insertion point).
python data-structures algorithms interview-prep competitive-programming computer-science dsa coding-interview leetcode programming
This project is licensed under the MIT License - see the LICENSE file for details.
If this repository helped you, please give it a ⭐️!
Happy Coding! 🚀
Made with ❤️ for the programming community