Introduction To: Algorithm Design and Analysis
Introduction To: Algorithm Design and Analysis
Jingwei Xu
http://cs.nju.edu.cn/ics/people/jingweixu
Institute of Computer Software
Nanjing University
In the last class …
• The sorting problem
• Assumptions
• InsertionSort
• Design
• Analysis: inverse
• QuickSort
• Design
• Analysis
HeapSort
• Heap
• HeapSort
• FixHeap
• ConstructHeap
• Complexity of HeapSort
• Accelerated HeapSort
How HeapSort Works
Elements to
Elements sorted
be sorted
Priority
Queue
Implementations
Fibonacci Binomial
Heap
Heap Heap
Elementary
Priority Queue ADT
• “FIFO” in some special sense. The “ rst” means some kind of
“priority”, such as value (largest or smallest)
• PriorityQ create()
• Precondition: none
• Postconditions: If pq=create(), then, pq refers to a newly created object and
isEmpty(pt)=true
• boolean isEmpty(PriorityQ pq)
• precondition: none
**pq can always be
• int getMax(PriorityQ pq) thought as a sequence
• precondition: isEmpty(pq)=false
• postconditions: ** of pairs (idi, wi), in
• void insert(PriorityQ pq, int id, oat w) non-decreasing order
• preconditions: none of wi
• postconditions: isEmpty(pq)=false; **
• void delete(PriorityQ pq)
• precondition: isEmpty(pq)=false
• postconditions: value of isEmpty(pq) updated; **
• void increaseKey(PriorityQ pq, int id, oat newKey)
fl
fi
fl
Heap: an Implementation of
Priority Queue
• A binary tree T is a heap structure if:
• T is complete at least though depth h-1
• All leaves are at depth h or h-1
• All path to a leaf of depth h are to the left of all path to
a leaf of depth h-1
50
9
24 30
5 7
20 21 18 3
12 5 6
1 4 3 6
Heap: an Implementation of
Priority Queue
Heap
structure
Heap
Partial
order
property
HeapSort: the Strategy
heapSort(E, n)
Construct H from E, the set of n elements to be sorted;
for (i = n; i ≥ 1; i -=1){
curMax = getMax(H);
deleteMax(H);
E[i] = curMax;
}
deleteMax(H)
copy the rightmost element on the lowest level of H into K;
Delete the rightmost element on the lowest level of H;
xHeap(H, K);
fi
FixHeap
• Input: A nonempty binary tree H with a “vacant” root and its
two subtrees in partial order. An element K to be inserted.
24 30 24 30
20 21 18 3 20 21 18 3
12 5 6 12 5
K=6
30 30
24 24 18
20 21 18 3 20 21 3
12 5 12 5
K=6 K=6
Worst Case Analysis for
xHeap
• 2 comparisons at most in one activation of the procedure
• The tree height decreases by one in the recursive call
• So, 2h comparisons are needed in the worst case, where h is the height of the tree
• Procedure: One comparison:
largerSubHeap is left- or right-
xheap(H, K) Subtree(H), the one with larger key at
if(H is a leaf) insert K in root(H); its root.
else Special case: rightSubtree is empty.
set largerSubHeap;
if(K.key≥root(largerSubHeap).key) insert K in root(H);
else
recursion
Insert root(largerSubHeap) in root(H);
xHeap(largerSubHeap, K);
“Vacant” moving down
return;
fi
fi
fi
Heap Construction
• Note: if left subtree and right subtree both
satisfy the partial order tree property, then
xHeap(H, root(H)) gets the thing done.
root
• Sum of heights
• At most n-1
• When the sum reaches n-1?
Implementing Heap
Using Array
9
50
5 7 24 30
20 21 18 3
1 4 3 6
9 5 7 1 4 3 6 12 5 6
50 24 30 20 21 18 3 12 5 6
Looking for the
Children Quickly
Starting from 1, not zero, then 50
the j th level has 2j-1 elements,
and there are 2j-1-1 elements in
24 30
the proceeding j-1 levels
altogether.
20 21 18 3
So, if E[i] is the kth
element at level j, then
i=(2j-1-1)+k, and the 12 5 6
index of its left child (if
existing) is
50 24 30 20 21 18 3 12 5 6
i+(2j-1-k)+2(k-1)+1=2i
The number of node on The number of children of the
the right of E[i] on level j nodes on level j on the left of E[i]
HeapSort: In-Space
Implementation
Heap
implemented as
a array (initial)
∑ ∫1
2 ⌈log k⌉ ≤ 2 log e ln xdx = 2 log e(n ln n − n) = 2(n log n − 1.443n)
k=1
“Vacant” ltering
90 65 90
down: left vs. right
80 65
80 75
70 75
70 35
60 35
25 60
25 50 Element(55) to be
45 50 inserted bubbling up:
45
40 element vs. parent
40 15
15
fi
Improvement by Divide-
and-Conquer
“Vacant” ltering down
90 65 90
only half-way
80 65
80 75
70 75
70 35
35
25 60
25 60 If the element is
45 50 smaller, ltering down
45
50 half-half-way
40 15
∑ ⌈ 2k ⌉ ⌈ 2t ⌉
t
h h
+ = h = log(n + 1)
k=1
• At most, lg(h) checks for reverse direction are executed. So,
the number of comparisons in a cycle is at most h+log(h)
⌈2 ⌉ (⌈ 2 ⌉ (⌊ 2 ⌋))
h h h
T(h) = + max ,1 + T
⌈2 ⌉ (⌊ 2 ⌋)
h h
T(h) = +1+T
Solving the Recurrence
Equation by Recursive Tree
For sorting a sequence of
size n, n cycles of xHeap
T(n) ⌈h/2⌉ + 1
are executed, so:
n(h + ⌈log(h + 1)⌉)
T(⌊n/2⌋) ⌈h/4⌉ + 1
T(1) 1+1
fi
T(1) = 2
Inductive Proof T(h) =
⌈2 ⌉ (⌊ 2 ⌋)
h h
+1+T
• The recurrence equation for xHeapFast:
• Proving the following solution by induction:
T(h) = h + ⌈log(h + 1)⌉
• According to the recurrence equation:
T(h + 1) = ⌈(h + 1)/2⌉ + 1 + T(⌊(h + 1)/2⌋)
• Applying the inductive assumption to the last term:
T(h + 1) = ⌈(h + 1)/2⌉ + 1 + ⌊(h + 1)/2⌋ + ⌈log(⌊(h + 1)/2⌋ + 1)⌉
(It can be proved that for any positive integer:
⌈log(⌊(h)/2⌋ + 1)⌉ + 1 = ⌈log(h + 1)⌉ )
Wn(n) = n log n + Θ(n log log n) For Accelerated Heap
fi
Generalization of a Heap
• d-ary heap
• Structure / partial order
• How to choose “d”?
4-art heap
• Top-down: x the parent node
• Cos: always 1
fi
fi
Not only for Sorting
• Eg1: how to nd the k th max element?
• In sorted order?
• Eg3: how to merge k sorted lists?
• Eg4: how to nd the median dynamically?
• ...
fi
fi
fi
fi
Thank you!
Q&A