9. Analysis and Design of Algorithms
9. Analysis and Design of Algorithms
🔹 Asymptotic Notations
Used to describe time and space complexity of algorithms in terms of input size n.
🔹 Steps
1. Divide – Split the problem into subproblems.
🔹 Operations
● Find(x): Returns the representative (root) of the set containing x.
🔹 Optimizations
● Path Compression (during Find): makes trees flat
🔹 Applications:
● Network connectivity
📘 4. Quick Sort
🔹 Concept
● Choose a pivot.
● Rearrange elements so that elements < pivot go to left and > pivot to right.
🔹 Time Complexity:
● Best/Average Case: O(n log n)
🔹 Properties:
● Not stable
● In-place
📘 5. Merge Sort
🔹 Concept
● Recursively divide the array into halves.
🔹 Time Complexity:
● All Cases: O(n log n)
🔹 Properties:
● Stable
● Two types:
🔹 Heapify
● Procedure to maintain heap property.
🔹 Time Complexity:
● Build Heap: O(n)
🔹 Space Complexity:
● O(1) (in-place)
📘 Greedy Algorithms
🔹 Introduction
A greedy algorithm makes locally optimal choices at each step with the hope of finding a
global optimum.
✅ Characteristics:
● Greedy-choice property
● Optimal substructure
✅ Greedy Strategy:
● Sort jobs by decreasing profit
✅ Kruskal’s Algorithm:
● Sort edges by weight
🔸 4. Huffman Coding
● Lossless data compression
✅ Steps:
1. Build a min-heap of frequencies
✅ Applications:
● Shortest path in unweighted graphs
● Level-order traversal
✅ Applications:
● Detecting cycles
● Topological sort
🔸 3. Topological Sort
● Applies only to Directed Acyclic Graphs (DAGs)
● Linear ordering such that for every edge u→v, u comes before v
✅ Techniques:
● DFS-based (store postorder in stack)
✅ Kosaraju’s Algorithm:
1. DFS & store finishing times
✅ Dijkstra’s Algorithm
● Only non-negative weights
● Matrix-based
✅ Key Concepts:
● Optimal Substructure: Optimal solution can be built from optimal solutions of
subproblems.
● DP Approach:
○ dp[i][j] = LCS of first i characters of X and first j of Y.
🔸 3. 0/1 Knapsack
● Given weights and values of n items and a capacity W, determine the max value that
can be carried in the knapsack.
● DP Table: dp[i][w] = max value with first i items and capacity w.
📘 Backtracking
🔹 What is Backtracking?
A technique for solving constraint satisfaction problems by exploring possible options and
backtracking when a constraint is violated.
🔸 1. 8-Queens Problem
● Place 8 queens on an 8×8 board such that no two queens attack each other.
● Use recursion with backtracking to place one queen per row and check column and
diagonal safety.
🔸 2. Sum of Subsets
● Find all subsets of a set of positive integers whose sum equals a given number.
● Explore all subset combinations using recursion and backtrack if sum exceeds target.
🔸 3. Graph Coloring
● Assign minimum number of colors to graph vertices so that no two adjacent vertices
have the same color.
🔸 4. Hamiltonian Cycle
● Find a cycle in a graph that visits each vertex once and returns to the starting point.
● Backtrack when adding a vertex to the cycle leads to dead-end (i.e., not a
Hamiltonian path).
📘 Computational Complexity
🔹 Complexity Measures:
● Time Complexity: Steps required as input size increases.
🔸 Key Definitions:
Class Description
P Problems solvable in polynomial time
2. If an algorithm has a running time of T(n)=3n2+2n+5, its asymptotic time complexity
is: a) O(n) b) O(nlogn) c) O(n2) d) O(n3)
Answer: c) O(n2)
3. The notation f(n)=Ω(g(n)) means that: a) f(n) grows asymptotically no faster than
g(n). b) f(n) grows asymptotically at least as fast as g(n). c) f(n) grows asymptotically
at the same rate as g(n). d) f(n) grows asymptotically strictly faster than g(n).
Answer: b) f(n) grows asymptotically at least as fast as g(n).
4. Which of the following describes the tight bound of an algorithm's running time? a)
Big-O b) Big-Omega c) Big-Theta d) Small-o
Answer: c) Big-Theta
5. Which of the following is true regarding O(1) complexity? a) The running time grows
linearly with the input size. b) The running time is constant, regardless of the input
size. c) The running time grows logarithmically with the input size. d) The running
time grows quadratically with the input size.
Answer: b) The running time is constant, regardless of the input size.
7. In the Union-Find algorithm, what is the primary purpose of path compression? a) To
balance the tree structure. b) To reduce the number of union operations. c) To flatten
the tree and speed up future find operations. d) To detect cycles in a graph.
Answer: c) To flatten the tree and speed up future find operations.
8. What is the worst-case time complexity of Quick Sort? a) O(nlogn) b) O(n) c) O(n2)
d) O(logn)
Answer: c) O(n2)
9. Merge Sort divides the array into two halves, sorts them recursively, and then merges
them. What is its time complexity in the worst case? a) O(n) b) O(n2) c) O(nlogn) d)
O(logn)
Answer: c) O(nlogn)
10.A Heap is a specialized tree-based data structure that satisfies the heap property. In
a Max-Heap, which element is always at the root? a) The smallest element b) The
largest element c) A random element d) The first inserted element
Answer: b) The largest element
11.Greedy Algorithms
The Knapsack problem with fractional items (allowing fractions of items) can be
optimally solved using which algorithmic paradigm? a) Dynamic Programming b)
Greedy Approach c) Divide and Conquer d) Backtracking
Answer: b) Greedy Approach
12.In the Job Sequencing with Deadlines problem, what is the primary greedy choice
made? a) Select jobs with the earliest deadline first. b) Select jobs with the highest
profit first. c) Select jobs with the shortest duration first. d) Select jobs that fit into the
available slots first.
Answer: b) Select jobs with the highest profit first.
13.Which algorithm uses a priority queue to always select the edge with the minimum
weight when constructing a Minimum Spanning Tree? a) Kruskal's algorithm b)
Prim's algorithm c) Dijkstra's algorithm d) Bellman-Ford algorithm
Answer: b) Prim's algorithm
14.Kruskal's algorithm for Minimum Spanning Tree relies on which data structure for
efficiently checking cycles? a) Adjacency List b) Adjacency Matrix c) Disjoint Set
Union (Union-Find) d) Hash Table
Answer: c) Disjoint Set Union (Union-Find)
15.Huffman codes are used for: a) Finding the shortest path in a graph. b) Constructing
a Minimum Spanning Tree. c) Lossless data compression. d) Solving the Knapsack
problem.
Answer: c) Lossless data compression.
16.Graph Algorithms
Which graph traversal algorithm explores as far as possible along each branch
before backtracking? a) Breadth-First Search (BFS) b) Depth-First Search (DFS) c)
Dijkstra's algorithm d) Prim's algorithm
Answer: b) Depth-First Search (DFS)
18.A Topological Sort is possible only for which type of graph? a) Undirected Graph b)
Weighted Graph c) Directed Acyclic Graph (DAG) d) Complete Graph
Answer: c) Directed Acyclic Graph (DAG)
19.Which algorithm can find the shortest path from a single source to all other vertices in
a graph with negative edge weights? a) Dijkstra's algorithm b) Bellman-Ford
algorithm c) Floyd-Warshall algorithm d) Prim's algorithm
Answer: b) Bellman-Ford algorithm
20.What is the time complexity of Dijkstra's algorithm with a min-priority queue (e.g.,
using a Fibonacci heap)? a) O(V2) b) O(ElogV) c) O(V+ElogV) d) O(E+VlogV)
Answer: d) O(E+VlogV)
21.The Warshall's algorithm is used to find: a) Single Source Shortest Paths b) All Pairs
Shortest Paths c) Minimum Spanning Tree d) Strongly Connected Components
Answer: b) All Pairs Shortest Paths
23.Strongly Connected Components (SCCs) are typically found using which graph
traversal algorithm as a core component? a) BFS b) DFS c) Dijkstra's d) Kruskal's
Answer: b) DFS
24.If a graph has a cycle, which algorithm will fail to produce a valid output? a) BFS b)
DFS c) Topological Sort d) Dijkstra's algorithm (with non-negative weights)
Answer: c) Topological Sort
26.Dynamic Programming
Which problem is an example of overlapping subproblems, a characteristic feature of
Dynamic Programming? a) Quick Sort b) Merge Sort c) Fibonacci sequence
calculation (naive recursive) d) Binary Search
Answer: c) Fibonacci sequence calculation (naive recursive)
27.In Matrix Chain Multiplication, the goal is to: a) Multiply all matrices in a given chain.
b) Find the optimal parenthesization of a chain of matrices to minimize the number of
scalar multiplications. c) Find the largest product of matrices. d) Determine if a chain
of matrices can be multiplied.
Answer: b) Find the optimal parenthesization of a chain of matrices to
minimize the number of scalar multiplications.
28.The Longest Common Subsequence (LCS) problem finds: a) The longest common
string within two given strings. b) The longest string that is a subsequence of two
given strings. c) The shortest common subsequence of two given strings. d) The
number of common characters between two strings.
Answer: b) The longest string that is a subsequence of two given strings.
29.The 0/1 Knapsack problem differs from the fractional knapsack problem because: a)
Items can be taken in fractions. b) Items must be taken entirely (0 or 1). c) There is
no weight limit. d) The goal is to minimize profit.
Answer: b) Items must be taken entirely (0 or 1).
30.The time complexity of solving the 0/1 Knapsack problem using dynamic
programming is: a) O(N) b) O(W) (where W is knapsack capacity) c) O(N⋅W) d)
O(N2)
Answer: c) O(N⋅W)
31.Backtracking
The 8-Queen Problem aims to place 8 queens on an 8×8 chessboard such that: a)
All queens are in a straight line. b) No two queens attack each other. c) Queens
attack as many pieces as possible. d) Queens are placed randomly.
Answer: b) No two queens attack each other.
32.Which algorithmic technique is typically used to solve the Sum of Subsets problem?
a) Dynamic Programming b) Greedy Approach c) Backtracking d) Divide and
Conquer
Answer: c) Backtracking
33.In graph coloring, what is the objective? a) To find the shortest path between two
nodes. b) To color the vertices of a graph such that no two adjacent vertices have the
same color, using the minimum number of colors. c) To assign a unique color to each
vertex. d) To color the edges of a graph.
Answer: b) To color the vertices of a graph such that no two adjacent vertices
have the same color, using the minimum number of colors.
34.A Hamiltonian Cycle in a graph is a cycle that: a) Visits every vertex exactly once and
returns to the starting vertex. b) Visits every edge exactly once. c) Connects all
vertices with the minimum number of edges. d) Passes through a specific set of
vertices.
Answer: a) Visits every vertex exactly once and returns to the starting vertex.
36.Computational Complexity
A problem is considered to be in class P if it can be solved by a deterministic Turing
machine in: a) Exponential time b) Polynomial time c) Logarithmic time d) Constant
time
Answer: b) Polynomial time
38.A problem X is NP-Hard if: a) It can be solved in polynomial time. b) Any NP problem
can be reduced to X in polynomial time. c) It is in NP and also NP-Complete. d) It is
equivalent to a P problem.
Answer: b) Any NP problem can be reduced to X in polynomial time.
40.Which statement best describes the relationship between P and NP? a) P is a subset
of NP. b) NP is a subset of P. c) P and NP are disjoint sets. d) P equals NP (P=NP)
has been proven true.
Answer: a) P is a subset of NP.
42.If f(n)=O(g(n)), it implies that: a) f(n) grows at the same rate as g(n). b) f(n) grows
strictly faster than g(n). c) There exist positive constants c and n0such that
0≤f(n)≤c⋅g(n) for all n≥n0. d) There exist positive constants c and n0such that
f(n)≥c⋅g(n) for all n≥n0.
Answer: c) There exist positive constants c and n0such that 0≤f(n)≤c⋅g(n) for
all n≥n0.
44.What is the time complexity of searching an element in a sorted array using binary
search? a) O(n) b) O(logn) c) O(nlogn) d) O(1)
Answer: b) O(logn)
45.The "little-o" notation (o) describes a(n) _________ upper bound. a) inclusive b)
exclusive c) tight d) lower
Answer: b) exclusive
47.In Quick Sort, the worst-case time complexity occurs when the pivot element: a)
Always divides the array into two equal halves. b) Is always the smallest or largest
element. c) Is chosen randomly. d) Is always the median.
Answer: b) Is always the smallest or largest element.
48.What is the space complexity of Merge Sort? a) O(1) b) O(logn) c) O(n) d) O(n2)
Answer: c) O(n) (due to the auxiliary array for merging)
49.Which of the following is NOT typically a phase in a Divide and Conquer algorithm?
a) Divide b) Conquer c) Combine d) Optimize
Answer: d) Optimize
50.In a Disjoint Set Union (DSU) data structure, the find operation with path
compression helps in: a) Reducing the height of the trees. b) Increasing the number
of sets. c) Performing union operations faster. d) Sorting the elements within a set.
Answer: a) Reducing the height of the trees.
51.Greedy Algorithms
The Activity Selection Problem, where the goal is to select the maximum number of
non-overlapping activities, is best solved using: a) Dynamic Programming b) Greedy
Approach c) Backtracking d) Divide and Conquer
Answer: b) Greedy Approach
52.In Prim's algorithm for Minimum Spanning Tree, the set of vertices A initially contains:
a) All vertices in the graph. b) No vertices. c) An arbitrary starting vertex. d) All
vertices with even degrees.
Answer: c) An arbitrary starting vertex.
53.Kruskal's algorithm builds the MST by: a) Adding edges to a growing tree, always
choosing the minimum weight edge connecting a vertex in the tree to one outside the
tree. b) Sorting all edges by weight in ascending order and adding them if they don't
form a cycle. c) Iteratively relaxing edges until all shortest paths are found. d)
Performing a depth-first traversal of the graph.
Answer: b) Sorting all edges by weight in ascending order and adding them if
they don't form a cycle.
54.The time complexity of Prim's algorithm using a min-priority queue (binary heap) is:
a) O(V2) b) O(ElogV) c) O(V+ElogV) d) O(ElogE)
Answer: c) O(V+ElogV)
56.Graph Algorithms
Which algorithm is guaranteed to find the shortest path in a graph with non-negative
edge weights? a) Bellman-Ford b) Floyd-Warshall c) Dijkstra's d) DFS
Answer: c) Dijkstra's
57.The time complexity of DFS for an adjacency list representation is: a) O(V) b) O(E) c)
O(V+E) d) O(V2)
Answer: c) O(V+E)
58.What is the main difference between BFS and DFS in terms of traversal order? a)
BFS explores neighbors before going deeper; DFS explores deeper before
backtracking. b) BFS uses a stack; DFS uses a queue. c) BFS is used for shortest
paths; DFS is not. d) BFS works only on directed graphs; DFS works on undirected.
Answer: a) BFS explores neighbors before going deeper; DFS explores deeper
before backtracking.
59.A graph has a topological sort if and only if it is a: a) Connected graph. b) Complete
graph. c) Directed Acyclic Graph (DAG). d) Bipartite graph.
Answer: c) Directed Acyclic Graph (DAG).
61.For a graph with V vertices and E edges, the Floyd-Warshall algorithm has a time
complexity of: a) O(V2) b) O(V⋅E) c) O(V3) d) O(E2)
Answer: c) O(V3)
63.Which algorithm is best suited for finding the shortest path in an unweighted graph?
a) Dijkstra's algorithm b) Bellman-Ford algorithm c) Breadth-First Search (BFS) d)
Floyd-Warshall algorithm
Answer: c) Breadth-First Search (BFS)
66.Dynamic Programming
Dynamic Programming is suitable for problems that exhibit: a) Optimal substructure
and greedy choice property. b) Optimal substructure and overlapping subproblems. c)
Overlapping subproblems and greedy choice property. d) Independent subproblems.
Answer: b) Optimal substructure and overlapping subproblems.
67.In the context of the Longest Common Subsequence, a subsequence does not
require: a) The elements to be in the same relative order. b) The elements to be
contiguous (adjacent). c) The elements to be from the original sequence. d) The
elements to be unique.
Answer: b) The elements to be contiguous (adjacent).
70.For the 0/1 Knapsack problem, if we have n items and a capacity W, the DP table
size is typically: a) N×N b) N×W c) W×W d) N+W
Answer: b) N×W
71.The principle of optimality states that: a) An optimal solution to a problem contains
optimal solutions to its subproblems. b) The greedy choice always leads to an
optimal solution. c) Every problem can be solved by dividing it into smaller,
independent subproblems. d) All subproblems must be solved before solving the
main problem.
Answer: a) An optimal solution to a problem contains optimal solutions to its
subproblems.
72.The edit distance (Levenshtein distance) between two strings can be computed
using: a) Greedy algorithms b) Divide and Conquer c) Dynamic Programming d)
Backtracking
Answer: c) Dynamic Programming
73.The subset sum problem (determining if a subset of a given set of numbers sums to
a target value) can be solved using: a) Greedy algorithms b) Dynamic Programming
c) BFS d) Prim's algorithm
Answer: b) Dynamic Programming (also Backtracking)
74.If the number of columns in matrix A is m and the number of rows in matrix B is p, for
matrix multiplication A×B to be valid, what condition must hold? a) m=p b) m=p c)
m>p d) m<p
Answer: a) m=p
75.When solving the Matrix Chain Multiplication problem using dynamic programming,
the subproblems involve finding the minimum cost of multiplying: a) Individual
matrices. b) Pairs of adjacent matrices. c) Subchains of matrices. d) The entire chain.
Answer: c) Subchains of matrices.
76.Backtracking
In backtracking, if a partial solution cannot be extended to a complete solution, the
algorithm: a) Continues to explore that path anyway. b) Stops immediately and
reports failure. c) Backtracks to the last decision point and tries another option. d)
Starts over from the beginning.
Answer: c) Backtracks to the last decision point and tries another option.
78.Which of the following problems typically involves finding all possible solutions rather
than just one optimal solution? a) Shortest Path b) Minimum Spanning Tree c)
Sudoku Solver (finding one solution) d) Sum of Subsets (finding all subsets that sum
to target)
Answer: d) Sum of Subsets (finding all subsets that sum to target)
79.The Graph Coloring problem aims to find the: a) Chromatic number of a graph. b)
Shortest path between two nodes. c) Maximum flow in a network. d) Longest cycle in
a graph.
Answer: a) Chromatic number of a graph.
80.A Hamiltonian path visits every vertex in a graph: a) Exactly once. b) At least once. c)
Multiple times. d) Only once if it's a cycle.
Answer: a) Exactly once.
83.The isSafe function in the 8-Queen problem checks for attacks along: a) Rows and
columns only. b) Diagonals only. c) Rows, columns, and diagonals. d) Adjacent
squares only.
Answer: c) Rows, columns, and diagonals.
86.Computational Complexity
If a problem can be verified in polynomial time, it belongs to which class? a) P b) NP
c) NP-Hard d) NP-Complete
Answer: b) NP
88.The question "P = NP?" asks whether: a) All problems in P are also in NP. b) All
problems in NP can be solved in polynomial time by a deterministic algorithm. c) All
problems in NP-Hard are also in NP-Complete. d) All problems can be solved in
polynomial time.
Answer: b) All problems in NP can be solved in polynomial time by a
deterministic algorithm.
92.A decision problem is one where the output is always: a) An integer b) A string c)
Yes/No (or True/False) d) An optimal solution
Answer: c) Yes/No (or True/False)
93.The class of problems for which a solution can be verified in polynomial time is: a) P
b) NP c) NP-Hard d) EXP (Exponential time)
Answer: b) NP
97.If P = NP, then: a) All NP-Complete problems would have polynomial-time solutions.
b) All NP-Hard problems would be in P. c) All problems would be solvable in
logarithmic time. d) No problem would be computationally intractable.
Answer: a) All NP-Complete problems would have polynomial-time solutions.
98.The "reduction" process in complexity theory means: a) Reducing the size of the
input. b) Transforming one problem into another in polynomial time. c) Making an
algorithm more efficient. d) Converting an NP-Hard problem to a P problem.
Answer: b) Transforming one problem into another in polynomial time.
100. A polynomial time algorithm is one whose running time is bounded by: a) O(cn)
for some constant c>1. b) O(nc) for some constant c≥0. c) O(logn). d) O(n!).
**Answer: b)
46.