Complete Notes
Complete Notes
3. Strings
4. Linked Lists
5. Stacks
6. Queues
7. Searching
8. Sorting
9. Recursion
10. Hashing
12. Trees
13. Graphs
Part 1 Page 1
Part 1 Page 2
1 - Introduction
Saturday, July 26, 2025 9:29 AM
• Importance of DSA
• Python Refresher
Part 1 Page 3
2. Arrays & Lists
Saturday, July 26, 2025 5:10 PM
• Arrays
• Lists
• Arrays vs Lists
• Complexity Analysis
• Coding Problems
Part 1 Page 4
3. Strings
Saturday, August 16, 2025 4:13 PM
• Introduction
• Properties
• Usecases
• Common Functions
• String Comparison
• Coding Problems
Part 1 Page 5
Part 1 Page 6
4. Linked Lists
Wednesday, August 27, 2025 1:55 PM
• Introduction
• Coding Problems
Part 1 Page 7
5. Stacks
Saturday, October 18, 2025 11:24 AM
• Introduction
• Common Operations
• Implementation
• Complexity Analysis
• Applications
• Coding Problems
Part 1 Page 8
6. Queues
Saturday, October 18, 2025 3:01 PM
• Introduction
• Common Operations
• Implementation
• Complexity Analysis
• Coding Problems
Part 1 Page 9
7. Searching
Wednesday, October 29, 2025 10:09 PM
• Linear Search
• Binary Search
• Exponential Search
Part 1 Page 10
8. Sorting
Wednesday, November 5, 2025 10:11 PM
• Introduction
• Bubble Sort
• Selection Sort
• Insertion Sort
• Merge Sort
• Quick Sort
• Counting Sort
• Complexity Analysis
Part 1 Page 11
9. Recursion
Sunday, November 23, 2025 5:51 PM
• Introduction to Recursion
• Common Mistakes
• Recursion vs Iteration
• Coding Problems
Part 1 Page 12
10 - Hashing
Wednesday, November 26, 2025 11:51 PM
• Introduction
• Hash Functions
• Hash Tables
• Load Factor
• Common Operations
• Complexity Analysis
• Coding Problems
Part 1 Page 13
Part 1 Page 14
11 - Problem Solving
Thursday, December 4, 2025 9:17 AM
• Sliding Window
• Two Pointers
• Prefix Sum
• Hashing
• Greedy Approach
Part 1 Page 15
Part 1 Page 16
12 - Trees
Saturday, December 20, 2025 12:09 AM
• Introduction
• Common Terminology
• Properties of Trees
• Traversal Algorithms
• N-ary Trees
• Coding Problems
Part 1 Page 17
Part 1 Page 18
12.1 - Introduction
Saturday, December 20, 2025 12:18 AM
What is a Tree?
Part 1 Page 19
Part 1 Page 20
12.2 - Terminology
Saturday, December 20, 2025 12:21 AM
Tree Terminology:
1. Root:
○ The topmost node of the tree
○ Has no parent
2. Parent:
○ A node that has one or more children
○ Immediate predecessor of a node
3. Child:
○ A node that is a direct descendant of another node
4. Leaf:
○ A node with no children
○ Also called external node
5. Depth:
○ Number of edges from the root to the node
○ Root always has depth = 0
○ Depth answers “how far am I from root?”
6. Height:
○ Longest path from a node to a leaf
○ Measured in edges
○ Height of a leaf node = 0
○ Height answers “how far can I go down?”
7. Level:
○ Is a mathematical representation
○ Depth + 1
8. Subtree:
○ Any node along with all its descendants
○ Every node forms a subtree
○ Crucial in recursion-based tree problems
Part 1 Page 21
Part 1 Page 22
12.3 - Properties
Saturday, December 20, 2025 12:20 AM
• Parent–Child Relationship:
○ Every node (except root) has exactly one parent
○ A node can have zero or more children
• No Cycles:
○ You can never return to a node once you move down the tree
○ This property distinguishes trees from graphs
• Connected Structure:
○ Every node is reachable from the root
Part 1 Page 23
Part 1 Page 24
12.4 - Traversal
Saturday, December 20, 2025 2:13 AM
• Tree traversal is the process of visiting every node of a tree exactly once in a systematic way
• Since a tree is non-linear, there is no single natural order like arrays or linked lists
• We define multiple traversal strategies depending on problem requirements
• Traversal answers:
○ How do we visit every node exactly once?
○ In what order should nodes be processed?
1. Depth First Search (DFS): uses recursion or stack; explores one branch completely before moving to another
○ Preorder
○ Inorder
○ Postorder
2. Breadth First Search (BFS): uses a queue; explores each node level by level
○ Level Order Traversal
Part 1 Page 25
Part 1 Page 26
12.4.1 - Preorder
Saturday, December 20, 2025 11:42 AM
Order of Traversal:
Intuition:
• Visit a node before its children
• Process current node, then go to left node, then right node
Usecases:
• Copying / cloning a tree
• Prefix expression evaluation: Operators appear before operands
• Serialize a tree: Convert a tree into a string or list
• Creating directory structures: Representing folders and files as a tree
Part 1 Page 27
Part 1 Page 28
12.4.2 - Inorder
Saturday, December 20, 2025 11:50 AM
Order of Traversal:
Intuition:
• Visit left child first
• Root comes in between
• Then visit right child
Use Cases:
• Validate a BST
• Retrieve sorted data from BST
• Expression trees (infix notation)
• Finding kth smallest / largest
Part 1 Page 29
Part 1 Page 30
12.4.3 - Postorder
Saturday, December 20, 2025 11:51 AM
Order of Traversal:
Intuition:
• Process children before parent
• Root is visited last
• Left node first, followed by right node
Use Cases:
• Deleting a tree
• Calculating height
• Postfix expression evaluation
• Bottom-up computations (DP on trees)
Part 1 Page 31
Part 1 Page 32
Part 1 Page 33
12.4.4 - Level Order
Saturday, December 20, 2025 11:52 AM
Order of Traversal:
• Visits nodes level by level
• Uses a queue (FIFO)
Intuition:
• Visit all nodes at depth 0
• Then depth 1
• Then depth 2
• Repeat until tree depth is reached
Use Cases:
• Level-based problems
• Tree views (top, bottom, left, right)
• Shortest distance in trees
• Checking tree completeness
Part 1 Page 34
• Checking tree completeness
• Zig-zag traversal
Part 1 Page 35
Part 1 Page 36
Part 1 Page 37
12.4.5 - Choose Traversal
Saturday, December 20, 2025 12:09 PM
Part 1 Page 38
Part 1 Page 39
12.5 - BST
Saturday, December 20, 2025 12:15 PM
Key Properties:
• Each node has zero, one, or two children
• All values in the left subtree are less than the node's value and all values in the right subtree are greater than the node's value
• Both, the left and right subtrees must also be Binary Search Trees themselves
Key Applications:
• Database Indexing
• Dictionary and Spell Checkers
• Dynamic Sorting
• Priority Queues
• 3D Graphics and Game Engines
Part 1 Page 40
Part 1 Page 41
12.5.1 - Insertion
Saturday, December 20, 2025 12:29 PM
Steps:
• Start from root
• If key < [Link] → go left
• If key > [Link] → go right
• Insert at first None position
Time Complexity:
• Average: O(log n)
• Worst case (skewed tree): O(n)
Part 1 Page 42
Part 1 Page 43
12.5.2 - Searching
Saturday, December 20, 2025 12:29 PM
Steps:
• Compare key with [Link]
• Go left or right accordingly
• Stop when found or reach None
Time Complexity:
• Average: O(log n)
• Worst case: O(n)
Part 1 Page 44
Part 1 Page 45
Part 1 Page 46
Part 1 Page 47
12.5.3 - Deletion
Saturday, December 20, 2025 12:29 PM
Part 1 Page 48
Part 1 Page 49
12.6 - N-ary Tree
Saturday, December 20, 2025 11:59 PM
Traversal:
• Preorder (Root → Children)
• Postorder (Children → Root)
• Level Order
Node Structure:
• children is a list
• No fixed left/right
• Order of children may or may not matter
Key Applications:
• File system (folders → many subfolders)
• Organization hierarchy
• XML / HTML DOM tree
• Trie (special N-ary tree)
Part 1 Page 50
Part 1 Page 51
Part 1 Page 52
Part 1 Page 53
Part 1 Page 54
12.6.1 - Traversals
Sunday, December 21, 2025 12:10 AM
1. Preorder:
○ Visit the root first
○ Then, visit all children, one at a time
○ Use cases:
□ Serialize tree
□ Copy tree
□ Prefix expressions
2. Postorder:
○ Visit the children first
○ Then, visit the root node
○ Use cases:
□ Deleting tree
□ Aggregation problems (sum, height, DP)
3. Level Order:
○ Natural level by level expansion
○ Used in hierarchy / shortest path problems
Part 1 Page 55
Part 1 Page 56
Part 1 Page 57
Part 1 Page 58
Part 1 Page 59
Part 1 Page 60
Part 1 Page 61
Part 1 Page 62
Part 1 Page 63
Part 1 Page 64
12.7 - Tree DP
Sunday, December 21, 2025 12:14 AM
Key Properties:
• No cycles → recursion can be used
• Each subtree is an independent problem
• Postorder traversal is the backbone of Tree DP
Tree DP Template:
Part 1 Page 65
Part 1 Page 66
12.8 - Coding Problems
Monday, December 22, 2025 12:21 AM
Part 1 Page 67
Part 1 Page 68
Part 1 Page 69
Part 1 Page 70
Part 1 Page 71
Part 1 Page 72
Part 1 Page 73
Part 1 Page 74
Part 1 Page 75
Part 1 Page 76
Part 1 Page 77
Part 1 Page 78
12.9 - DS Context
Saturday, December 27, 2025 11:52 AM
• Decision Trees:
○ These can be binary or multi-branch trees depending on problem statement
○ Each node results from a feature-value split
○ Each edge corresponds to an outcome/decision
○ Leaf nodes state the final prediction value
• Random Forests
• Hierarchical Clustering:
○ Forms a tree-like structure called a Dendrogram
○ Clusters of elements forms the nodes
○ Cutting the tree at a certain heights gives different clusters
Part 1 Page 79
○ Grammar-based NLP
○ Semantic analysis
○ Syntax-aware transformers
Part 1 Page 80
Part 1 Page 81
Part 1 Page 82
Part 1 Page 83
Part 1 Page 84
13 - Graphs
Sunday, December 28, 2025 12:21 AM
• Introduction
• Types of Graphs
• Degree of Graph
• Graph Connectivity
• Graph Representation
• Graph Traversal
• State-Space Graphs
• Coding Problems
Part 1 Page 85
Part 1 Page 86
13.1 - Introduction
Sunday, December 28, 2025 12:21 AM
What is a Graph?
• A graph is a non-linear data structure used to model relationships between entities
• Unlike arrays, linked lists, or trees, graphs do not impose a strict hierarchy or order
• Mathematically, a graph is defined as → G = (V, E)
○ V (Vertices / Nodes) → A finite set of points
○ E (Edges) → A set of connections between pairs of vertices
• Example:
○ V = {A, B, C, D}
○ E = {(A, B), (B, C), (C, D)}
Vertices (Nodes):
• Vertices are the fundamental units of the graph
• Vertices are also known as vertex or nodes
• Vertices represent entities in a system
• Can store values or labels
• Can have attributes (weight, color, metadata)
• Total number of vertices = |V|
Edges (Connections):
• Edges represent relationships between vertices
• Edges are used to connect two nodes of the graph
• Edges can connect any two nodes in any possible way, there are no rules
• Every edge can be labelled/unlabelled
• Edges are also known as arcs
Applications:
• Social Networks: Represent users and their connections; used to find mutual friends, suggest new connections, and detect communities
• Computer Networks: Model routers and data links; used for efficient routing, fault detection, and network optimization
• Transportation Networks: Represent cities and routes; used to find shortest or fastest paths and plan optimal travel routes
• Neural Networks: Represent neurons and synapses; used to simulate learning, brain behavior, and data processing
• Compilers: Represent data dependencies and control flows; used for optimization, register allocation, and code analysis
• Robot Path Planning: Represent states and transitions; used to compute the safest or shortest route for autonomous movement
• Project Dependencies: Represent tasks and dependencies; used in topological sorting to determine the correct execution order
• Network Optimization: Represent network nodes and links; used to minimize cost, reduce latency, and improve efficiency
Part 1 Page 87
• Network Optimization: Represent network nodes and links; used to minimize cost, reduce latency, and improve efficiency
Advantages of Graphs:
• Flexibility: Unlike arrays, linked lists, or trees, graphs have no restrictions and can represent any type of relationship
• Model real-world problems: Useful for pathfinding, data clustering, network analysis, and machine learning
• Represent items and relationships: Any set of items and their connections can be modeled as a graph
• Simplifies complex data: Graphs make complex relationships easy to visualize and understand
Part 1 Page 88
Part 1 Page 89
13.2 - Types of Graphs
Sunday, December 28, 2025 10:05 AM
1. Undirected Graph:
• A graph in which edges do not have any direction
• Nodes are unordered pairs in the definition of every edge
• Edges in an undirected graph are bidirectional in nature
• There is no concept of a "parent" or "child" vertex as there is no direction to the edges
• An undirected graph may contain loops, which are edges that connect a vertex to itself
2. Directed Graph:
• A graph in which edges have direction
• One-way relationship between vertices
• A directed graph can contain cycles
• Paths in a directed graph follow the direction of the edges
3. Weighted Graph:
• A weighted graph is one where the edges are assigned some weights
• Weights can represent cost, distance, or any other relative measuring unit
Part 1 Page 90
4. Unweighted Graph:
• Graph in which the edges do not have weights or costs associated with them
• Simply represent the presence of a connection between two vertices
5. Cyclic Graph:
• A cyclic graph contains one or more cycles or closed paths
• A cyclic graph can be either directed or undirected
○ Directed Cyclic Graph: edges have a direction, and the cycle must follow the direction of the edges
○ Undirected Cyclic Graph: edges have no direction, and the cycle can go in any direction
• A cyclic graph may have multiple cycles of different lengths and shapes; some cycles may be contained within other cycles
Part 1 Page 91
6. Acyclic Graph:
• An acyclic graph is a graph that contains no cycles or closed loops
7. Complete Graph:
• A complete graph is a graph in which every node is adjacent to every other node
• A complete graph has nC2 = n(n-1)/2 edges
Part 1 Page 92
8. Incomplete Graph:
• An incomplete graph is a graph in which every node is not adjacent to every other node
Part 1 Page 93
Part 1 Page 94
Part 1 Page 95
Part 1 Page 96
Part 1 Page 97
Part 1 Page 98
Part 1 Page 99
Part 1 Page 100
13.3 - Degree
Sunday, December 28, 2025 10:14 AM
Degree Sequence:
• A degree sequence is a list of degrees of all vertices in a graph
• Usually written in descending order (largest to smallest)
1. Connected Graph:
• A graph is connected, if every vertex is reachable from every other vertex
• There is at least one path between any two vertices in the graph
• The path between vertices can be direct or indirect
2. Disconnected Graph:
• A graph is disconnected if at least one pair of vertices cannot reach each other
• This occurs when the graph is split into separate parts with no edges between them
• Adjacency Matrix
• Adjacency List
• An adjacency matrix is a way of representing a graph as a boolean matrix of (0's and 1's)
• The matrix represents the mapping between various edges and vertices
• The order of the matrix is given as n*n where n is the number of nodes in the graph
• In the matrix, each row and column represents a vertex and the values determine the presence of edges
• The representation varies for directed and undirected graphs, despite similar structure
What is DFS?
• Graph traversal algorithm that explores as deep as possible along one path before backtracking
• Starts from a source vertex, visits a neighbor, then a neighbor’s neighbor, and so on, until no unvisited
neighbors remain; then it backtracks
• DFS works best with Adjacency List
Intuition:
• Exploring a maze
• Going deep into one corridor before trying others
Algorithm:
1. Create a visited set/array
2. Start DFS from a source node
3. Mark the current node as visited
4. For each adjacent node:
○ If not visited → perform DFS on it
What is BFS?
• Graph traversal algorithm that explores all neighbors of a node first, then their neighbors
• Traversal happens level by level starting from a source node
• Involves a Queue (FIFO), which ensures level-order traversal
• Ideal for shortest path problems in unweighted graphs
Algorithm:
1. Start from a source node
2. Mark it as visited
3. Push it into a queue
4. While the queue is not empty:
○ Dequeue a node
○ Visit all its unvisited neighbors
○ Mark them visited and enqueue them
Algorithm:
1. Identify all source nodes
2. Add all sources to queue
3. Mark all sources visited
4. Run normal BFS
Intuition:
• Use a deque
• For each edge:
○ Weight 0 → push to front
○ Weight 1 → push to back
• This keeps nodes with lower cost processed first
• Dijkstra’s Algorithm is a shortest path algorithm used in graphs to find the minimum distance from a single source vertex to all other vertices
• It is one of the most important algorithms in DSA and graphs, especially for weighted graphs
• Does not work with negative weights
• Very fast for large and sparse graphs
• Helps compute the shortest path distance from the source to every other node, given:
○ Graph
○ Source node
Key Conditions:
• Graph can be directed or undirected
• Graph must have non-negative edge weights ( >= 0 )
Algorithm:
1. Set distance of all nodes = ∞
2. Set source distance = 0
3. Push (0, source) into min-heap
4. Pop smallest distance node
5. Relax its neighbors
6. Repeat until heap is empty
Complexity Analysis:
• Time: O((V + E) log V)
• Space: O(V)
• Bellman–Ford Algorithm is a single-source shortest path algorithm used in graphs that can handle negative edge weights
• It is especially important because it can also detect negative weight cycles, which Dijkstra’s algorithm cannot do
• Generally, its slower than Dijkstra's algorithm
• The shortest distance from the source to all other vertices, even when negative edges are present, given:
○ Graph
○ Source node
Key Features:
• Works with negative edge weights
• Works for directed and undirected graphs
• Can detect negative cycles
Algorithm:
• Initialize:
○ Distance of source = 0
○ Distance of all other vertices = ∞
• Repeat V−1 times:
○ For every edge (u → v, weight w)
• Negative cycle check:
• Do one more relaxation
• If any distance still reduces → negative cycle exists
Complexity Analysis:
• Time: O(V * E)
• Space: O(V)
• Floyd–Warshall Algorithm is an all-pairs shortest path algorithm used in graphs to find the shortest distance between every pair of vertices
• It is especially useful for dense graphs and graphs with negative edge weights (but no negative cycles)
• Works best when the graph is small or dense
• Works even if negative edges exist
Key Features:
• Computes all-pairs shortest paths
• Works with negative edge weights
Intuition: Try every vertex as an intermediate point between every pair of vertices
• Try every vertex as an intermediate point between every pair of vertices
Algorithm:
• Create a Distance Matrix:
○ dist[i][j] = weight(i → j)
○ 0 if i == j
○ ∞ if no edge exists
Complexity Analysis:
• Time: O(V * V * V)
• Space: O(V * V)
• Prim's MST
What is DSU?
• DSU is a data structure that keeps track of connected components dynamically
• Used when:
○ Graph edges are added gradually
○ Fast cycle detection
○ Check connectivity
Key Operations:
• Union
• Find
What is MST?
A spanning tree that:
• Connects all vertices
• Has no cycles
• Has minimum total edge weight
Intuition:
• Grow the MST one vertex at a time, always choosing the cheapest edge that connects:
○ a vertex inside the MST
○ to a vertex outside the MST
• Analogy: Expanding a network step-by-step, always picking the lowest-cost connection available
Algorithm:
• Pick any start vertex
• Mark it as visited (part of MST)
• Push all its edges into a min-heap (priority queue)
• Loop:
○ Extract the minimum weight edge
○ If the destination vertex is not visited:
▪ Add edge to MST
▪ Mark vertex as visited
▪ Push its edges into heap
▪ Stop when MST has V − 1 edges
Algorithm:
1. Sort edges by weight
2. Pick smallest edge that does not form a cycle
3. Stop after V - 1 edges
Note: Cycle detection is done using DSU
1. Initial State:
○ The position where the problem starts
2. State:
○ A state captures all information needed to continue the problem
3. Actions / Transitions:
○ Valid moves from one state to another
○ Move Up, Down, Left, Right, etc.
4. Goal State:
The state that solves the problem
Part 1 Page 167
○ The state that solves the problem
5. Constraints:
○ Rules that decide whether a state is valid or invalid
Complexity Analysis:
Real-World Applications:
• AI Search Problems
• Game Solvers (Chess, Sudoku)
• Robot Navigation
• Path Planning
• Compiler optimization
What is a DAG?
A Directed Acyclic Graph (DAG) is a graph which is:
1. Directed: edges have direction
2. Acyclic: no cycles
• Dependency Resolution:
○ Used in software development
○ Docker image layers
• Kahn's Algorithm finds a topological order of a DAG by repeatedly removing nodes with no dependencies
• If this process cannot remove all nodes, the graph contains a cycle
Topological Ordering:
• Linear arrangement of vertices in a DAG where for every directed edge from vertex u to v, u always appears before v in the list
• This shows dependency order like task prerequisites or build processes
• Allows representing workflows, ensuring that a task is listed only after its dependencies are met
Main Ideas:
1. Compute in-degree of every node
2. Push nodes with in-degree = 0 into queue
3. Remove them and reduce neighbors’ in-degree
Conclusions:
• If at end, all nodes processed → DAG
• Else → cycle exists
Algorithm:
• Find all nodes with in-degree = 0
• Put them in a queue
• Process nodes level by level (BFS)
• Reduce in-degree of neighbors
• Add new zero in-degree nodes to queue
Properties:
Algorithm:
2. Initialize Distances:
○ Set distance of source = 0
○ Set all other nodes = ∞
Properties:
Properties:
• Only works correctly for DAG
• Uses topological sorting
• Each node’s path count is finalized once
• Linear time complexity
• Key Idea: Number of ways to reach a node = sum of ways to reach its parents
Algorithm:
Idea:
• Assign a color (0 or 1) to a node
• All its neighbors must get the opposite color
• If a conflict appears → not bipartite
Safe Node:
○ Starting from that node, every possible path eventually ends at a terminal node
○ A terminal node has no outgoing edges
Unsafe Node:
• A node that is part of a cycle
• Can reach a cycle
1. Recommendation Systems:
Graph Modeling
• Nodes: Users, Items
• Edges: Click, view, purchase, rating
ML Tasks
• Link prediction
• Node embeddings
• Personalized ranking
Algorithms Used
• Random Walks
• Node2Vec
• Graph Neural Networks (GNNs)
Real-World Examples
• Netflix movie recommendations
• Amazon product suggestions
• Spotify song recommendations
Graph Modeling
• Nodes: Accounts, cards, users
• Edges: Transactions, transfers
ML Tasks
• Node classification (fraud / non-fraud)
Industry Examples
• Banking fraud
• Insurance fraud
• Money laundering detection
Graph Modeling
• Nodes: Users
• Edges: Follow, friend, message
ML Tasks
• Community detection
• Influence maximization
• Link prediction
Algorithms
• PageRank
• Louvain clustering
• Graph embeddings
Examples
• Facebook friend suggestions
• Twitter (X) influencer ranking
• LinkedIn connection recommendations
Graph Modeling
Part 1 Page 221
Graph Modeling
• Nodes: Web pages
• Edges: Hyperlinks
ML Tasks
• Page ranking
• Authority detection
Algorithms
• PageRank
• HITS
• GNN-based ranking
Real-World Example
• Google search ranking
Graph Modeling
• Nodes: Proteins, genes, drugs
• Edges: Interactions, reactions
ML Tasks
• Graph classification
• Link prediction
• Node classification
Use Cases
• Drug discovery
• Protein interaction prediction
• Disease pathway analysis
6. Computer Vision:
Part 1 Page 222
6. Computer Vision:
Graph Modeling
• Nodes: Objects in image
• Edges: Spatial relationships
ML Tasks
• Scene graph generation
• Object interaction detection
Examples
• Autonomous driving
• Image captioning
7. NLP:
Graph Modeling
• Nodes: Words, entities, documents
• Edges: Co-occurrence, dependency
ML Tasks
• Document classification
• Entity linking
• Semantic similarity
Examples
• Knowledge-based QA
• Document clustering
1. Mathematical Foundations
○ Discrete Mathematics
○ Recurrences & Growth Functions
○ Probability & Expected Value
2. Trees
○ Binary Search Tree
○ Tree DP
○ N-ary Tree
3. Graphs
○ Graph Theory
○ Traversal Algorithms
○ Shortest Path Algorithms
○ Topological Sorting & DAG Problems
○ Disjoint Set Union
4. Heaps
○ Mathematical Foundation
○ Implementation
6. Tries
○ Foundations
○ Advanced Problems
7. Backtracking
○ Theory
○ Classic Problems
○ Advanced Problems & Optimizations