Professional Documents
Culture Documents
Structures
Week 6-Lecture 15
Dr. M. Pirouz
Wednesday, September 25, 2019
Copyright Notice
• These slides are provided to students for their private use in
learning the material, and are not permitted for any other use
without proper permission
• A large portion of the material in these slides are obtained
from the publisher MIT Press as instructor supplements for the
course textbook: Introduction to Algorithms by Cormen et al.,
and used according to their stipulations
– The publisher and the author retain the copyright on their material
© 2018 MIT Press. All Rights Reserved.
CS Department
General Advice for Study
CS Department
Asymptotic notations
CS Department
Exercise
logn
n
n
nlogn
n2
2n
CS Department
5
Running Time Analysis
s=0
for i = 1 to n
for j = 1 to i
s=s+I
CS Department
Sorting
• Insertion sort
– Design approach: incremental
– Sorts in place: Yes
– Best case: Θ(n)
– Worst case: Θ(n2)
• Bubble Sort
– Design approach: incremental
– Sorts in place: Yes
– Running time: Θ(n2)
CS Department
Sorting
• Selection sort
– Design approach: incremental
– Sorts in place: Yes
– Running time: Θ(n2)
• Merge Sort
– Design approach: divide and conquer
– Sorts in place: No
– Running time: Let’s see!!
CS Department
Analysis of Insertion Sort
∑
n
t
do A[i + 1] ← A[i] c6 j =2 j
∑
n
(t j − 1)
i←i–1 c7 j =2
T(n) = c1(n+1) + c2 ∑ (n − i + 1) + c3 ∑ (n − i ) + c4 ∑ (n − i )
i =1
i =1 i =1
n
= Θ(n) + (c2 + c2 + c4) ∑ (n − i )
i =1
2
n n n
n ( n + 1) n n
≈ ∑ (n − i ) =∑ n − ∑ i = n −
2
= −
i =1 i =1 i =1 2 2 2
T(n) = Θ(n2)
Analysis of Selection Sort
Alg.: SELECTION-SORT(A) cost times
n ← length[A] c1 1
for j ← 1 to n - 1 c2 n
do smallest ← j c3 n-1
≈n2/2
c4 ∑nj=−11 (n − j + 1)
comparisons for i ← j + 1 to n
do if A[i] < A[smallest] c5 ∑
n −1
j =1
(n − j )
≈n
exchanges then smallest ← i c6 ∑
n −1
j =1
(n − j )
Def.: Recurrence = an equation or inequality that describes a function in terms of its value
on smaller inputs, and one or more base cases
• Recurrences arise when an algorithm contains recursive calls to itself
• Methods for solving recurrences
– Substitution method
– Iteration method
– Recursion tree method
– Master method
• Unless explicitly stated choose the simplest method for solving recurrences
CS Department
Analyzing Divide-and Conquer Algorithms
CS Department
Merge Sort
Alg.: MERGE-SORT(A, p, r) p q r
1 2 3 4 5 6 7 8
MERGE-SORT(A, p, q) Conquer
MERGE-SORT(A, q + 1, r) Conquer
MERGE(A, p, q, r) Combine
CS Department
Merge - Pseudocode
Alg.: MERGE(A, p, q, r)
1. Compute n1 and n2
2. Copy the first n1 elements into L[1 . . n1 + 1] and the next n2 elements into R[1 . . n2 + 1]
3. L[n1 + 1] ← ∞; R[n2 + 1] ← ∞
4. i ← 1; j ← 1 p q r
5. for k ← p to r 1 2 3 4
2 4 5 7 1 2 3 6
5 6 7 8
6. do if L[ i ] ≤ R[ j ]
7. then A[k] ← L[ i ]
n1 n2
8. i ←i + 1 p q
9. else A[k] ← R[ j ] L 2 4 5 7 ∞
10. j←j+1
q+1 r
R 1 2 3 6 ∞
CS Department
Running Time of Merge (assume last for loop)
CS Department
MERGE-SORT Running Time
• Divide:
– compute q as the average of p and r: D(n) = Θ(1)
• Conquer:
– recursively solve 2 subproblems, each of size n/2 ⇒ 2T (n/2)
• Combine:
– MERGE on an n-element subarray, takes Θ(n) time ⇒ C(n) = Θ(n)
Θ(1) if n =1
T(n) = 2T(n/2) + Θ(n) if n > 1
CS Department
Master’s method
• Used for solving recurrences of the form:
n
T (n) = aT + f (n)
b
where, a ≥ 1, b > 1, and f(n) > 0
Compare f(n) with nlogba:
af(n/b) ≤ cf(n) for some c < 1 and all sufficiently large n, then:
19
CS Department regularity condition T(n) = Θ(f(n))
Quicksort
• Quicksort
Partition the array A into 2 subarrays A[p..q] and A[q+1..r], such that each
– Idea: element of A[p..q] is smaller than or equal to each element in A[q+1..r].
Then sort the subarrays recursively.
CS Department
Analysis of Quicksort
CS Department
The Heap Data Structure
7 4
5 2
Heap
CS Department
Array Representation of Heaps
A heap can be stored as an
array A.
Root of tree is A[1]
Parent of A[i] = A[ i/2 ]
Left child of A[i] = A[2i]
Right child of A[i] = A[2i + 1]
Heapsize[A] ≤ length[A]
The elements in the subarray
A[(n/2+1) .. n] are leaves
The root is the max/min element
of the heap
A heap is a binary tree that is filled in order
CS Department
Operations on Heaps
(useful for sorting and priority queues)
• MAX-HEAPIFY O(lgn)
• BUILD-MAX-HEAP O(n)
• HEAP-SORT O(nlgn)
• MAX-HEAP-INSERT O(lgn)
• HEAP-EXTRACT-MAX O(lgn)
• HEAP-INCREASE-KEY O(lgn)
• HEAP-MAXIMUM O(1)
• You should be able to show how these algorithms perform on a given heap, and tell their running time
CS Department
Questions!
CS Department