CS 60 Slides - PowerPoint PPT Presentation

About This Presentation
Title:

CS 60 Slides

Description:

Circuses problem. A. E. C. F. D. B. G ... circuses. 9 smaller squares that cover a larger 7x7 square. 3. 1. 1. 2. Input. Output. 3 ... – PowerPoint PPT presentation

Number of Views:99
Avg rating:3.0/5.0
Slides: 48
Provided by: Kel7163
Learn more at: https://www.cs.hmc.edu
Category:

less

Transcript and Presenter's Notes

Title: CS 60 Slides


1
ACM today
The Fall competition is getting organized
it's going to be lonely
2
Mock 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
3
Infinities seen
4
Infinities seen
int BIG 10000000 Michael E.
5
Infinities seen
int BIG 10000000 Michael E. gridjk
1000000 Scott P.
6
Infinities seen
int BIG 10000000 Michael E. gridjk
1000000 Scott P. pathsij 9001
Martin P.
7
Infinities seen
int BIG 10000000 Michael E. gridjk
1000000 Scott P. pathsij 9001
Martin P. direct 2400 Russ R.
8
Infinities seen
int BIG 10000000 Michael E. gridjk
1000000 Scott P. pathsij 9001
Martin P. direct 2400 Russ R. int
constant 1000 Zvi E.
9
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.
10
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 0
Andrew H.
11
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.
12
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.
13
int 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)
14
Schedule
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
15
Max 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 ?
16
Max 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
17
Find 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
18
Create 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
19
R 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
20
Keep 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 !
21
1. 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
22
Pseudo-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
23
Max 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?
24
How 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)
25
Pseudo-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
26
Problem Set 5 Search algorithms, etc.
orchard
gumbo
getout
sq
circuses
27
Subdividing 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
28
Counting 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
29
Java Geometry
30
Ascii 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
31
Ascii 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)
33
Jotto
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
34
See you next week!
35
ACM today
The Fall competition is getting organized
36
Schedule
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
37
Max 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
38
Create 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
39
R 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
40
Keep 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 !
41
1. 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
42
Pseudo-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
43
Max 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?
44
Subdividing 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
45
Java Geometry
46
Jotto
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
47
Ascii 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
Write a Comment
User Comments (0)
About PowerShow.com