DAA Summarized Unit 5
DAA Summarized Unit 5
Algebraic Computation
Algebraic computation deals with efficient algorithms to perform arithmetic
operations on polynomials and matrices.
Example: Polynomial Multiplication
Given two polynomials A(x) and B(x), the goal is to compute their product
efficiently.
Code Implementation
python
CopyEdit
def polynomial_multiplication(A, B):
n = len(A)
m = len(B)
result = [0] * (n + m - 1)
for i in range(n):
for j in range(m):
result[i + j] += A[i] * B[j]
return result
# Example Usage
A = [1, 2, 3] # Represents 1 + 2x + 3x^2
B = [4, 5] # Represents 4 + 5x
print("Resultant Polynomial:", polynomial_multiplication(A, B))
y = [0] * n
for i in range(n // 2):
y[i] = a_even[i] + w * a_odd[i]
y[i + n // 2] = a_even[i] - w * a_odd[i]
if invert:
y[i] /= 2
y[i + n // 2] /= 2
w *= w_n
return y
# Example Usage
A = [1, 2, 3, 4]
result = fft(A)
print("FFT Result:", result)
3. String Matching
String matching is finding occurrences of a pattern in a text.
Example: Knuth-Morris-Pratt (KMP) Algorithm
The KMP algorithm preprocesses the pattern to create a prefix table and avoids
rechecking characters.
Code Implementation
python
CopyEdit
def kmp_search(text, pattern):
def compute_lps(pattern):
lps = [0] * len(pattern)
length = 0
i=1
while i < len(pattern):
if pattern[i] == pattern[length]:
length += 1
lps[i] = length
i += 1
else:
if length != 0:
length = lps[length - 1]
else:
lps[i] = 0
i += 1
return lps
lps = compute_lps(pattern)
i=j=0
matches = []
while i < len(text):
if pattern[j] == text[i]:
i += 1
j += 1
if j == len(pattern):
matches.append(i - j)
j = lps[j - 1]
elif i < len(text) and pattern[j] != text[i]:
if j != 0:
j = lps[j - 1]
else:
i += 1
return matches
# Example Usage
text = "ababcabcabababd"
pattern = "ababd"
print("Pattern found at indices:", kmp_search(text, pattern))
4. Theory of NP-Completeness
The Theory of NP-Completeness studies problems where:
• The solution can be verified in polynomial time (NP problems).
• Some problems (NP-complete) are as hard as any other problem in NP.
Example: SAT Problem
The Boolean Satisfiability Problem (SAT) is a classic NP-complete problem.
Example Statement
Given a Boolean formula, determine if there is an assignment of variables that
makes the formula true:
scss
CopyEdit
(x1 OR NOT x2) AND (x2 OR x3)
5. Approximation Algorithms
Approximation algorithms provide near-optimal solutions to optimization
problems in polynomial time.
Example: Vertex Cover
Find the minimum number of vertices to cover all edges in a graph.
Code Implementation
python
CopyEdit
def vertex_cover(graph):
visited = set()
for u, neighbors in graph.items():
if u not in visited:
for v in neighbors:
if v not in visited:
visited.add(u)
visited.add(v)
break
return visited
# Example Usage
graph = {
1: [2, 3],
2: [1, 3],
3: [1, 2, 4],
4: [3]
}
print("Vertex Cover:", vertex_cover(graph))
6. Randomized Algorithms
Randomized algorithms use random choices to achieve a result with high
probability.
Example: Randomized QuickSort
Randomly selects a pivot and partitions the array around it.
Code Implementation
python
CopyEdit
import random
# Example Usage
arr = [10, 7, 8, 9, 1, 5]
randomized_quicksort(arr, 0, len(arr) - 1)
print("Sorted Array:", arr)
Let me know if you need diagrams or detailed explanations for any specific
algorithm!
You said:
yes do it
ChatGPT said:
ChatGPT
Here are detailed explanations, diagrams, and Python implementations for
each of the topics: Algebraic Computation, Fast Fourier Transform (FFT), String
Matching, Theory of NP-Completeness, Approximation Algorithms, and
Randomized Algorithms. Each topic includes suitable examples.
1. Algebraic Computation
Algebraic computation involves efficient algorithms for operations on
polynomials, matrices, or other algebraic structures.
Example: Polynomial Multiplication
Let A(x)=1+2x+3x2A(x) = 1 + 2x + 3x^2A(x)=1+2x+3x2 and B(x)=4+5xB(x) = 4 +
5xB(x)=4+5x. Their product is:
A(x)⋅B(x)=4+13x+22x2+15x3A(x) \cdot B(x) = 4 + 13x + 22x^2 +
15x^3A(x)⋅B(x)=4+13x+22x2+15x3
Code Implementation
python
CopyEdit
def polynomial_multiplication(A, B):
n = len(A)
m = len(B)
result = [0] * (n + m - 1)
for i in range(n):
for j in range(m):
result[i + j] += A[i] * B[j]
return result
# Example Usage
A = [1, 2, 3] # Represents 1 + 2x + 3x^2
B = [4, 5] # Represents 4 + 5x
result = polynomial_multiplication(A, B)
print("Resultant Polynomial:", result)
y = [0] * n
for i in range(n // 2):
y[i] = a_even[i] + w * a_odd[i]
y[i + n // 2] = a_even[i] - w * a_odd[i]
if invert:
y[i] /= 2
y[i + n // 2] /= 2
w *= w_n
return y
# Example Usage
A = [1, 2, 3, 4]
result = fft(A)
print("FFT Result:", result)
3. String Matching
Knuth-Morris-Pratt (KMP) Algorithm
The KMP algorithm preprocesses the pattern to create a prefix table, avoiding
unnecessary comparisons.
Diagram Explanation
• Prefix Table: Stores lengths of proper prefixes that are also suffixes.
• Text and Pattern: Compare characters, and use the prefix table for
mismatches.
Code Implementation
python
CopyEdit
def kmp_search(text, pattern):
def compute_lps(pattern):
lps = [0] * len(pattern)
length = 0
i=1
while i < len(pattern):
if pattern[i] == pattern[length]:
length += 1
lps[i] = length
i += 1
else:
if length != 0:
length = lps[length - 1]
else:
lps[i] = 0
i += 1
return lps
lps = compute_lps(pattern)
i=j=0
matches = []
while i < len(text):
if pattern[j] == text[i]:
i += 1
j += 1
if j == len(pattern):
matches.append(i - j)
j = lps[j - 1]
elif i < len(text) and pattern[j] != text[i]:
if j != 0:
j = lps[j - 1]
else:
i += 1
return matches
# Example Usage
text = "ababcabcabababd"
pattern = "ababd"
print("Pattern found at indices:", kmp_search(text, pattern))
4. Theory of NP-Completeness
Concept
A problem is NP-complete if:
• It is in NP (solution can be verified in polynomial time).
• Every problem in NP can be reduced to it in polynomial time.
Example: SAT Problem
Determine if there is an assignment of variables that satisfies the formula:
(x1∨¬x2)∧(x2∨x3)(x_1 \lor \neg x_2) \land (x_2 \lor x_3)(x1∨¬x2)∧(x2∨x3)
Code for SAT Solver
python
CopyEdit
from pysat.solvers import Glucose3
solver = Glucose3()
if solver.solve():
print("Satisfiable Assignment:", solver.get_model())
else:
print("No Solution")
5. Approximation Algorithms
Example: Vertex Cover
Find the minimum number of vertices to cover all edges.
Code Implementation
python
CopyEdit
def vertex_cover(graph):
visited = set()
for u, neighbors in graph.items():
if u not in visited:
for v in neighbors:
if v not in visited:
visited.add(u)
visited.add(v)
break
return visited
# Example Usage
graph = {
1: [2, 3],
2: [1, 3],
3: [1, 2, 4],
4: [3]
}
print("Vertex Cover:", vertex_cover(graph))
6. Randomized Algorithms
Example: Randomized QuickSort
Randomly selects a pivot to partition the array.
Code Implementation
python
CopyEdit
import random
def randomized_partition(arr, low, high):
pivot_index = random.randint(low, high)
arr[pivot_index], arr[high] = arr[high], arr[pivot_index]
pivot = arr[high]
i = low - 1
for j in range(low, high):
if arr[j] <= pivot:
i += 1
arr[i], arr[j] = arr[j], arr[i]
arr[i + 1], arr[high] = arr[high], arr[i + 1]
return i + 1
# Example Usage
arr = [10, 7, 8, 9, 1, 5]
randomized_quicksort(arr, 0, len(arr) - 1)
print("Sorted Array:", arr)
1. Algebraic Computation: Polynomial Multiplication
Diagram: Polynomial Multiplication
Given two polynomials:
A(x)=a0+a1x+a2x2+...+anxnA(x) = a_0 + a_1x + a_2x^2 + ... + a_nx^nA(x)=a0+a1
x+a2x2+...+anxn B(x)=b0+b1x+b2x2+...+bmxmB(x) = b_0 + b_1x + b_2x^2 + ... +
b_mx^mB(x)=b0+b1x+b2x2+...+bmxm
The result of the multiplication:
A(x)⋅B(x)=c0+c1x+c2x2+...+cn+mxn+mA(x) \cdot B(x) = c_0 + c_1x + c_2x^2 + ...
+ c_{n+m}x^{n+m}A(x)⋅B(x)=c0+c1x+c2x2+...+cn+mxn+m
Where:
ck=∑i=0n∑j=0maibjc_k = \sum_{i=0}^{n} \sum_{j=0}^{m} a_i b_jck=i=0∑nj=0∑m
aibj
The coefficients of ckc_kck are derived by multiplying the corresponding terms
from both polynomials.
0A 0
1 AB 0
2 ABA 1
3 ABAB 2
mathematica
CopyEdit
Pattern: ABAB
Prefix Table: [0, 0, 1, 2]
KMP Matching Flow:
1. Preprocess the pattern to create the prefix table.
2. Compare characters in the text and pattern.
3. If mismatch occurs, use the prefix table to shift the pattern efficiently.
Text Matching Example:
Pattern: "ABAB", Text: "ABABABAB"
• At index 0, match first "A".
• Shift pattern using the prefix table to avoid unnecessary comparisons.
4. Theory of NP-Completeness
Diagram: NP-Completeness Concept
• NP (Nondeterministic Polynomial Time): Problems where solutions can
be verified in polynomial time.
• NP-Complete: Problems that are as hard as any problem in NP. If you can
solve one NP-complete problem in polynomial time, you can solve all NP
problems in polynomial time.
CopyEdit
NP-Complete Problems → NP Problems
Example: SAT Problem:
• Given a Boolean formula (like (x1∨¬x2)∧(x2∨x3)(x_1 \lor \neg x_2) \land
(x_2 \lor x_3)(x1∨¬x2)∧(x2∨x3)), check if there is a satisfying assignment
of truth values to variables.
5. Approximation Algorithms
Diagram: Vertex Cover Approximation
The Vertex Cover Problem aims to find the smallest set of vertices such that
every edge in the graph is covered by at least one vertex from the set.
1. Input: A graph G=(V,E)G = (V, E)G=(V,E)
2. Process: Select an uncovered edge and add both endpoints to the vertex
cover set.
3. Repeat: Continue until all edges are covered.
Example Graph:
lua
CopyEdit
1 -- 2 -- 3
| |
4 -- 5
Approximation Algorithm: Select vertices {1, 2, 5}, which cover all edges.
Vertex Cover Approximation Algorithm:
1. Traverse through the graph and cover edges greedily.
2. The result is a 2-approximation, meaning the solution size will be at most
twice the optimal size.
6. Randomized Algorithms
Diagram: Randomized QuickSort
Randomized QuickSort is a variation of the QuickSort algorithm where the
pivot is chosen randomly rather than deterministically.
1. Choose a Random Pivot.
2. Partition the array based on the pivot.
3. Recursively sort the subarrays on either side of the pivot.
Flow of Randomized QuickSort:
mathematica
CopyEdit
Random Pivot → Partition → Recursively Sort Subarrays
Example Array: [10, 7, 8, 9, 1, 5]
• Choose random pivot (e.g., 8), partition the array.
• Recurse on subarrays [1, 5, 7] and [10].
Randomized QuickSort Flow Example:
• Step 1: Choose a random pivot (e.g., 7).
• Step 2: Partition array around the pivot.
• Step 3: Recursively apply the same steps on subarrays.
Classifications of Algorithms
1. Divide and Conquer Algorithms
These algorithms divide the problem into smaller subproblems, solve them
independently, and then combine their results.
• Examples: Merge Sort, Quick Sort, Binary Search.
2. Greedy Algorithms
These algorithms make the locally optimal choice at each step with the hope of
finding the global optimum.
• Examples: Kruskal's Algorithm (Minimum Spanning Tree), Dijkstra's
Algorithm (Shortest Path), Huffman Coding.
3. Dynamic Programming
Dynamic programming breaks a problem into overlapping subproblems and
stores the results to avoid redundant work.
• Examples: Knapsack Problem, Fibonacci Sequence, All Pair Shortest
Path (Floyd-Warshall).
4. Backtracking
These algorithms build a solution incrementally and abandon a solution as
soon as it's determined that it can't lead to a valid solution.
• Examples: N-Queens Problem, Sudoku Solver, Graph Coloring.
5. Randomized Algorithms
These algorithms use randomization to make decisions and typically offer good
average-case performance.
• Examples: Randomized QuickSort, Monte Carlo Methods, Randomized
Prim's Algorithm.
6. Approximation Algorithms
These algorithms find near-optimal solutions when finding the exact solution is
too computationally expensive.
• Examples: Vertex Cover, Traveling Salesman Problem (TSP)
Approximation.
Further Details:
• Fast Fourier Transform (FFT) is crucial in applications like signal
processing, where it is used to compute the discrete Fourier transform
(DFT) and its inverse efficiently.
• NP-Completeness categorizes problems based on their computational
difficulty, and understanding it is crucial for understanding the limits of
what can be computed efficiently.
• Randomized Algorithms are widely used in cryptography and big data
applications, where exact solutions may be infeasible due to size or
complexity.
Conclusion:
These topics cover a broad range of fundamental algorithms and techniques
used in computational theory and real-world problem-solving. The diagrams
and code snippets provided above are designed to offer a deeper
understanding of how these algorithms function and are applied in various
domains.