DAA Unit 3
DAA Unit 3
DAA Unit 3
Algorithms
(CSC-314)
B.Sc. CSIT
Unit-3:Divide and Conquer Algorithms
Introduction:
●
In computer science, divide and conquer is an algorithm
design paradigm.
●
A divide-and-conquer algorithm recursively breaks down
a problem into two or more sub-problems of the same or
related type, until these become simple enough to be
solved directly.
●
The solutions to the sub-problems are then combined to
give a solution to the original problem.
●
The divide-and-conquer technique is the basis of efficient
algorithms for many problems, such as sorting (e.g.,
quicksort, merge sort).
●
Here,
– n=input size
– a = no. of sub-problems
– n/b = input size of the sub-problems
●
The number of comparisons can be reduced using the
divide and conquer approach.
●
Consider the following examples
– Given data sequence{4,1,3,2,16,9,10,14,8,7}
– Here we need to construct binary tree first and
– We need to carry out heapify operations on every non
leaf nodes to build the Max-heap.
Build-Max-Heap(A)
n = length[A]
for i ← n/2 downto 1
{
MAX-HEAPIFY(A, i, n)
}
– Time Complexity:
– Running time: Loop executes O(n) times and complexity of Heapify
is O(logn), therefore complexity of Build-Max-Heap is O(nlogn).
●
Analysis:
– Building heap takes O(n)
– Loop executes n times
– Heapify operations takes O(logn)
– So total T(n)=O(nlogn)
Design and Analysis of Algorithms (CSC-314)
Unit-3:Divide and Conquer Algorithms
Order statistics
●
Order statistics are sample values placed in ascending order. The study of order
statistics deals with the applications of these ordered values and their functions.
●
Let’s say you had three weights:
– X1 = 22 kg, X2 = 44 kg, and X3 = 12 kg.
●
To get the order statistics (Yn), put the items in numerical increasing order:
– Y1 = 12 kg
– Y2 = 22 kg
– Y3 = 44 kg
●
The kth smallest X value is normally called the kth order statistic.
●
More formally,
– If X1, X2,…, Xn are random iid observations taken from a population with n
continuous observations, then
– the random variables Y1 < Y2 < …, < Yn denote the sample’s order
statistics.
●
Median order
●
A median, informally, is the "halfway point" of the set.
●
When n is odd, the median is unique, occurring at i = (n + 1)/2. When n
is even, there are two medians, occurring at i = n/2 and i = n/2 + 1.
●
Thus, regardless of the parity of n, medians occur at i = lower bound(n +
1)/2 and i = upper bound(n + 1)/2.
●
Here, The n elements are represented by small circles, and each group
occupies a column.
●
The medians of the groups are whitened, and the median-of-medians x is
labeled. Arrows are drawn from larger elements to smaller, from which it
can be seen that 3 out of every group of 5 elements to the right of x are
greater than x, and 3 out of every group of 5 elements to the left of x are
less than x.
●
The elements greater than x are shown on a shaded background.
Design and Analysis of Algorithms (CSC-314)
Unit-3:Divide and Conquer Algorithms
Selection in Worst Case Linear Time algorithm
●
Algorithms: