Solutions: Homework 2
CSOR W4231: Analysis of Algorithms I
Fall 2015
Problem 1
(graded by Nivvedan)
(a)
T (n) = 9T (n/3) + n2
Master Theorem can be applied to this recurrence relation. a = 9, b = 3 here.
nlogb a = n2 . By case 2 of the Masters theorem, T

COMS 4231: Analysis of Algorithms I, Fall 2012
Problem Set 2, due Thursday October 4, in class
Please follow the homework submission guidelines posted on the web
Problem 1. Solve the following recurrences asymptotically. For each recurrence show
T(n)=f(n)

CSOR W4231.002 Spring, 2016
Homework 1
Out: Monday, January 25, 2015
Due: 8pm, Monday, February 8, 2015
Please keep your answers clear and concise. Collaboration is limited to discussion of ideas
only. You should write up the solutions entirely on your ow

Solutions: Homework 1
CSOR W4231: Analysis of Algorithms I
Fall 2015
Problem 1
(Graded by Nivvedan)
(a)
f (n) = 3n3 + 2n2 + 5n + 2; g(n) = (n2 1)2
We saw in class that a polynomial of degree d is (nd ).
f (n) is a third-degree polynomial whose highest ter

Analysis of Algorithms
Solutions to Problem Set #1
Problem 1 (graded by Dan)
(a)
O, , and because both f and g are polynomials of degree 3, i.e. they both are
(n3 ).
(b)
O, , and because g (n) = 2n+5 = 25 2n = 32 2n = (2n ).
(c)
o and O because f (n) = 23

COMS 4231: Analysis of Algorithms I, Fall 2015
Problem Set 1, due Thursday September 24, 11:40am on Courseworks
Please follow the homework submission guidelines posted on
courseworks
For all algorithms that you give, show their correctness and analysis of

CSOR 4231: Final Practice Problems, Fall 2014
These problems are ungraded, and are intended as a study aid. Solutions will also be posted
on courseworks. They are very similar to the problems that will appear on the nal. The nal is
closed book, closed not

COMS 4231: Analysis of Algorithms I, Fall 2015
Problem Set 5, due Tuesday November 24, 11:40am in pdf format on
Courseworks.
Please follow the homework submission guidelines posted on courseworks.
As usual, for each of the algorithms that you give, includ

COMS 4231: Analysis of Algorithms I, Fall 2015
Problem Set 2, due Thursday October 8, 11:40am on Courseworks
Please follow the homework submission guidelines posted on courseworks.
In all problems that ask you to give an algorithm, include a justification

COMS 4231: Analysis of Algorithms I, Fall 2012
Problem Set 3, due Thursday October 18, in class
Note: We plan to post solutions on Friday October 19, in preparation
for the midterm, so no homeworks will be accepted after Friday 1pm.
Please follow the home

CSOR 4231: Midterm Practice Problems, Fall 2014
These problems are ungraded, and are intended as a study aid. Solutions will also be posted
on courseworks. They are very similar to the problems that will appear on the midterm. The
midterm is closed book,

CSOR 4231: Midterm Practice Problems, Fall 2014
These problems are ungraded, and are intended as a study aid. Solutions will also be posted
on courseworks. They are very similar to the problems that will appear on the midterm. The
midterm is closed book,

COMS 4231: Analysis of Algorithms I, Fall 2012
Problem Set 5, due Thursday November 15, in class
Please follow the homework submission guidelines posted on the web
As usual, for each of the algorithms that you give, include an explanation of how the
algor

Counting Sort, Radix Sort
CS 4231, Fall 2015
Mihalis Yannakakis
1
Counting Sort
Restricted domain: D=cfw_1,k
Idea: Count how many input elements for each i in D
Example: Input A = [ 1, 2, 1, 3, 2, 5, 3, 2, 5]
Counts: C(1)=2, C(2)=3, C(3)=2, C(4)=0, C(5)

Quicksort
CS 4231, Fall 2015
Mihalis Yannakakis
Quicksort
Based on divide and conquer
practical, fast,
sorts in place
Quicksort
Divide: Partition the input array A of
elements with respect to a pivot element x
into two parts:
x
x
x
Conquer: Sort recu

Solving Recurrences and the
Master Theorem
CS 4231, Fall 2015
Mihalis Yannakakis
Recurrences
Arise in the analysis of recursive algorithms that reduce an
instance of a problem to smaller instances
Function of T(smaller n ') if n cst.
T (n)
if n cst.
c

Heap Priority Queue
and Heapsort
CS 4231, Fall 2015
Mihalis Yannakakis
1
Priority Queue
Max-Priority Queue: Data structure for a set S of
items, each with a key (its priority)
Basic Operations:
Insert: insert item x ( S := S cfw_x)
Max: returns an ite

Comparison Lower Bounds
CS 4231, Fall 2015
Mihalis Yannakakis
1
Comparison Sorts
Many sorting algorithms that we saw (Quicksort,
Mergesort, Insertion Sort) use at least n logn)
time.
Is this the best possible?
All these algorithms are comparison sorts:

Randomization
CS 4231, Fall 2015
Mihalis Yannakakis
1
Randomized algorithms
Make random choices (coin flips, random numbers .)
Different random choices are assumed independent
Outcome of algorithm and running time depends on
random choices (besides inp

3 IowveworK A
My y32867 Yao Song
6b,? ,- b3; Lh):"\,z {\lojba: ML”: 159(7QVH)
Thus by (use?) Tux): 0C“)
C T 3* " m 2n
m): IL'iVT [$85
azg, m ,n‘ vazn‘wzém )
3.
“Ms: A”? 5 ﬂ-KO‘I : A”: O (IL/Bf2 g) Wm fwﬂa’f
g /
5—Wwa by CM)? 1 , Tun: 9(11‘1585)
7U1)

Selection
CS 4231, Fall 2015
Mihalis Yannakakis
1
Selection (Order Statistics)
Input: Set A of n numbers (or more
generally, elements from an ordered
domain), number i, 1in
Output: i-th smallest (rank i) element of A
i=1:
minimum
i=n:
maximum
i=(n+1)

Solutions: Homework 3
CSOR W4231: Analysis of Algorithms I
Fall 2015
Problem 1
(Graded by Nivvedan)
In a min-heap, the min-heap property holds. That is, for each node l, except the root
node,
A[parent(l)] A[l]
The goal is to print out all elements in the

COMS 4231: Analysis of Algorithms I, Fall 2015
Problem Set 4, due Thursday November 12, 11:40am in pdf format on
Courseworks.
Please follow the homework submission guidelines posted on courseworks.
As usual, for each of the algorithms that you give, inclu

Dynamic Programming
CS 4231, Fall 2015
Mihalis Yannakakis
1
Dynamic Programming
General method that applies to a class of problems, often
a class of optimization problems (but not only
optimization)
Problem reduced to smaller (possibly overlapping)
proble

Search Trees
CS 4231, Fall 2015
Mihalis Yannakakis
1
Dictionary
Search/ Index structure
Maintain set S of items, each item has key and other info
Basic Operations:
Insert an item x
Search for an item with given key k
Other operations: empty?, size, list a

Selected Solutions for Chapter 2:
Getting Started
Solution to Exercise 2.2-2
S ELECTION -S ORT.A/
n D A:length
for j D 1 to n 1
smallest D j
for i D j C 1 to n
if Ai < Asmallest
smallest D i
exchange Aj with Asmallest
The algorithm maintains the loop inva