UNIT 5: DESIGN & ANALYSIS OF ALGORITHMS
I. FOUNDATIONS OF ALGORITHM ANALYSIS
Algorithm Definition & Characteristics
-
Definition: A finite sequence of well-defined instructions for solving a specific problem.
-
Characteristics: Input, Output, Definiteness, Finiteness, Effectiveness.
Time Complexity Analysis
-
Best Case: Minimum time required (e.g., binary search finds target in first comparison).
-
Average Case: Expected time over all inputs. Requires probability distribution of inputs.
-
Worst Case: Maximum time required (e.g., binary search exhausts all comparisons).
-
[!TIP] Exam Focus: Average case analysis often involves summing costs over all possible input positions and dividing by
n.
Average Case Analysis of Binary Search (Dec 2024)
-
Problem: Find expected number of comparisons in successful search.
-
Model: Assume all
nelements equally likely to be searched. -
Derivation:
-
For an element at depth
i(root depth 0), comparisons =i+1. -
Number of nodes at depth
iin a complete binary tree: ≤2^i. -
Total comparisons
C(n) ≈ Σ_{i=0}^{h} (i+1) * min(2^i, n - (2^i - 1)), whereh = ⌊log₂ n⌋. -
Simplified:
C(n) ≈ (n+1)⌊log₂(n+1)⌋ - 2^{⌊log₂(n+1)⌋+1} + 2. -
Result:
C(n) = Θ(log n). Hence, average case isO(log n).
\boxed{\text{Average Case Time Complexity of Binary Search} = O(\log n)}
-
Average Case Analysis of Quicksort (Nov 2022)
-
Model: Pivot chosen uniformly at random; all
n!permutations equally likely. -
Recurrence for expected comparisons
C(n):
$$C(n) = n-1 + \frac{1}{n} \sum_{k=0}^{n-1} [C(k) + C(n-k-1)]$$
-
Solution:
C(n) ≈ 2n ln n = O(n log n). -
[!TIP] Key insight: Each pair of elements is compared only if one is chosen as pivot before any element between them. Probability =
2/(j-i+1).
Asymptotic Notations
-
Big-O (O): Upper bound.
f(n) = O(g(n))iff ∃c>0, n₀s.t.0 ≤ f(n) ≤ c*g(n)∀n ≥ n₀. -
Big-Omega (Ω): Lower bound.
f(n) = Ω(g(n))iff ∃c>0, n₀s.t.0 ≤ c*g(n) ≤ f(n)∀n ≥ n₀. -
Big-Theta (Θ): Tight bound.
f(n) = Θ(g(n))ifff(n) = O(g(n))andf(n) = Ω(g(n)). -
Little-o (o): Strict upper bound.
f(n) = o(g(n))ifflim_{n→∞} f(n)/g(n) = 0. -
Little-omega (ω): Strict lower bound.
f(n) = ω(g(n))ifflim_{n→∞} g(n)/f(n) = 0.
Proving Asymptotic Bounds (Nov 2022 Example)
-
Claim:
f(n) = 5n² + 6n + 4isO(n²). -
Proof: For
n ≥ 1,5n² + 6n + 4 ≤ 5n² + 6n² + 4n² = 15n². Choosec=15, n₀=1. Hence proved.\boxed{5n^2 + 6n + 4 = O(n^2)}
Space Complexity
-
Total Space: Space used by algorithm + input + output.
-
Auxiliary Space: Extra/temporary space used by algorithm (excluding input/output).
-
[!TIP] Short Note (Dec 2024): Auxiliary space is crucial for recursive algorithms (stack space) and in-place algorithms (constant auxiliary space).
II. DIVIDE AND CONQUER (D&C) PARADIGM
General Method & Recurrence Relations
-
Divide: Break problem into smaller subproblems.
-
Conquer: Solve subproblems recursively.
-
Combine: Merge subproblem solutions.
-
Solving Recurrences:
-
Substitution Method: Guess solution, verify by induction.
-
Recursion Tree: Visualize cost per level, sum costs.
-
Master Theorem: For
T(n) = aT(n/b) + f(n):\boxed{
T(n) =
\begin{cases}
\Theta(n^{\log_b a}) & \text{if } f(n)=O(n^{\log_b a - \epsilon}) \
\Theta(n^{\log_b a} \log n) & \text{if } f(n)=\Theta(n^{\log_b a}) \
\Theta(f(n)) & \text{if } f(n)=\Omega(n^{\log_b a + \epsilon}) \text{ and } af(n/b) \leq cf(n)
\end{cases}
}
-
Strassen's Matrix Multiplication (Dec 2024, Nov 2022)
-
Algorithmic Steps:
-
Partition
n×nmatricesA, Binto fourn/2 × n/2submatrices. -
Compute 7 products (
P₁toP₇) recursively:-
P₁ = (A₁₁ + A₂₂) × (B₁₁ + B₂₂) -
P₂ = (A₂₁ + A₂₂) × B₁₁ -
P₃ = A₁₁ × (B₁₂ - B₂₂) -
P₄ = A₂₂ × (B₂₁ - B₁₁) -
P₅ = (A₁₁ + A₁₂) × B₂₂ -
P₆ = (A₂₁ - A₁₁) × (B₁₁ + B₁₂) -
P₇ = (A₁₂ - A₂₂) × (B₂₁ + B₂₂)
-
-
Combine to get result submatrices:
-
C₁₁ = P₁ + P₄ - P₅ + P₇ -
C₁₂ = P₃ + P₅ -
C₂₁ = P₂ + P₄ -
C₂₂ = P₁ - P₂ + P₃ + P₆
-
-
-
Recurrence:
T(n) = 7T(n/2) + Θ(n²)(7 recursive calls,Θ(n²)for additions). -
Solution by Master Theorem:
a=7, b=2, log_b a ≈ 2.81. Sincef(n)=Θ(n²) = O(n^{2.81-ε}), Case 1 applies.\boxed{T(n) = \Theta(n^{\log_2 7}) \approx \Theta(n^{2.81})}
Merge Sort
-
Algorithm:
-
If
n > 1:-
Divide array into two halves.
-
Recursively sort left and right halves.
-
Merge two sorted halves in
Θ(n)time.
-
-
-
Time Complexity:
T(n) = 2T(n/2) + Θ(n). Master Theorem Case 2:T(n) = Θ(n log n)(best, average, worst).\boxed{T_{\text{merge sort}}(n) = \Theta(n \log n)}
Quicksort (Dec 2024)
-
Algorithm (Lomuto Partitioning):
QUICKSORT(A, low, high): if low < high: p = PARTITION(A, low, high) // pivot final index QUICKSORT(A, low, p-1) QUICKSORT(A, p+1, high) PARTITION(A, low, high): pivot = A[high] i = low - 1 for j = low to high-1: if A[j] ≤ pivot: i = i+1; swap A[i] and A[j] swap A[i+1] and A[high] return i+1 -
Example (Dec 2024):
A = {6, 15, 3, 14, 25, 36, 18}. First pivot=18. After partition:{6,15,3,14,18,36,25}. Recurse on left{6,15,3,14}and right{36,25}. -
Time Complexity:
-
Worst Case (already sorted):
T(n) = T(n-1) + Θ(n) = Θ(n²). -
Best Case (balanced):
T(n) = 2T(n/2) + Θ(n) = Θ(n log n). -
Average Case (random pivot):
Θ(n log n)(proved earlier).
-
III. GREEDY ALGORITHM PARADIGM
General Method & Greedy Choice Property
-
Method: At each step, make a locally optimal choice hoping for global optimum.
-
Greedy Choice Property: A global optimum can be arrived at by selecting a local optimum.
-
Optimal Substructure: Optimal solution contains optimal solutions to subproblems.
-
[!TIP] Correctness Proof (Nov 2022): Often by exchange argument or staying ahead.
Fractional/Continuous Knapsack (Dec 2024, Nov 2023, Nov 2022)
-
Problem: Maximize profit with weight capacity
m. Items can be taken fractionally. -
Greedy Strategy: Sort items by profit/weight ratio
(p_i/w_i)descending. Take as much as possible of each item in order until capacity full. -
Example (Dec 2024):
n=4, m=15, p=(10,10,12,18), w=(2,4,6,9)-
Ratios:
5, 2.5, 2, 2. -
Sort by ratio: Item1 (5), Item2 (2.5), Item3 (2), Item4 (2).
-
Take full Item1 (w=2, p=10, rem cap=13).
-
Take full Item2 (w=4, p=10, rem cap=9).
-
Take full Item3 (w=6, p=12, rem cap=3).
-
Take
3/9 = 1/3of Item4 (p=6). -
Total Profit = 10+10+12+6 = 38.
\boxed{\text{Optimal Profit (Fractional Knapsack)} = 38}
-
Job Sequencing with Deadlines (Dec 2024, Nov 2023)
-
Problem: Schedule jobs (each takes 1 unit time) to maximize total profit, each job must finish by its deadline.
-
Greedy Strategy:
-
Sort all jobs by profit descending.
-
Find maximum deadline
max_d. -
Create array
slot[1..max_d]initialized empty. -
For each job in sorted order:
-
Find latest empty slot
≤ job.deadline. -
If found, schedule job there.
-
-
-
Example (Dec 2024):
n=6, p=(200,180,190,300,120,100), d=(5,3,2,4,2,4)-
Sort by profit: J4(300,d4), J1(200,d5), J3(190,d2), J2(180,d3), J5(120,d2), J6(100,d4).
-
Schedule:
-
J4 → slot 4.
-
J1 → slot 5.
-
J3 → slot 2.
-
J2 → slot 3.
-
J5 → no slot (d2 full).
-
J6 → slot 1? Wait, slot1 empty? Let's check: slots 1,2,3,4,5. After J4(s4), J1(s5), J3(s2), J2(s3): slots 1 empty. J6(d4) → slot1? No, slot1 ≤4, but we schedule backwards. Actually, for J6, find latest empty ≤4: slots 4,3,2 full, slot1 empty → schedule at slot1? But typical algorithm schedules at latest possible. Let's redo properly:
-
Slots:
[1,2,3,4,5] -
J4(300,d4) → slot4 →
[_,_,_,300,_] -
J1(200,d5) → slot5 →
[_,_,_,300,200] -
J3(190,d2) → slot2 →
[_,190,_,300,200] -
J2(180,d3) → slot3 →
[_,190,180,300,200] -
J5(120,d2) → no slot ≤2 (slot2 full).
-
J6(100,d4) → no slot ≤4 (slots 2,3,4 full).
-
-
Scheduled Jobs: J4, J1, J3, J2. Total Profit = 300+200+190+180 = 870.
\boxed{\text{Optimal Profit (Job Sequencing)} = 870}
-
Optimal Merge Pattern / Huffman Coding (Dec 2024, Nov 2023, Nov 2022)
-
Problem: Merge
ksorted files with lengthsl₁, l₂, ..., lₖinto one file. Cost of merging two files of lengthsa,bisa+b. Minimize total cost. -
Greedy Strategy: Always merge the two smallest files first. Use a min-heap.
-
Huffman Coding: Optimal prefix-free binary codes for characters with frequencies.
-
Create min-heap of nodes (char, freq).
-
While heap size > 1:
-
Extract two smallest nodes
x, y. -
Create new node
zwithfreq = x.freq + y.freq, left=x, right=y. -
Insert
zinto heap.
-
-
The final node is root of Huffman tree. Traverse to assign codes (left=0, right=1).
-
-
Example (Frequencies: a:5, b:9, c:12, d:13, e:16, f:45):
-
Merge 5+9=14 → heap: (12,13,14,16,45)
-
Merge 12+13=25 → heap: (14,16,25,45)
-
Merge 14+16=30 → heap: (25,30,45)
-
Merge 25+30=55 → heap: (45,55)
-
Merge 45+55=100.
-
Codes: f:0, c:100, d:101, a:1100, b:1101, e:111.
-
IV. DYNAMIC PROGRAMMING (DP) PARADIGM
Characteristics & Comparison with Divide & Conquer (Dec 2024)
| Feature | Divide & Conquer | Dynamic Programming |
|---|---|---|
| Subproblems | Independent, disjoint | Overlapping, dependent |
| Approach | Top-down (recursive) | Top-down (memoization) or Bottom-up (tabulation) |
| Time Complexity | Often O(n log n) or O(n²) |
Often O(n²) or O(n³) for DP tables |
| Space Complexity | Recursion stack O(log n) |
DP table O(n²) or more |
| Example | Merge Sort, Quicksort | 0/1 Knapsack, Floyd-Warshall |
Algorithmic Steps:
-
Characterize optimal substructure.
-
Define value of optimal solution recursively.
-
Compute value bottom-up (tabulation) or top-down with memoization.
-
Construct optimal solution from computed information.
0/1 Knapsack Problem (Dec 2024, Nov 2022)
-
Problem: Given
nitems (profitp_i, weightw_i), capacitym. Each item either taken (1) or not (0). Maximize total profit without exceedingm. -
DP Table:
dp[i][w]= max profit using firstiitems with capacityw. -
Recurrence:
$$ dp[i][w] = \begin{cases} 0 & \text{if } i=0 \text{ or } w=0 \\ dp[i-1][w] & \text{if } w_i > w \\ \max(p_i + dp[i-1][w-w_i], dp[i-1][w]) & \text{otherwise} \end{cases} $$
-
Example (Nov 2022):
n=3, m=6, p=(1,2,5), w=(2,3,4).-
Table
dp[4][7](rows 0..3, cols 0..6). -
Fill row-wise. Final
dp[3][6] = 5(take item3 only). -
Optimal Profit = 5.
-
Multistage Graph Problem (Dec 2024, Nov 2023)
-
Problem: Directed acyclic graph with stages
1..k. Find minimum cost path from start (stage 1) to end (stage k). -
Forward DP Approach:
-
Let
f_i(v)= min cost from vertexvin stageito end. -
Base:
f_k(v) = cost(v, terminal)forvin stagek. -
Recurrence:
f_i(v) = min_{u∈adj(v)} [ cost(v,u) + f_{i+1}(u) ]. -
Compute from stage
k-1down to1.
-
-
Backward DP: Similar but compute from start.
-
Path Reconstruction: At each vertex
vin stagei, chooseuthat minimizescost(v,u)+f_{i+1}(u).
All-Pairs Shortest Paths: Floyd-Warshall (Nov 2023)
-
Algorithm:
-
Initialize distance matrix
DwithD[i][j] = weight(i,j)(∞ if no edge, 0 if i=j). -
For
k = 1 to n:-
For
i = 1 to n:-
For
j = 1 to n:D[i][j] = min(D[i][j], D[i][k] + D[k][j])
-
-
-
-
Time Complexity:
Θ(n³). -
Path Reconstruction: Maintain predecessor matrix
P. UpdateP[i][j] = P[k][j]ifD[i][k]+D[k][j]improves.
Optimal Binary Search Tree (OBST)
-
Problem: Given keys
k₁<..<kₙwith search probabilitiesp₁..pₙ(and dummy keysd₀..dₙ), construct BST minimizing expected search cost. -
DP:
e[i][j]= expected cost of subtree containingk_i+1..k_j.e[i][j] = min_{i≤r≤j} { e[i][r-1] + e[r+1][j] + w(i,j) }, wherew(i,j)=Σ_{s=i+1}^j p_s + Σ_{s=i}^j q_s.
-
Complexity:
O(n³)naive,O(n²)with Knuth's optimization if probabilities satisfy quadrangle inequality.
V. BACKTRACKING PARADIGM
General Method & State Space Tree
-
Method: Systematically explore solution space via depth-first search with pruning (backtrack when partial solution cannot be extended to a full solution).
-
State Space Tree: Nodes represent partial solutions. Root = initial state. Leaves = complete solutions or dead ends.
-
Efficiency: Depends on ability to prune early.
N-Queens Problem (Dec 2024)
-
Problem: Place
nqueens onn×nchessboard so no two attack each other (no same row, column, diagonal). -
Backtracking Algorithm:
NQUEENS(row): if row > n: print solution; return for col = 1 to n: if PLACE(row, col) is safe: set queen at (row, col) NQUEENS(row+1) remove queen from (row, col) // backtrack PLACE(row, col): for i = 1 to row-1: if queen at (i, col) or queen at (i, row-i+col) or // main diagonal queen at (i, row+col-i): // anti-diagonal return false return true -
State Space Tree for 4-Queens (Nov 2022):
-
Root → 4 branches (col1-col4 for row1).
-
Each branch tries cols for row2, prune if conflict.
-
Leaves at depth 4 are solutions (e.g.,
[2,4,1,3]meaning queen at (1,2), (2,4), (3,1), (4,3)).
DiagramCANVAS: A tree with root, first level 4 nodes (row1 placements). From each, some branches die early. Only two leaves at depth 4 survive: one with path [2,4,1,3] and another [3,1,4,2]. -
Graph Coloring Problem (m-coloring) (Nov 2023, Nov 2022)
-
Problem: Color vertices of graph with
mcolors so no adjacent vertices share color. -
Backtracking Algorithm:
COLOR(v): if v > n: return true // all colored for c = 1 to m: if SAFE(v, c): // check all neighbors of v color[v] = c if COLOR(v+1): return true color[v] = 0 // backtrack return false -
Solving for Given Graph: Assign colors sequentially, backtrack on conflict.
Hamiltonian Cycle Problem (Nov 2022)
-
Problem: Find cycle visiting each vertex exactly once and returning to start.
-
Backtracking Algorithm:
HAMILTONIAN(path, pos): if pos == n: // all vertices in path if edge exists from last to start: print path; return true else: return false for each vertex v not in path: if edge exists from path[pos-1] to v: path[pos] = v if HAMILTONIAN(path, pos+1): return true path[pos] = -1 // backtrack return false -
Example: For graph with vertices 1,2,3,4 and edges (1-2,2-3,3-4,4-1), path
[1,2,3,4]with edge 4-1 gives cycle.
VI. BRANCH AND BOUND (B&B) PARADIGM
General Method (Dec 2024)
-
Systematic Enumeration of candidate solutions using state space tree.
-
Key Concepts:
-
Live Node: Node generated but not fully explored.
-
Dead Node: Node that cannot produce better solution (pruned).
-
E-node (Expanding Node): Live node being explored.
-
Cost Function (
ĉ): Estimate of cost of any solution reachable from node. Must satisfyĉ(node) ≥ cost(optimal solution in subtree).
-
-
Search Strategies:
-
FIFO (Breadth-First): Queue. Explore level by level.
-
LIFO (Depth-First): Stack. Explore one branch to leaf before backtracking.
-
Least Cost Search (Best-First): Priority queue ordered by
ĉ. Most promising node expanded first.
-
Traveling Salesperson Problem (TSP) using Reduction (Dec 2024, Nov 2023)
-
Problem: Find minimum cost Hamiltonian cycle in weighted graph.
-
Reduction Method (Cost Matrix Reduction):
-
Row Reduction: For each row, subtract row minimum from all elements in that row. Sum of subtracted values =
cost_so_far. -
Column Reduction: For each column, subtract column minimum.
-
Total Lower Bound:
cost_so_far(from step 1) + sum of column minima (step 2). -
Branching: For each edge
(i,j)not yet fixed:-
Include edge: Set row
iand columnjto ∞ (to prevent other edges from same row/col), and also set(j,i)=∞to prevent subtour. Compute new lower bound. -
Exclude edge: Set
(i,j)=∞. Compute new lower bound.
-
-
Choose node with smallest lower bound to expand next (Least Cost).
-
-
Bounding Function: Reduced cost matrix's total reduction + current path cost.
-
Example: Start with 4×4 cost matrix. Reduce rows/cols, get initial bound. Branch on edge with smallest reduced cost (often 0 if present). Continue until a complete tour is found with cost
C. Prune any live node with bound ≥C.
0/1 Knapsack using B&B
-
Node: Represents decision on first
iitems (some taken, some not). -
State:
(i, current_weight, current_profit). -
Bounding: Compute upper bound on profit achievable from node:
-
Fill remaining capacity greedily (fractionally) with items by
p/wratio. -
bound = current_profit + (remaining_capacity * next_item_ratio)(if items sorted by ratio).
-
-
Branching: At node
(i, w, p), create two children:-
Left (include item i+1):
(i+1, w+w_{i+1}, p+p_{i+1}) -
Right (exclude item i+1):
(i+1, w, p)
-
-
Pruning: Discard node if
bound ≤ best_profit_so_farorweight > capacity.
VII. NP-HARDNESS & NP-COMPLETENESS
Complexity Classes
-
P: Decision problems solvable in polynomial time by deterministic Turing machine.
-
NP: Decision problems verifiable in polynomial time (or solvable by nondeterministic TM in poly time).
P ⊆ NP(all P problems are in NP).
-
NP-Hard: Problems at least as hard as hardest problems in NP. Every NP problem reduces to them in poly time. Not necessarily in NP.
-
NP-Complete: Problems that are both NP-Hard and in NP.
- If any NP-Complete has poly-time solution, then
P=NP.
- If any NP-Complete has poly-time solution, then
Polynomial Time Reductions
-
Definition: Transform instances of problem
Ato instances of problemBin polynomial time, such that answer is "yes" forAiff "yes" forB. -
Purpose: Show
Bis at least as hard asA. IfAis NP-Hard andA ≤ₚ B, thenBis NP-Hard. -
Common Reductions:
-
SAT → 3-SAT: Convert clauses to 3-literal form using new variables.
-
Clique → Vertex Cover: Graph
Ghas clique of sizekiffGhas vertex cover of size|V|-k. -
Vertex Cover → Independent Set:
Sis vertex cover iffV\Sis independent set. -
Subset Sum → Partition: Special case.
-
Comparison: NP-Hard vs NP-Complete (Dec 2024, Nov 2023, Nov 2022)
| Feature | NP-Complete | NP-Hard |
|---|---|---|
| Membership in NP | Yes | Not necessarily |
| Definition | In NP and NP-Hard | At least as hard as NP problems |
| Example | SAT, Clique, 0/1 Knapsack (decision version) | TSP (optimization), Halting Problem |
| Implication of Poly-time Solution | P = NP |
P = NP (if also in NP) or more severe |
| Reduction Direction | From any NP problem to it | From any NP problem to it |
Classic NP-Complete Problems
-
Boolean Satisfiability (SAT): Given Boolean formula, is there satisfying assignment? (Cook-Levin theorem: first NP-Complete).
-
Clique: Does graph contain complete subgraph of size
k? -
Vertex Cover: Does graph have vertex cover of size
k? -
Subset Sum: Given set of integers, is there subset summing to
K? -
Traveling Salesperson Problem (Decision): Given graph and
K, is there tour with cost ≤K? -
0/1 Knapsack (Decision): Given items, capacity
m, profitP, is there subset with total weight ≤mand total profit ≥P?
VIII. ADVANCED & SPECIALIZED TOPICS (SHORT NOTES)
Horner's Algorithm for Polynomial Evaluation (Dec 2024)
-
Problem: Evaluate polynomial
P(x) = a₀ + a₁x + a₂x² + ... + aₙxⁿat pointx. -
Naive:
Θ(n²)operations (compute eachx^kseparately). -
Horner's Rule: Rewrite as
P(x) = a₀ + x(a₁ + x(a₂ + ... + x(aₙ) ... ). -
Algorithm:
result = a_n for i = n-1 downto 0: result = result * x + a_i return result -
Complexity:
Θ(n)multiplications/additions. -
Example:
P(x)=2+3x+4x²→((4)*x + 3)*x + 2.
Prim's Algorithm for MST (Dec 2024)
-
Problem: Find minimum spanning tree (connected, acyclic, all vertices, min total edge weight) of undirected graph.
-
Algorithm (using min-heap):
-
Start with arbitrary vertex
sin MST. -
Maintain min-heap of edges connecting MST to outside vertices, keyed by edge weight.
-
While heap not empty:
-
Extract min edge
(u,v)whereu∈MST,v∉MST. -
Add
vand edge to MST. -
For each neighbor
wofvnot in MST, insert/update heap edge(v,w).
-
-
-
Time Complexity:
-
With binary heap:
O((V+E) log V) = O(E log V)(sinceE ≥ V-1). -
With array (no heap):
O(V²).
-
-
Working Example: For given graph, show step-by-step addition of vertices and edges with min weight.
B-Trees (Nov 2023)
-
Definition: Self-balancing search tree where each node can have multiple keys and multiple children.
-
Properties:
-
All leaves at same depth.
-
Node with
kchildren hask-1keys. -
Keys in node sorted; keys in subtree
iare between keysi-1andi. -
Order
t(minimum degree): Each node (except root) has at leastt-1keys, at most2t-1keys. Root at least 1 key. -
Height
h = O(log_t n).
-
-
Creation (Insertion): Search leaf, insert key in sorted order. If overflow (
2tkeys), split middle key to parent. -
Advantages over BST:
-
Better for disk/storage (fewer disk accesses due to high branching factor).
-
Always balanced (no need for rotations like AVL/Red-Black).
-
Efficient for range queries.
-
Parallel Algorithms (Dec 2024, Nov 2023, Nov 2022)
-
Definition: Algorithms designed to run on multiple processors/cores simultaneously.
-
Models: PRAM (Parallel Random Access Machine) - shared memory, processors execute synchronously.
-
Key Metrics:
-
Work: Total operations across all processors =
T₁(sequential time). -
Time:
T_pwithpprocessors. -
Speedup:
S_p = T₁ / T_p. -
Efficiency:
E_p = S_p / p = T₁ / (p T_p). -
Parallelism:
T₁ / T_∞(max speedup with unlimited processors).
-
-
Examples:
-
Parallel Merge Sort: Divide array, sort halves in parallel, merge in parallel.
-
Matrix Multiplication (Cannon's, Fox's): Assign blocks to processors.
-
-
Challenges: Load balancing, synchronization, communication overhead.
Data Stream Algorithms (Nov 2023, Nov 2022)
-
Context: Data arrives as a stream (high volume, one-pass, limited memory).
-
Goal: Compute summaries/statistics without storing all data.
-
Key Problems & Algorithms:
-
Counting Distinct Elements (Heavy Hitters): Flajolet-Martin algorithm, HyperLogLog.
-
Frequent Items (Misra-Gries, Count-Min Sketch): Find items with frequency >
εN. -
Quantiles (Greenwald-Khanna): Approximate median, percentiles.
-
Windowed Queries (Sliding Windows): Count in last
Welements (using exponential histograms).
-
-
Example: HyperLogLog estimates number of distinct IP addresses in a stream using
O(log log n)memory with small error.
Logic Optimization (Nov 2023, Nov 2022)
-
Goal: Simplify Boolean functions (circuits) to minimize cost (gate count, delay, power).
-
Techniques:
-
Algebraic Simplification: Using Boolean identities (
x+x'=1,x·1=x, etc.). -
Karnaugh Map (K-map): Graphical method for up to 6 variables. Group adjacent 1s to find prime implicants.
-
Quine-McCluskey Method: Tabular method for many variables. Find prime implicants, then essential primes, then cover with minimal set.
-
Espresso Algorithm: Heuristic for large functions (two-level logic).
-
-
Example: Simplify
f(a,b,c) = Σ(0,1,2,4,5,6)using K-map →f = b' + a'c'.
Traveling Salesperson Problem (TSP) Short Note (Dec 2024)
-
Problem: Given weighted graph (complete or not), find Hamiltonian cycle of minimum total cost.
-
Exact Methods:
-
Branch and Bound (Reduction): As described above. Works for small
n(≤20). -
Dynamic Programming (Held-Karp):
O(n² 2ⁿ)time,O(n 2ⁿ)space. State:(S, i)= min cost to visit setSending ati.
-
-
Approximate Methods:
-
Nearest Neighbor: Start at city, repeatedly go to nearest unvisited. Fast (
O(n²)), but can be bad (up tolog nfactor). -
Minimum Spanning Tree (MST) Based: Double MST (preorder walk) gives 2-approximation for metric TSP (triangle inequality). Christofides algorithm gives 1.5-approximation.
-
Genetic Algorithms, Simulated Annealing: Metaheuristics for large instances.
-
-
Complexity: NP-Hard (decision version is NP-Complete). No poly-time algorithm unless
P=NP.
END OF UNIT 5 NOTES