Berkeley Algorithms
Berkeley Algorithms
function fib1( )
if : return
if : return
return fib1( ) + fib1( )
fib1
fib1
fib1
fib1
fib1
fib1
fib1
fib1
function fib2( )
if return
create an array f[ ]
f[ ] , f[ ]
for :
f[ ] = f[ ] + f[ ]
return f[ ]
fib1
fib1
fib2
fib1
fib2
fib1
fib2
100
90
80
70
60
n2
50
40
30 2n+20
20
10
0
1 2 3 4 5 6 7 8 9 10
n
fib2
fib3
fib2
fib2
fib3
fib3
fib3
fib3 fib2
fib3
function multiply( )
Input: Two -bit integers and , where
Output: Their product
if : return
multiply
if is even:
return
else:
return
function divide( )
Input: Two -bit integers and , where
Output: The quotient and remainder of divided by
if : return
divide
if is odd:
if :
return
function modexp( )
Input: Two -bit integers and , an integer exponent
Output:
if : return
modexp
if is even:
return
else:
return
function Euclid( )
Input: Two integers and with
Output:
if : return
return Euclid( )
function extended-Euclid( )
Input: Two positive integers and with
Output: Integers such that and
if : return
Extended-Euclid( )
return
function primality( )
Input: Positive integer
Output: yes/no
yes
function primality2( )
Input: Positive integer
Output: yes/no
sign
verify
function multiply( )
Input: Positive integers and , in binary
Output: Their product
max(size of , size of )
if : return
multiply
multiply
multiply
return
function mergesort( )
Input: An array of numbers
Output: A sorted version of this array
if :
return merge(mergesort( ), mergesort( ))
else:
return
merge
function merge( )
if : return
if : return
if :
return merge
else:
return merge
merge
merge mergesort
mergesort
mergesort
mergesort
inject
eject
function iterative-mergesort( )
Input: elements to be sorted
(empty queue)
for to :
inject
while :
inject merge eject eject
return eject
sort
Input: Coefficients of two polynomials, and , of degree
Output: Their product
Compute and
Recover
function FFT( )
Input: Coefficient representation of a polynomial
of degree , where is a power of
, an th root of unity
Output: Value representation
if : return
express in the form
call FFT( ) to evaluate at even powers of
call FFT( ) to evaluate at even powers of
for to :
compute
return
function FFT( )
Input: An array , for a power of
A primitive th root of unity,
Output:
if : return
for to :
return
function f(n)
if n > 1:
print_line(‘‘still going’’)
f(n/2)
f(n/2)
split
split
merge
fastmultiply
function pwr2bin( )
if : return
else:
???
return fastmultiply( )
function dec2bin( )
if : return binary
else:
split into two decimal numbers , with digits each
return ???
binary
binary binary binary
binary
for downto :
procedure explore( )
Input: is a graph;
Output: visited( ) is set to true for all nodes reachable from
visited( ) true
previsit( )
for each edge :
if not visited( ): explore( )
postvisit( )
previsit postvisit
explore
explore
explore
explore
explore
explore
explore visited
pre postvisit
explore
procedure dfs( )
for all :
visited( ) false
for all :
if not visited( ): explore( )
explore( )
explore( )
explore
explore
explore
ccnum
procedure previsit( )
ccnum[ ] = cc
cc
explore
previsit postvisit
pre post
clock
procedure previsit( )
pre[ ] = clock
clock = clock + 1
procedure postvisit( )
post[ ] = clock
clock = clock + 1
pre post
pre post
pre post
pre
post
post post
post
post post
post
explore
explore
explore
post
post post
post
post
post
post
post
post
post
post
explore
pre post
G H I
pre post
pre post
G H I
twodegree
twodegree
explore
previsit postvisit
post post
cost
cost
cost
cost
true false
true false
pre
low
pre
low
low
S
E S A
A D
B E
D C B
C
S
E S A
A C D E
D C B
B
procedure bfs( )
Input: Graph , directed or undirected; vertex
Output: For all vertices reachable from , dist( ) is set
to the distance from to .
for all :
dist
dist
(queue containing just )
while is not empty:
eject
for all edges :
if dist :
inject
dist dist
S
A C D E
B
2 B
2
D B D
A 1 3
2 A
1
C 4 E C E
100 A A
S 50 S
200
B B
dist
prev
procedure dijkstra( )
Input: Graph , directed or undirected;
positive edge lengths ; vertex
Output: For all vertices reachable from , dist( ) is set
to the distance from to .
for all :
dist
prev nil
dist
dist
eject inject
makequeue insert deletemin
insert decreasekey
insert/ deletemin
deletemin
decreasekey insert
insert
insert
insert decreasekey
deletemin
insert
decreasekey
deletemin
dist
procedure update( )
dist dist dist
dist
dist
update
update
procedure shortest-paths( )
Input: Directed graph ;
edge lengths with no negative cycles;
vertex
Output: For all vertices reachable from , dist( ) is set
to the distance from to .
for all :
dist
prev nil
dist
repeat times:
for all :
update( )
update dist
dist
procedure dag-shortest-paths( )
Input: Dag ;
edge lengths ; vertex
Output: For all vertices reachable from , dist( ) is set
to the distance from to .
for all :
dist
prev nil
dist
Linearize
for each , in linearized order:
for all edges :
update( )
1 2 1
4 8 6 6 2 1 4
5 1 G 1 H
prev prev
procedure decreasekey( )
bubbleup( )
function deletemin( )
if :
return null
else:
siftdown( )
return
function makeheap( )
empty array of size
for :
for downto :
siftdown( )
return
procedure bubbleup( )
(place element in position of , and let it bubble up)
procedure siftdown( )
(place element in position of , and let it sift down)
minchild
while and key key :
minchild
function minchild( )
(return the index of the smallest child of )
if :
return 0 (no children)
else:
return key
makeheap
best
best
best
cost cost
cost
makeset
find
union
makeset find
union
procedure kruskal( )
Input: A connected undirected graph with edge weights
Output: A minimum spanning tree defined by the edges
for all :
makeset
procedure makeset( )
rank
function find( )
while
return
makeset find
union
procedure union( )
find
find
if : return
if rank rank :
else:
find union
makeset makeset makeset
union union
union
union find
union find
find find
find
find
function find( )
if find
return
find union
find
union
union
find
find
find
find
find
(edges picked so far)
repeat until :
pick a set for which has no edges between and
let be the minimum-weight edge between and
cost
cost
prev
cost prev
procedure prim( )
Input: A connected undirected graph with edge weights
Output: A minimum spanning tree defined by the array prev
for all :
cost
prev nil
Pick any initial node
cost
insert
first second third other
first
second
third
other
true
true false
true false
false
true
false true
true
true true
G
1 2 3
4 8 6 6 2 1 4
5 1 G 1 H
cost
union union union union union union union find
union find
sort the edges according to their weights
for each edge , in decreasing order of :
if is part of a cycle of :
(that is, remove from )
return
increment
reset
increment reset
I you is and to from
velociraptor
fire! help! run!
dist dist dist
dist
dist
dist
dist
for :
return
prev
mergesort
SNOWY SUNNY
S N O W Y S N O W Y
S U N N Y S U N N Y
SNOWY SUNNY
U O N W
EXPONENTIAL POLYNOMIAL
EXPO POL
O O
or or
L L
for :
for :
for :
for :
diff
return
E X P O N E N T I A L
P O L Y N O M I A L
for to :
return
function knapsack( )
if is in hash table: return
knapsack
insert into hash table, with key
return
for to :
for to :
for to :
return
dist
dist
dist dist
dist
dist
dist
for to :
for to :
dist
for all :
dist
for to :
for to :
for to :
dist dist dist dist
for to :
for all subsets of size and containing :
for all :
return
dist
dist
dist
dist dist
prev
dist
dict
true
dict
false
dict
procedure find-path( )
compute the value above
find-path( )
find-path( )
concatenate these two paths, with in the middle
B S A
D C T
let be any vertex of the feasible region
while there is a neighbor of with better objective value:
set
( )
Input: A system of equations in unknowns :
gauss
return
true false
true
false
true
1000 1000
1000 1000
false true
true
true
true
true
true
true
false
true
true false
true
false
false
true
false
false true
false
true false true
true
false true true
true
true
true
terminates(p,x) p x
p x
terminates
terminates(p,x)
function paradox(z:file)
1: if terminates(z,z) goto 1
paradox z
paradox
paradox(paradox)
true
false
Start with some problem
Let , the set of active subproblems
Repeat while is nonempty:
choose a subproblem and remove it from
expand it into smaller subproblems
For each :
If test( ) succeeds: halt and announce this solution
If test( ) fails: discard
Otherwise: add to
Announce that there is no solution
choose expand
4 states, cost 2
1 state, cost 0
choose
expand
choose
expand
choose
expand
lowerbound
return
for each variable:
set its value to either or by flipping a coin
start with any
while there is a subset such that
and do:
set