HW3 Solution
7.2-2
If the elements in A are the same, then the returned element from each call to PARTITION(A; p; r)
is r - 1 thus yielding the worst-case partitioning. The total running time is easily seen to be (n2).
7.2-5
The minimum depth follows a pa
6.5-3
6.5-7.
FIFO queue: When inserting an element in the queue, a key value is assigned to it according to the inserting
order. The first inserted element has the smallest key value; the latest inserted element has the biggest
key value. To extract an el
Solution to HW1
Solution to Exercise 2.3-6
The while loop of lines 5-7 of procedure INSERTION-SORT scans backward through the sorted array A[1.j1] to find the appropriate place for A[j]. The hitch is that the loop not only searches for the proper place
fo
COT 6405 Introduction to Theory of
Algorithms
Midterm I review
10/2/2013
1
About midterm I
Midterm I will cover everything we have
learned so far (quicksort is not included)
From Intro lecture to Lecture 7 (inclusive)
Function growth rate analysis, div
COT 6405 Introduction to Theory of Algorithms
First Midterm, Fall 2013
Oct 09, 2013
Time: 3:05pm 4:20pm
Name: _
Score: _
This is a closed book, closed notes exam and no calculators are
allowed.
The exam ends with a five minute warning at the end of the pe
COT 6405 Introduction to Theory of
Algorithms
Topic 3. Divide and Conquer
8/31/2015
1
General rule
To solve (an instance of) a problem P
IF (the instance of) P is large enough THEN
Divide P into smaller instances of the same problem
Recurse to solve the s
COT 6405 Introduction to Theory of
Algorithms
Topic 6. Heapsort
9/16/2015
1
Heaps
A heap is a complete binary tree or a nearly
complete binary tree;
9/16/2015
2
Merge Sort v.s. Insertion Sort
The number of comparisons in merge sort
()
The number of co
COT 6405 Introduction to Theory of
Algorithms
Topic 6. Heapsort (contd)
9/21/2015
1
Heap operations: BuildHeap
We can build a max-heap in a bottom-up
manner by running MAX-Heapify(x) as x
runs through all nodes
for i n downto 1 do MAX-Heapify(i)
Order
COT 6405 Introduction to Theory of
Algorithms
Topic 4. Recurrences
9/15/2014
1
Recurrences
What is a recurrence?
An equation that describes a function in terms of
its value on smaller functions
The time complexity of divide-and-conquer
algorithms can b
COT 6405 Introduction to Theory of
Algorithms
Topic 11. Order Statistics
10/15/2014
1
Order statistic
The i-th order statistic in a set of n elements is
the i-th smallest element
The minimum is thus the 1st order statistic
The maximum is the n-th order
COT 6405 Introduction to Theory of
Algorithms
Topic 10. Linear Time Sorting
10/13/2014
2
How fast can we sort?
The sorting algorithms we learned so far
Insertion Sort, Merge Sort, Heap Sort, and
Quicksort
How fast are they?
Insertion sort O(2 )
Merge
COT 6405 Introduction to Theory of
Algorithms
Topic 2. Algorithm Analysis
9/4/2014
1
Growth rate analysis
9/4/2014
2
Why is growth rate important?
Actual execution time assuming 1,000,000
basic operations per second.
Input
n
nlgn
size
10
0.00001
sec
100
COT 6405 Introduction to Theory of
Algorithms
Topic 8. Quicksort
9/29/2014
1
Quicksort
Sorts in place
Only a constant number of elements stored
outside the sorted array
Sorts O(n lg n) in the average case
Sorts O(n2) in the worst case
So why people u
COT 6405 Introduction to Theory of
Algorithms
Topic 11. Order Statistics
10/28/2013
1
Order statistic
The i-th order statistic in a set of n elements is
the i-th smallest element
The minimum is thus the 1st order statistic
The maximum is the n-th order
COT 6405 Introduction to Theory of
Algorithms
Topic 13. Binary Search Tree
11/6/2013
1
Binary Search Trees
Binary Search Trees (BSTs) are an important
data structure for dynamic sets
In addition to satellite data, nodes have:
key: an identifying field
COT 6405 Introduction to Theory of
Algorithms
Midterm II review
11/7/2013
1
Quicksort
Sorts in place
Only a constant number of elements stored
outside the sorted array
Sorts O(n lg n) in the average case
Sorts O(n2) in the worst case
So why people us
Does binary search improve the
performance?
Can we improve the worst-case running time
of insertion sort using binary search?
No, because the loop not only searches for the
proper place, but also moves array elements.
Binary search cannot reduce the ti
COT 6405 Introduction to Theory of
Algorithms
Topic 9. Randomized Quicksort
10/28/2013
1
Worst case quicksort
What will happen if the array is already
sorted?
The partitioning routine produces n-1 elements
and one with 0 elements.
How about the running
COT 6405 Introduction to Theory of
Algorithms
Topic 6. Heapsort (contd)
9/24/2013
1
Heap operations: BuildHeap
We can build a max-heap in a bottom-up
manner by running MAX-Heapify(x) as x
runs through all nodes
for i n downto 1 do MAX-Heapify(i)
Order
COT 6405 Introduction to Theory of
Algorithms
Topic 8. Quicksort
11/6/2013
1
Quicksort
Sorts in place
Only a constant number of elements stored
outside the sorted array
Sorts O(n lg n) in the average case
Sorts O(n2) in the worst case
So why people u
COT 6405 Introduction to Theory of
Algorithms
Final exam review
12/4/2013
1
About final exam
The final will cover everything we have
learned so far, including NP-Completeness
Closed books, closed computers, and closed
notes.
Collaboration at the exam i
COT 6405 Introduction to Theory of
Algorithms
Topic 9. Randomized Quicksort
10/6/2015
1
Worst case quicksort
What will happen if the array is already
sorted?
The partitioning routine produces n-1 elements
and one with 0 elements.
How about the running
COT 6405 Introduction to Theory of
Algorithms
Topic 5. Master Theorem
9/17/2014
1
Solving the recurrences
Substitution method
Recursion tree
Master method
9/17/2014
2
The Master Theorem
Given: a divide-and-conquer algorithm
An algorithm that divides
COT 6405 Introduction to Theory of
Algorithms
Topic 12. Hash tables
11/4/2013
1
Data Structures
We focus on data structures in this part
Ch.10: stack, linked list, queue, tree, pointer,
object,
In particular, structures for dynamic sets
Elements have