UNIT5 Comparison Tree
UNIT5 Comparison Tree
THEORY
UNIT-5
LOWER BOUND THEORY
Lower Bound Theory Concept is based upon the calculation of minimum time or
minimum number of comparisons that is required to execute an algorithm is
known as a lower bound theory or Base Bound Theory.
Concept/Aim: The main aim is to calculate a minimum number of comparisons
required to execute an algorithm
1.Comparisons Trees.
2.Oracle and adversary argument
3.State Space Method
COMPARISON TREE
UNIT-5
COMPARISON TREE
• ai = a j equal to
• To determine their relative order, if we assume all elements are distinct, then we just need to consider
ai ≤ aj '=' is excluded &, ≥,≤,>,< are equivalent.
• Consider sorting three numbers a1, a2, and a3. There are 3! = 6 possible combinations:
• Decision Tree: A decision tree is a full binary tree that shows the
comparisons between elements that are executed by an appropriate
sorting algorithm operating on an input of a given size. Control, data
movement, and all other conditions of the algorithm are ignored.
COMPARISON TREE FOR BINARY SEARCH:
• A full binary tree is a binary tree with either zero or two child nodes for
each node.
TECHNIQUES FOR ALGEBRAIC PROBLEM
FOR MORE DETAIL
UNIT-5
• In summary, oracles and adversary arguments are complementary
techniques used in the analysis of algorithms. Oracles help establish
lower bounds by assuming the existence of efficient solutions, while
adversary arguments are used to derive upper bounds by considering
the worst-case behavior against a strategically chosen opponent.
Together, they provide insights into the fundamental limits and
capabilities of algorithms.
https://www.youtube.com/watch?v=hz3-YUNf3Y4(basic starting es toh
thora jha dekhna bes )
Javatpooint
Chatgpt
This topic in Ppt has done from above links
ORACLE AND ADVERSORY EXAMPLE:
(MERGING PROBLEM)
• Example: (Merging Problem) given the sets A (1: m) and B (1: n),
where the information in A and in B are sorted. Consider lower bounds
for algorithms combining these two sets to give an individual sorted
set.
• Consider that all of the m+n elements are specific and A (1) < A (2)
< ....< A (m) and B (1) < B (2) < ....< B (n).
SOLUTION ACC TO ORACLE & ADVISORY APPROACH
• Let's break down the problem of merging two sorted sets A and B into a
single sorted set using an oracle and adversary argument. We'll
consider both upper and lower bounds on the number of comparisons
required by any comparison-based merging algorithm.
• Given two sorted sets A and B, each with m and n elements
respectively, the goal is to merge them into a single sorted set.
ORACLE APPROACH (LOWER BOUND):
• Assume we have an oracle that instantly tells us the correct position to insert an
element into the merged set, given its value and the current state of the merged set.
• With this oracle, we can guarantee that we'll need at leastm+n−1 comparisons to
merge the two sets. This is because each element from A or B needs to be compared
at least once with another element from the other set.
• For example, if A has m elements and B has n elements, then there are m+n−1
positions where elements from both sets will be compared.
• This lower bound of m+n−1 comparisons is achievable only with the help of the
oracle, as it assumes we have perfect knowledge about where each element should
be placed.
EXAMPLE
// Merge elements from A and B into mergedArray until one of the arrays is exhausted
while (i < m && j < n)
{ if (A[i] <= B[j])
{ merged[k] = A[i]; i++; }
else { merged[k] = B[j]; j++; }
k++;
}
#include <iostream>
// Merge elements from A and B into mergedArray until one of the arrays is exhausted
while (i < m && j < n) {
if (A[i] <= B[j]) {
merged[k] = A[i];
i++;
} else {
merged[k] = B[j];
j++;
}
k++;
}
int main() {
// Example arrays A and B (sorted)
int A[] = {1, 3, 5, 7, 9};
int m = sizeof(A) / sizeof(A[0]);
int B[] = {2, 4, 6, 8, 10};
int n = sizeof(B) / sizeof(B[0]);
return 0;
}
ADVERSARY ARGUMENT (UPPER
BOUND):
• Now, let's consider an adversary that strategically selects elements from sets A and B to
maximize the number of comparisons required by any merging algorithm.
• The adversary's goal is to force the merging algorithm to make as many comparisons as
possible.
• One strategy the adversary might use is to alternate between selecting elements from sets A
and B in such a way that the merging algorithm is forced to compare every element with every
other element, leading to m×n comparisons.
• However, with careful consideration, it can be shown that the upper bound on the number of
comparisons is m×n.
• This upper bound of m×n comparisons is achieved in the worst-case scenario, where every
element in A is compared with every element in B.
SUMMARY /NOTE
In summary, the lower bound provided by the oracle tells us that at least
m+n−1 comparisons are required, assuming perfect knowledge. On the
other hand, the upper bound derived from the adversary argument tells
us that in the worst-case scenario, m×n comparisons may be needed.
These bounds help us understand the range of comparisons required by
any comparison-based merging algorithm, considering both ideal and
worst-case scenarios.
LOWER BOUNDS
THROUGH REDUCTIONS
UNIT-5
LOWER BOUNDS THROUGH REDUCTIONS
Find largest element in array (First sort elemnts then find largest )
for(i=0;i<sizeofarray ;i++)
{
if(a[i]>a[i+1])
{
temp=a[i]
a[i]=a[i+1] Total Comparisons are : n-1 where n= total no
of elemnts
a[i+1]=temp
}
}
cout<<a[n]
1. Lower Bound on Sorting(directly find largest element without sorting ):
1. From the comparison-based sorting lower bound theorem, we know that any comparison-based sorting algorithm requires at least Ω(n)
comparisons in the worst case.
2. Since finding the maximum element can be reduced to sorting, it follows that any algorithm for finding the maximum element must also
perform at least Ω(n) comparisons in the worst case.
max=10
for(i=1;i<sizeofarray ;i++)
{
if(max<a[i])
{
max=a[i] Total Comparisons are : n-1 where n=
total no of elemnts
}
}
CONCLUSION: