













































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Divide and Conquer: General Method, Defective chessboard, Binary Search, finding the maximum and minimum, Merge sort, Quick sort. The Greedy Method: The general Method, container loading, knapsack problem, Job sequencing with deadlines, minimum-cost spanning Trees.
Typology: Study notes
1 / 53
This page cannot be seen from the preview
Don't miss anything!
Department of Computer Science and Engineering
Divide and Conquer: General Method, Defective chessboard, Binary Search, finding the maximum and minimum, Merge sort, Quick sort.
The Greedy Method: The general Method, container loading, knapsack problem, Job sequencing with deadlines, minimum-cost spanning Trees.
Divide and Conquer
General Method:
In divide and conquer approach , a problem is divided into smaller problems, then the smaller problems are solved independently, and finally the solutions of smaller problems are combined into a solution for the large problem.
Divide and conquer is a design strategy which is well known to breaking down efficiency barriers. When the method applies, it often leads to a large improvement in time complexity. For example, from O (n^2 ) to O (n log n) to sort the elements.
Divide and Conquer is one of the best-known general algorithm design technique. It works according to the following general plan:
Given a function to compute on ‘n’ inputs the divide-and-conquer strategy suggests splitting the inputs into ‘k’ distinct subsets, 1<k<=n, yielding ‘k’ sub problems. These sub problems must be solved, and then a method must be found to combine sub solutions into a solution of the whole. If the sub problems are still relatively large, then the divide-and-conquer strategy can possibly be reapplied. Often the sub problems resulting from a divide-and-conquer design are of the same type as the original problem. For those cases the reapplication of the divide-and- conquer principle is naturally expressed by a recursive algorithm.
Department of Computer Science and Engineering
Control Abstraction of Divide and Conquer
A control abstraction is a procedure whose flow of control is clear but whose primary operations are specified by other procedures whose precise meanings are left undefined. The control abstraction for divide and conquer technique is DANDC(P), where P is the problem to be solved.
In the above specification,
Initially DAndC(P) is invoked, where ‘P’ is the problem to be solved. Small (P) is a Boolean-valued function that determines whether the input size is small enough that the answer can be computed without splitting. If this so, the function ‘ S’ is invoked. Otherwise, the problem P is divided into smaller sub problems. These sub problems P 1 , P 2 …Pk are solved by recursive application of DAndC. Combine is a function that determines the solution to P using the solutions to the ‘k’ sub problems.
Department of Computer Science and Engineering
Binary Search:
Binary Search is a searching algorithm for finding an element's position in a sorted array. In this approach, the element is always searched in the middle of a portion of an array. Binary search can be implemented only on a sorted list of items. If the elements are not sorted already, we need to sort them first
Problem definition: Let ai, 1 ≤ i ≤ n be a list of elements that are sorted in non-decreasing order.
The problem is to find whether a given element x is present in the list or not. If x is present we
have to determine a value j (element’s position) such that aj=x. If x is not in the list, then j is set
to zero.
Department of Computer Science and Engineering
Solution:
Let P = (n, ai…al , x) denote an arbitrary instance of search problem where n is the number of
elements in the list, ai…al is the list of elements and x is the key element to be searched for in the
given list. Binary search on the list is done as follows:
Step1: Pick an index q in the middle range [i, l] i.e. q= [(n + 1)/2] and compare x with aq.
Step 2: if x = aq i.e key element is equal to mid element, the problem is immediately solved.
Step 3: if x <aqin this case x has to be searched for only in the sub-list ai, ai+1, ……, aq-
Therefore, problem reduces to (q-i, ai…aq-1, x).
Step 4: if x >aq,x has to be searched for only in the sub-list aq+1, ...,., al. Therefore problem
reduces to (l-i, aq+1…al, x).
For the above solution procedure, the Algorithm can be implemented as recursive or non-
recursive algorithm.
Recursive binary search algorithm:
Department of Computer Science and Engineering
Continuing in this manner the number of element comparisons needed to find each of nine
elements is:
No element requires more than 4 comparisons to be found. Summing the comparisons needed to find all nine items and dividing by 9, yielding 25/9 or approximately 2.77 comparisons per successful search on the average. There are ten possible ways that an un-successful search may terminate depending upon the value of x.
Analysis: In binary search the basic operation is key comparison. Binary Search can be analyzed with the best, worst, and average case number of comparisons. The numbers of comparisons for the recursive and iterative versions of Binary Search are the same, if comparison counting is relaxed slightly. For Recursive Binary Search, count each pass through the if-then-else block as one comparison. For Iterative Binary Search, count each pass through the while block as one comparison. Let us find out how many such key comparison does the algorithm make on an array of n elements.
Best case – Θ(1) In the best case, the key is the middle in the array. A constant number of comparisons (actually just 1) are required.
Worst case - Θ(log 2 n) In the worst case, the key does not exist in the array at all. Through each recursion or iteration of Binary Search, the size of the admissible range is halved. This halving can be done ceiling(log2n ) times. Thus, [ log 2 n ] comparisons are required. Sometimes, in case of the successful search, it may take maximum number of comparisons. [ log 2 n ]. So worst case complexity of successful binary search is Θ (log 2 n).
Average case - Θ (log 2 n) To find the average case, take the sum of the product of number of comparisons required to find each element and the probability of searching for that element. To simplify the analysis, assume that no item which is not in array will be searched for, and that the probabilities of searching for each element are uniform.
Department of Computer Science and Engineering
Space Complexity – The space requirements for the recursive and iterative versions of binary search are different. Iterative Binary Search requires only a constant amount of space, while Recursive Binary Search requires space proportional to the number of comparisons to maintain the recursion stack.
Department of Computer Science and Engineering
Divide and conquer approach for Max. Min problem works in three stages.
If a 1 is the only element in the array, a 1 is the maximum and minimum. If the array contains only two elements a 1 and a 2 , then the single comparison between two elements can decide the minimum and maximum of them. If there are more than two elements, the algorithm divides the array from the middle and creates two sub problems. Both sub problems are treated as an independent problem and the same recursive process is applied to them. This division continues until sub problem size becomes one or two.
Department of Computer Science and Engineering
Analysis – Time Complexity:
Department of Computer Science and Engineering
Algorithm:
The merging of two sorted arrays can be done as follows. Two pointers (array indices) are initialized to point to the first elements of the arrays being merged. The elements pointed to are compared, and the smaller of them is added to a new array being constructed
Department of Computer Science and Engineering
After that, the index of the smaller element is incremented to point to its immediate successor in the array it was copied from. This operation is repeated until one of the two given arrays is exhausted, and then the remaining elements of the other array are copied to the end of the new array.
Example:
Department of Computer Science and Engineering
In quick sort, the entire work happens in the division stage, with no work required to combine the solutions to the sub problems.
The function partition() makes use of two pointers ‘i’ and ‘j’ which are moved toward each other in the following fashion:
Department of Computer Science and Engineering
Department of Computer Science and Engineering
Department of Computer Science and Engineering