DAA PPT DAA PPT by Dr. Preeti Bailke
DAA PPT DAA PPT by Dr. Preeti Bailke
DAA ppt
by Dr. Preeti Bailke
Ti
Time complexity of binary search
l it f bi h
Let the iteration in Binary Search terminates after k iterations.
iterations
At each iteration, the array is divided by half.
Let length of array is n
At Iteration 1,
1
Length of array = n
At Iteration 2,
Length of array = n∕2
At Iteration 3,
Length of array = (n∕2)∕2 = n∕22
Therefore after Iteration k,
Therefore, k
Length of array = n∕2k
Time complexity of binary search
Time complexity of binary search
Also, we know that after k divisions, the length of array becomes 1
Th f
Therefore
Length of array = n ∕ 2^k = 1
=> n = 2^k
A l i log
Applying l function
f i on both
b h sides:
id
=> log2 (n) = log2 (2^k)
=> log2 (n) = k log2 (2)
As (loga (a) = 1)
Therefore,
=> k = log2 (n)
H
Hence, th
the time
ti complexity
l it off Binary
Bi Search
S h is
i
log2 (n)
Time complexity of linear search
Time complexity of linear search
Best case: takes O(1) ( ) operations
p
Worst case: takes O(n) operations
Avgg case:
If it is at 1 then you find it in 1 comparison,
if it is at 2,, yyou find it in 2 comparisons,
p , ……,,
if it is at n then you do n comparisons to find it.
In order to average it you sum the total number
of comparisons 1+2+ +n = n (n+1) / 2
and divide it byy n resultingg in n+1/2 = O(n) ( )
Majority element
Majority element
• Assume you have an array A[1..n]
A[1 n]
• A majority element of A is any element
occurring in more than n/2 positions
• (so if n = 6 or n = 7, any majority element will
occur in
i at least
l 4 positions).
ii )
• Assume that elements cannot be ordered or
sorted, but can be compared for equality.
Majority element
Majority element
• Which approaches can be adopted?
• What’s algorithm?
Majority element: Basic approach
Majority element: Basic approach
• The basic solution is to have two loops and
keep track of the maximum count for all
different elements.
• If maximum count becomes greater than n/2
then break the loopsp and return the element
having maximum count.
• If the maximum count doesn
doesn’tt become more
than n/2 then majority element doesn’t exist.
Majority element: Basic approach
Majority element: Basic approach
• Complexity Analysis:
• Time Complexity: ?
• Auxiliary Space :
Auxiliary Space : ?
Majority element: Basic approach
Majority element: Basic approach
• Time Complexity: O(n*n)
A nested loop is needed where both the loops
traverse the array from start to end, so the
time complexity is O(n^2)
• Auxiliary Space :
y p O(1)
As no extra space is required for any operation
so the space complexity is constant
Majority element: Divide & Conquer
Majority element: Divide & Conquer
• The algorithm begins by splitting the array in half
repeatedly and calling itself on each half.
half
(similar to merge sort algorithm)
• When we get down to single elements, that
single element is returned as the majority
• At every level, it will get return values from its
two recursive
i calls
ll
• The key to this algorithm is the fact that if there is
a majority element in the combined array,array then
that element must be the majority element in
either the left half or right half of the array or
b th
both
Majority element
j y
There are 4 scenarios:
• 1. Both return “no majority.”
j y i.e. neither half of
the array has a majority element, and the
combined array cannot have a majority element.
• 2.
2 The right side is a majority,
majority and the left isn’t.
isn’t
• The only possible majority for this level is with
the value that formed a majority on the right half,
• therefore, just compare every element in the
combined array and count the number of
elements
l that
h are equall to this
hi value.
l
• If it is a majority element then return that
element else return “no
element, no majority.
majority ”
Majority element
Majority element
• 3. Same as above, but with the left returning a
majority and the right returning “no
majority, no majority.
majority ”
• 4. Both sub‐calls return a majority element.
• Count the number of elements equal to both of
the candidates for majority element.
• If either is a majority element in the combined
array, then
h return it.
i
• Otherwise, return “no majority.”
• The top level
le el simply
simpl returns
ret rns either a majority
majorit
element or that no majority element exists in the
same way.
Majority element: Recursive partitions
Majority element: Recursive partitions
• Ex 1: {5 5 2 5 9 9 3 9 9}
• a {5 5 2 5} b {9 9 3 9 9}
• a1 {5 5} a2 {2 5}
• a11 {5} a12 {5}
• M=5 M=5 => a1: M=5
• a21 {2} a22 {5}
• M=2 M=5 => a2: M=nil
• a: M=5 ((consideringg 5 5 2 5,, cnt[5]=3)
[ ] )
• B: M=9
• Ans:nil
Majority element: time complexity
Majority element: time complexity
• Find recurrence relation
Majority element: Recursive partitions
Majority element: Recursive partitions
Majority element: Recursive partitions
Majority element: Recursive partitions
• GetFrequency computes the number of times an
element (elemlsub or elemrsub) appears in the given
array a[1...n].
• Two calls to GetFrequency is O(n).
• After that comparisons are done to validate the
existence of majority element.
• GetFrequency
G tF i the
is th linear
li ti
time equality
lit operation.
ti
• Pair up the elements arbitrarily to get n/2 pairs.
• In each pair if the two elements are different we
discard both of them.
• If they are same only one of them is kept.
Majority element: time complexity
Majority element: time complexity
• At each level, two calls are made recursively
with
ith eachh callll having
h i h lf the
half th sizei off the
th
original array.
• For
F theth non‐recursive
i costs,
t we can see that
th t att
each level, we have to compare each number
at most twice (which only happens in the last
case described above).
• Therefore,
Therefore the non‐recursive
non recursive cost is at most 2n
comparisons when the procedure is called
with an array of size n.
Majority element: time complexity
Majority element: time complexity
• This lets us upper bound the number of
comparisons done by T(n) defined by the
recurrence
• T(1) = 0 and
• T(n) = 2T(n/2) + 2n
• We can then determine that T(n) Θ(n log n)
as desired using Case 2 of the Master
Theorem.
Boyer Moore Voting Algorithm
Boyer Moore Voting Algorithm
• Initialize an element
Initialize an element m=0 and i
and i = 0
=0
• For each element x of the input sequence:
– If i = 0, then assign
0 th i m = x and d i = 1
1
– else if m = x, then assign i ++
– else assign i ‐‐
• Return m
• Second pass also required to verify
Boyer Moore Voting Algorithm
Boyer Moore Voting Algorithm
Time Complexity = ?
Boyer Moore Voting Algorithm
Boyer Moore Voting Algorithm
• Ex.1 = {5 4 5 4 5}
Ex 1 = {5 4 5 4 5}
• Ex.2 = {5 4 5 4}
• Ex.3 = {4 4 4 0 0 0 4 0 0}
3 { 0 0 0 0 0}
Time Complexity = ?