Unit 4
Unit 4
Often leads to algorithms with better time complexity than naive solutions.
1. Binary Search
Problem Statement:
Given a sorted array of size n, find the position of a target element x or determine that it does
not exist.
Algorithm:
Pseudocode:
kotlin
CopyEdit
if arr[mid] == x:
return mid
else:
Time Complexity:
Problem Statement:
Find both the maximum and minimum elements in an array of size n with minimum number of
comparisons.
Naive Approach:
Compare max of left and right halves to find overall max. Similarly for min.
Algorithm:
Divide array into halves until base case (one or two elements).
Time Complexity:
3. Merge Sort
Problem Statement:
Idea:
Merge Process:
Use two pointers to compare elements and copy the smaller one to the main array.
Pseudocode:
sql
CopyEdit
MergeSort(arr, left, right):
Time Complexity:
Space Complexity:
Summary Table
Divide and
Binary Search O(logn)O(\log n)O(logn) O(1)O(1)O(1)
Conquer
Finding Max & O(n)O(n)O(n), fewer O(logn)O(\log n)O(logn) due to Divide and
Min comparisons recursion stack Conquer
30 MCQs on Divide and Conquer (Binary Search, Max & Min, Merge Sort)
7. How many comparisons does the naive method for finding max and min in an array of size
n take?
A) nnn
B) 2n2n2n
C) 2n−22n - 22n−2
D) 3n/23n/23n/2
Answer: C
Explanation: Naive method compares every element with current max and min, leading to
2(n−1)2(n-1)2(n−1) comparisons.
8. The divide and conquer method for finding max and min requires how many comparisons?
A) nnn
B) 2n2n2n
C) 2n−22n - 22n−2
D) 3n/2−23n/2 - 23n/2−2
Answer: D
Explanation: It reduces comparisons to approximately 1.5n1.5n1.5n.
11. Which of the following sorting algorithms is NOT based on divide and conquer?
A) Quick Sort
B) Merge Sort
C) Heap Sort
D) Binary Search
Answer: C
Explanation: Heap Sort is based on heap data structure, not divide and conquer.
14. What happens in the "combine" step of divide and conquer for Merge Sort?
A) Split the array into halves
B) Merge two sorted halves
C) Recursively sort each half
D) Find the middle element
Answer: B
Explanation: Combine step merges two sorted halves into one sorted array.
17. In Merge Sort, the splitting of the array stops when the size becomes:
A) 0
B) 1
C) 2
D) n/2
Answer: B
Explanation: Recursion stops when sub-array size is 1 (single element arrays are sorted).
18. Which sorting algorithm uses divide and conquer and has average time complexity
O(nlogn)O(n \log n)O(nlogn) but is NOT stable?
A) Merge Sort
B) Quick Sort
C) Binary Search
D) Bubble Sort
Answer: B
Explanation: Quick Sort is divide and conquer, O(nlogn)O(n \log n)O(nlogn) average, but not
stable.
25. What is the minimum number of comparisons needed to find both max and min in an
array of size n using divide and conquer?
A) 2n2n2n
B) 1.5n−21.5n - 21.5n−2
C) n−1n - 1n−1
D) nnn
Answer: B
Explanation: Divide and conquer reduces comparisons to about 1.5n−21.5n - 21.5n−2.
26. Which step in divide and conquer algorithms can often be parallelized?
A) Divide
B) Conquer
C) Combine
D) All of the above
Answer: B
Explanation: Subproblems can be solved independently in parallel during conquer.
Problems are grouped into complexity classes like P, NP, NP-hard, and NP-complete.
Halting problem
Problems at least as hard as the hardest problems in NP.
NP-hard (undecidable),
May or may not be in NP (may not be verifiable quickly).
Optimization problems
4. NP-hard Problems
They are used in optimization and decision problems where no known polynomial
algorithm exists.
5. NP-complete Problems
Understanding which problems are NP-complete helps in deciding whether to look for
exact algorithms or approximate/heuristic solutions.
9. Summary Table
Polynomial Polynomial
Term Meaning
Verifier? Reducible?
P ⊆ NP ⊆ NP-hard
Would you like me to continue now with 30 MCQs on NP Hard and NP-Completeness or move
next to 4-star topics?
You said:
ChatGPT said:
Understood! From now on, I’ll give you detailed notes on the topic first, then immediately 30
MCQs with answers and explanations for that topic.
Now, here are the 30 MCQs on NP Hard and NP-Completeness with answers and explanations.
30. Which of these is true about the relationship between P and NP?
A) P ⊆ NP
B) NP ⊆ P
C) P = NP is proven true
D) P = NP is proven false
Answer: A
Explanation: Problems solvable in polynomial time are also verifiable in polynomial time.
1. Algorithm Analysis
2. Time-Space Tradeoff
Sometimes improving time complexity increases space usage and vice versa.
3. Asymptotic Notations
Used to describe the limiting behavior of an algorithm’s complexity as input size nnn grows
large.
Big-O Notation O(f(n))O(f(n))O(f(n))
Describes an upper bound on the time/space complexity, i.e., the worst-case scenario.
Example: T(n)=O(n2)T(n) = O(n^2)T(n)=O(n2) means the time grows at most proportional
to n2n^2n2.
For example, quicksort has average-case O(nlogn)O(n \log n)O(nlogn) but worst-case
O(n2)O(n^2)O(n2).
To simplify analysis, often worst-case asymptotic notations are used (e.g., Big-O),
ignoring average/best cases.
O(nlogn)O(n \log n)O(nlogn) Merge sort, Heap sort Divide and conquer sorting
Understanding these concepts is crucial for algorithm design, optimizing solutions, and
writing efficient code.
Now, following your instruction, I will generate 30 MCQs on this topic with answers and
explanations.
4. Which sorting algorithm has an average time complexity of O(nlogn)O(n \log n)O(nlogn)?
A) Bubble sort
B) Merge sort
C) Selection sort
D) Insertion sort
Answer: B
Explanation: Merge sort uses divide and conquer with average and worst time complexity
O(nlogn)O(n \log n)O(nlogn).
16. What does removing condition from conditional asymptotic notation usually imply?
A) Considering only average case
B) Considering worst-case only
C) Considering best-case only
D) Ignoring all cases
Answer: B
Explanation: Usually, worst-case Big-O is used for simplicity.
19. Which of the following algorithms has a worst-case time complexity of O(n2)O(n^2)O(n2)?
A) Quick sort
B) Merge sort
C) Selection sort
D) Binary search
Answer: C
Explanation: Selection sort is O(n2)O(n^2)O(n2) in all cases.
22. Which of these sorting algorithms is NOT based on divide and conquer?
A) Merge sort
B) Quick sort
C) Bubble sort
D) Heap sort
Answer: C
Explanation: Bubble sort is a simple comparison-based sort without divide and conquer.
1. Recurrence Equations
General form:
Where:
a) Substitution Method
c) Master Theorem
T(n)=Θ(nlogbalogn)T(n) =
f(n)=Θ(nlogba)f(n) = \Theta(n^{\log_b
2 \Theta(n^{\log_b a} \log
a})f(n)=Θ(nlogba)
n)T(n)=Θ(nlogbalogn)
f(n)=Ω(nlogba+ε)f(n) = \Omega(n^{\log_b a +
T(n)=Θ(f(n))T(n) =
3 \varepsilon})f(n)=Ω(nlogba+ε) and regularity
\Theta(f(n))T(n)=Θ(f(n))
condition holds
✅ Example 1:
✅ Example 2:
Linear Search: Sequentially checks every element in the list until a match is found or the
list ends.
Time Complexity:
Space Complexity:
O(1)O(1)O(1), since no extra space is used.
18. In linear search, if item is not present, how many comparisons are made?
A) 1
B) n/2n/2n/2
C) nnn
D) nlognn \log nnlogn
Answer: C
Explanation: In worst-case, it checks all nnn elements.
22. If each recursive call halves the input, how many calls occur?
A) logn\log nlogn
B) nnn
C) n2n^2n2
D) nlognn \log nnlogn
Answer: A
Explanation: Halving means logarithmic depth.
27. If base case is T(1)=1T(1) = 1T(1)=1, how many calls does T(n)=T(n−1)+1T(n) = T(n-1) +
1T(n)=T(n−1)+1 make?
A) 1
B) nnn
C) n2n^2n2
D) logn\log nlogn
Answer: B
Explanation: It recurses nnn times.
29. Which method is best when solving recurrences with irregular terms?
A) Recursion tree
B) Master theorem
C) Substitution
D) Trial and error
Answer: C
Explanation: Substitution helps guess and verify solutions.
🔷 1. What is Backtracking?
🔷 2. Characteristics of Backtracking
Recursive in nature.
Follows DFS traversal.
python
CopyEdit
if solution is complete:
output(solution)
else:
if is_valid(candidate, solution):
solution.add(candidate)
backtrack(solution, constraints)
solution.remove(candidate)
Steps:
🧠 Problem:
Place 8 queens on an 8×8 chessboard such that no two queens attack each other (no same row,
column, or diagonal).
🎯 Approach:
Python Pseudocode:
python
CopyEdit
if row == 8:
print(board)
return
board[row][col] = 1
solve(board, row + 1)
board[row][col] = 0
🧠 Problem:
Given a graph G(V, E), determine if there is a cycle that visits every vertex exactly once and
returns to the starting point.
🎯 Approach:
Start at a vertex.
Try to add one vertex at a time to the path.
Pseudocode:
python
CopyEdit
if pos == len(graph):
path[pos] = v
return True
path[pos] = -1
return False
o N-Queens
o Sudoku
o Crossword Fillers
Graph problems
o Hamiltonian cycle
o Graph Coloring
Combinatorial problems
o Subset generation
🔷 7. Time Complexity
Pruning
Heuristics
Forward checking
✅ Summary:
Concept Description
✅ Answer: C
📘 Explanation: Backtracking uses Depth-First Search to explore all possible solutions
recursively.
✅ Answer: C
📘 Backtracking eliminates non-viable solutions through pruning.
✅ Answer: C
📘 Backtracking may have exponential complexity where k is the number of options and n is
depth.
✅ Answer: B
📘 Backtracking ensures constraints are met at each decision point.
✅ Answer: C
📘 Pruning helps avoid exploring invalid or unproductive branches.
✅ Answer: C
📘 The 8-Queens problem is a famous backtracking example.
9. In the N-Queens problem, how many queens are placed on an N×N board?
A) N
B) N/2
C) 2N
D) 1
✅ Answer: A
📘 N queens are to be placed on an N×N board with no mutual attack.
✅ Answer: B
📘 Backtracking relies on the call stack for recursive calls (LIFO).
✅ Answer: C
📘 Hamiltonian Cycle is solved using backtracking over graphs.
✅ Answer: C
📘 The solution is complete when N queens are placed successfully.
✅ Answer: C
📘 Hamiltonian cycle must include each vertex exactly once and return.
✅ Answer: C
📘 Valid path must use only unvisited, adjacent vertices.
✅ Answer: C
📘 Backtracking means reverting to the last decision point.
16. In backtracking, we proceed only if:
A) All elements are sorted
B) Solution meets constraints
C) There are cycles
D) We have completed recursion
✅ Answer: B
📘 At every recursive step, solution must meet the given constraints.
✅ Answer: C
📘 Graph coloring is a classic constraint problem, solved using backtracking.
✅ Answer: A
📘 Initially N choices for each queen, resulting in N! permutations.
✅ Answer: B
📘 After placing a queen, move to the next row.
20. Which operation is essential to remove a wrong decision in backtracking?
A) Stack pop
B) Return
C) Undo the choice
D) Break
✅ Answer: C
📘 If a choice leads to failure, it is undone to explore new paths.
✅ Answer: B
📘 Quick Sort is a divide-and-conquer problem, not backtracking.
✅ Answer: C
📘 Each level represents a decision point in the recursive solution tree.
✅ Answer: C
📘 Without pruning, backtracking degenerates into brute-force search.
✅ Answer: C
📘 Backtracking can explore all feasible solutions if not stopped early.
📘 Detailed Notes:
Importance: Frequently appears in graph-related questions. Moderate priority for Bihar STET
Paper 2.
🔷 1. Connected Components
✅ Definition:
🔹 Example:
For a graph with vertices:
{A, B, C} connected
{D, E} connected
Then it has 2 connected components.
🔹 Algorithms to Find:
🔹 Time Complexity:
🔷 2. Spanning Tree
✅ Definition:
Is connected
🔹 Properties:
🔹 MST Algorithms:
Prim’s Algorithm
Kruskal’s Algorithm
🔹 Applications:
Network design (minimum wiring cost, etc.)
🔷 3. Biconnected Components
✅ Definition:
🔹 Importance:
Helps identify articulation points – nodes whose removal increases the number of
connected components.
🔹 How to Find:
Maintain:
o Discovery Time
o Low Value
o Parent node
🔹 Key Concept:
🔷 Summary Table
Time
Concept Key Idea Algorithm
Complexity
Connected
Max set of connected vertices DFS/BFS O(V + E)
Components
DFS /
Spanning Tree Tree including all vertices O(E log V)
Prim/Kruskal
Time
Concept Key Idea Algorithm
Complexity
Next up:
You said:
yes
ChatGPT said:
Here are:
✅ 20 MCQs on
✅ Correct Answer
📘 Explanation
✅ Answer: B
📘 Explanation: DFS or BFS explores all connected nodes, helping identify components.
3. How many connected components are there in a graph with no edges and 5 nodes?
A) 0
B) 1
C) 5
D) 10
✅ Answer: C
📘 Explanation: Each isolated node is a separate component.
✅ Answer: C
📘 Explanation: DFS visits each vertex and edge once.
✅ Answer: C
📘 Explanation: DFS uses a stack (explicit or via recursion).
✅ Answer: C
📘 Explanation: Both explore full components effectively.
8. In a graph with 10 vertices and no edges, how many components are there?
A) 0
B) 1
C) 10
D) 100
✅ Answer: C
📘 Explanation: Each vertex is disconnected, so 10 separate components.
✅ Answer: C
📘 Explanation: A connected graph has only one connected component.
✅ Answer: C
📘 Explanation: Spanning tree must include all vertices.
12. How many edges does a spanning tree of a graph with V vertices have?
A) V
B) V – 1
C) V + 1
D) 2V
✅ Answer: B
📘 Explanation: Always V – 1 edges in a spanning tree.
✅ Answer: C
📘 Explanation: Both Prim’s and Kruskal’s are standard MST algorithms.
✅ Answer: B
📘 Explanation: A connected graph may have many spanning trees.
✅ Answer: D
📘 Explanation: Spanning tree does not include all edges.
✅ Answer: C
📘 Explanation: Biconnected graph has no vertex whose removal disconnects it.
17. Which algorithm is used to find articulation points and biconnected components?
A) Kruskal
B) Dijkstra
C) Tarjan’s Algorithm
D) Floyd-Warshall
✅ Answer: C
📘 Explanation: Tarjan’s algorithm detects articulation points using DFS.
✅ Answer: B
📘 Explanation: Removing this vertex disconnects part of the graph.
19. What is the minimum number of articulation points in a tree with more than 2 vertices?
A) 0
B) 1
C) ≥ 1
D) Cannot say
✅ Answer: C
📘 Explanation: Trees often have articulation points unless it’s a star or chain.
✅ Answer: B
📘 Explanation: Tarjan’s algorithm runs in linear time relative to graph size.
⭐⭐⭐⭐ Topic 6:
General Method, Multistage Graphs, All-Pair Shortest Paths, Optimal Binary Search Trees
Detailed Notes:
The key idea: solve each subproblem once and use its result multiple times.
2. Multistage Graphs
A multistage graph is a directed graph where vertices are divided into stages, and edges
only go from one stage to the next.
The goal is to find the shortest path from the source node in the first stage to the sink
node in the last stage.
Dynamic Programming can solve this by computing shortest distances from the last
stage backward to the first.
The problem involves finding shortest paths between all pairs of vertices in a weighted
graph.
Given a set of sorted keys and probabilities of searching for each key, OBST finds a BST
that minimizes the expected search cost.
It uses DP to consider all possible roots and combines costs of left and right subtrees to
find the minimal total cost.
Cost matrix and root matrix are computed to build the OBST structure.
✅ Answer: B
📘 Explanation: DP stores results of subproblems to reuse them.
✅ Answer: B
📘 Explanation: Edges only go from one stage to the next.
✅ Answer: B
📘 Explanation: Floyd-Warshall finds shortest paths between all pairs.
4. Time complexity of Floyd-Warshall is:
A) O(V + E)
B) O(V²)
C) O(V³)
D) O(log V)
✅ Answer: C
📘 Explanation: It uses triple nested loops over vertices.
✅ Answer: C
📘 Explanation: OBST minimizes weighted path length based on search frequencies.
✅ Answer: C
📘 Explanation: OBST uses DP to find minimal cost structures.
✅ Answer: C
📘 Explanation: DP can be top-down or bottom-up.
8. Multistage graphs must be:
A) Undirected
B) Weighted and directed with stages
C) Cyclic graphs
D) Unweighted
✅ Answer: B
📘 Explanation: Multistage graphs are directed and weighted.
✅ Answer: B
📘 Explanation: It tries all intermediate vertices for shorter paths.
✅ Answer: C
📘 Explanation: Cost is weighted sum of key frequencies by their depth.
✅ Answer: B
📘 Explanation: DP handles overlapping subproblems efficiently.
12. Multistage graph shortest path can be solved:
A) Using DFS only
B) Using DP backward from destination
C) Using BFS only
D) Using brute force only
✅ Answer: B
📘 Explanation: DP backward approach calculates shortest paths.
✅ Answer: B
📘 Explanation: It detects negative cycles; negative weights allowed if no cycle.
✅ Answer: A
📘 Explanation: Cost and root matrices store minimal costs and subtree roots.
✅ Answer: B
📘 Explanation: Edges only connect consecutive stages.
16. Which method can solve OBST problem efficiently?
A) Brute force recursion
B) Greedy method
C) Dynamic Programming
D) Backtracking
✅ Answer: C
📘 Explanation: DP optimally computes minimal expected cost.
✅ Answer: C
📘 Explanation: All pairs distances are updated iteratively.
✅ Answer: D
📘 Explanation: All use DP techniques.
✅ Answer: C
📘 Explanation: It's all-pairs shortest path algorithm.
20. What is the main difference between DP and Divide & Conquer?
A) DP solves overlapping subproblems, divide and conquer solves independent subproblems
B) DP uses recursion only
C) Divide and conquer stores subproblems
D) No difference
✅ Answer: A
📘 Explanation: DP stores solutions to overlapping problems, divide and conquer does not.
✅ Answer: B
📘 Explanation: OBST optimizes expected search time based on key frequencies.
✅ Answer: A
📘 Explanation: DP approach runs efficiently based on stages and vertices.
✅ Answer: B
📘 Explanation: It uses DP to iteratively update paths.
24. In OBST, if all keys have equal frequency, the optimal tree is:
A) Balanced BST
B) Linked list
C) Random BST
D) Skewed tree
✅ Answer: A
📘 Explanation: Balanced BST minimizes search cost when frequencies are equal.
✅ Answer: C
📘 Explanation: Floyd-Warshall solves all pairs, not single source shortest path.