Basic Algorithms Fall 2012 Problem Set 4
Due: Wednesday, Oct. 10
Problems 13 assume the following. You have n coins they all look identical, and all have the
same weight except one, which is heavier t
Basic Algorithms Fall 2012 Problem Set 3
Due: Wednesday, Oct. 3
NOTE: All students should complete exercises 15. Students in the honors section should additionally complete exercises 68, and hand in t
Homework 11: solutions
Isaac Henrion
Problem 1
We are going to keep track of the queue Q and the unstarted vertices U . Initially, Q = and
U = cfw_1, 2, 3, 4, 5, 6, 7, 8, 9.
We start with the line for
Homework 10: solutions
Isaac Henrion
Problem 1
We do it in two steps. First, find the smallest number in the right subtree of 2. This is the tree
rooted at 6, whose minimum element is the one furthest
Homework 8: solutions
Isaac Henrion
Remark In these solutions we use to denote the sample space (set of elementary events).
Problem 1
= cfw_HHH, HHT, HT H, HT T, T HH, T HT, T T H, T T T
X
Pr [one h
Homework 7: solutions
Isaac Henrion
Here is an example trace with a = 72.
1
Problem 2
We use the recursion tree method to solve the recurrence relation. Looking at the equation, we
can read off the wo
Homework 9: solutions
Ojas Deshpande
Problem 1
Starting with B = 2, and T = 10, wed first swap A[3]=10 and A[10]=4. The next swap would
be A[4] = 11 and A[7] = 3. Finally, when B = 7 and T = 6, wed sw
Homework 3: solutions
Ojas Deshpande
Remainder about self-grading.
A good self-criticism
briey
points explicitly at the mistakes of your solution,
summarizes the idea, step, or technique that you had
Homework 5: solutions
Isaac Henrion
Remark All initializations of lookup tables and main function calls can be (and in real software)
should be put into wrapper functions.
Problem 1
We begin by immedi
n=0
n=1
25
25 + 16 = 41
8
2 8 = 16
i
n=1
n=2
n = 3
n = 2
n=3
AB
AC
BA
BC
CA
CB
! ABA
! ACA
! BAB
! BCA
! CAB
! CBA
ABC
ACB
BAC
BCB
CAC
CBC
i
3 2n
1
s
i
n
i
List
List
List
Recitation November 10
1. Three people line up to board a flight and everyone has a ticket with an assigned seat. However, the first person in line has lost his ticket and takes a random seat. After t
CSCI-UA.0310: Basic Algorithms
Homework assignment 10
Please read these instructions carefully for each assignment, though they are generally do not
vary between the assignments
1. You need to follow
A small tree
F
F
A
0
1
A
F
F
-1
-1
2
3
4
5
6
7
Computer memory; here of size 24
F
A
-1
2
16
F
8
-1
8
9
10 11 12 13 14 15 16 17 18 19 20 21 22 23
Variable Tree is stored in location 13
An example of st
1
2
3
4
5
6
We have two functions f .n/ and g.n/. n are positive integers and the functions are non-negative,
that is, f .n/ 0 and g.n/ 0 for all n.
We want to compare these two functions. But they ar
CSCI-UA.0310: Basic Algorithms
Homework assignment 12
Please read these instructions carefully for each assignment, though they are generally do not
vary between the assignments
1. You need to follow
1
Lower Bound for the Tower of Hanoi Problem
2
3
4
5
6
7
8
9
10
11
Theorem 1. The problem of the Tower of Hanoi of size n requires at least 2n
Proof. Consider any algorithm for the problem, recursive
Pn
iD1 i
1
D
n.nC1/
2
2
3
Pn
i? It is
n.nC1/
,
2
4
What is
5
Example 1. Consider the simple case of n D 4. Then
iD1
but why?
n
1 C 2 C 3 C 4 D .1 C 4/ C .2 C 3/ D 5 C 5 D .n C 1/ C .n C 1/ D .n C 1/ :
1
1
Example where does not hold, for page 138
2
I covered this on the blackboard.
3
Consider two functions
4
1. f .n/ D n2 and
5
2. g.n/ D n if n is not a prime and g.n/ D n3 if n is a prime.
7
Then n
Train Robber
Isaac Henrion
This is a thorough exposition of the train robber problem from recitation - I messed up some
of the details of retrieving the actual cars to rob, so here is a better solutio
The wood-cutting problem assuming length 4 only
and with the Value as in the book
1
2
3
4
5
1. Best.4/ D maxf2; 4; 7; 6g D 7
6
2.
Best.0/ C Value0; 4 D 0 C 2 D 2
7
3.
Best.1/ C Value1; 4 D maxf2g C 2
1
1
The sum of a geometric series
Theorem 1.
n
X
a qi
1
1 qn
1 q
Da
i D1
2
for n 1:
Theorem 1
3
4
Proof. By induction on n.
5
n D 1. The claim is that a q 0 D a
6
n > 1. We know that
n
X1
7
1 q1
1
Proving that n D o.n2/
1
Zvi M. Kedem
2
3
4
What does n D o.n2 / mean? Looking at the handout I gave you, this means that
n D O.n2 /
and
n2 6D O.n/
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
So lets c
CSCI-UA.0310: Basic Algorithms
Homework assignment 08
Please read these instructions carefully for each assignment, though they are generally do not
vary between the assignments
1. You need to follow
CSCI-UA.0310: Basic Algorithms
Homework assignment 07
Please read these instructions carefully for each assignment, though they are generally do not
vary between the assignments
1. You need to follow
CSCI-UA.0310-001/002 Basic Algorithms
October 27, 2016
Problem Set 6
Lecturer: Yevgeniy Dodis
Due: Tuesday, November 1
Problem 6-1 (Walks on Augmented Trees)
8 (+4) Points
Consider a binary search tre
CSCI-UA.0310: Basic Algorithms
Homework assignment 09
Please read these instructions carefully for each assignment, though they are generally do not
vary between the assignments
1. You need to follow
ways[i]
ways[n]
i
ways[0] = 1
1
0
i
1, 2,
i
3
i
ways[i] = ways[i
1] + ways[i
2] + ways[i
3]
N +1
N
3
O(N 3) = O(N )
N
1, N
2, N
3
N
3
O(n)
cubes[i]
i
cubes[n]
1
0
cubes[n]
n
cubes[0] = 1
n
n
cubes[n]
Dynamic Programming: Subset Sum & Knapsack
Slides by Carl Kingsford
Mar. 22, 2013
Based on AD Section 6.4
Dynamic Programming
Extremely general algorithm design technique
Similar to divide & conquer:
CS161, Winter 2011
Handout #19
Lecture Notes on QuickSort Analysis
1
The Algorithm
We are given an unsorted array A containing n numbers. QuickSort relies on the same array partitioning
subroutine use
CSCI-UA.0310: Basic Algorithms
Homework assignment 05
Please read these instructions carefully for each assignment, though they are generally do not
vary between the assignments
1. You need to follow