Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Algorithm 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Divide and Conquer Introduction

Divide and Conquer is an algorithmic pattern. In algorithmic methods, the design is to take a dispute on a
huge input, break the input into minor pieces, decide the problem on each of the small pieces, and then
merge the piecewise solutions into a global solution. This mechanism of solving the problem is called the
Divide & Conquer Strategy.
Divide and Conquer algorithm consists of a dispute using the following three steps.
1. Divide the original problem into a set of subproblems.
2. Conquer: Solve every subproblem individually, recursively.
3. Combine: Put together the solutions of the subproblems to get the solution to the whole problem.

Generally, we can follow the divide-and-conquer approach in a three-step process.


Examples: The specific computer algorithms are based on the Divide & Conquer approach:
1. Maximum and Minimum Problem
2. Binary Search
3. Sorting (merge sort, quick sort)
4. Tower of Hanoi.
Fundamental of Divide & Conquer Strategy:
There are two fundamental of Divide & Conquer Strategy:
1. Relational Formula
2. Stopping Condition
1. Relational Formula: It is the formula that we generate from the given technique. After generation of
Formula we apply D&C Strategy, i.e. we break the problem recursively & solve the broken subproblems.
2. Stopping Condition: When we break the problem using Divide & Conquer Strategy, then we need to
know that for how much time, we need to apply divide & Conquer. So the condition where the need to
stop our recursion steps of D&C is called as Stopping Condition.
Applications of Divide and Conquer Approach:
Following algorithms are based on the concept of the Divide and Conquer Technique:
1. Binary Search: The binary search algorithm is a searching algorithm, which is also called a half-
interval search or logarithmic search. It works by comparing the target value with the middle
element existing in a sorted array. After making the comparison, if the value differs, then the half
that cannot contain the target will eventually eliminate, followed by continuing the search on the
other half. We will again consider the middle element and compare it with the target value. The
process keeps on repeating until the target value is met. If we found the other half to be empty
after ending the search, then it can be concluded that the target is not present in the array.
2. Quicksort: It is the most efficient sorting algorithm, which is also known as partition-exchange sort.
It starts by selecting a pivot value from an array followed by dividing the rest of the array elements
into two sub-arrays. The partition is made by comparing each of the elements with the pivot value.
It compares whether the element holds a greater value or lesser value than the pivot and then sort
the arrays recursively.
3. Merge Sort: It is a sorting algorithm that sorts an array by making comparisons. It starts by
dividing an array into sub-array and then recursively sorts each of them. After the sorting is done, it
merges them back.
4. Closest Pair of Points: It is a problem of computational geometry. This aalgorithm
lgorithm emphasizes
finding out the closest pair of points in a metric space, given n points, such that the distance
between the pair of points should be minimal.
5. Strassen's Algorithm: It is an algorithm for matrix multiplication, which is named after Volker
Volk
Strassen. It has proven to be much faster than the traditional algorithm when works on large
matrices.
6. Cooley-Tukey
Tukey Fast Fourier Transform (FFT) algorithm: The Fast Fourier Transform algorithm is
named after J. W. Cooley and John Turkey. It follows the Divide and Conquer Approach and
imposes a complexity of O(nlogn).
7. Karatsuba algorithm for fast multiplication: It is one of the fastest multiplication algorithms of
the traditional time, invented by Anatoly Karatsuba in late 1960 and got published in 1962. It
multiplies two n-digit
digit numbers in such a way by reducing it to at most single
single-digit.
Binary Search
Case 1: item = A[mid], then LOC = mid, but it the best case and T (n) = 1

Case 2: item ≠A [mid], then we will split the array into two equal parts of size .
And again find the midpoint of the half-sorted
half sorted array and compare with search element.
Repeat the same process until a search element is found.

T (n) = ...... (Equation 1)


{Time to compare the search element with mid element, then with half of the selected half part of array}

At least there will be only one term left that's why that term will compare out, and only one comparison be

done that's why


Is the last term of the equation and it will be equal to 1

Merge Sort
Merge sort is yet another sorting algorithm that falls under the category of Divide and Conquer technique.
It is one of the best sorting techniques that successfully build a recursive algorithm.
Merge( ) Function Explained Step-By-Step
Step
Consider the following example of an unsorted array, which we are going to sort with the help of the
Merge Sort algorithm.
A= (36,25,40,2,7,80,15)
Step1: The merge sort algorithm iteratively divides an array into eq equal
ual halves until we achieve an atomic
value. In case if there are an odd number of elements in an array, then one of the halves will have more
elements than the other half.
Step2: After dividing an array into two subarrays, we will notice that it did not h hamper
amper the order of
elements as they were in the original array. After now, we will further divide these two arrays into other
halves.
Step3: Again, we will divide these arrays until we achieve an atomic value, i.e., a value that cannot be
further divided.
Step4: Next, we will merge them back in the same way as they were broken down.
Step5: For each list, we will first compare the element and then combine them to form a new sorted list.
Step6: In the next iteration, we will compare the lists of two data valu
values
es and merge them back into a list of
found data values, all placed in a sorted manner.
Hence the array is sorted.
Analysis of Merge Sort:
Let T (n) be the total time taken by the Merge Sort algorithm.

o Sorting two halves will take at the most 2T time.


o When en we merge the sorted lists, we come up with a total n
n-11 comparison because the last element
which is left will need to be copied down in the combined list, and there will be no comparison.
Thus, the relational formula will be

But we ignore '-1' because the element will take some time to be copied in merge lists.

So T (n) = 2T + n...equation 1

Note: Stopping Condition T (1) =0 because at last, there will be only 1 element left that need to be copied, and
there will be no comparison.
Best Case Complexity: The merge sort algorithm has a best-case
best case time complexity of O(n*log n) for the
already sorted array.

Average Case Complexity: The average-case


case time complexity for the merge sort algorithm is O(n*log n),
n)
which happens when 2 or more elements are jumbled, i.e.,
i.e., neither in the ascending order nor in the
descending order.

Worst Case Complexity: The worst-case


worst case time complexity is also O(n*log n),, which occurs when we sort
the descending order of an array into the ascending order.

Space Complexity: The space complexity


comp of merge sort is O(n)..
Merge Sort Applications
The concept of merge sort is applicable in the following areas:

o Inversion count problem


o External sorting
o E-commerce
commerce applications

Max - Min Problem


Problem: Analyze the algorithm to find the maximum and minimum element from an array.
Algorithm: Max ?Min Element (a [])
Max: a [i]
Min: a [i]
For i= 2 to n do
If a[i]> max then
max = a[i]
if a[i] < min then
min: a[i]
return (max, min)
Analysis:
Method 1: if we apply the general approach to the array of sizesize n, the number of comparisons required
are 2n-2.
Method-2: In another approach, we will divide the problem into sub sub-problems
problems and find the max and min
of each group, now max. Of each group will compare with the only max of another group and min with
min.
Let n = is the size of items in an array
Let T (n) = time required to apply the algorithm on an array of size n. Here we divide the terms as T(n/2).
2 here tends to the comparison of the minimum with minimum and maximum with maximum as in above
example.

T (n) = 2 T → Eq (i)

T (2) = 1, time required to compare two elements/items. (Time is measured in units of the number of
comparisons)

→ Eq (ii)

Put eq (ii) in eq (i)

Similarly, apply the same procedure recursively on each subproblem or anatomy

{Use recursion means, we will use some stopping condition to stop the algorithm}

Recursion will stop, when → (Eq. 4)

Put the equ.4 into equation3.


Number of comparisons requires applying the divide and conquering algorithm on n elements/items

Number off comparisons requires applying general approach on n elements = (n


(n-1) + (n-1) = 2n-2
From this example, we can analyze, that how to reduce the number of comparisons by using this
technique.
Analysis: suppose we have the array of size 8 elements.

It is evident;
vident; we can reduce the number of comparisons (complexity) by using a proper technique.

Dynamic Programming
Dynamic programming is a technique that breaks the problems into sub sub-problems,
problems, and saves the result
for future purposes so that we do not need to compute the result again. The subproblems are optimized
to optimize the overall solution is known as optimal substructure
substructure property. The main use of dynamic
programming is to solve optimization problems. Here, optimization problems mean that when we are
trying to find out the minimum or the maximum solution of a problem. The dynamic programming
guarantees to find the
he optimal solution of a problem if the solution exists.
The definition of dynamic programming says that it is a technique for solving a complex problem by first
breaking into a collection of simpler subproblems, solving each subproblem just once, and then storing
their solutions to avoid repetitive computations.
Let's understand this approach through an example.

You might also like