Summarised Content Data Structures & Algorithm
Summarised Content Data Structures & Algorithm
• Divide:
• Break the problem into smaller subproblems
that are similar to the original problem but
easier to solve.
• This step involves dividing the problem into
smaller and more manageable parts.
Conquer:
• Solve each smaller subproblem recursively. If
the subproblem is small enough, solve it
directly without further division.
• This step involves solving the smaller
subproblems either recursively or directly.
Combine
• :
• Merge the solutions of the smaller
subproblems to obtain the solution for the
original problem.
• This step involves combining the solutions of
the subproblems to get the final solution.
Example: Finding Maximum Number
in an Array
• Let's apply the divide and conquer approach
to find the maximum number in an array:
• Divide:
– Divide the array into two halves.
• Conquer:
– Recursively find the maximum number in each
half of the array.
– If the array has only one element, return that
element as the maximum.
Combine:
• Compare the maximum numbers obtained
from the two halves.
• Return the larger of the two as the maximum
number for the entire array.
Illustrative Steps:
• Consider an array: [4, 9, 2, 7, 5, 3]
• Divide:
– Divide the array into two halves: [4, 9, 2] and [7, 5,
3]
• Conquer:
– For the first half: recursively find the maximum (9)
– For the second half: recursively find the maximum
(7)
Combine:
• Compare the maximums obtained: max(9, 7) =
9
• Return 9 as the maximum number in the array
[4, 9, 2, 7, 5, 3]
Karatsuba algorithm
Karatsuba algorithm
• The Karatsuba algorithm is a fast
multiplication algorithm used to multiply two
large numbers efficiently. It is a divide-and-
conquer algorithm that reduces the number
of required multiplications compared to
traditional methods, such as grade-school
multiplication.
Brief Overview of the Karatsuba
Algorithm
Algorithm Steps:
• Divide the input numbers into two halves.
• Recursively compute three multiplications:
the products of the two halves of each input
and the product of the sum of the halves.
• Combine the results using additions to obtain
the final product.
Example
• Let's multiply two 2-digit numbers, 12 and 34,
using the Karatsuba algorithm:
• Divide 12 into a=1 and b=2, and 34 into c=3
and d=4.
• Compute ac = 1 * 3 = 3, bd = 2 * 4 = 8.
• Compute (a+b)(c+d) = (1+2)(3+4) = 3 * 7 = 21.
• Use the Karatsuba formula to combine the
results: xy = ac * 10^2 + ((a+b)(c+d) - ac - bd) *
10^1 + bd = 300 + (21 - 3 - 8) * 10 + 8 = 408.
Karatsuba algorithm
• Advantages:
– Reduces the number of required multiplications from
four to three in each recursive step.
– Particularly efficient for large numbers, where
traditional methods become computationally
expensive.
• Limitations:
– The recursive nature of Karatsuba can introduce
additional overhead for small numbers.
– Implementation requires understanding of recursion
and careful handling of corner cases.
Real-World Example
• Imagine you are working on a project that
involves handling very large quantities, such
as calculating the total cost of items in an
inventory system.
• Let's say you have two large numbers
representing the quantity and price of items:
• Quantity of Items (x): 1,234,567
• Price per Item (y): $98.76
Real-World Example
• You want to calculate the total cost, which is
the product of the quantity and price per
item. Traditionally, this would involve
multiplying each digit of the quantity with
each digit of the price, which can be time-
consuming and prone to errors, especially
with large numbers.
Divide the Numbers:
• Break down the quantity (x) and price per
item (y) into smaller parts. For example:
– �=1,234∗1,000+567x=1,234∗1,000+567
– �=98∗100+76y=98∗100+76
• Step 1: Divide the Numbers:
• Break down the quantity (x) and price per
item (y) into smaller parts. For example:
x=1,234∗1,000+567x=1,234∗1,000+567
y=98∗100+76y=98∗100+76
Step 2: Apply Karatsuba Algorithm:
• Use the Karatsuba algorithm to recursively
compute the products:
• ac=1,234×98ac=1,234×98 (multiply the left
halves of x and y)
bd=567×76bd=567×76 (multiply the right halves of x
and y)
(a+b)×(c+d)=1,234+567)×(98+76)
(a+b)×(c+d)=(1,234+567)×(98+76) (multiply the
sum of left and right halves of x and y)
Step 3: Combine Results:
• Use the Karatsuba formula xy=ac×10 4
• +((a+b)(c+d)−ac−bd)×10 2 +bd to combine the
results obtained from the recursive
multiplications.
Final Result: