Basic Algorithms (V22.0310); Fall 2005; Yap
HOMEWORK 4
Date Due: Nov 7
Instructions
Please write CLEARLY. If you have bad handwriting, please consider printing your solutions.
Be concise, but not ob
Basic Algorithms (V22.0310); Fall 2005; Yap
MIDTERM (with SOLUTION)
Oct 17, 2005
Instructions
You are allowed one 8x11 sheet of notes.
Do all quesions. Start each question on its own page.
Read car
Hashing (4)
Cuckoo Hashing
1
Cuckoo Hashing
A simple scheme for resolving collisions in a hash table
Guaranteed constant time lookup
Expected constant time insertion
Requires stronger assumption for h
Hashing (2)
1
Recall: Universal Hashing
Let H = cfw_h be a family of hash functions
from U to V
m := V
Defn: H is called universal if for all , b U with
6= b,

cfw_ : h () = h (b)
.
m
Probabili
23 Trees
1
Dictionary: an abstract data type
A container that maps keys to values
Dictionary operations
Insert
Search
Delete
Several possible implementations
Balanced search trees
Hash tables
2
Congruences
Definition: n is a positive integer, , b Z
b (mod n) means = b + ny for some y Z
Equivalently, n  ( b)
Fact: for fixed n, the binary relation (mod n) is
an equivalence relation on the se
Number Theory Basics
Z = cfw_. . . , 2, 1, 0, 1, 2, . . .
For , b Z, we say that divides b if z = b for some
zZ
Notation:  b
Fact: for all , b, c Z:
 , 1  , and  0
0  = 0
 b and b  c =  c
Dijkstra Demo
Nodes
Blue: currently in Q
Red: currently in C
White: undiscovered
Edges
Black: unexplored
Red: in shortest path tree
Gray: useless
Blue numbers: dvalues
1
0
5
15
8
4
12
3
7
9
the one that is alphabetically first. Classify each edge as a tree edge or back edge, and give the
pre and post number of each vertex.
A
B
C
Basic Algorithms Spring 2017 Problem Set 3
Due:
F
E 19
D Ma
Basic Algorithms Spring 2017 Problem Set 6
Due: April 28
1. Weird recursion tree analysis. Suppose we have an algorithm that on problems of size n, recursively
solves two problems of size n/2, with a
Shortest paths in a DAG
Let G = (V, E) be a DAG with edge weights wt : E R
(edge weights may be negative)
Linear time (i.e., O(V + E) algorithm for Single
Destination variant (reverse G for Single
Basic Algorithms Spring 2017 Problem Set 1
Due: Feb. 14
1. Rates of growth. Compare the following pairs of functions, and indicate whether f = o(g), g = o(f ), or
f = (g). Justify your answers as best
Dynamic Programming
1
Dynamic Programming
A way of speeding up certain recursive algorithms
Idea: dont compute the same thing twice!
Example: Subset Sum
Given: positive integers 1 , . . . , n , and a
Probability Review
Basic definitions
Discrete probability distribution:
a function
P
Pr : [0, 1] such that Pr() = 1
called sample space
a point represents the outcome of some
experiment
Pr() repres
Depth First Search (DFS)
An extremely simple, fast, recursive agorithm to visit
all nodes reachable from a given node
Let G = (V, E) be a graph
We assume adjacency list (i.e., sparse) representation
A
Rings and Polynomials
Definition: A ring is a set R together with addition and
multiplication operations on R, such that:
(i) addition is commutative and association;
(ii) there exists an additive ide
Shortest Paths
The problem:
Let G = (V, E) be a directed graph
Edge weights wt : E R
Convention: wt(, ) := if (, ) 6 E
The weight of a path p = 0 , 1 , . . . , k :
wt(p) :=
k
X
wt(1 , )
=1
The sh
What Every Computer Scientist Should Know about
Number Theory
(v0.2)
Victor Shoup
Chapter 1
Basic properties of the integers
This chapter discusses some of the basic properties of the integers, includ
Greedy Algorithms
Example
The Line Breaking Problem
Given a sequence of words
Form a paragraph, breaking lines as necessary
Assumptions:
fixed width spacing
need at least one space between words
Polynomial arithmetic and the FFT
For algorithms on polynomials in R[X], running time =
number of operations in R
Naive algorithms:
+ g: O(n) operations in R
g: O(n2 ) operations in R
where n bounds
Hashing (1)
1
The general setup:
U (large, finite) universe of possible keys
V (small) set of slots of size m
typically V = [0 . . m)
h : U V a hash function from U to V
maps keys to slots
T[V] a
Basic Algorithms Spring 2017 Problem Set 7
Due: May 8
1. Halfpriced hash. In class, we studied a family of hash functions based on taking inner products. That
family was a family of hash functions fr
A Primer on Probability
Victor Shoup
1
Basic definitions
Let be a finite, nonempty set. A probability distribution on is a function Pr : [0, 1]
that satisfies the following property:
X
Pr() = 1.
(1)
Divide and Conquer
A first example: Merge Sort
A generic recursive sorting algorithm
Input: a list L
Output: a sorted list
if L 1 then / / L means length of L
return L
else
split L into two nonemp
Selection
1
General problem: Given a list L of n keys, and
k cfw_1, . . . , n, find kth smallest element in L
Special case: k = bn/ 2c . the median
One solution: sort the keys into increasing order, r
Graphs
G = (V, E):
V = set of nodes (a.k.a., vertices)
E = set of edges
G is usually assumed to be directed, so that an edge is
a pair of nodes (, ) (graphically, )
If (, ) E, lets call a successsor
Hashing (3)
1
Perfect Hashing
We have n fixed keys 1 , . . . , n
We want to be able to build a table with these keys, so
that lookups take constant time in the worst case
Basic strategy: universal has
Basic Algorithms Spring 2017 Problem Set 4
Due: March 30
1. Road trip. You are going on a long trip. You start on the road at mile post 0. Along the way there are n
hotels, at mile posts a1 < a2 < < a
Basic Algorithms Spring 2017 Problem Set 2
Due: Feb. 24
1. 23 trees basics. Suppose we insert the following numbers into a 23 tree (which is initially empty), in the
given order:
9, 15, 4, 3, 6, 13,