acm so far… sep 11 welcome! and dp problems ~ 6 problems sep 18 lab session ~ 6 problems sep 25...
TRANSCRIPT
ACM so far…
Sep 11 Welcome! and DP problems ~ 6 problems
Sep 18 Lab session ~ 6 problems
Sep 25 Discussion session on graph problems ~ 6 problems
Oct 2 Lab session on graph problems ~ 6 problems
Oct 9 Scott Ellsworth on Google Irvine ~ 6 problems
Oct 16 Discussion session on maxflow problems ~ 6 problems
Oct 23 (9pm) Lab & local ACM qualifying contest ~ 6 problems
Oct 30 Discussion session on geometry problems ~ 6 problems
Nov 6 Lab session on geometry problems ~ 6 problems
Nov 10 (Sat.) ACM Regional contest (in Riverside...)
Nov 13 Final meeting
Job-fair thoughts?
Our "sources"• Aaron Gable, Amber Yust, and other alums…
• Cory Simmonsen
Web technologies
Build systems
Distributed systems (scaling up)
Testing!
Working with a large existing codebase…
What should be improved?
IT always seems mysterious to me…
Hooray!
Not sure about these, however…
Matrix of skillsets
Ford-Fulkerson algorithm
What's the maximum flow possible, from src to sink?
s
B
E
D
C13
t
16
10 4 9
12
14
7
20
4source
capacity
Max Flow !
sink or target
s
B
E
D
C13
sB
C
D
E
FROM
s B C D E
t
- 16 13 - -- - 10 12 -
- 4 - - 14
- - 9 - -
- - - 7 -
--
-
20
4
- - - - - -t
t
16
10 4 9
12
14
7
20
4
TO
Capacity Graph
source
sink
(Step #1) Use depth- or breadth-first search to find any path from s to t.
Max Flow
What's left ?
s
B
E
D
C13
sB
C
D
E
FROM
s B C D E
t
- 16 13 - -- - 10 12 -
- 4 - - 14
- - 9 - -
- - - 7 -
--
-
20
4
- - - - - -t
t
4/16
10 4 9
0/12
14
7
8/20
4
TO
Old capacities
source
sink
(Step #1) Use depth- or breadth-first search to find any path from s to t.
Max Flow
What's left… sB
C
D
E
FROM
s B C D E
- 4 13 - -12 - 10 0 -
- 4 - - 14
- 12 9 - -
- - - 7 -
--
-
8
4
- - - 12 - -t
t
TO
Residual capacities.
and the red edges?Backwards capacities!
12 12
12
s
B
E
D
C13
sB
C
D
E
FROM
s B C D E
t
- 16 13 - -- - 10 12 -
- 4 - - 14
- - 9 - -
- - - 7 -
--
-
20
4
- - - - - -t
t
4
10 4 9
0
14
7
8
4
TO
source
sink
(Step #1) Use depth- or breadth-first search to find any path from s to t.
Max Flow
sB
C
D
E
FROM
s B C D E
- 4 13 - -12 - 10 0 -
- 4 - - 14
- 12 9 - -
- - - 7 -
--
-
8
4
- - - 12 - -t
t
TO
12 12
12
(Step #2) Continue with the remaining capacities until no path exists!
Old capacities
Residual capacities.
Backwards capacities.
New capacities
B
E
D
C12/13
11/16
0/10 1/4 0/9
12/12
11/14
7/7
19/20
4/4
max flow: 23
(Step #1) Use depth- or breadth-first search to find any path from s to t.
Max Flow
(Step #2) Continue with the remaining capacities until no path exists!
ssource
t
sink
Setting up…
if __name__ == "__main__":
# make a capacity graph # node A B C D E F C = [ [ 00, 16, 13, 00, 00, 00 ], # A [ 00, 00, 10, 12, 00, 00 ], # B [ 00, 04, 00, 00, 14, 00 ], # C [ 00, 00, 9, 00, 00, 20 ], # D [ 00, 00, 00, 7, 00, 4 ], # E [ 00, 00, 00, 00, 00, 00 ] ] # F
print "C is", C source = 0 # A sink = 5 # F
max_flow_value = max_flow( C, source, sink ) print "max_flow_value is", max_flow_value
And the code needed to run it…
Linked at the ACM website by the slides…
Get into the flow!
def max_flow(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 C[u][v] - F[u][v]
while True: path = BFS(C, F, source, sink) if not path: break # no path - we're done!
# find the path's flow, that is, the "bottleneck" edges = [C[u][v]-F[u][v] for u,v in path] path_flow = min( edges ) print "Augmenting by", path_flow for u,v in path: # traverse path to update flow F[u][v] += path_flow # forward edge up F[v][u] -= path_flow # backward edge down
return sum([F[source][i] for i in range(n)]) # out from source
A little bit of name contention…
edmonds_karpThis is the algorithm.
Useful alone, too
def BFS(C, F, source, sink): queue = [source] # the BFS queue paths = {source: []} # stores 1 path per graph node while queue:
u = queue.pop(0) # next node to explore (expand) for v in range(len(C)): # for each possible next node
# path from u to v? and not yet at v? if C[u][v] - F[u][v] > 0 and v not in paths: paths[v] = paths[u] + [(u,v)] if v == sink: return paths[v]
queue.append(v) # go from v in the future return None
A brief BFS algorithm using the Capacity matrix
But is max flow good for anything?
that is, beyond solving "max flow" problems...
we have four brides and six grooms
Matching! and some acceptable possibilities ...
a bipartite graph
we have four brides and six grooms
Matching! and some acceptable possibilities ...
a maximal matching == no more matchings without rearrangement
we have four brides and six grooms
Matching! and some acceptable possibilities ...
a maximum matching == no rearrangements will yield more matchings
Maximum matching is max flow...
ssource
connect a source to the left side...
all 1s
Maximum matching is max flow...
ssource
connect a source to the left side...
make all capacities = 1
1
1
11
1
1
all 1s
Maximum matching is max flow...
ssource
t
sink
connect a source to the left side...
put a sink on the right
make all capacities = 1
1
1
11
1
1
all 1sall 1s
what do the source and sink constraints ensure?
Max flow thought experiment...
ssource
t
sink
1
1
11
1
1
all 1sall 1s
Suppose this is the flow so far (3 units):
Draw what happens in the next step of the max-flow algorithm!how to get from maximal matching to maximum matching…
Max flow thought experiment...
ssource
t
sink
1
1
11
1
1
all 1sall 1s
... the path it finds ...
What's going on here?
Max flow thought experiment...
ssource
t
sink
1
1
11
1
1
all 1sall 1s
Done!
Maximum matching == 4
general problems: max-flow problems:
cowcarn
feeding
optimilk
sandcastimecards
tswift
difficult-to-classify problems:
This week's problemsTry max flow!
soda (last week)tour (last week)
The challenge:
is sometimes setting up the graph
4 44218910100050 1 320 1 3 8 2 3 1 4
How do we use the results?
What is flowing?
tswiftThere are four ingredients available ~ at these costs
hay-flavored
coffee
spam
chocolate
There are four smoothie recipes available ~ with these rewards
4 ingredients (hay) & 4 smoothie recipes
each recipe requires ingredients
1
2
3
4
4 44218910100050 1 320 1 3 8 2 3 1 4
source
42
189
10
1000ingredient
costs
How do we use the results?
What is flowing?
tswiftThere are four ingredients available ~ at these costs
hay-flavored
coffee
spam
chocolate
There are four smoothie recipes available ~ with these rewards
Hay
coffee
Spam
Choc.
4 ingredients (hay) & 4 smoothie recipes
each recipe requires ingredients
1
2
3
4ingredients
4 44218910100050 1 320 1 3 8 2 3 1 4
sourcesink
42
189
10
1000ingredient
costs
recipe rewards
50
20
8
3
How do we use the results?
What is flowing?
tswiftThere are four ingredients available ~ at these costs
hay-flavored
coffee
spam
chocolate
There are four smoothie recipes available ~ with these rewards
Hay
coffee
Spam
Choc.
4 ingredients (hay) & 4 smoothie recipes
each recipe requires ingredients
Hay-spam
Spam-hay
coffee
Choco-hay
1
2
3
4ingredients recipes
4 44218910100050 1 320 1 3 8 2 3 1 4
sourcesink
42
189
10
1000ingredient
costs
recipe rewards
50
20
8
3
How do we use the results?
What is flowing?
tswiftThere are four ingredients available ~ at these costs
hay-flavored
coffee
spam
chocolate
There are four smoothie recipes available ~ with these rewards
Hay
coffee
Spam
Choc.
4 ingredients (hay) & 4 smoothie recipes
each recipe requires ingredients
Hay-spam
Spam-hay
coffee
Choco-hay
1
2
3
4
One purple edge is missing… Which?
ingredients recipes
What should these capacities be?
general problems: max-flow problems:
cowcarn
feeding
optimilk
sandcastimecards
tswift
difficult-to-classify problems:
This week's problemsTry max flow!
soda (last week)tour (last week)
This code only looks
obfuscated!
Donut!
http://www.ioccc.org/
donut.c
International Obfuscated C Coding Contest
This code
IS
obfuscated!
2011's winner…
http://www.ioccc.org/
donut.c
International Obfuscated C Coding Contest
This code
IS
obfuscated!
Tools
4 44218910100050 1 320 1 3 8 2 3 1 4
source
Jobs
sink
42
189
10
1000tool costs
task rewards
50
20
8
3
How do we use the results?
What is flowing?
hardwareThere are four tools available ~ at these costs
hammer
TV
coffee
PC
There are four tasks available ~ with these rewards
hammer
TV
coffee
PC
4 tools & 4 tasks
each task requires some tools
E4
waking folks in
east
sleep
coding
dinner
4 54 5 3 53 5 2 6 44 54 5 3 53 5 2 6 30 0
number of teams Input
Output
number of tables
# of people in each team
can an assignment be made without putting teammates together?
01
capacity of each table
again…
end…
35
2 6 4
tables with capacities
teams with sizes
5
34
5
seating assignments!
no teammates
dinner's maxflow graph
ssource t
sink
How does the maxflow here relate to whether the seating is possible or not?
Team
Team
Team
Team
Table
Table
Table
Table
Table
4
3
6
5
2
5
5
3
4
fully connected with edge
weights of 1
How do these edge weights reflect the problem constraints?
JRs SRs Elderly
slate 3
This term's first class to guess another's word earns 1 problem...
slate 2 slate 1
This term's last class to have its word guessed earns 1 problem...
Sophs
slate 1
flair 0 flair 1 flair 2 flair 0
Pomona
slate 3
flair 2
stems 3 stems 1 stems 2 stems 1 stems 2
loser 2 loser 3 loser 2loser 1 loser 3
stone 3 stone 2 stone 1 stone 1 stone 2
guppy 1 guppy 0 guppy 1 guppy 2 guppy 0
Try max flow!
lasso 1 lasso 3 lasso 1 lasso 1 lasso 2
pluot 1 pluot 2 pluot 1 pluot 1 pluot 0
old years…
hardware
Tools
4 44218910100050 1 320 1 3 8 0 3 1 4
source
Jobs
sink
42
189
10
1000
tool costs
job rewards
50
20
8
3
How can max flow help us here?
What is flowing?
hardware
Tools
4 44218910100050 1 320 1 3 8 0 3 1 4
source
Jobs
sink
42
189
10
1000
tool costs
job rewards
50
20
8
3
How can max flow help us here?
What is flowing?
4 3 32 2 1 2 3 12 2 2 3 1 22 2 1 3 1 22 1 1 3 3
number of cows
Inputtotal # of foods
total # of drinks
# of foods cow[i] likes
# of drinks cow[i] likesfoods drinks
0
Output
# of cows that can receive both a food and
a drink they like…
3
each can be used only once
Likes
foods drinks
1
2
3
1 2
2 3
1 3
1 3
3 1
1 2
1 2
3What is a cow-satisfying assignment here?
dining
Jotto! Sophs Jrs Srs
audio 1 audio 2 audio 1
Frosh
audio 2 graze 3 graze 1 graze 1 graze 2
alloy 1 alloy 1 alloy 1 alloy 2 fresh 2 fresh 2 fresh 2 fresh 1
This term's first class to guess another's word earns 1 problem...
This term's last class to have its word guessed earns 1 problem...
armor 2 armor 2 armor 1 armor 2 brave 3 brave 1 brave 1 brave 2 wreak 3 wreak 1 wreak 2 wreak 2 fjord 1 fjord 5 fjord 1 fjord 2
Stake
4 1 0 1 1 0 1 0 0 0 0 0 1 0 1 0 1
Input
Output3
Height and width of the field
the
patt
ern
Maximum number of cows such that no two share a column and no two share a row.
Tools
4 44218910100050 1 320 1 3 8 2 3 1 4
source
Jobs
sink
50
20
8
3task rewards
tool costs
42
189
10
1000
How do we use mf to maximize our profit?
What is flowing?
hardwareThere are four tools available ~ at these costs
hammer
TV
coffee
PC
There are four tasks available ~ with these rewards
hammer
TV
coffee
PC
4 tools & 4 tasks
each task requires some tools
E4
waking folks in
east
sleep
coding
Stake as matchingwho are the brides? and the grooms?
and the constraints?
This week's problems…
Try one or more of this week's problems!
this one is from last week... ?
Dijkstra, for single-source shortest paths
Put <S,0> into your queue Q.
While Q not empty:
Remove Q's nearest node <nc,dc>
For each edge [nc, nk, dc2k]:
For all nk , track <nk, Inf>
Let dk be nk's distance: <nk,dk>
If dc + dc2k < dk: set dk = dc + dc2k
Put <nk,dk> into Q...
S
shortest dist from S
Dijkstra, for single-source shortest paths
Put <S,0> into your queue Q.
While Q not empty:
Remove Q's nearest node <nc,dc>
For each edge [nc, nk, dc2k]:
For all nk , track <nk, Inf>
Let dk be nk's distance: <nk,dk>
If dc + dc2k < dk: set dk = dc + dc2k
Put <nk,dk> into Q...
S
shortest dist from S
Jotto! Sophs Jrs Srs
audio 1 audio 2 audio 1
Frosh
audio 2 graze 3 graze 1 graze 1 graze 2
alloy 1 alloy 1 alloy 1 alloy 2 fresh 2 fresh 2 fresh 2 fresh 1
This term's first class to guess another's word earns 1 problem...
This term's last class to have its word guessed earns 1 problem...
armor 2 armor 2 armor 1 armor 2 brave 3 brave 1 brave 1 brave 2 wreak 3 wreak 1 wreak 2 wreak 2 fjord 1 fjord 5 fjord 1 fjord 2 taper 4 taper 1 taper 1 taper 2 tater 4 tater 1 tater 1 tater 2