The Decision Tree Model: Definition 1
The Decision Tree Model: Definition 1
A[i] = A[j]
k:l r:s
p:q
A[k] < A[l]
t:u
9th October, 2012
At each leaf of the tree the output of the algorithm on the corresponding
execution branch will be displayed. Outputs of sorting algorithms
correspond to permutations of the input array.
ADS: lect 7 – slide 1 – 9th October, 2012 ADS: lect 7 – slide 3 – 9th October, 2012
comparison based.
t:u
ADS: lect 7 – slide 2 – 9th October, 2012 ADS: lect 7 – slide 4 – 9th October, 2012
Example A Lower Bound for Comparison Based Sorting
Insertion sort for n = 3: Proof of Theorem 3 uses Decision-Tree Model of sorting.
1:2
It is an Information-Theoretic Lower Bound:
2:3 1:3 I “Information-Theoretic” means that it is based on the amount of
“information” that an instance of the problem can encode.
A[1] A[2] A[3] 1:3 A[2] A[1] A[3] 2:3 I For sorting, the input can encode n! outputs.
I Proof does not make any assumption about how the sorting might
A[1] A[3] A[2] A[3] A[1] A[2] A[2] A[3] A[1] A[3] A[2] A[1] be done (except it is comparison-based).
In insertion sort, when we get the result of a comparison, we often swap some
elements of the array. In showing decision trees, we don’t implement a swap.
Our indices always refer to the original elements at that position in the array.
To understand what I mean, draw the evolving array of InsertionSort beside
this decision tree.
ADS: lect 7 – slide 5 – 9th October, 2012 ADS: lect 7 – slide 7 – 9th October, 2012
We shall prove a lower bound for the height of the decision tree for any algorithm S.
Theorem 3 Remark
For all comparison based sorting algorithms S we have Maybe you are wondering . . . was it really ok to assume all keys are
distinct?
CS (n) = Ω(n lg n). It is ok - because the problem of sorting n keys (with no distinctness
assumption) is more general than the problem of sorting n distinct
keys.
Corollary 4 The worst-case for sorting certainly is as bad as the worst-case for
The worst-case running time of any comparison based sorting algorithm all-distinct keys sorting.
is Ω(n lg n).
ADS: lect 7 – slide 6 – 9th October, 2012 ADS: lect 7 – slide 8 – 9th October, 2012
Observation 6 Average root-leaf length in Binary tree
Each permutation of the inputs must occur at at least one leaf of the decision tree.
(Obs 6 must be true, if our algorithm is to sort properly for all inputs.).
Definition 9
I By Obs 6, the decision tree on inputs of size n has at least n! leaves (for any For any binary tree T , let AvgRL(T ) denote the average root-to-leaf length
algorithm S). for T .
I The (simplified) decision tree is a binary tree. A binary tree of height h has at
most 2h leaves.
Definition 10
A near-complete binary tree T is a binary tree in which every internal node has
I Putting everything together, we get
exactly two child nodes, and all leaves are either at depth h or depth h − 1.
n! ≤ number of leaves of decision tree
≤ 2height of decision tree Theorem 11
≤ 2CS (n)
. Any “near-complete” binary tree T with leaf set L(T ), |L(T )| ≥ 4, has Average
root-to-leaf length AvgRL(T ) at least lg (|L(T )|)/2.
I Thus
CS (n) ≥ lg(n!) = Ω(n lg(n)). Theorem 12
To obtain the last inequality, we can use the following inequality: For any binary tree T , there is a near-complete binary tree T 0 such that
L(T ) = L(T 0 ) (same leaf set) and such that AvgRL(T 0 ) ≤ AvgRL(T ).
nn/2 ≤ n! ≤ nn
Hence AvgRL(T ) ≥ lg(|L(T )|/2) holds for all binary trees.
This tells us that lg n! ≥ lg(nn/2 ) = (n/2) lg n = Ω(n lg(n)).
Proof of Theorems 11 and 12 on BOARD.
Thm 3 QED
ADS: lect 7 – slide 9 – 9th October, 2012 ADS: lect 7 – slide 11 – 9th October, 2012
Reading Assignment
[CLRS] Section 8.1 (2nd and 3rd edition) or
[CLR] Section 9.1
Well-worth reading - this is a nice chapter of CLRS (not too long).
Problems
1. Draw (simplified) decision trees for Insertion Sort and
Quicksort for n = 4.
2. Exercise 8.1-1 of [CLRS] (both 2nd and 3rd ed).
3. Resolve the complexity (in terms of no-of-comparisons) of sorting 4
numbers.
3.1 Give an algorithm which sorts any 4 numbers and which uses at
most 5 comparisons in the worst-case.
3.2 Prove (using the decision-tree model) that there is no algorithm to
sort 4 numbers, which uses less than 5 comparisons in the worst-case.