DAA Soltions
DAA Soltions
Characteristics of an Algorithm:
f(n)∈O(g(n)) if there exist constants c>0 and n0 such that f(n)≤c⋅g(n) for all n≥n0.f(n) \in O(g(n)) \text{
if there exist constants } c > 0 \text{ and } n_0 \text{ such that } f(n) \leq c \cdot g(n) \text{ for all } n
\geq n_0.f(n)∈O(g(n)) if there exist constants c>0 and n0 such that f(n)≤c⋅g(n) for all n≥n0.
Let f(n)=10n2+9f(n) = 10n^2 + 9f(n)=10n2+9 and g(n)=n2g(n) = n^2g(n)=n2. For large nnn, the
dominant term is 10n210n^210n2.
10n2+9≤11n2(for n≥1).10n^2 + 9 \leq 11n^2 \quad \text{(for \(n \geq 1\))}.10n2+9≤11n2(for n≥1).
Thus, we can choose c=11c = 11c=11 and n0=1n_0 = 1n0=1. Therefore, 10n2+9=O(n2)10n^2 + 9 =
O(n^2)10n2+9=O(n2).
A Fibonacci Heap is a data structure for priority queues that supports a collection of trees. It is
particularly efficient for operations like decrease-key and delete.
Key Features:
o Insert: O(1)O(1)O(1)
o Find-Min: O(1)O(1)O(1)
Fibonacci Heaps are used in algorithms like Dijkstra's shortest path and Prim's minimum spanning
tree.
A Binary Search Tree (BST) is a binary tree where each node has at most two children, and the tree
satisfies the following properties:
• The left subtree of a node contains nodes with values less than the node's value.
• The right subtree of a node contains nodes with values greater than the node's value.
• Both left and right subtrees are also binary search trees.
Applications:
• Searching, insertion, and deletion (time complexity O(logn)O(\log n)O(logn) for balanced
BSTs).
The Fractional Knapsack Problem is a greedy algorithm problem where a thief can take fractions of
items rather than whole items to maximize the total value in the knapsack.
Problem Statement:
• Given nnn items with weights wiw_iwi and values viv_ivi, and a knapsack with capacity
WWW, the goal is to maximize the total value by selecting items or fractions of items such
that their total weight does not exceed WWW.
Approach:
• Select items or fractions of items based on the sorted order until the knapsack is full.
• Kruskal's Algorithm: O(ElogE)O(E \log E)O(ElogE), where EEE is the number of edges.
Applications:
The minimum number of colors required for a graph is called its chromatic number.
The Activity Selection Problem is a greedy algorithm problem that involves selecting the maximum
number of activities that can be performed by a single person or resource, given that no two
selected activities overlap in time.
Approach:
• Select the first activity and iteratively choose the next activity that starts after the current
one finishes.
The Boyer-Moore Algorithm is a string-searching algorithm known for its efficiency in finding
substrings within a text.
Key Features:
• Preprocesses the pattern to determine the bad character rule and good suffix rule, which
help in skipping unnecessary comparisons.
Time Complexity:
• Best Case: O(n/m)O(n/m)O(n/m), where nnn is the text length and mmm is the pattern
length.
• Worst Case: O(n⋅m)O(n \cdot m)O(n⋅m), but often performs better in practice.
The Fast Fourier Transform (FFT) is an efficient algorithm to compute the Discrete Fourier Transform
(DFT) and its inverse. The DFT converts a signal from its time domain to its frequency domain, widely
used in signal processing, image analysis, and solving differential equations.
1. Find the range of the elements (minimum =0= 0=0, maximum =5= 5=5).
4. Place elements from AAA into their correct positions in a sorted array.
Implementation Example:
3. Child Distribution: The root of BkB_kBk has kkk children, which are roots of
Bk−1,Bk−2,…,B0B_{k-1}, B_{k-2}, \ldots, B_0Bk−1,Bk−2,…,B0.
Algorithms used for optimization problems where finding the exact solution is computationally
expensive. Example: Greedy algorithms for TSP.
Stable Algorithms:
• Bubble Sort
• Merge Sort
• Insertion Sort
Unstable Algorithms:
• Quick Sort
• Heap Sort
Example: Sorting [(4,A),(4,B)][(4, A), (4, B)][(4,A),(4,B)]: Stable sorts keep AAA before BBB.
Algorithm:
Time Complexity:
4. Every path from a node to its descendant leaves contains the same number of black nodes.
Steps to Insert:
1. Insert the element using the BST rules.
A B-Tree is a self-balancing search tree that maintains sorted data for efficient insertions, deletions,
and searches.
Properties:
2. Each node has at most 2t−12t-12t−1 keys and 2t2t2t children (for degree ttt).
Deletion Cases:
Definition:
Backtracking systematically explores all subsets to find the ones that sum to XXX.
Steps:
2. Add elements to the subset if the total sum does not exceed XXX.
3. Backtrack (remove the last added element) if the sum exceeds XXX.
Example:
• Start: [][][], Add 111, Add 333, Add 444 → Success (1+3+4=81+3+4=81+3+4=8).
An MST connects all vertices in a graph with the minimum total edge weight.
Kruskal’s Algorithm:
Steps:
1. Initialize distances from source to all vertices (000 for source, ∞\infty∞ for others).
2. Use a priority queue to select the vertex with the smallest distance.
The goal of the Traveling Salesman Problem (TSP) is to find the shortest possible route that visits
each city exactly once and returns to the starting city.
o Represent the cities as a cost matrix. The cost at (i,j)(i, j)(i,j) represents the distance
between city iii and city jjj.
2. Bounding Function:
o Perform row and column reductions to calculate a lower bound (minimum cost of
visiting all cities from the current node).
3. Branching:
o Divide the problem into smaller subproblems by considering all possible paths from
the current city.
4. Priority Queue:
5. Solution:
o Terminate when all cities are visited and return to the starting city.
Example:
Cost matrix:
[∞20301015∞35251030∞15202530∞]\begin{bmatrix} \infty & 20 & 30 & 10 \\ 15 & \infty & 35 &
25 \\ 10 & 30 & \infty & 15 \\ 20 & 25 & 30 & \infty \end{bmatrix}∞15102020∞30253035∞30
102515∞
1. P (Polynomial Time):
2. NP (Nondeterministic Polynomial):
3. NP-Hard:
4. NP-Complete:
Relationship:
1. Preprocessing:
Compute the prefix function (π\piπ) for the pattern PPP. This array indicates the longest
prefix of PPP that is also a suffix.
2. Pattern Matching:
Use π\piπ to skip unnecessary comparisons while searching for PPP in TTT.
Example:
Prefix Function:
π=[0,1,0,1,2,0,1].\pi = [0, 1, 0, 1, 2, 0, 1].π=[0,1,0,1,2,0,1].
Key Idea Make the best local choice at each step. Solve overlapping subproblems.
Optimality May not produce the global optimum. Guarantees the global optimum.
Examples Kruskal’s Algorithm, Prim’s Algorithm. Longest Common Subsequence, Matrix Chain.
Here:
Since f(n)=n2>n1.26f(n) = n^2 > n^{1.26}f(n)=n2>n1.26, the recurrence falls under Case 3 of Master
Theorem:
Definition:
Find the shortest paths from a source vertex to all other vertices in a weighted graph.
Algorithms:
1. Dijkstra's Algorithm:
2. Bellman-Ford Algorithm:
Time Complexity:
Space Complexity:
Trade-Offs:
Some algorithms, like quicksort, are time-efficient but may use less additional space, while dynamic
programming uses more memory for optimal solutions.
1. Finiteness:
2. Definiteness:
3. Input:
4. Output:
5. Effectiveness:
o All steps must be basic enough to be carried out in a finite amount of time using
resources like computation or memory.
6. Generality:
Discards paths that violate Discards paths with a bound worse than the best
Pruning
constraints. solution found.
B-Tree Properties:
1. Each node has a maximum of 2t−12t - 12t−1 keys and a minimum of t−1t-1t−1 keys (except
the root).
Process:
Given t=3t = 3t=3, start with an empty B-Tree and sequentially insert:
F,S,Q,K,C,L,H,T,V,W,M,R,N,P,A,B,X,Y,D,Z,E,G,I.F, S, Q, K, C, L, H, T, V, W, M, R, N, P, A, B, X, Y, D, Z, E, G,
I.F,S,Q,K,C,L,H,T,V,W,M,R,N,P,A,B,X,Y,D,Z,E,G,I.
• When a node exceeds 5 keys, it splits into two nodes and promotes the middle key to the
parent.
This iterative insertion will result in a balanced B-Tree. (You can simulate this step-by-step if needed.)
A subset of the edges of a graph that connects all vertices without cycles and has the minimum
possible total edge weight.
Kruskal’s Algorithm:
Example:
Graph:
Steps:
Definition:
4. All paths from a node to its descendants have the same number of black nodes.
Insertion Algorithm:
Example: Insert 10,20,30,1510, 20, 30, 1510,20,30,15. The tree balances after each step.
Algorithm:
Given a set of points, the convex hull is the smallest convex polygon containing all points.
Algorithms:
1. Graham’s Scan:
Steps:
2. Use a priority queue to select the vertex with the smallest tentative distance.
Definition:
Example:
Subset sum for w={5,7,10,12,15,18,20}w = \{5, 7, 10, 12, 15, 18, 20\}w={5,7,10,12,15,18,20}, m=35m
= 35m=35.
Use a state-space tree to explore all subsets.
Relationship:
If P=NPP = NPP=NP, every NP problem has a polynomial-time solution.
• Example: Sorting algorithms like Merge Sort, finding the greatest common divisor (GCD).
• These problems may not have a known polynomial-time solution but can be verified in
polynomial time.
NP-Complete (NPC):
• These are the hardest problems in NP. If any NP-Complete problem can be solved in
polynomial time, all NP problems can be solved in polynomial time.
3. P=NPP = NPP=NP (hypothetical): If this equality holds, all NP problems are solvable in
polynomial time.
Properties:
2. Each binomial tree follows the min-heap property (the key at the root is the smallest).
pseudo
Copy code
BinomialHeapUnion(H1, H2):
if H is empty:
return H
prev_x = None
x = H.head
next_x = x.sibling
prev_x = x
x = next_x
else:
if x.key ≤ next_x.key:
x.sibling = next_x.sibling
BinomialLink(next_x, x)
else:
if prev_x == NULL:
H.head = next_x
else:
prev_x.sibling = next_x
BinomialLink(x, next_x)
x = next_x
next_x = x.sibling
return H
pseudo
Copy code
BinomialHeapMinimum(H):
min_key = ∞
x = H.head
while x ≠ NULL:
min_key = x.key
x = x.sibling
return min_key
42. Knapsack Problem Using Greedy Approach
Given Items:
Steps:
2. Sort Items by Ratio: Sorted order = {Item 1, Item 2, Item 3, Item 5, Item 4}.
o Add a fraction (2/3) of Item 3 (Value = 23×120=80 \frac{2}{3} \times 120 = 8032
×120=80).
2. Iterate through the pattern, updating the longest prefix-suffix match for each position.
Approximation Algorithm:
Explanation:
• KMP algorithm efficiently searches for occurrences of a pattern PPP in a text TTT.
• It preprocesses the pattern to create a "prefix table" (or π[]\pi[]π[]) that allows the algorithm
to avoid redundant comparisons.
Algorithm:
pseudo
Copy code
ComputePrefixFunction(P):
m = length(P)
π[0] = 0
k=0
for q = 1 to m - 1:
k = π[k - 1]
if P[k] == P[q]:
k=k+1
π[q] = k
return π
Pattern Matching:
pseudo
Copy code
KMPMatcher(T, P):
n = length(T)
m = length(P)
π = ComputePrefixFunction(P)
q=0
for i = 0 to n - 1:
q = π[q - 1]
if P[q] == T[i]:
q=q+1
if q == m:
q = π[q - 1]
Time Complexity:
• Matching: O(n)O(n)O(n)
Purpose:
• Finds the shortest paths from a single source to all other vertices in a weighted graph.
Algorithm:
pseudo
Copy code
BellmanFord(G, src):
for i = 1 to |V| - 1:
distance[v] = distance[u] + w
return distance
Time Complexity:
• Overall: O(VE)O(VE)O(VE)
Problem:
Steps:
3. Greedy Approach:
Stable Sorting:
pseudo
Copy code
HeapSort(A):
BuildMaxHeap(A)
for i = length(A) to 2:
swap(A[1], A[i])
heap_size = heap_size - 1
MaxHeapify(A, 1)
Heapify on Input [25,57,48,36,12,91,86,32][25, 57, 48, 36, 12, 91, 86, 32][25,57,48,36,12,91,86,32]:
Sorted Array: [12,25,32,36,48,57,86,91][12, 25, 32, 36, 48, 57, 86, 91][12,25,32,36,48,57,86,91].
Algorithm:
pseudo
Copy code
RabinKarp(T, P, q):
m = length(P)
n = length(T)
h = pow(d, m-1) % q
p=0
t=0
for i = 0 to m-1:
p = (d * p + P[i]) % q
t = (d * t + T[i]) % q
for s = 0 to n-m:
if p == t:
if P == T[s:s+m]:
print("Pattern found at index", s)
if s < n-m:
t = (d * (t - T[s] * h) + T[s+m]) % q
• Spurious hits occur when hash values match but actual substrings do not.
• Place 4 queens on a 4x4 chessboard such that no two queens attack each other.
Algorithm:
Algorithm:
pseudo
Copy code
BinomialHeapDecreaseKey(H, x, k):
if k > x.key:
return
x.key = k
y=x
z = y.parent
while z is not NULL and y.key < z.key:
swap(y.key, z.key)
y=z
z = y.parent
• Matches: 1.
Definition:
The problem involves finding all subsets of a given set w={w1,w2,…,wn}w = \{w_1, w_2, \dots,
w_n\}w={w1,w2,…,wn} that sum up to a target value mmm.
Input:
2. Include the element in the subset and proceed if the current sum does not exceed mmm.
Algorithm:
pseudo
Copy code
if currentSum == targetSum:
print(subset)
return
return
Output Subsets:
{5,10,20},{5,12,18},{7,10,18},{15,20}.\{5, 10, 20\}, \{5, 12, 18\}, \{7, 10, 18\}, \{15,
20\}.{5,10,20},{5,12,18},{7,10,18},{15,20}.
Definition:
Rabin-Karp finds all occurrences of a pattern PPP in a text TTT using a hash function for efficient
comparison.
Algorithm:
1. Compute the hash value of PPP and the first window of TTT.
pseudo
Copy code
RabinKarp(T, P, q):
n = length(T)
m = length(P)
h = pow(d, m-1) % q
p_hash = 0
t_hash = 0
for i = 0 to m-1:
for s = 0 to n-m:
if p_hash == t_hash:
if P == T[s:s+m]:
if s < n-m:
Example:
• Matches are detected based on hash, with possible spurious hits corrected by direct
comparison.
Algorithm:
Algorithm:
pseudo
Copy code
CountingSort(A):
maxVal = max(A)
for x in A:
count[x] += 1
count[i] += count[i-1]
sortedArray = [0] * len(A)
for x in reversed(A):
sortedArray[count[x] - 1] = x
count[x] -= 1
return sortedArray
Algorithm:
pseudo
Copy code
NaiveStringMatcher(T, P):
n = length(T)
m = length(P)
for s = 0 to n - m:
if T[s:s+m] == P:
Definitions:
Key Relationships: