0% found this document useful (0 votes)
19 views

Berkeley Algorithms

The document describes several algorithms for solving graph problems: 1. BFS and Dijkstra's algorithm are described for finding shortest paths from a source node in a graph. BFS uses a queue while Dijkstra's uses a priority queue. 2. DFS is described for exploring a graph and finding connected components using previsit and postvisit callbacks. 3. Topological sorting of DAGs and finding shortest paths in DAGs using repeated updates is outlined. 4. FFT algorithms are summarized for multiplying polynomials and transforming data between coefficient and value representations efficiently.

Uploaded by

alexciuciucu13
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Berkeley Algorithms

The document describes several algorithms for solving graph problems: 1. BFS and Dijkstra's algorithm are described for finding shortest paths from a source node in a graph. BFS uses a queue while Dijkstra's uses a priority queue. 2. DFS is described for exploring a graph and finding connected components using previsit and postvisit callbacks. 3. Topological sorting of DAGs and finding shortest paths in DAGs using repeated updates is outlined. 4. FFT algorithms are summarized for multiplying polynomials and transforming data between coefficient and value representations efficiently.

Uploaded by

alexciuciucu13
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 318

sort

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

Pick a positive integer at random


if :
return yes
else:
return no
yes
yes

yes
function primality2( )
Input: Positive integer
Output: yes/no

Pick positive integers at random


if for all :
return yes
else:
return no
sign verify sign
verify
sign

sign

verify
function multiply( )
Input: Positive integers and , in binary
Output: Their product

max(size of , size of )
if : return

, leftmost , rightmost bits of


, leftmost , rightmost bits of

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

Pick some points , where

Compute and

Compute for all

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

explore( ) pre pre post 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 false


true

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

makequeue (using dist-values as keys)


while is not empty:
deletemin
for all edges :
if dist dist :
dist dist
prev
decreasekey
dist
dist
Initialize dist to 0, other dist values to
(the ‘‘known region’’)
while :
Pick the node with smallest dist
Add to
for all edges :
if dist dist :
dist 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

usp usp true


usp true
procedure insert( )
bubbleup( )

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)

while and key key :

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

Sort the edges by weight


for all edges , in increasing order of weight:
if find find :
add edge to
union

procedure makeset( )

rank

function find( )
while
return

makeset find

union

procedure union( )
find
find
if : return
if rank rank :

else:

if rank rank rank rank

find union
makeset makeset makeset

union union union

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

makequeue (priority queue, using cost-values as keys)


while is not empty:
deletemin
for each :
if cost :
cost
prev
decreasekey
procedure Huffman( )
Input: An array of frequencies
Output: An encoding tree with leaves

let be a priority queue of integers, ordered by


for to : insert( )
for to :
deletemin deletemin
create a node numbered with children

insert
first second third other

first
second
third
other

first second third other


true false

true

true false

true false
false
true

Input: a Horn formula


Output: a satisfying assignment, if one exists

set all variables to false


while there is an implication that is not satisfied:
set the right-hand variable of the implication to true

if all pure negative clauses are satisfied: return the assignment


else: return ‘‘formula is not satisfiable’’

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

initialize all dist values to


dist
for each , in linearized order:
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

Initialize all and all


for to :
for to :
if :
else:
return
A hash table, initially empty, holds values of indexed by

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 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 :

Output: A solution of the system, if one exists

if all coefficients are zero:


halt with message ‘‘either infeasible or not linearly independent’’
if : return

choose the coefficient of largest magnitude, and swap equations


for to :

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

true false true


true

false

true
false
false true

false true 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

test expand choose


Start with some problem
Let , the set of active subproblems
bestsofar
Repeat while is nonempty:
choose a subproblem (partial solution) and remove it from
expand it into smaller subproblems
For each :
If is a complete solution: update bestsofar
else if lowerbound bestsofar: add to
return bestsofar
Find a maximal matching
Return all endpoints of edges in
Pick any point as the first cluster center
for to :
Let be the point in that is farthest from
(i.e., that maximizes )
Create clusters: all whose closest center is
Given any graph :
compute (with ) and run algorithm on it
if the resulting tour has length :
conclude that has a Rudrata path
else: conclude that has no Rudrata path
Discard any item with weight
Let max
Rescale values max
Run the dynamic programming algorithm with values
Output the resulting choice of items
let be any initial solution
while there is some solution in the neighborhood of
for which cost cost : replace by
return
let be any starting solution
repeat
randomly choose a solution in the neighborhood of
if cost cost is negative:
replace by
else:
replace by with probability .
4 states, cost 6

8 states, cost 4 2 states, cost 4

8 states, cost 3 8 states, cost 3

4 states, cost 2

1 state, cost 0
choose
expand

choose
expand

choose
expand
lowerbound

Let be any spanning tree of


while there is an edge-swap which reduces cost :

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

You might also like