Title: CS 60 Slides
1ACM today
The Fall competition is getting organized
it's going to be lonely
2Mock mock contest 2
More teams solved problems more problems were
solved
Still one computer per team
All problems solved by some team
API-reference is OK on another machine
shipping
forward
panic
all-pairs shortest paths could be used for this
one
following calls through a graph of forwarding
instructions
3Infinities seen
4Infinities seen
int BIG 10000000 Michael E.
5Infinities seen
int BIG 10000000 Michael E. gridjk
1000000 Scott P.
6Infinities seen
int BIG 10000000 Michael E. gridjk
1000000 Scott P. pathsij 9001
Martin P.
7Infinities seen
int BIG 10000000 Michael E. gridjk
1000000 Scott P. pathsij 9001
Martin P. direct 2400 Russ R.
8Infinities seen
int BIG 10000000 Michael E. gridjk
1000000 Scott P. pathsij 9001
Martin P. direct 2400 Russ R. int
constant 1000 Zvi E.
9Infinities seen
int BIG 10000000 Michael E. gridjk
1000000 Scott P. pathsij 9001
Martin P. direct 2400 Russ R. int
constant 1000 Zvi E. public static
int Inf 200 George T.
10Infinities seen
int BIG 10000000 Michael E. gridjk
1000000 Scott P. pathsij 9001
Martin P. direct 2400 Russ R. int
constant 1000 Zvi E. public static
int Inf 200 George T. adjacencyab 0
Andrew H.
11Infinities seen
int BIG 10000000 Michael E. gridjk
1000000 Scott P. pathsij 9001
Martin P. direct 2400 Russ R. int
constant 1000 Zvi E. public static
int Inf 200 George T. adjacencyab 100
Andrew H. int infinity 31 Kwang
K.
12Infinities seen
int BIG 10000000 Michael E. gridjk
1000000 Scott P. pathsij 9001
Martin P. direct 2400 Russ R. int
constant 1000 Zvi E. public static
int Inf 200 George T. adjacencyab 100
Andrew H. int infinity 31 Kwang
K. define INFINITE 0x0fffffff Andrew F.
13int hashnode(char buf) int hv 0
while(buf) hv ltlt 6 hv buf
31 return hv
Infinities seen
int BIG 10000000 Michael E. gridjk
1000000 Scott P. pathsij 9001
Martin P. direct 2400 Russ R. int
constant 1000 Zvi E. public static
int Inf 200 George T. adjacencyab 100
Andrew H. int infinity 31 Kwang
K. define INFINITE 0x0fffffff Andrew F.
268435455
for (int woo 0 woo lt tries woo)
14Schedule
10/2/2007
Today -- search and related problems
one more algorithm
10/9/2007
Lab / mock mock contest
10/16/2007
No class conference fall break
10/23/2007
10/30/2007
Real mock contest - "real" rules apply
9pm to 1am Tuesday, 10/30
11/6/2007
Meeting for ACM teams
11/13/2007
Final CS 189 meeting contest wrap-up
15Max Flow
Ford-Fulkerson algorithm
12
B
D
20
16
sink
F
A
10
4
9
7
source
4
13
E
C
14
capacity
The problem how much traffic can get from the
source to the sink ?
16Max Flow
The problem how much traffic can get from the
source to the sink ?
12
B
D
20
16
sink
F
A
10
4
9
7
source
4
13
Capacity Graph
E
C
14
TO
C
capacity
A
B
C
D
E
F
A
B
C
FROM
D
E
F
17Find a path in C via BFS
The problem how much traffic can get from the
source to the sink ?
need to add this into here
12
B
D
20
16
sink
F
A
10
4
9
7
source
4
13
E
C
14
TO
C
capacity
A
B
C
D
E
F
A
B
C
FROM
D
E
F
18Create F
Create a FLOW GRAPH with the minimum weight from
that path
12
B
D
12
20
16
sink
12
12
F
A
10
4
9
7
source
4
13
Flow Graph
E
C
14
TO
F
capacity
A
B
C
D
E
F
A
B
flow in one direction is negative flow in the
other direction
C
FROM
D
E
F
19R C - F
Create a RESIDUAL GRAPH with the rest of the
capacity after that flow
0
B
D
12
8
4
sink
12
12
F
A
10
4
9
7
source
4
13
Residual Graph
E
C
14
TO
R
capacity
A
B
C
D
E
F
A
B
reverse edges allow the "undoing" of previous
flow!
C
FROM
D
E
F
20Keep going!
Continue running this on the residual capacity
until BFS fails
12/12
B
D
19/20
11/16
sink
F
A
0/10
1/4
0/9
7/7
source
4/4
12/13
E
C
11/14
There is no longer a path from A to F !
211. Set F to all 0
The max flow algorithm
2. Compute R C - F
3. BFS in R from source to sink.
3a. If no path, youre done.
3b. If ? a path, add the available capacity to
F goto (2).
Floyd-Fulkerson
12
B
D
3
20
16
sink
2
4
1
0
F
A
10
4
2
9
7
1
2
source
4
4
13
E
C
5
14
capacity
flow
residual
Residual Graph
3
B
D
12
18
9
Don't need to keep R around explicity Keep only
the current flow (F) and the original capacity
(C).
4
2
A
9
1
2
F
7
5
6
2
5
C
E
4
11
9
22Pseudo-code (Python)
Get into the flow!
def edmonds_karp(C, source, sink) n len(C)
C is the capacity matrix F 0 n for i
in range(n) F is the flow matrix
residual capacity from u to v is Cuv -
Fuv while True path BFS(C, F,
source, sink) if not path
break flow float(1e309) Infinity !
traverse path to find smallest capacity
for (u,v) in path flow
min(flow, Cuv - Fuv) traverse
path to update flow for u,v in path
Fuv flow Fvu -
flow return sum(Fsourcei for i in
xrange(n)) def BFS(C, F, source, sink)
queue source paths
source while queue u
queue.pop(0) for v in range(len(C))
if Cuv - Fuv gt 0 and v not in
paths Python English !
pathsv pathsu (u,v) if
v sink return pathsv
queue.append(v) return None
A little bit of name contention
edmonds_karp
is really Ford-Fulkerson, but with other people
getting the credit
23Max flow examples
Dinner problem
tables with capacities
Circuses problem
3
5
Circus inspectors need to visit every circus --
starting from anywhere.
6
3
2
team sizes
4 5 3 5
There are M teams with Mi team members
T tables with Ti seating capacity
What is the smallest of inspectors needed to
visit each node without overlap?
No two members from one team should share a
table. Can you still seat everyone?
24How does maxflow help ?
The problem Fewest number of people to reach
all nodes without coinciding...
one instance
graph of constraints
answer is N-maxflow (all edges 1 unit)
25Pseudo-code (Python)
Get into the flow!
def edmonds_karp(C, source, sink) n len(C)
C is the capacity matrix F 0 n for i
in range(n) F is the flow matrix
residual capacity from u to v is Cuv -
Fuv while True path BFS(C, F,
source, sink) if not path
break flow float(1e309) Infinity !
traverse path to find smallest capacity
for (u,v) in path flow
min(flow, Cuv - Fuv) traverse
path to update flow for u,v in path
Fuv flow Fvu -
flow return sum(Fsourcei for i in
xrange(n)) def BFS(C, F, source, sink)
queue source paths
source while queue u
queue.pop(0) for v in range(len(C))
if Cuv - Fuv gt 0 and v not in
paths pathsv pathsu
(u,v) if v sink
return pathsv
queue.append(v) return None
A little bit of name contention
edmonds_karp
is really Floyd-Fulkerson, but with other people
getting the credit
many problems require no more than BFS or DFS
26Problem Set 5 Search algorithms, etc.
orchard
gumbo
getout
sq
circuses
27Subdividing Squares
Input
2
3 4 3 7
orchard
1
of problems
1
gumbo
3
getout
side length in the larger square
sq
9 smaller squares that cover a larger 7x7 square.
4 6 9
circuses
smallest number of smaller squares covering the
original
Output
28Counting trees
Input
orchard.X
1.5 1.5 1.5 6.8 6.8 1.5 0.9 0.9 1.1 0.9
1.1 1.1 0.9 1.1
vertices of a polygon on a single line of input
7,7
Output
15 1
right-justified number of lattice points in each
polygon
0,0
29Java Geometry
30Ascii Geometry
getout.X
Input
START 3 -10,-2,1 300,14.5,-20 350,-80,0 400,28.75,
26 _at__at__at__at_ n oU
o ooooooooooo o oooooo o oooo o DDDD oo
DDDD D D D D TT TT TT TT
ltTTT TTTgt END
number of bullets to be fired
velocity of the target
bullet velocities N, E, up
target shape
the bullets are fired 10m due south of the center
of the target
31Ascii Geometry
getout.X
Input
START 3 -10,-2,1 300,14.5,-20 350,-80,0 400,28.75,
26 _at__at__at__at_ n oU
o ooooooooooo o oooooo o oooo o DDDD oo
DDDD D D D D TT TT TT TT
ltTTT TTTgt END
Output
number of bullets to be fired
asterisk indicating hits
velocity of the target
bullet velocities N, E, up
_at__at__at__at_ oU o
ooooooooooo o oooooo o oooo o DDDD oo
DDDD D D D D TT TT TT TT
ltTTT TTTgt
target shape
the bullets are fired 10m due south of the center
of the target
each ASCII character is 10cm x 10cm
Matt Streshinsky
32(No Transcript)
33Jotto
Sophomores
Juniors
Seniors
Me
fjord 3
fjord 0
fjord 1
fjord 2
tempt 1
tempt 1
tempt 2
tempt 0
marks 1
marks 3
marks 0
marks 1
diner 1
diner 1
diner 0
diner 2
chore 2
chore 0
chore 1
chore 1
34See you next week!
35ACM today
The Fall competition is getting organized
36Schedule
10/2/2007
Today -- search and misc problems
10/9/2007
Lab / mock mock contest
10/16/2007
No class conference fall break
10/23/2007
10/30/2007
Real mock contest - "real" rules apply
9pm to 1am Tuesday, 10/30
11/6/2007
Meeting for ACM teams
11/13/2007
Final CS 189 meeting contest wrap-up
37Max Flow
The problem how much traffic can get from the
source to the sink ?
12
B
D
20
16
sink
F
A
10
4
9
7
source
4
13
Capacity Graph
E
C
14
TO
C
capacity
A
B
C
D
E
F
A
B
C
FROM
D
E
F
38Create F
Create a FLOW GRAPH with the minimum weight from
that path
12
B
D
12
20
16
sink
12
12
F
A
10
4
9
7
source
4
13
Flow Graph
E
C
14
TO
F
capacity
A
B
C
D
E
F
A
B
flow in one direction is negative flow in the
other direction
C
FROM
D
E
F
39R C - F
Create a RESIDUAL GRAPH with the rest of the
capacity after that flow
0
B
D
12
8
4
sink
12
12
F
A
10
4
9
7
source
4
13
Residual Graph
E
C
14
TO
R
capacity
A
B
C
D
E
F
A
B
reverse edges allow the "undoing" of previous
flow!
C
FROM
D
E
F
40Keep going!
Continue running this on the residual capacity
until BFS fails
12/12
B
D
19/20
11/16
sink
F
A
0/10
1/4
0/9
7/7
source
4/4
12/13
E
C
11/14
There is no longer a path from A to F !
411. Set F to all 0
The max flow algorithm
2. Compute R C - F
3. BFS in R from source to sink.
4a. If no path, youre done.
4b. If ? a path, add the available capacity to
F goto (2).
Floyd-Fulkerson
12
B
D
3
20
16
sink
2
4
1
0
F
A
10
4
2
9
7
1
2
source
4
4
13
E
C
5
14
capacity
flow
residual
Residual Graph
3
B
D
12
18
9
Don't need to keep R around explicity Keep only
the current flow (F) and the original capacity
(C).
4
2
A
9
1
2
F
7
5
6
2
5
C
E
4
11
9
42Pseudo-code (Python)
Get into the flow!
def edmonds_karp(C, source, sink) n len(C)
C is the capacity matrix F 0 n for i
in range(n) F is the flow matrix
residual capacity from u to v is Cuv -
Fuv while True path BFS(C, F,
source, sink) if not path
break flow float(1e309) Infinity !
traverse path to find smallest capacity
for (u,v) in path flow
min(flow, Cuv - Fuv) traverse
path to update flow for u,v in path
Fuv flow Fvu -
flow return sum(Fsourcei for i in
xrange(n)) def BFS(C, F, source, sink)
queue source paths
source while queue u
queue.pop(0) for v in range(len(C))
if Cuv - Fuv gt 0 and v not in
paths pathsv pathsu
(u,v) if v sink
return pathsv
queue.append(v) return None
A little bit of name contention
edmonds_karp
is really Floyd-Fulkerson, but with other people
getting the credit
43Max flow examples
Dinner problem
tables with capacities
Circuses problem
3
5
Circus inspectors need to visit every circus --
starting from anywhere.
6
3
2
team sizes
4 5 3 5
There are M teams with Mi team members
T tables with Ti seating capacity
What is the smallest of inspectors needed to
visit each node without overlap?
No two members from one team should share a
table. Can you still seat everyone?
44Subdividing Squares
Input
2
3 4 3 7
orchard
1
of problems
1
gumbo
3
getout
side length in the larger square
sq
9 smaller squares that cover a larger 7x7 square.
4 6 9
circuses
smallest number of smaller squares covering the
original
Output
45Java Geometry
46Jotto
Sophomores
Juniors
Seniors
Me
fjord 3
fjord 0
fjord 1
fjord 2
tempt 1
tempt 1
tempt 2
tempt 0
marks 1
marks 3
marks 0
marks 1
47Ascii Geometry
Input
getout.X
2 0 0 2 2 0 0 0 1 0 1 0 2 0 2 1 2 1 2 2 2 -1 3 1
1 1 1 2 3 1 1 2 1 1 3 1 1 3 1 2 3 1 1 1 1 1 0 1 1
0 1 0 0 1 0 0 0 0 0 -1 0
number of dimensions
starting point and destination point
edges in the graph
next number of dimensions
1 2
2 2
0 2
0 1
Output
Maze 1 can be traveled Maze 2 cannot be traveled
0 0