📚 DSA Unit 3 - Premium Master Notes

Graph Traversal • Shortest Paths • Dynamic Programming

Compiled & Formatted by Ankush Raj

📘 Chapter 3.1 — Graph Traversal in Data Structures
🔹 1. Meaning of Graph Traversal

Graph traversal means visiting all vertices and edges of a graph in a systematic order. Useful for:

✔ Searching
✔ Path finding
✔ Detecting cycles
✔ Connected components
✔ Shortest paths
✔ MST building
🔹 2. Types of Traversal
(A) BFS — Breadth-First Search

Explores graph level by level.

• Uses Queue (FIFO)
• Good for unweighted shortest path

Where used?

🌐 Web crawling
📡 Network broadcasting
👥 Social media friend suggestions

Steps:

1. Start at source
2. Mark visited, push to queue
3. Pop → visit neighbours
4. Continue until queue empty
(B) DFS — Depth-First Search

Explores deep path first, then backtracks.

• Uses Stack / Recursion

Where used?

🔍 Cycle detection
🔗 Connected components
📐 Topological sorting
🧩 Maze solving

Steps:

1. Visit node
2. Recur for each unvisited neighbour
3. Backtrack when no neighbour left
🔹 3. BFS vs DFS
Feature BFS DFS
Data Structure Queue Stack/Recursion
Exploration Level-wise Depth-wise
Shortest Path? ✔ Yes ❌ No
Applications Unweighted shortest path Cycle detect, topo sort
📘 Chapter 3.2 — Shortest Paths & MST
🔹 1. Dijkstra's Algorithm (Greedy)

Finds shortest path from one source to every vertex in graphs with non-negative weights.

⭐ Core Concepts
• Maintains distance array
• Uses min-heap (priority queue)
• Always picks vertex with minimum distance
⭐ Steps
1. dist[source] = 0, others = ∞
2. Insert source in min-heap
3. Extract minimum
4. Relax all edges
5. Update distances
6. Continue till heap empty
⭐ Applications
🗺 Google Maps
🔌 Network routing (OSPF)
🤖 Robotics path planning
🔹 2. Huffman Coding

A lossless compression technique using variable-length prefix codes.

⭐ Key Points
• High frequency → short code
• Low frequency → long code
• Uses min-heap & binary tree
⭐ Used in
📦 ZIP files
🎵 MP3, JPEG
📄 Text compression
🔹 3. Minimum Spanning Tree (MST)

A spanning tree that connects all nodes with minimum total edge weight, no cycles.

🌟 Applications
📡 Network design
🚉 Transport routes
📊 Clustering
⭐ Prim's Algorithm
• Vertex-based greedy method
• Grows MST from any starting node
• Selects minimum outgoing edge
⭐ Kruskal's Algorithm
• Edge-based greedy
• Sort edges by weight
• Uses Union-Find to avoid cycles
⭐ Comparison
Feature Prim's Kruskal's
Method Vertex-based Edge-based
Best For Dense graphs Sparse graphs
Cycle Checking Not needed Required
📘 Chapter 3.3 — Dynamic Programming (DP)
🔹 1. Introduction

DP solves problems by breaking them into overlapping subproblems and storing answers to avoid repetition.

Used when:

✔ Optimal substructure
✔ Overlapping subproblems
🔹 2. DP Concepts
⭐ Optimal Substructure

Big problem → built from small optimal solutions.

Example: Fibonacci, knapsack.
⭐ Overlapping Subproblems

Same subproblems repeat many times. DP stores them in table.

🔹 3. DP Approaches
(A) Top-Down (Memoization)
• Recursive
• Stores subproblem answers
(B) Bottom-Up (Tabulation)
• Iterative
• Fills table starting from smallest cases
• Faster, no recursion overhead
🔹 4. Steps to Build DP
1. Define subproblems
2. Write recurrence relation
3. Choose top-down / bottom-up
4. Create DP table
5. Compute final answer
🔹 5. Major Applications of DP
⭐ Fibonacci

F(n) = F(n-1) + F(n-2), stored in table.

⭐ 0/1 Knapsack

Max profit under weight limit. Used in: resource allocation, investments.

⭐ Longest Common Subsequence (LCS)

For DNA comparison, plagiarism check.

⭐ Matrix Chain Multiplication

Minimizes multiplications.

⭐ Coin Change

Minimum coins or number of ways.

⭐ Longest Increasing Subsequence (LIS)

Used in stock prediction, rankings.

⭐ Edit Distance

Insert/Delete/Replace operations. Used in spell check, NLP.

✅ 📌 SHORT REVISION (Quick Recap)
🚀 Graph Traversal
BFS → Queue → Level-wise → Unweighted shortest path
DFS → Stack/Recursion → Depth-wise → Cycle detection
🛣 Shortest Path & MST
Dijkstra → Non-negative weights → Min-heap
Huffman → Compression → Prefix codes
Prim → Vertex-based; Kruskal → Edge-based (Union-Find)
🧠 Dynamic Programming
Overlapping + Optimal substructure
Approaches: Memoization & Tabulation
Important Problems: Knapsack, LCS, LIS, Coin change, Edit distance