Docsity

DAA exam preparation notes, Study notes of Design and Analysis of Algorithms

Toggle sidebar
bg1
DAA Exam Preparation Notes
Oct 30, 2026
Syllabus Covered
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.
Contents
1. Unit 1: Introduction and Asymptotic Analysis
2. Unit 2: Divide and Conquer
3. Unit 3: Greedy Method
4. Unit 4: Dynamic Programming
5. Unit 5: Backtracking and Branch and Bound
6. Unit 6: Graph Traversal and NP-Completeness
7. Complexity Cheat Sheet
8. Frequently Asked Exam Questions
Notation Used
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
How to Study
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.
DAA Exam Preparation Notes
Page 1 of 14
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download DAA exam preparation notes and more Study notes Design and Analysis of Algorithms in PDF only on Docsity!

DAA Exam Preparation Notes

Oct 30, 2026

Syllabus Covered

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.

Contents

  1. Unit 1: Introduction and Asymptotic Analysis
  2. Unit 2: Divide and Conquer
  3. Unit 3: Greedy Method
  4. Unit 4: Dynamic Programming
  5. Unit 5: Backtracking and Branch and Bound
  6. Unit 6: Graph Traversal and NP-Completeness
  7. Complexity Cheat Sheet
  8. Frequently Asked Exam Questions

Notation Used

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

How to Study

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).

Asymptotic notations

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, worst and average case

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.

Solving recurrences

Recursive algorithms give a recurrence such as T(n) = 2T(n/2) + n. Three methods are standard.

  1. Substitution method: guess the solution, then prove it by induction.
  2. Recursion tree method: draw the tree of calls, sum the work done at each level, then sum the levels.
  3. Master theorem: for T(n) = a·T(n/b) + f(n) with a ≥ 1 and b > 1, compare f(n) with n^(log_b a).

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

  1. Choose a pivot element.
  2. Partition: put elements smaller than the pivot on its left and larger ones on its right.
  3. Recursively sort the left and right parts.

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.

Merge sort vs quick sort

Strassen's matrix multiplication

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

Other divide and conquer problems

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.

Fractional knapsack

  1. Compute the ratio value/weight for every item.
  2. Sort items by ratio in decreasing order.
  3. Take whole items while they fit; take a fraction of the next item to fill the bag.

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.

Job sequencing with deadlines

Each job has a deadline and a profit and takes one time unit. Maximise total profit.

  1. Sort jobs by profit in decreasing order.
  2. Place each job in the latest free slot on or before its deadline; skip it if none is free.

Time: O(n²) with a simple slot search, O(n log n) with a disjoint-set structure.

Huffman coding

Builds an optimal prefix code: frequent characters get short codes.

  1. Put every character in a min-heap keyed by frequency.
  2. Repeatedly remove the two smallest nodes and join them under a new node whose frequency is their sum.
  3. Stop when one node remains. Label left edges 0 and right edges 1 to read the 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).

0/1 knapsack

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.

Longest common subsequence (LCS)

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 chain multiplication

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.

All-pairs shortest path: Floyd-Warshall

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²).

Single-source shortest path: Bellman-Ford

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.

Other classic DP problems

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).

Greedy vs dynamic programming

Unit 5: Backtracking and Branch and Bound

Backtracking

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.

N-Queens problem

Place N queens on an N×N board so that no two share a row, column or diagonal.

  1. Place one queen per row, trying columns left to right.

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

Breadth-first search (BFS)

Visits vertices level by level from a start vertex using a queue.

  1. Mark the start vertex visited and enqueue it.
  2. Dequeue a vertex and visit every unvisited neighbour, marking and enqueueing each.
  3. Repeat until the queue is empty.

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.

Depth-first search (DFS)

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

Topological sort

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.

Complexity classes

Every problem in P is also in NP, so P ⊆ NP. Whether P = NP is still an open question.

Reduction and proving NP-completeness

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:

  1. Show X is in NP (a given solution can be checked in polynomial time).
  2. Pick a known NP-complete problem Y and show Y ≤p X in polynomial time.

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).

Dealing with NP-hard problems

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

Short answers (2 marks)

  1. What is an algorithm? A finite, ordered set of clear steps that solves a problem and ends for every valid input.
  2. Define Big-O. f(n) = O(g(n)) if there are constants c and n0 such that f(n) ≤ c·g(n) for all n ≥ n0.
  3. Difference between Big-O and Theta? Big-O is only an upper bound; Theta is both an upper and a lower bound, so it is tight.
  4. State the Master theorem. For T(n) = a·T(n/b) + f(n), compare f(n) with n^(log_b a): smaller gives Θ(n^(log_b a)), equal gives an extra log n factor, larger gives Θ(f(n)).
  5. What is the principle of optimality? An optimal solution to a problem contains optimal solutions to its subproblems.
  6. Why does greedy fail for 0/1 knapsack? Taking the best ratio item first can leave unused capacity that a different combination would fill; items cannot be split.
  7. Why can Dijkstra not handle negative edges? It fixes a vertex's distance when it is picked, but a later negative edge could shorten that distance.

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

  1. What is a stable sorting algorithm? One that keeps equal elements in their original relative order (merge sort is stable, quick sort is not).
  2. What is memoization? Storing the result of each subproblem in a table so it is computed only once.
  3. Define P, NP and NP-complete. P: solvable in polynomial time. NP: solution verifiable in polynomial time. NP-complete: in NP and as hard as every NP problem.

Long answers (8 to 10 marks)

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.

Last-minute checklist

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?