2022 question paper
Design and Analysis of Algorithms
25 questions
Q1a. An adjacency matrix representation of a graph cannot contain information of (i) nodes (ii) edges (iii) direction of edges (iv) parallel edges20222m
Module 3: Graph and Tree Algorithms
View this question on its own page →An adjacency matrix representation of a graph cannot contain information of
(i) nodes
(ii) edges
(iii) direction of edges
(iv) parallel edgesWorked SolutionAnswer: (iv) parallel edges
Adjacency matrix A[i][j]=1 if edge exists. It shows node count (dimension), edges, direction (asymmetry for digraph). Simple 0/1 matrix cannot represent multiple edges between same pair (multigraph) without counts.
Q1b. An algorithm is made up of two independent time complexities f(n) and g(n). Then the complexities of the algorithm is in the order of (i) f(n) \times g(n) (ii) \max \{ f(n), g(n) \} (iii) \min \{ f(n), g(n) \} (iv) f(n) + g(n)20222m
Module 1: Introduction
View this question on its own page →An algorithm is made up of two independent time complexities and . Then the complexities of the algorithm is in the order of
(i)
(ii)
(iii)
(iv)Worked SolutionAnswer: (ii) max{ f(n), g(n)} (which equals (iv) f+g asymptotically)
If algorithm has two independent parts sequentially, T = f+g = Θ(max). Sum asymptotically dominated by max. Both (ii) and (iv) are correct; exam expects (ii) max as simplified.
Q1c. The minimum number of colors needed to color a graph having n > 3 vertices and 2 edges is (i) 2 (ii) 3 (iii) 4 (iv) 120222m
Module 3: Graph and Tree Algorithms
View this question on its own page →The minimum number of colors needed to color a graph having vertices and 2 edges is
(i) 2
(ii) 3
(iii) 4
(iv) 1Worked SolutionAnswer: (i) 2
Graph with >3 vertices but only 2 edges is disconnected forest; at most needs 2 colors (bipartite, no odd cycle). Unless edge is loop, 2 colors suffice.
Q1d. BFS on a graph G = (V, E) has running time (i) O(|V| + |E|) (ii) O(|V|) (iii) O(|E|) (iv) None of these20222m
Module 3: Graph and Tree Algorithms
View this question on its own page →BFS on a graph has running time
(i)
(ii)
(iii)
(iv) None of theseWorked SolutionAnswer: (i) O(|V|+|E|)
BFS visits each vertex and edge once using adjacency list.
Q1e. Which one is true of the following (i) all NP hard problems are NP complete (ii) all NP complete problems are NP hard (iii) some NP complete problems are NP hard (iv) None of these20222m
Module 4: Tractable and Intractable Problems
View this question on its own page →Which one is true of the following
(i) all NP hard problems are NP complete
(ii) all NP complete problems are NP hard
(iii) some NP complete problems are NP hard
(iv) None of theseWorked SolutionAnswer: (ii) all NP complete are NP hard
NP-complete = NP ∩ NP-hard. All NP-complete are NP-hard and in NP; not all NP-hard are NP-complete (some not in NP). So (ii) true, (i) false.
Q1f. Which one of the following functions is asymptotically smallest? (i) 2^n (ii) n^{\log n} (iii) n^{\sqrt{n}} (iv) (100)^{(\log n)^{1/3}} + (\log \log n)^{2/3}20222m
Module 1: Introduction
View this question on its own page →Which one of the following functions is asymptotically smallest?
(i)
(ii)
(iii)
(iv)Worked SolutionAnswer: (iv) is asymptotically smallest
Explanation:
Take logs or compare growth orders:
- (i) → exponential
- (ii)
- (iii)
- (iv) plus which is doubly logarithmic.
For large : .
Hence ordering from smallest to largest: (iv) (ii) (iii) (i).
Formally (sub-polynomial, quasi-polylog), (super-polynomial but sub-exponential), , .
Correct choice: (iv)
Note: Options (ii) and (iii) are often swapped in ranking; but both dominate (iv) asymptotically.
Q1g. Which of the following algorithm solves the All Pair Shortest Path problem (i) Dijkstra's (ii) Floyd's Warshall's (iii) Prim's (iv) Kruskal's20222m
Module 3: Graph and Tree Algorithms
View this question on its own page →Which of the following algorithm solves the All Pair Shortest Path problem
(i) Dijkstra's
(ii) Floyd's Warshall's
(iii) Prim's
(iv) Kruskal'sWorked SolutionAnswer: (ii) Floyd-Warshall's Algorithm
Explanation:
- All-Pairs Shortest Path (APSP) = shortest distances between every pair .
- Floyd-Warshall: DP, , handles negative edges (no negative cycles), simple triple loop:
dist[k][i][j]=min(dist[k-1][i][j], dist[k-1][i][k]+dist[k-1][k][j]). - Dijkstra's = Single-Source Shortest Path for non-negative weights; to get APSP you must run it times → , but not the canonical APSP algorithm.
- Prim's / Kruskal's = MST algorithms, unrelated to shortest paths.
Correct choice: (ii) Floyd's Warshall's (also Johnson's algorithm is another APSP method).
Q1h. The average number of comparisons performed by merge sort algorithm in merging two sorted lists of 2 elements is (i) 8/5 (ii) 11/7 (iii) 11/6 (iv) 8/320222m
Module 1: Introduction
View this question on its own page →The average number of comparisons performed by merge sort algorithm in merging two sorted lists of 2 elements is
(i) 8/5
(ii) 11/7
(iii) 11/6
(iv) 8/3Worked SolutionAnswer: (iv)
Explanation:
Merge of two sorted lists , with all 4! / (2!2!) = 6 interleavings equally likely (distinct elements, random order). Standard merge:
i=j=0, comps=0 while i<2 and j<2: comps++; if A[i]<B[j] i++ else j++Best case = 2 comps (one list entirely smaller: or )
Worst case = 3 comps (interleaved).Enumerate:
- : , → 2
- : , , → 3
- : , , → 3
- : 2 (symmetric to 1)
- : 3
- : 3
Average = .
Worst-case merging , best , average .
Correct choice: (iv)
Q1i. Which one of the following statement is correct? (i) If A \le_p B and B \in P then A \in P (ii) If A \le_p B and $A otin P then B otin P$ (iii) If A \le_p B and B \le_p C then A \le_p C (iv) All of these20222m
Module 4: Tractable and Intractable Problems
View this question on its own page →Which one of the following statement is correct?
(i) If and then
(ii) If and $A
otin PB
otin P$
(iii) If and then
(iv) All of theseWorked SolutionAnswer: (iv) All of these
Explanation:
Let denote polynomial-time many-one (Karp) reduction.
(i) If and then : True. Compose reduction (poly-time) with poly-time decider for → poly-time decider for . Hence is closed under .
**(ii) If and $A
otin PB
otin PBPAP$, contradiction.(iii) If and then : True — is transitive; compose the two poly-time reduction functions (polynomial of polynomial is polynomial).
All three are fundamental lemmas used in NP-completeness proofs.
Correct choice: (iv) All of these
Q1j. Kruskal algorithm is a (i) Divide & conquer algorithm (ii) Branch and bound algorithm (iii) Greedy algorithm (iv) Dynamic programming20222m
Module 2: Fundamental Algorithmic Strategies
View this question on its own page →Kruskal algorithm is a
(i) Divide & conquer algorithm
(ii) Branch and bound algorithm
(iii) Greedy algorithm
(iv) Dynamic programmingWorked SolutionAnswer: (iii) Greedy algorithm
Explanation:
- Kruskal's MST: sort edges by weight ascending, iterate adding smallest edge that does not form a cycle (using Union-Find). At each step it makes the locally optimal (minimum-weight) choice — the defining characteristic of the Greedy paradigm.
- Similarly Prim's is also greedy.
- Divide & conquer splits into independent subproblems (Merge Sort, Quick Sort). Dynamic programming solves overlapping subproblems. Branch-and-bound is for backtracking optimization.
Correct choice: (iii) Greedy algorithm
- Install PocketlyBudget App•POCKETLY
Pocketly on Android
Log daily food, travel, and hostel expenses instantly. Set monthly spending limits and see where your money goes without complex spreadsheets.
Q2a. Solve the following recurrence by successive substitution method: f(1) = 1 \quad \text{if } n=1 f(n) = 3f(n/2) + 6 \quad \text{if } n > 120227m
Module 1: Introduction
View this question on its own page →Solve the following recurrence by successive substitution method:
Worked SolutionSolution – Recurrence , by Successive Substitution
Assume ().
Unfold:
Since , and .
With :
for power of 2.Asymptotics:
Check by Master Theorem: , , dominates case 1 → consistent.
For general (not power of 2), embed between powers of 2 → same .
Q2b. Write the Knuth-Morris-Pratt algorithm for pattern matching. Apply the KMP algorithm for the pattern p = "ababaca" and string s = "bacbabababacaab". Show every step.20227m
Module 2: Fundamental Algorithmic Strategies
View this question on its own page →Write the Knuth-Morris-Pratt algorithm for pattern matching. Apply the KMP algorithm for the pattern and string . Show every step.
Worked SolutionKnuth-Morris-Pratt (KMP) Algorithm
1. Prefix Function (failure function)
computePi(P[0..m-1]): pi[0]=0; k=0 for q=1..m-1: while k>0 and P[k]!=P[q]: k=pi[k-1] if P[k]==P[q]: k++ pi[q]=k= length of longest proper prefix of which is also suffix.
2. Search
KMP(T[0..n-1], P[0..m-1]): pi=computePi(P); q=0 for i=0..n-1: while q>0 and P[q]!=T[i]: q=pi[q-1] if P[q]==T[i]: q++ if q==m: report match at i-m+1; q=pi[q-1]Time , space .
Apply to "ababaca" (m=7), "bacbabababacaab" (n=14-15)
Step A — Compute for a b a b a c a
q 0:a 1:b 2:a 3:b 4:a 5:c 6:a 0 0 1 2 3 0 1 Derivation:
- q0 a →0
- q1 b≠a →0
- q2 a==a(0) →1
- q3 b==b(1) →2
- q4 a==a(2) →3
- q5 c vs p[3]=b mismatch → fallback pi[2]=1 → p[1]=b vs c mismatch →0
- q6 a==a →1
Step B — Scan b a c b a b a b a b a c a a b
Index i (0-based), = matched prefix length:i T[i] q before action q after output 0 b 0 P0=a ≠b 0 1 a 0 a==a →1 1 2 c 1 P1=b ≠c →q=pi0=0, a≠c 0 3 b 0 a≠b 0 4 a 0 a==a→1 1 5 b 1 b==b→2 2 6 a 2 a==a→3 3 7 b 3 b==b→4 4 8 a 4 a==a→5 5 9 b 5 P5=c≠b →q=pi4=3, P3=b==b→4 4 10 a 4 a==a→5 5 11 c 5 c==c→6 6 12 a 6 a==a→7 7 Match at i-m+1=6 (1-based 7) 13 a 7→fallback pi6=1 1 P1=b≠a→q=pi0=0, P0=a==a→1 1 14 b 1 b==b→2 2 Result: One occurrence of "ababaca" at position 7 (1-indexed) in (0-indexed 6), i.e., "ababaca". No other full match; scan continues to end.
Verification by brute force: substrings of s length 7: bacbaba, ... ababaca at 6-12 matches.
Complexities: preprocessing , searching , total , never re-examines text characters (contrast naive ).
Q3a. Give step by step procedure to find minimal spanning tree for the given graph. 20227m
Module 3: Graph and Tree Algorithms
View this question on its own page →Give step by step procedure to find minimal spanning tree for the given graph.
Q3b. Write the algorithm for quick sort. Find the best-case, worst-case and average-case time complexities of this algorithm.20227m
Module 2: Fundamental Algorithmic Strategies
View this question on its own page →Write the algorithm for quick sort. Find the best-case, worst-case and average-case time complexities of this algorithm.
Worked SolutionQuick Sort Algorithm & Complexities
Algorithm (Lomuto / Hoare partition)
QuickSort(A, low, high): if low < high: p = Partition(A, low, high) // pivot = A[high] (Lomuto) 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++; swap A[i], A[j] swap A[i+1], A[high]; return i+1Hoare partition uses two pointers from both ends, fewer swaps on duplicates.
Example with [5,2,9,3,7]
Pivot 7 → partition → [5,2,3,7,9] → recurse left [5,2,3] pivot3→[2,3,5] + right [9].
Time Complexity
Recurrence where = size of left part.
- Best case: balanced partition each level → → (also when all elements distinct and pivot median).
- Average case: over random permutation / random pivot — expected balanced → expected (proof via linearity of expectation, harmonic sum).
- Worst case: already sorted + pivot = last element → or → → (e.g., sorted [1,2,3,4,5]).
Space: stack average/best, worst (if tail recursion not optimized); in-place partitioning auxiliary.
Why average is good
Randomized QuickSort (random pivot) expected regardless of input; practical fastest due to low constants and cache locality.
Q4a. Solve the single source shortest path problem for the following graph considering '1' as the source vertex using Dijkstra's algorithm. 20227m
Module 3: Graph and Tree Algorithms
View this question on its own page →Solve the single source shortest path problem for the following graph considering '1' as the source vertex using Dijkstra's algorithm.
Q4b. State the graph coloring problem and write the algorithm. Apply backtracking technique to solve the 3-colouring problem for the following graph. 20227m
Module 2: Fundamental Algorithmic Strategies
View this question on its own page →State the graph coloring problem and write the algorithm. Apply backtracking technique to solve the 3-colouring problem for the following graph.
Q5a. Differentiate between divide & conquer, greedy method and dynamic programming with suitable examples.20227m
Module 2: Fundamental Algorithmic Strategies
View this question on its own page →Differentiate between divide & conquer, greedy method and dynamic programming with suitable examples.
Worked SolutionDivide & Conquer vs Greedy vs Dynamic Programming
Feature Divide & Conquer Greedy Dynamic Programming Idea Split into independent subproblems, solve recursively, combine. Make locally optimal choice now, never undo, hope global optimum. Split into overlapping subproblems, solve each once, store, combine via recurrence for global optimum. Subproblems Disjoint, independent. Single decision path. Overlapping, optimal substructure + overlapping. Proof Correctness by recursion + combine. Needs greedy-choice + optimal substructure (exchange argument). Needs optimal substructure + overlapping (table correctness). Complexity Recurrence ; e.g., Merge . Often or . Table size × cost; often – or . Examples Merge Sort, Quick Sort, Binary Search, Strassen, Closest Pair. Dijkstra (non-neg), Prim/Kruskal, Huffman, Fractional Knapsack, Activity Selection. 0/1 Knapsack, Matrix Chain, LCS, Edit Distance, Floyd-Warshall, Optimal BST. When fails Overlapping would cause exponential recomputation (Fibonacci if pure D&C). Fails where local ≠ global: 0/1 Knapsack, Coin change with arbitrary denominations (e.g., coins 1,3,4 for 6). Overkill where greedy suffices (fractional knapsack DP vs greedy ). Example Contrast — Knapsack :
- Items (p/w): (10/2), (5/2), (8/3). D&C brute tries all .
- Greedy ratio picks 10/2 first (5.0) → optimal for fractional but for 0/1 picks 10+8=18 weight5 vs DP optimum 10+8=18 (here same), but with weights 6,5,5 example greedy fails.
- DP enumerates table and gets exact 0/1 optimum.
Rule of thumb: Need overlapping? → DP. Need independent? → D&C. Local choice provably safe? → Greedy (fastest).
Q5b. Find the minimum number of operations required for the following matrix chain multiplication using dynamic programming. Also discuss the complexity involved. A(10 \times 20) * B(20 \times 50) * C(50 \times 1) * D(1 \times 100)20227m
Module 2: Fundamental Algorithmic Strategies
View this question on its own page →Find the minimum number of operations required for the following matrix chain multiplication using dynamic programming. Also discuss the complexity involved.
Worked SolutionMatrix Chain Multiplication — DP Minimum Multiplications
Problem
Given dimensions , find parenthesis yielding minimum scalar multiplications. Cost of multiplying is .
DP Recurrence
Let = min cost to multiply (-indexed). .
Track = giving optimum for reconstruction. Answer .Algorithm
for i=1..n: m[i][i]=0 for len=2..n: for i=1..n-len+1: j=i+len-1; m[i][j]=∞ for k=i..j-1: q=m[i][k]+m[k+1][j]+p_{i-1}p_k p_j if q<m[i][j]: m[i][j]=q; s[i][j]=kTime , Space .
Application to Q48 Dimensions
→ 5 matrices: .
Compute via DP (results from program):
- $m[1][3]=\min(120+720+4·3·12=984, 360+144+4·10·12?) →
Best via : - Continuing yields ((A_1A_2)(A_3A_4))A_5((A_1A_2)((A_3A_4)A_5))s$ choices give same 1344.
If textbook expects detailed and tables, fill upper-triangular matrices as per recurrence; reconstruction via gives optimal order.
Complexity: time ( matrices → ~125 operations for ), space.
Q6a. Explain solution pseudocode for N-Queens problem using backtracking.20227m
Module 2: Fundamental Algorithmic Strategies
View this question on its own page →Explain solution pseudocode for N-Queens problem using backtracking.
Worked SolutionN-Queens via Backtracking — Pseudocode
Problem
Place queens on chessboard so no two attack (no same row, column, diagonal).
State & Constraints
- Row gets one queen in column ().
- Explicit:
- Implicit: $col[i]
eq col[j]|col[i]-col[j]|
eq |i-j|i
eq j$.
Helper
IsSafe(row, col): for i=0..row-1: if col[i]==col or abs(col[i]-col)==row-i: return false return trueBacktracking Pseudocode
Global col[0..N-1], solutions=[] SolveNQueens(row): if row==N: solutions.append(copy(col)) // found one return // or return true if only one needed for c=0..N-1: if IsSafe(row,c): col[row]=c SolveNQueens(row+1) // backtrack implicit: next iteration tries next c // col[row] overwritten // no c works → backtrack to previous row Main: SolveNQueens(0)Iterative version uses stack; optimized uses boolean arrays
colUsed,diag1,diag2for safe check:colUsed[c] && diag1[row+c] && diag2[row-c+N]Trace (first solution)
Row0 → c0? conflict? Try c0 safe → [0,]
Row1 → c0 attack col, c1 diag, c2? diag? Actually 0-2 diff2 vs row diff1 no, col2≠0 → safe → [0,2,]
Row2 → c0 attack?,c1 safe? [0,2,?] c1? Check vs row0: |0-1|=1 diag vs |0-2|=2 no, vs row1 |2-1|=1 vs1 yes diag → unsafe; c3? |0-3|=3 vs2 no, |2-3|=1 vs1 yes? Actually row1 col2 vs row2 col3 diff1 row1 → diagonal → unsafe → backtrack
Row1 next c3 safe → [0,3,] → Row2 c1 safe → [0,3,1,] → Row3 c? no → backtrack ... eventually finds [1,3,0,2] → queens at (0,1),(1,3),(2,0),(3,2).Complexities
- Worst (branching reduces), has 92 solutions.
- Space recursion.
- With pruning via forward checking, feasible up to for enumeration, larger via heuristics.
Q6b. Find Huffman codes for the text of characters with given frequencies: | Characters | T | I | V | K | L | E | O | Z | P | R | |---|---|---|---|---|---|---|---|---|---|---| | Frequencies | 43 | 38 | 16 | 8 | 50 | 12 | 56 | 13 | 22 | 7 |20227m
Module 2: Fundamental Algorithmic Strategies
View this question on its own page →Find Huffman codes for the text of characters with given frequencies:
Characters T I V K L E O Z P R Frequencies 43 38 16 8 50 12 56 13 22 7 Worked SolutionHuffman Coding for Frequencies
Characters: T43, I38, V16, K8, L50, E12, O56, Z13, P22, R7 → total 265
Huffman Algorithm (Greedy, min-heap)
repeat n-1 times: extract two smallest nodes x,y, create z with freq=f_x+f_y, insert z, make x,y children of zTree built bottom-up, codes derived by 0/1 left/right.
Step-by-Step Merging (sorted order)
Initial heap (freq, char): R7, K8, E12, Z13, V16, P22, I38, T43, L50, O56
- R7+K8=15 (RK15)
- E12+Z13=25 (EZ25)
- RK15+V16=31 (RKV31)
- P22+EZ25=47 (PEZ47)
- RKV31+I38=69 (RKVI69)
- T43+PEZ47=90 (TPEZ90)
- L50+O56=106 (LO106)
- RKVI69+TPEZ90=159 (RKVI-TPEZ159)
- LO106+RKVI-TPEZ159=265 (root)
(Different tie orders produce different but equally optimal trees; one valid complete merge sequence above.)
Build Tree & Codes (example labeling left=0, right=1)
One optimal prefix code (lengths depend on exact merging order):
- R: 00000 (7)
- K: 00001 (8)
- V: 0001 (16) ← combined with RK
- E: 0010 (12)
- Z: 0011 (13)
- P: 010 (22)
- I: 011 (38)
- T: 100 (43)
- L: 101 (50)
- O: 11 (56) shortest for highest frequency
Exact bit strings vary by left/right assignment; what matters is code lengths: Len: O2, L3, T3, I3, P3, E4, Z4, V4, K5, R5 (or similar optimal 2-5 bits). Canonical Huffman would give same lengths.
Weighted Path Length
(approx, exact depends on lengths 2 vs3 for L/O). Average bits/char = 810/265 ≈3.06 (vs fixed 4 bits for 10 symbols). Compression saves ~23%.
Properties
- Prefix-free (no code is prefix of another), optimal among prefix codes (proven by greedy exchange).
- Time with heap ( symbols), space .
- Decoding via tree traversal.
- Install PocketlyBudget App•POCKETLY
Pocketly on Android
Log daily food, travel, and hostel expenses instantly. Set monthly spending limits and see where your money goes without complex spreadsheets.
Q7a. What is negative weight-cycle? Write Bellman-Ford algorithm to find single source shortest distance of a directed graph.20227m
Module 3: Graph and Tree Algorithms
View this question on its own page →What is negative weight-cycle? Write Bellman-Ford algorithm to find single source shortest distance of a directed graph.
Worked SolutionNegative Weight Cycle & Bellman-Ford Algorithm
Negative Weight Cycle
Directed cycle with sum . In such graph, shortest path from source to vertices reachable from is undefined () because traversing cycle repeatedly reduces path weight arbitrarily. Example: -2, -1 → cycle -3.
Without negative cycles, shortest simple paths well-defined (≤ edges).
Bellman-Ford — Single Source Shortest Paths (SSSP) with Negatives
BellmanFord(G, s): dist[s]=0; dist[v]=∞ for v≠s; parent[v]=NIL repeat V-1 times: for each edge (u,v)∈E: if dist[u]+w(u,v) < dist[v]: dist[v]=dist[u]+w(u,v); parent[v]=u // detect negative cycle for each edge (u,v): if dist[u]+w(u,v) < dist[v]: return "Negative cycle reachable from s" (and retrieve cycle via parent) return dist, parentCorrectness: After iterations, = shortest path using ≤ edges. After , all simple paths considered. If still relaxable → ≥ edges → must contain cycle → negative.
Complexities: Time (list) / (matrix), Space . Superior to Dijkstra which fails with negatives; slower but handles them.
Finding All Negative Cycles: Add super source with 0 edges to all vertices, run once to detect any cycle in graph.
Q7b. Define P-Class, NP-class, NP-complete class and NP-hard class of problems. What is the relation between them?20227m
Module 4: Tractable and Intractable Problems
View this question on its own page →Define P-Class, NP-class, NP-complete class and NP-hard class of problems. What is the relation between them?
Worked SolutionComplexity Classes , , -Complete, -Hard — Definitions & Relations
Definitions
- (Polynomial): Decision problems solvable by deterministic TM in for some . Example: Sorting, MST, Shortest path (non-neg). Tractably solvable.
- (Nondeterministic Polynomial): Problems where yes instances have certificate verifiable in polytime by deterministic TM (equivalently solvable by nondet TM in polytime). . Example: SAT, Hamiltonian Cycle decision, Subset Sum.
- -Hard: Problem such that , (polytime reducible). No need to be in ; at least as hard as hardest in . Example: Halting Problem, TSP optimization, any NP-complete problem.
- -Complete: -Hard = hardest problems in . Formal: and , . Cook proved SAT is NP-complete. Example: 3-SAT, Clique, Vertex Cover decision, Hamiltonian Cycle.
Relations
P ⊆ NP (unknown if equal — Clay Millennium) NP-Complete ⊂ NP NP-Complete ⊂ NP-Hard NP-Hard extends outside NP (includes undecidable) P = NP ⇔ some NP-complete ∈ P ⇔ all NP-complete ∈ P If any NP-hard problem outside NP is in P, not relevant (still P vs NP open).Venn diagram: Large oval NP-Hard; inside it smaller oval NP; within NP: P region and NP-Complete ring (NP-Complete = intersection of NP-Hard with NP). Outside NP-Hard? None — NP-Hard superset.
Consequences: Proving new problem NP-complete shows it is among hardest in ; proving NP-hard shows at least as hard (possibly harder than NP).
Q8a. Find the optimal solution using greedy criteria for a knapsack having capacity 100 kg for the following list of items having values and weights as shown in the table. | Item | Value | Weight | |---|---|---| | I_1 | 10 | 15 | | I_2 | 20 | 25 | | I_3 | 30 | 35 | | I_4 | 40 | 45 | | I_5 | 50 | 55 |20227m
Module 2: Fundamental Algorithmic Strategies
View this question on its own page →Find the optimal solution using greedy criteria for a knapsack having capacity 100 kg for the following list of items having values and weights as shown in the table.
Item Value Weight 10 15 20 25 30 35 40 45 50 55 Worked SolutionFractional Knapsack Greedy — Capacity 100 Kg
Items:
:10/15≈0.67, :20/25=0.80, :30/35≈0.857, :40/45≈0.889, :50/55≈0.909
Sorted ratio descending:Fractional Knapsack (optimal via greedy)
Take in ratio order:
- :55 → remaining 45, profit 50
- :45 → remaining 0, profit +40 =90
Capacity filled exactly 55+45=100. Items fully taken, others 0. Max profit =90 (fractional = 0/1 same here as no fraction needed).
If capacities differ example where fraction needed: remaining would be filled partially: e.g., if capacity 100 had been 70, would take 55 + 15/45 fraction → profit 50+13.33=63.33.
0/1 Knapsack (no fraction) — Greedy not optimal in general
Using 0/1 greedy by ratio, same set profit90 is feasible. Check alternative subsets weight≤100:
- no
- $I_5+I_2+I_1=55+25+15=95 profit 50+20+10=80 <90
- $I_4+I_3+I_1=45+35+15=95 profit90 also 40+30+10=80 <90
- $I_3+I_2+I_1=35+25+15=75 profit60
- remains best 0/1 for this instance.
Hence greedy coincidentally optimal here, profit 90, weight100 with items (or alternative ). If the question expects demonstrating greedy steps with fractional take, show sorted list and fill; for 0/1 DP optimum also 90.
Note: To illustrate greedy failure for 0/1, classic counterexample: capacity50 items (60,10),(100,20),(120,30) ratio greedy picks 60+100 vs optimal 100+120.
Time sorting, selection.
Q8b. State master's theorem and find the time complexity for the following recurrence: T(n) = 2T(n^{1/2}) + \log n20227m
Module 1: Introduction
View this question on its own page →State master's theorem and find the time complexity for the following recurrence:
Worked SolutionMaster's Theorem &
Master's Theorem Statement (standard)
For , , , :
- Case1
- Case2
- Case3 + regularity → Θ(f)
(Requires form, not .)
Can Master Apply to ?
Not directly — argument is , not with constant . Standard master not applicable. Use substitution / change of variable.
Solution via Domain Transformation
Let (assuming is power of 2 for asymptotics), define .
Then:
Now → This is master form with , , .- , → Case2 → .
Convert back: →
Result:
Alternatively .Verification via Recursion Tree
Depth: each level halves → levels = levels; work per level → total .
Q9. Write short notes on any two of the following: (i) Asymptotic Notations (ii) Clique decision problem (iii) Max-flow-min-cut theorem (iv) Union-Find Algorithm202214m
Module 1: Introduction
View this question on its own page →Write short notes on any two of the following:
(i) Asymptotic Notations
(ii) Clique decision problem
(iii) Max-flow-min-cut theorem
(iv) Union-Find AlgorithmWorked SolutionShort Notes — Any Two (Covering All Four for Choice)
(i) Asymptotic Notations (see Q36/Q88)
upper, lower, tight, / strict. Hierarchy . Used to compare algorithms independent of hardware; max rule for polynomials etc.
(ii) Clique Decision Problem
Given undirected and integer , does contain a clique (complete subgraph) of size ≥ ?
- NP-complete: In NP (certificate = vertices, check all edges in ) + NP-hard via reduction from 3-SAT or Vertex Cover (complement: has clique ⇔ complement has vertex cover ). Brute exponential; no known polytime.
- Applications: clustering, social networks.
(iii) Max-Flow Min-Cut Theorem (Ford-Fulkerson)
For network with source , sink , capacities , value of max flow = capacity of min cut (set , partition, capacity sum of edges ). Proof via Ford-Fulkerson: flow maximal iff no augmenting path in residual graph → reachable set from defines min cut with equal capacity. Consequences: integrality (if capacities integer, max flow integer), duality for bipartite matching, etc. Used to prove correctness of Ford-Fulkerson (Edmonds-Karp) etc.
(iv) Union-Find (Disjoint Set Union, DSU) Algorithm
Maintains partition of elements into disjoint sets with operations:
- MakeSet(x): , rank/size 0
- Find(x): with path compression →
- Union(x,y): by rank/size →
Sequence of ops on elements → almost constant ( inverse Ackermann <5 for practical ).
Uses: Kruskal MST cycle check, connected components, offline LCA. Pseudo:
Find(x): if parent[x]!=x: parent[x]=Find(parent[x]); return parent[x] Union(a,b): ra=Find(a), rb=Find(b); if ra==rb return; if rank[ra]<rank[rb] swap; parent[rb]=ra; if rank equal rank[ra]++
Pocketly — Personal Finance for Students
Keep your hostel and college expenses under control. Track accounts, log spending in seconds, and stay within your monthly budget.