








Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!









Oct 30, 2026
These notes cover the core units of Design and Analysis of Algorithms (DAA) in the order most university syllabi follow. Each unit has simple definitions, algorithm steps, worked examples, time complexity and important points to remember.
n = size of the input V = number of vertices, E = number of edges (in graphs) W = capacity of the knapsack T(n) = time taken for input of size n
Learn the definition and the steps of each algorithm first. Practise each algorithm on a small example by hand, because most long questions ask for a traced example. Revise the cheat sheet table at the end just before the exam. The question pattern differs between universities, so match these notes with your own syllabus.
Unit 1: Algorithm Basics and Asymptotic Analysis
An algorithm is a finite sequence of well-defined steps that solves a problem. Its efficiency is judged by how running time (time complexity) and memory (space complexity) grow as the input size n grows.
Properties of an algorithm: input, output, definiteness (each step is clear), finiteness (it ends) and effectiveness (each step is doable).
Order of growth, slowest to fastest:
1 < \log n < \sqrt{n} < n < n \log n < n^2 < n^3 < 2^n < n!
Example: f(n) = 3n² + 2n + 5. Choose c = 4 and n0 = 4, then f(n) ≤ 4n², so f(n) = O(n²). Since f(n) ≥ 3n², it is also Ω(n²), hence Θ(n²).
Best case: least time on any input of size n (linear search: target at position 1, so 1 step). Worst case: most time on any input of size n (linear search: target absent, so n steps). Average case: expected time over all inputs, about n/2 steps for linear search.
Recursive algorithms give a recurrence such as T(n) = 2T(n/2) + n. Three methods are standard.
Notation Meaning Reads as O(g(n)) Upper bound: f(n) ≤ c·g(n) for all n ≥ n0 Worst-case growth is at most g(n) Ω(g(n)) Lower bound: f(n) ≥ c·g(n) for all n ≥ n0 Growth is at least g(n) Θ(g(n)) Tight bound: c1·g(n) ≤ f(n) ≤ c2·g(n) Growth is exactly g(n) o(g(n)) Strict upper bound, not tight Grows strictly slower than g(n)
Trace: [38, 27, 43, 3] splits to [38, 27] and [43, 3], then to single elements. Merging gives [27, 38] and [3, 43], then merging those gives [3, 27, 38, 43].
Quick sort is in place (O(log n) stack space) and not stable. Choosing a random pivot or the median of three makes the worst case unlikely.
The normal method multiplies two n×n matrices in Θ(n³) time using 8 multiplications of n/2 × n/2 blocks. Strassen reduces this to 7 block multiplications using extra additions.
T(n) = 7T(n/2) + \Theta(n^2) ;\Rightarrow; T(n) = \Theta(n^{\log_2 7}) \approx \Theta(n^{2.81})
Case When Recurrence Time Best Pivot splits the array evenly T(n) = 2T(n/2) + n
Θ(n log n) Average Random-like splits Θ(n log n) Worst Already sorted input with first or last element as pivot
T(n) = T(n−1) + n Θ(n²)
Point Merge sort Quick sort Worst-case time Θ(n log n) Θ(n²) Extra space O(n) O(log n) Stable Yes No Practical speed Slower constant factor Usually faster on arrays
Finding maximum and minimum: pairing elements needs about 3n/2 − 2 comparisons instead of 2n − 2. Counting inversions and the closest pair of points also use this method with Θ(n log n) time.
Unit 3: Greedy Method
A greedy algorithm builds a solution step by step, always taking the choice that looks best right now and never undoing it. It works when the problem has the greedy choice property (a locally best choice leads to a global optimum) and optimal substructure.
Time: Θ(n log n) because of sorting. Greedy gives the optimum here, but it does not for 0/ knapsack.
Example: capacity 50; items (value, weight) = (60, 10), (100, 20), (120, 30). Ratios are 6, 5 and 4. Take items 1 and 2 (weight 30, value 160), then 20/30 of item 3 (value 80). Total value = 240.
Each job has a deadline and a profit and takes one time unit. Maximise total profit.
Time: O(n²) with a simple slot search, O(n log n) with a disjoint-set structure.
Builds an optimal prefix code: frequent characters get short codes.
Time: O(n log n). No code is a prefix of another, so decoding is unambiguous.
Two styles: top-down (recursion with memoization) and bottom-up (fill the table iteratively).
Each item is taken whole or not at all. Let K[i][w] be the best value using the first i items with capacity w.
K[i][w] = \max\bigl(K[i-1][w],; v_i + K[i-1][w - w_i]\bigr) \quad \text{if } w_i \le w, \text{ else } K[i-1][w]
Time and space: Θ(nW). This is pseudo-polynomial because it depends on the value of W, not only on the input length.
For strings X and Y, let L[i][j] be the LCS length of the first i characters of X and the first j of Y.
L[i][j] = \begin{cases} 0 & i = 0 \text{ or } j = 0 \ L[i-1][j-1] + 1 & x_i = y_j \ \max(L[i-1][j],, L[i][j-1]) & x_i \ne y_j \end{cases}
Time and space: Θ(mn) for strings of length m and n. Trace back from L[m][n] to read the subsequence.
Example: X = ABCBDAB and Y = BDCABA have LCS length 4 (for instance BCBA).
Matrix multiplication is associative, so the order of bracketing changes the cost but not the answer. Given dimensions p0, p1, ..., pn, find the cheapest order.
M[i][j] = \min_{i \le k < j} \bigl( M[i][k] + M[k+1][j] + p_{i-1}, p_k, p_j \bigr), \quad M[i][i] = 0
Time: Θ(n³). Space: Θ(n²).
Example: for A(10×30), B(30×5), C(5×60): (AB)C costs 10·30·5 + 10·5·60 = 4500, while A(BC) costs 30·5·60 + 10·30·60 = 27000. The first order is far cheaper.
Works with negative edges but no negative cycles. Let d_k[i][j] be the shortest path from i to j using only the first k vertices as intermediates.
d_k[i][j] = \min\bigl(d_{k-1}[i][j],; d_{k-1}[i][k] + d_{k-1}[k][j]\bigr)
Time: Θ(V³). Space: Θ(V²).
Relax every edge V − 1 times. A further pass that still improves a distance shows a negative cycle.
Time: O(VE). Unlike Dijkstra, it handles negative edge weights.
Optimal binary search tree (Θ(n³)), travelling salesman with bitmask DP (Θ(n²·2ⁿ)), coin change , edit distance (Θ(mn)) and longest increasing subsequence (Θ(n²), or Θ(n log n) with binary search).
Unit 5: Backtracking and Branch and Bound
Backtracking builds a solution one component at a time and abandons (backtracks from) a partial solution as soon as it cannot lead to a valid answer. It searches a state space tree depth-first and uses bounding (pruning) functions to cut branches early.
Explicit constraints say which values each component may take. Implicit constraints say how components must relate to each other.
Place N queens on an N×N board so that no two share a row, column or diagonal.
Point Greedy Dynamic programming Decisions One choice per step, never revised Tries all choices, keeps the best Guarantees optimum
Only if greedy choice property holds
Always, if substructure holds
Speed Usually faster Usually slower, needs a table Knapsack Fractional version 0/1 version
Travelling salesman (TSP): find the shortest tour visiting every city once. Compute a lower bound for each partial tour (for example by reducing the cost matrix row by row and column by column), expand the node with the smallest bound, and prune any node whose bound exceeds the best tour found. The worst case is still O(n!), but pruning makes it much faster in practice.
Unit 6: Graph Traversal and NP-Completeness
Visits vertices level by level from a start vertex using a queue.
Time: O(V + E) with an adjacency list, O(V²) with an adjacency matrix. BFS finds the shortest path (fewest edges) in an unweighted graph.
Goes as deep as possible along each branch before backtracking, using recursion or a stack.
Time: O(V + E) with an adjacency list. Uses: cycle detection, topological sort, connected and strongly connected components, solving mazes.
Point Backtracking Branch and bound Example N-Queens, subset sum 0/1 knapsack, travelling salesman
Point BFS DFS Data structure Queue Stack or recursion Order Level by level Branch by branch Shortest path (unweighted) Yes No Space O(V) for the widest level O(V) for the deepest path
For a directed acyclic graph (DAG) , a topological order lists vertices so that every edge u → v has u before v. Run DFS and output vertices in decreasing finish time (or repeatedly remove a vertex with in-degree 0). Time: O(V + E). A graph with a cycle has no topological order.
Every problem in P is also in NP, so P ⊆ NP. Whether P = NP is still an open question.
A problem A reduces to problem B (A ≤p B) if any instance of A can be converted to an instance of B in polynomial time, so a fast algorithm for B would give a fast algorithm for A.
To prove a problem X is NP-complete:
Cook's theorem: the Boolean satisfiability problem (SAT) is NP-complete. It was the first problem proved so, and others are proved by reduction from it.
Known NP-complete problems: 3-SAT, clique, vertex cover, Hamiltonian cycle, travelling salesman (decision version), subset sum, graph colouring (3 colours), and 0/1 knapsack (decision version).
Approximation algorithms give a solution guaranteed to be within a factor of the optimum (vertex cover has a 2-approximation).
Class Meaning P Decision problems solvable in polynomial time by a deterministic algorithm (sorting, shortest path, MST) NP Decision problems whose proposed solution can be verified in polynomial time NP-hard At least as hard as every problem in NP; need not be in NP itself NP-complete Both in NP and NP-hard: the hardest problems in NP
Frequently Asked Exam Questions
Algorithm Technique Worst-case time
One fact to remember
Floyd-Warshall Dynamic programming
Θ(V³) All pairs, allows negative edges Bellman-Ford Dynamic programming
O(VE) Detects negative cycles
N-Queens Backtracking O(N!) Prune on column and diagonal clash Subset sum Backtracking O(2ⁿ) Prune when sum exceeds target Graph colouring Backtracking O(m^V) Smallest m is the chromatic number TSP Branch and bound O(n!) Prune by lower bound BFS Graph traversal O(V + E) Queue, shortest path in unweighted graph DFS Graph traversal O(V + E) Stack, used in topological sort
Explain merge sort with an example and derive its time complexity using a recurrence. Explain quick sort. Derive best, average and worst case. When does the worst case occur? Solve the fractional knapsack problem for a given instance and prove the greedy choice is optimal. Find the minimum spanning tree of a given graph using both Prim's and Kruskal's algorithms. Construct Huffman codes for given character frequencies and compute the average code length. Find the LCS of two given strings by filling the table and tracing back. Find the optimal bracketing for a matrix chain of given dimensions. Apply Floyd-Warshall to a given weighted digraph and show the matrices. Solve the 4-Queens problem using backtracking and draw the state space tree. Explain the classes P, NP, NP-hard and NP-complete with a diagram, and state Cook's theorem. Compare divide and conquer, greedy and dynamic programming with examples.
Can you write the recurrence and solution for merge sort, quick sort and binary search? Can you trace one MST, one shortest-path and one DP table by hand? Do you know which algorithms are greedy, which are DP and which use backtracking? Can you state the time complexity of every row in the cheat sheet without looking?