Skip to content

Latest commit

 

History

History
466 lines (361 loc) · 23.8 KB

File metadata and controls

466 lines (361 loc) · 23.8 KB

🚀 Data Structures & Algorithms in Python

Your Complete DSA Journey Starts Here

Python License Contributions Welcome PRs Welcome

A comprehensive repository for mastering Data Structures & Algorithms through clean, well-documented Python implementations.

Perfect for: Coding Interviews • Competitive Programming • Learning DSA • Interview Preparation


📚 Table of Contents

🎯 Overview

Welcome to the ultimate Python DSA repository! Whether you're preparing for technical interviews at top tech companies or simply want to strengthen your algorithmic thinking, this collection has everything you need.

✨ Key Features

Clean, Production-Ready Code
Well-commented and easy to understand
🎓 Multiple Solution Approaches
From brute force to optimal solutions
Complexity Analysis
Time and space complexity for each solution
🧪 Test Cases Included
Comprehensive test coverage
📝 Real Interview Problems
From LeetCode, HackerRank, and more
🔄 Regular Updates
Continuously adding new problems and solutions

📂 Repository Structure

Click to expand/collapse directory tree
📦 DSA Python
┣━━ 📁 Arrays
┃   ┣━━ 📁 Binary_search          # Binary search variations & problems
┃   ┣━━ 📁 Problems               # Array manipulation & algorithms
┃   ┗━━ 📁 Subarrays Problems     # Kadane's, Sliding Window, etc.
┃
┣━━ 📁 Binary Trees               # Tree traversals & operations
┃
┣━━ 📁 Bitwise                    # Bit manipulation techniques
┃
┣━━ 📁 Builtin data_structures    # Python's native data structures
┃
┣━━ 📁 Dynamic Programming
┃   ┣━━ 📁 Starting               # DP Basics (Fibonacci, Stairs, etc.)
┃   ┣━━ 📁 Dp On Grids           # 2D Grid Problems
┃   ┣━━ 📁 Dp on strings         # LCS, LPS, Edit Distance
┃   ┗━━ 📁 Subsets and Subsequences  # Knapsack, Coin Change
┃
┣━━ 📁 Graphs
┃   ┣━━ 📁 Graphs Cycles and making  # Graph creation & cycle detection
┃   ┣━━ 📁 MST s                  # Prim's & Kruskal's algorithms
┃   ┣━━ 📁 Path Algos            # Dijkstra, Bellman-Ford, Floyd-Warshall
┃   ┗━━ 📁 Problems              # Graph problem solutions
┃
┣━━ 📁 Linked_List               # LL implementations & problems
┃
┣━━ 📁 Main Notes for syntax     # Python syntax cheat sheets
┃
┣━━ 📁 Recursions and Backtracking
┃   ┣━━ 📁 2D Arrays problems    # Matrix recursion
┃   ┣━━ 📁 Merge sort            # Divide & conquer
┃   ┣━━ 📁 Permutations          # Permutation generation
┃   ┗━━ 📁 Subsequences          # Subsequence generation
┃
┣━━ 📁 Stack & Queues            # Stack/Queue implementations
┃
┣━━ 📁 String Problems           # String algorithms
┃
┗━━ 📁 Trie                      # Trie data structure

🔥 Topics Covered

📊 Arrays & Searching
  • ✨ Binary Search (iterative & recursive)
  • 🎯 Lower/Upper Bound
  • 🔄 Search in Rotated Array
  • ⛰️ Peak Element
  • 🧮 Array manipulation problems
  • 📐 Subarray problems (Kadane's, Sliding Window)
💎 Dynamic Programming

Fundamentals

  • 🌟 Fibonacci Numbers
  • 🪜 Climbing Stairs
  • 🏠 House Robber

Grid Problems

  • 🗺️ Unique Paths
  • 🛤️ Minimum Path Sum
  • 🔺 Triangle Problems

String Problems

  • 📝 Longest Common Subsequence (LCS)
  • 🔄 Longest Palindrome Substring
  • ✏️ Edit Distance

Knapsack Problems

  • 🎒 0/1 Knapsack
  • ♾️ Unbounded Knapsack
  • 🪙 Coin Change (Min coins & Number of ways)
🕸️ Graphs
  • 🗂️ Graph Representations (Adjacency List/Matrix)
  • 🔍 BFS & DFS Traversals
  • 🔄 Cycle Detection
  • 📍 Shortest Path: Dijkstra, Bellman-Ford, Floyd-Warshall
  • 🌲 Minimum Spanning Tree: Prim's, Kruskal's
  • 📊 Topological Sort
🌳 Trees
  • 🌿 Binary Tree Traversals (Preorder, Inorder, Postorder)
  • 📊 Level Order Traversal
  • 🔨 Tree Construction & Manipulation
🔗 Linked Lists
  • ⛓️ Singly Linked List
  • ➕ Insert, ➖ Delete, 🔄 Reverse
  • 🎯 Common Patterns & Problems
📚 Stack & Queues
  • 📦 Stack Implementation (arrays, linked list, queues)
  • 🎫 Queue Implementation (arrays, linked list, stacks)
  • 📉 Min Stack / Min Queue
  • 🧩 Common Problems
🔁 Recursion & Backtracking
  • 🔀 Permutations
  • 📋 Subsequences
  • 🔄 Merge Sort
  • 👑 N-Queens
  • 🎲 Sudoku Solver
🔤 String Algorithms
  • ✂️ String Manipulation Methods
  • 🔎 Pattern Matching
  • 🧵 Common String Algorithms
💻 Bitwise Operations
  • ⚡ Bit Manipulation Techniques
  • ⊕ XOR Problems
  • 🔢 Power of 2 Checks
🌲 Trie
  • 📖 Trie Implementation
  • 🔍 Prefix-based Searching

🚀 Getting Started

📋 Prerequisites

✓ Python 3.7 or higher
✓ Basic understanding of Python syntax
✓ Familiarity with data structures concepts

⚙️ Installation

Step 1: Clone the repository

git clone <repository-url>
cd "DSA Python"

Step 2: Create a virtual environment (recommended)

python -m venv .venv

Step 3: Activate the virtual environment

# Windows (PowerShell)
.venv\Scripts\Activate.ps1

# Windows (CMD)
.venv\Scripts\activate.bat

# macOS/Linux
source .venv/bin/activate

Step 4: You're ready to code! 🎉

💻 Usage Examples

🔍 Example 1: Binary Search

from Arrays.Binary_search.binary_search import binary_search

# Search in a sorted array
arr = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
target = 7

result = binary_search(arr, target)
print(f"✓ Element {target} found at index: {result}")
# Output: ✓ Element 7 found at index: 3

🧮 Example 2: Dynamic Programming - Fibonacci

from Dynamic_Programming.Starting.fibonacci_number import (
    fibonacci_with_tabulation,
    fibonacci_most_optimized
)

# Calculate 10th Fibonacci number
n = 10

# Method 1: Tabulation (Bottom-up DP)
result1 = fibonacci_with_tabulation(n)
print(f"📊 10th Fibonacci (Tabulation): {result1}")

# Method 2: Space Optimized
result2 = fibonacci_most_optimized(n)
print(f"⚡ 10th Fibonacci (Optimized): {result2}")

🌲 Example 3: Binary Tree Traversal

from Binary_Trees.binary_tree import BinaryTree

# Create a binary tree
tree = BinaryTree(1)
tree.root.left = Node(2)
tree.root.right = Node(3)
tree.root.left.left = Node(4)
tree.root.left.right = Node(5)

# Perform different traversals
print("Preorder:", tree.print_tree("preorder"))
print("Inorder:", tree.print_tree("inorder"))
print("Postorder:", tree.print_tree("postorder"))

🕸️ Example 4: Graph Traversal

from Graphs.graph_traversals import bfs, dfs

# Create an adjacency list
graph = {
    0: [1, 2],
    1: [0, 3, 4],
    2: [0, 4],
    3: [1],
    4: [1, 2]
}

# Breadth-First Search
print("BFS:", bfs(graph, 0))

# Depth-First Search
print("DFS:", dfs(graph, 0))

📊 Complexity Analysis

Each implementation includes detailed complexity analysis:

Algorithm Time Complexity Space Complexity
Binary Search O(log n) O(1)
Merge Sort O(n log n) O(n)
Quick Sort O(n log n) avg O(log n)
DFS/BFS O(V + E) O(V)
Dijkstra O((V + E) log V) O(V)
Dynamic Programming Varies O(n) to O(n²)

🤝 Contributing

We love contributions! 💕 Help us make this repository even better!

How to Contribute
  1. 🍴 Fork the repository
  2. 🌿 Create a new branch
    git checkout -b feature/amazing-algorithm
  3. ✨ Make your changes
  4. 💾 Commit your changes
    git commit -m '✨ Add amazing algorithm'
  5. 🚀 Push to the branch
    git push origin feature/amazing-algorithm
  6. 🎉 Open a Pull Request

🌟 What Can You Contribute?

  • 🆕 New algorithm implementations
  • 🐛 Bug fixes
  • 📚 Documentation improvements
  • ✅ More test cases
  • 🎨 Code optimization
  • 💡 Better explanations

📖 Learning Resources

LeetCode
Practice Problems
GFG
Tutorials & Theory
Striver
Interview Prep
NeetCode
Curated Problems

📚 Recommended Books

  • 📕 "Introduction to Algorithms" by CLRS
  • 📗 "Cracking the Coding Interview" by Gayle Laakmann McDowell
  • 📘 "Elements of Programming Interviews in Python"
  • 📙 "Algorithm Design Manual" by Steven Skiena

📄 License

This project is open source and available under the MIT License.


⭐ Support This Project

If you find this repository helpful, please give it a star!
It helps others discover this resource and motivates us to keep improving!

GitHub stars


🌈 Made with ❤️ for the coding community

Happy Coding! Keep Learning! Stay Awesome! 🚀

Built with Love Made with Python