SiddharthashankarDaa
SiddharthashankarDaa
UNIT 1
Properties of an Algorithm:
Asymptotic Notations:
Types of Complexity:
1. Best Case: The minimum time taken by the algorithm for any input of size nn.
Represented as Ω\Omega.
2. Worst Case: The maximum time taken by the algorithm for any input of size nn.
Represented as OO.
3. Average Case: The expected time taken by the algorithm, averaged over all possible
inputs. Represented as Θ\Theta.
Proof:
By definition, f(n)=O(g(n))f(n) = O(g(n)) if there exist constants c>0c > 0 and n0>0n_0
> 0 such that f(n)≤c⋅g(n)f(n) \leq c \cdot g(n) for all n≥n0n \geq n_0.
• As n→∞n \to \infty, 2n2^n grows exponentially while 2n+12n+1 grows linearly.
• Thus, there exists a constant c>0c > 0 such that 2n+1≤c⋅2n2n+1 \leq c \cdot 2^n.
• Hence, 2n+1=O(2n)2n+1 = O(2^n).
Algorithm:
Complexity:
Time Complexity:
1. Best Case: O(nlogn)O(n \log n), when the array is already sorted.
2. Worst Case: O(n3/2)O(n^{3/2}), depending on the gap sequence.
3. Average Case: Depends on the gap sequence, often approximated as
O(n5/4)O(n^{5/4}).
Steps:
Radix Sort: A non-comparative sorting algorithm that processes integer digits from least
significant to most significant.
Steps:
Complexity:
Definition: A Binary Search Tree (BST) is a binary tree where each node has the following
properties:
1. The left subtree of a node contains only nodes with values less than the node's key.
2. The right subtree of a node contains only nodes with values greater than the node's
key.
3. Both left and right subtrees must also be BSTs.
Properties:
Definition: A Red-Black Tree is a self-balancing binary search tree where each node has an
additional attribute called color, which can be either red or black.
Properties:
Definition: A B-tree is a self-balancing search tree where nodes can have multiple keys
and children. It is designed to work well on disk-based systems by minimizing disk I/O
operations.
Significance of Degree:
• The degree (tt) determines the minimum and maximum number of keys a node can
hold:
o Minimum keys per node: t−1t - 1.
o Maximum keys per node: 2t−12t - 1.
• The degree also affects the branching factor, where the number of children is at
most 2t2t.
Example:
Definition: A Binomial Heap is a collection of binomial trees that satisfies the min-heap
property (parent node ≤ child nodes). Each binomial tree in the heap has a unique degree.
Example:
1. Merging: Combine the two heaps into a single sorted list of binomial trees based on
degree.
2. Linking: Merge trees of the same degree by linking one as a child of the other.
3. Reordering: Ensure no two binomial trees of the same degree exist.
Key Features:
Complexity of
O(logn)O(\log n). O(1)O(1).
Union
UNIT 3
Steps:
1. Divide: Break the problem into smaller sub-problems of the same type.
2. Conquer: Solve the sub-problems recursively.
3. Combine: Merge the solutions of the sub-problems to solve the original problem.
Definition: The convex hull of a set of points is the smallest convex polygon that can
enclose all the given points.
Solution:
1. Graham's Scan: Sort points by polar angle, then construct the hull using a stack.
Time complexity: O(nlogn)O(n \log n).
2. Jarvis March (Gift Wrapping): Start with the leftmost point and wrap around the
points. Time complexity: O(nh)O(nh), where hh is the number of hull vertices.
Definition: The greedy approach involves solving problems by making the locally optimal
choice at each step, assuming that this will lead to the globally optimal solution.
Steps:
Examples:
1. Problem: Given nn items with weights and values, maximize the total value for a
given weight capacity.
2. Approach:
a. Calculate value-to-weight ratio for each item.
b. Sort items by this ratio in descending order.
c. Pick items with the highest ratio until the capacity is filled.
Note: The greedy approach does not work for the 0/1 Knapsack problem.
Problem: Multiply two matrices AA (p×qp \times q) and BB (q×rq \times r) to get a
resultant matrix CC (p×rp \times r).
Optimized Approaches:
Definition: A spanning tree of a graph is a subgraph that includes all vertices of the graph,
is connected, and has no cycles.
Properties:
Applications:
1. Kruskal's Algorithm: Sort edges by weight and add them to the MST if they don’t
form a cycle.
2. Prim's Algorithm: Start with a single vertex and grow the MST by adding the
smallest edge connecting a vertex in the MST to one outside it.
Evaluation:
1. Use an algorithm that computes the shortest path from a source vertex to all other
vertices in a weighted graph.
2. Ensure no negative weight cycles exist if using algorithms like Dijkstra’s.
Approach:
UNIT 4
Steps:
Examples:
1. Problem: Given nn items with weights and values, maximize the total value for a
given weight capacity WW, where an item can either be included or excluded.
2. Dynamic Programming Approach:
a. Let dp[i][w]dp[i][w] represent the maximum value obtainable using the first
ii items with weight limit ww.
b. Recurrence relation: dp[i][w]={dp[i−1][w]if
wi>wmax(dp[i−1][w],dp[i−1][w−wi]+vi)otherwisedp[i][w] =
\begin{cases} dp[i-1][w] & \text{if } w_i > w \\ \max(dp[i-1][w], dp[i-1][w-
w_i] + v_i) & \text{otherwise} \end{cases}
c. Base case: dp[0][w]=0dp[0][w] = 0 .
Evaluation:
Problem: Place NN queens on an N×NN \times N chessboard such that no two queens
attack each other.
Backtracking Approach:
Steps:
Problem: Assign colors to vertices of a graph such that no two adjacent vertices share the
same color.
Backtracking Approach:
Example:
For a graph with three vertices V1,V2,V3V_1, V_2, V_3 and edges V1−V2,V2−V3V_1-V_2,
V_2-V_3:
Steps:
Definition: The Sum of Subset problem determines whether a subset of a given set of
numbers adds up to a specific target sum.
Backtracking Approach:
Example:
UNIT 5
Applications:
1. Search engines.
2. DNA sequence analysis.
3. Spam filters and plagiarism detection.
b) Give the name of any two algorithms to solve the string matching
problem.
1. Naïve Algorithm:
a. Complexity: O(nm)O(nm), where nn is the length of the text and mm is the
length of the pattern.
b. Works by checking all possible positions in the text.
2. KMP Algorithm:
a. Complexity: O(n+m)O(n + m).
b. Preprocesses the pattern to create a "longest prefix suffix" (LPS) array to skip
unnecessary comparisons.
Comparison:
KMP is more efficient than the Naïve algorithm due to its preprocessing step that avoids
redundant comparisons.
d) Define NP class.
Key Properties:
• Example: The Subset Sum Problem asks if there is a subset of numbers in a given
set that sums to a specific value.
• Verification: If a solution (subset) is given, it can be verified in polynomial time by
summing the subset elements.
Key Features:
Examples:
Definition: Randomized algorithms use random numbers at some point during their
execution to make decisions.
Types:
1. Las Vegas Algorithms: Always produce a correct result but the runtime is
probabilistic. Example: Randomized Quick Sort.
2. Monte Carlo Algorithms: May produce incorrect results with a small probability.
Applications: Cryptography, game theory, and probabilistic data structures (e.g., Bloom
filters).
h) Write any approximation algorithm.
Concept: Randomized Quick Sort selects a pivot randomly instead of using a fixed strategy
(e.g., first or last element). This helps avoid the worst-case scenario of O(n2)O(n^2) in
poorly distributed inputs.
Steps:
SECTION B
UNIT 1
Asymptotic Notations are mathematical tools to describe the running time or space
complexity of an algorithm as the input size (nn) Grows large.
Diagram:
| Running Time
| Θ(f(n))
| / \
| / \
| O(f(n)) -> Upper Bound (worst-case)
| Ω(f(n)) -> Lower Bound (best-case)
|_________________________
Input Size (n)
Selection Sort iteratively selects the smallest element and places it at the correct
position.
Algorithm:
Sorting 5,3,8,1,4,6,25, 3, 8, 1, 4, 6, 2 :
Algorithm:
Sorting 2,5,3,0,2,3,0,32, 5, 3, 0, 2, 3, 0, 3 :
1. Frequency Array:
Count[0]=2,Count[1]=0,Count[2]=2,Count[3]=3,Count[4]=0,Count[5]=1Count[0]
=2, Count[1]=0, Count[2]=2, Count[3]=3, Count[4]=0, Count[5]=1 .
2. Cumulative Count:
Count[0]=2,Count[1]=2,Count[2]=4,Count[3]=7,Count[4]=7,Count[5]=8Count[0]
=2, Count[1]=2, Count[2]=4, Count[3]=7, Count[4]=7, Count[5]=8 .
3. Sorted Array: 0,0,2,2,3,3,3,50, 0, 2, 2, 3, 3, 3, 5 .
Algorithm:
PARTITION Procedure:
Given:
1. T(1)=1T(1) = 1
2. T(n)=4T(n/3)+n2T(n) = 4T(n/3) + n^2 for n≥2n \geq 2.
Result:
T(n)=Θ(n2)T(n) = \Theta(n^2).
Given Recurrence:
Result:
T(n)=Θ(n2)T(n) = \Theta(n^2).
Base Case:
Inductive Step:
Final Solution:
T(n)=Θ(log2(n))T(n) = \Theta(\log_2(n)).
i) Solve the recurrence T(n)=4T(n/2)+n2lognT(n) = 4T(n/2) + n^2 \log
n using Master Method.
Given:
p=log2(4)=2p = \log_2(4) = 2.
• f(n)=n2lognf(n) = n^2 \log n has degree 2+ϵ2 + \epsilon, where ϵ>0\epsilon >
0.
• Since p=2<2+ϵp = 2 < 2 + \epsilon, the solution is dominated by f(n)f(n).
Result:
1. T(n)=c+3T(n−1)T(n) = c + 3T(n-1).
2. T(n−1)=c+3T(n−2)T(n-1) = c + 3T(n-2).
3. Substituting: T(n)=c+3[c+3T(n−2)]=c+3c+9T(n−2)T(n) = c + 3[c + 3T(n-2)] =
c + 3c + 9T(n-2).
4. Generalize: T(n)=c(1+3+32+...+3n−1)+3nT(0)T(n) = c(1 + 3 + 3^2 + ... + 3^{n-
1}) + 3^n T(0).
Geometric Series:
Result:
T(n)=Θ(3n)T(n) = \Theta(3^n).
UNIT 2
Insertion in a Red-Black (R-B) tree follows these rules to maintain its properties (balance
and coloring):
We will follow the Red-Black tree insertion rules. Below is a step-by-step process:
1. Insert 1010:
a. Becomes the root and is colored black.
2. Insert 1818:
a. Place as a red child of 1010.
3. Insert 77:
a. Place as a red child of 1010.
4. Insert 1515:
a. Causes Case 3 (recoloring): 1010 becomes red, 77 and 1818 become black.
5. Insert 1616:
a. Causes Case 4 (rotation): Perform a left rotation on 1515.
6. Continue inserting 30,25,40,60,2,1,7030, 25, 40, 60, 2, 1, 70 , applying recoloring and
rotations as needed.
Proof Sketch:
d) Delete EE, FF, and MM from a given B-Tree where degree (tt) = 3.
Given t=3t = 3 :
1. Deletion in a B-Tree involves maintaining balance and the properties of the tree:
a. Case 1: If the key to delete is in a leaf node, simply remove it.
b. Case 2: If the key is in an internal node, replace it with its in-order
predecessor or successor and delete recursively.
c. Case 3: If the node has fewer than t−1t-1 keys, borrow or merge nodes to
maintain balance.
Steps:
1. Delete EE: If EE is in a leaf, remove it directly. If not, replace it with its in-order
predecessor or successor.
2. Delete FF: Apply similar steps as for EE.
3. Delete MM: Apply the deletion process recursively, maintaining the B-Tree
properties.
e) Explain the process (cases) of insertion operation in B-tree.
Combines two binomial heaps into one by merging the root lists and ensuring the binomial
heap properties are maintained.
Algorithm:
1. Merge the root lists of the two heaps in increasing order of degree.
2. Traverse the merged list to combine trees of the same degree:
a. If three trees of the same degree are encountered, keep the leftmost one
separate and combine the other two.
3. Update the head pointer of the new heap.
Time Complexity:
Algorithm:
1. Find the root with the minimum key in the root list.
2. Remove this root and retrieve its children.
3. Reverse the order of the children and treat them as a separate binomial heap.
4. Union the resulting heap with the remaining heap.
Time Complexity:
Example:
Time Complexity:
• Amortized O(1)O(1).
1. Insert:
a. Add a new node to the root list.
b. Time Complexity: O(1)O(1).
2. Find Min:
a. Return the root with the minimum key.
b. Time Complexity: O(1)O(1).
3. Union:
a. Merge two heaps by concatenating their root lists.
b. Time Complexity: O(1)O(1).
4. Extract Min:
a. Remove the minimum root and union its children with the root list.
b. Time Complexity: O(logn)O(\log n).
5. Decrease Key:
a. Decrease the value of a node’s key and perform cascading cuts if necessary.
b. Amortized Time Complexity: O(1)O(1).
6. Delete:
a. Decrease the key to −∞-\infty, making it the minimum.
b. Extract the minimum.
c. Time Complexity: O(logn)O(\log n).
UNIT 3
Strassen's matrix multiplication method reduces the complexity of multiplying two n×nn
\times n matrices from the traditional O(n3)O(n^3) to approximately
O(n2.81)O(n^{2.81}). It achieves this by reducing the number of multiplications required.
Steps:
Example Calculation: Provide AA and BB, and their product will be computed step-by-
step.
Graham's Scan is a method to find the convex hull of a set of points in O(nlogn)O(n \log
n) time. The convex hull is the smallest polygon that encloses all the points.
Steps:
1. Find the pivot point: Choose the point with the lowest y-coordinate (and the lowest
x-coordinate if ties exist).
2. Sort points: Sort all points based on the polar angle they make with the pivot.
3. Process points: Use a stack to construct the convex hull:
a. Push the first three points onto the stack.
b. For each subsequent point, check the orientation of the top two points of the
stack and the current point.
c. If they form a right turn, pop the top point. Otherwise, push the current
point.
4. Continue until all points are processed.
Optimization Problem:
An optimization problem seeks to find the best solution from a set of feasible solutions.
Examples include finding the shortest path, minimal spanning tree, or maximum profit.
Greedy Method:
A greedy algorithm solves optimization problems by:
Examples:
This is a classical optimization problem where we aim to select the maximum number of
activities that don't overlap, given their start and finish times.
Steps:
Given:
Steps:
Given Data:
• Items: A,B,C,D,E,FA, B, C, D, E, F
• Weight: {100,50,40,20,10,10}\{100, 50, 40, 20, 10, 10\}
• Value: {40,35,20,4,10,6}\{40, 35, 20, 4, 10, 6\}
• Knapsack Capacity: 100100.
Ratio=ValueWeight\text{Ratio} = \frac{\text{Value}}{\text{Weight}}
a. A:0.4,B:0.7,C:0.5,D:0.2,E:1.0,F:0.6A: 0.4, B: 0.7, C: 0.5, D: 0.2, E: 1.0, F: 0.6 .
2. Sort items by ratio (descending order):
a. Order: E,B,F,C,A,DE, B, F, C, A, D.
3. Add items to the knapsack:
a. Add EE (Weight = 10, Value = 10). Remaining Capacity = 9090.
b. Add BB (Weight = 50, Value = 35). Remaining Capacity = 4040.
c. Add FF (Weight = 10, Value = 6). Remaining Capacity = 3030.
d. Add a fraction of CC (30/40=0.7530/40 = 0.75, Value = 20×0.75=1520
\times 0.75 = 15).
Optimal Solution:
Steps:
Algorithm:
Prim(Graph, Start):
1. Initialize MST = ∅, Visited = {Start}
2. While MST contains fewer than |V| - 1 edges:
a. Find the minimum edge (u, v) where u ∈ Visited and v ∉ Visited.
b. Add edge (u, v) to MST.
c. Add v to Visited.
3. Return MST.
Complexity:
Steps:
1. Sort all edges by weight.
2. Initialize MST as an empty set.
3. Add edges to the MST in increasing order of weight, ensuring no cycles are formed
(using Union-Find).
4. Stop when MST contains ∣V∣−1|V| - 1 edges.
Algorithm:
Kruskal(Graph):
1. Initialize MST = ∅
2. Sort edges by weight.
3. For each edge (u, v) in sorted order:
a. If u and v are in different sets:
i. Add (u, v) to MST.
ii. Union(u, v).
4. Return MST.
Complexity:
Steps:
1. Initialize distance d[v]d[v] for all vertices as ∞\infty, except source ss where
d[s]=0d[s] = 0.
2. Use a priority queue to store vertices based on their distance from ss.
3. Extract the vertex uu with the minimum distance.
4. For each neighbor vv of uu, update d[v]d[v] if a shorter path is found through uu.
5. Repeat until all vertices are processed.
Algorithm:
Dijkstra(Graph, Source):
1. Initialize distances: d[v] = ∞ for all v ≠ Source, d[Source] = 0.
2. PriorityQueue = {Source}.
3. While PriorityQueue is not empty:
a. u = Extract-Min(PriorityQueue).
b. For each neighbor v of u:
i. If d[u] + weight(u, v) < d[v]:
- Update d[v].
- Add/Update v in PriorityQueue.
4. Return distances.
Complexity:
Steps:
Algorithm:
BellmanFord(Graph, Source):
1. Initialize distances: d[v] = ∞ for all v ≠ Source, d[Source] = 0.
2. For i = 1 to |V| - 1:
a. For each edge (u, v) with weight w:
i. If d[u] + w < d[v]:
- Update d[v].
3. For each edge (u, v) with weight w:
a. If d[u] + w < d[v]:
- Negative weight cycle detected.
4. Return distances.
Example Graph: Let me know if you'd like the specific solution for the provided graph.
Complexity:
• O(V⋅E)O(V \cdot E).
UNIT 4
Definition:
The 0/1 Knapsack problem is a combinatorial optimization problem where each item can
either be included (1) or excluded (0) in a knapsack. The goal is to maximize the total profit
while not exceeding the knapsack's weight capacity.
Given Data:
Define DP[i][w]DP[i][w] as the maximum profit using the first ii items with a knapsack
capacity ww.
Recurrence Relation:
DP[i][w]={DP[i−1][w],if
weight[i]>wmax(DP[i−1][w],profit[i]+DP[i−1][w−weight[i]]),otherwise.DP[i][w] =
\begin{cases} DP[i-1][w], & \text{if } weight[i] > w \\ \max(DP[i-1][w], profit[i] + DP[i-
1][w - weight[i]]), & \text{otherwise}. \end{cases}
Steps:
Solution Table:
Item/Weigh
0 1 2 3 4 5 6 7 8 9 10
t
00 0 0 0 0 0 0 0 0 0 0 0
11 0 1 1 1 1 1 1 1 1 1 1
22 0 1 6 7 7 7 7 7 7 7 7
33 0 1 6 7 7 18 19 24 25 25 25
44 0 1 6 7 7 18 22 24 28 29 40
55 0 1 6 7 7 18 22 28 29 34 40
Result:
Steps:
Complexity:
Steps:
Complexity:
• O(V3)O(V^3).
Steps:
Recurrence Relation:
Algorithm:
LCS(X, Y):
1. Initialize L[0][j] = L[i][0] = 0 for all i, j.
2. For i = 1 to m:
For j = 1 to n:
If X[i] == Y[j]:
L[i][j] = L[i-1][j-1] + 1
Else:
L[i][j] = max(L[i-1][j], L[i][j-1])
3. Return L[m][n].
Complexity:
• O(m⋅n)O(m \cdot n).
The 8-Queens problem is a classic combinatorial problem where the goal is to place 8
queens on a chessboard such that no two queens threaten each other. This means no two
queens can be in the same row, column, or diagonal.
Backtracking Approach:
Steps:
4 Solutions:
1. Solution 1:
(1,1),(2,5),(3,8),(4,6),(5,3),(6,7),(7,2),(8,4)(1, 1), (2, 5), (3, 8), (4, 6), (5, 3), (6, 7), (7, 2),
(8, 4).
Chessboard:
Q . . . . . . .
. . . . Q . . .
. . . . . . . Q
. . . . . Q . .
. . Q . . . . .
. . . . . . Q .
. Q . . . . . .
. . . Q . . . .
2. Solution 2:
(1,1),(2,6),(3,8),(4,3),(5,7),(6,4),(7,2),(8,5)(1, 1), (2, 6), (3, 8), (4, 3), (5, 7), (6, 4), (7, 2),
(8, 5).
Chessboard:
Q . . . . . . .
. . . . . Q . .
. . . . . . . Q
. . Q . . . . .
. . . . . . Q .
. . . Q . . . .
. Q . . . . . .
. . . . Q . . .
3. Solution 3:
(1,2),(2,4),(3,6),(4,8),(5,3),(6,1),(7,7),(8,5)(1, 2), (2, 4), (3, 6), (4, 8), (5, 3), (6, 1), (7, 7),
(8, 5).
Chessboard:
. Q . . . . . .
. . . Q . . . .
. . . . . Q . .
. . . . . . . Q
. . Q . . . . .
Q . . . . . . .
. . . . . . Q .
. . . . Q . . .
4. Solution 4:
(1,2),(2,5),(3,7),(4,3),(5,1),(6,6),(7,8),(8,4)(1, 2), (2, 5), (3, 7), (4, 3), (5, 1), (6, 6), (7, 8),
(8, 4).
Chessboard:
. Q . . . . . .
. . . . Q . . .
. . . . . . Q .
. . Q . . . . .
Q . . . . . . .
. . . . . Q . .
. . . . . . . Q
. . . Q . . . .
Definition:
Graph coloring is the assignment of colors to the vertices of a graph such that no two
adjacent vertices share the same color. Optimal coloring uses the minimum number of
colors to achieve this.
Applications:
• Scheduling problems.
• Register allocation in compilers.
• Map coloring.
Example:
Result:
A Hamiltonian Cycle is a closed path in a graph that visits each vertex exactly once and
returns to the starting vertex. It is a special case of the Hamiltonian path, where the
starting and ending points are the same.
Key Properties:
Example:
E={(A,B),(B,C),(C,D),(D,E),(E,A),(A,C),(B,E)}E = \{(A, B), (B, C), (C, D), (D, E), (E, A), (A, C),
(B, E)\}.
Problem Statement:
Given weights W={3,4,5,6}W = \{3, 4, 5, 6\}, find a subset whose sum equals 13 using
backtracking.
Backtracking Approach:
Steps:
Solution:
Solution Explores all possible solutions Prunes subtrees that cannot lead
Space systematically. to optimal solutions.
The Travelling Salesman Problem (TSP) is a classic optimization problem in the field of
operations research and computer science. In this problem, a salesman is given a set of
cities to visit. The goal is to determine the shortest possible route that the salesman can
take to visit each city exactly once and return to the starting city.
Objective:
The objective is to minimize the total travel distance (or cost) while visiting each city
exactly once and returning to the origin.
Example:
If there are 4 cities, A, B, C, and D, the salesman needs to find the shortest route that
allows him to visit each city once and return to the starting point.
Approach to Solve TSP:
One common approach to solving the TSP is using the Brute Force Method. Here's how it
works:
For larger datasets, more efficient approaches are used, such as:
These methods aim to reduce the computational complexity and find near-optimal
solutions in a more efficient manner.
j) Knapsack Problem
1. 0/1 Knapsack Problem: Each item can either be taken entirely or not at all (no
fractional items allowed).
2. Fractional Knapsack Problem: Items can be divided into fractions, allowing partial
selection of items.
To solve the fractional knapsack problem, we use the value-to-weight ratio for each item.
The idea is to prioritize items with the highest value-to-weight ratios and add them to the
knapsack until it reaches its capacity.
Thus, the optimal value that can be obtained with the given items and knapsack capacity
is 38.
UNIT 5
Significance:
The KMP algorithm efficiently searches for a pattern in a given text by preprocessing the
pattern to create a longest prefix-suffix (LPS) table. This table avoids unnecessary re-
checking of characters, leading to linear time complexity.
c) Write and explain the Naïve String Matching Algorithm. Discuss its time
complexity.
The Naïve algorithm checks for the occurrence of a pattern P[0...m−1]P[0...m-1] in a text
T[0...n−1]T[0...n-1] by sliding the pattern over the text one character at a time.
Steps:
1. Compare the pattern PP with the substring of TT starting at each position ii.
2. If P[0...m−1]=T[i...i+m−1]P[0...m-1] = T[i...i+m-1], report a match.
3. Repeat until all positions ii are checked.
Pseudocode:
for i = 0 to n - m:
for j = 0 to m - 1:
if T[i + j] != P[j]:
break
if j == m:
print "Pattern found at index", i
Time Complexity:
Rabin-Karp Algorithm:
Steps:
1. Compute the hash value of the pattern PP and the first substring of text
T[0...m−1]T[0...m-1].
2. Slide the pattern over the text and recompute the hash value for
T[i...i+m−1]T[i...i+m-1] using a rolling hash technique.
3. Compare the hash values:
a. If the hash values match, verify the substring character by character to
confirm the match.
4. Repeat until all positions are checked.
Significance:
• Average Case: O(n+m)O(n + m), where nn is the length of the text and mm is the
length of the pattern.
• Worst Case: O(n×m)O(n \times m), if all hash values match but substrings differ.
The Rabin-Karp algorithm uses hashing to efficiently search for a pattern in a given text. It
computes the hash values of the pattern and the substrings of the text and uses these
hash values to find potential matches. If the hash values of a substring and the pattern
match, a character-by-character comparison is performed to confirm the match.
Steps:
Pseudocode:
Time Complexity:
• Average Case: O(n+m)O(n + m), where nn is the length of the text and mm is the
length of the pattern. This is because, on average, hashing is done in constant time.
• Worst Case: O(n×m)O(n \times m), if there are hash collisions, requiring
character-by-character comparison for each match.
The algorithm is efficient when multiple patterns are being searched, as the hashing step
can be reused.
Steps:
Time Complexity:
Thus, the overall time complexity is O(n+m)O(n + m), making KMP very efficient
compared to the Naïve String-Matching Algorithm.
g) Compare NP-Hard and NP-Complete Problems
NP-Hard Problems:
NP-Complete Problems:
Key Differences:
Verification in
Not necessarily Yes
Polynomial Time
May or may not have a solution in A solution exists and can be
Solvability
polynomial time verified in polynomial time
Halting problem, Traveling Boolean satisfiability, 3-SAT,
Examples
Salesman (optimization version) Knapsack Problem
Conclusion:
h) Approximation Algorithm
Approximation algorithms are mainly used for NP-hard problems that do not have known
efficient algorithms to find the exact optimal solution. These algorithms offer a trade-off
between time complexity and the quality of the solution.
• Traveling Salesman Problem (TSP): Finding the shortest possible route to visit all
cities. Approximation algorithms such as the Christofides’ algorithm guarantee a
solution within 1.5 times the optimal.
• Vertex Cover Problem: An approximation algorithm for the vertex cover problem
provides a solution that is at most 2 times the optimal.
Approximation Ratio:
ρ=C(A)C(O)\rho = \frac{C(A)}{C(O)}
Where:
If ρ=1\rho = 1, the algorithm is exact, i.e., it always produces the optimal solution. If
ρ>1\rho > 1, the algorithm's solution is at most ρ\rho times worse than the optimal
solution.
Randomized algorithms are algorithms that use random numbers or random choices
during execution to make decisions or improve performance. These algorithms are
designed to have a good expected performance, even though they may behave
unpredictably for specific inputs due to the inherent randomness.
1. Las Vegas Algorithms: Always produce a correct result, but their running time is
subject to randomness. The expected running time may be better than
deterministic algorithms.
2. Monte Carlo Algorithms: May produce incorrect results with a certain probability,
but their running time is fixed. The probability of error can be reduced by repeating
the algorithm multiple times.
Applications:
Advantages:
Disadvantages:
j) Randomized QuickSort
Choose a pivot element randomly from the array instead of using the first element, the last
element, or the median as in traditional QuickSort.
Partition the array into two subarrays – one with elements smaller than the pivot and one
with elements greater than the pivot. The pivot is placed in its final position.
Recursively apply QuickSort to the subarrays formed on either side of the pivot.
• Average Case Time Complexity: O(nlogn)O(n \log n), where nn is the number of
elements in the array.
o On average, the random pivot divides the array into two equal halves,
resulting in a logarithmic number of recursive calls.
• Worst Case Time Complexity: O(n2)O(n^2), which happens when the pivot
always ends up being the smallest or largest element (i.e., when the array is already
sorted or nearly sorted).
• Best Case Time Complexity: O(nlogn)O(n \log n), when the pivot divides the
array into roughly equal parts.
Randomized QuickSort is less likely to encounter the worst-case scenario compared to the
standard deterministic QuickSort. The random selection of the pivot significantly reduces
the likelihood of encountering the worst-case time complexity, making it more efficient on
average.