ACM so far… Sep 6 Welcome! and DP problems ~ 5 problems Sep 13 Lab session ~ 4 problems Sep 20 Discussion session on graph problems ~ 4 problems Sep 27 Lab session on graph problems ~ 4 problems Oct 4 Discussion session on geometry problems ~ 4 problems Oct 11 Lab session on geometry problems ~ 4 problems Oct 18 Lab & local ACM qualifying contest ~ 6 problems Oct 25 No meeting Nov 1 Discussion session on Max Flow ~ 4 problems Nov 8 Lab session ~ 4 problems Nov 12 (Sat.) ACM Regional contest (in Riverside...) Nov 15 Final meeting (may be a make-up lab if we miss one)
This code looks obfuscated!
International Obfuscated C Coding Contest Donut! International Obfuscated C Coding Contest http://www.ioccc.org/ donut.c
What's the maximum flow possible, from src to sink? Max Flow ! B 12 D 16 20 sink or target t s 10 4 9 7 source 13 4 C E 14 capacity What's the maximum flow possible, from src to sink? Ford-Fulkerson algorithm
Max Flow B D t s C E What's left ? Capacity Graph TO Max Flow Capacity Graph s B C D E t s - 16 13 10 12 4 14 9 7 20 B (Step #1) Use depth- or breadth-first search to find any path from s to t. C FROM D E B 12 D t 16 20 sink t s 10 4 9 7 source 13 4 C E 14 What's left ?
Max Flow B D t s C E What's left… Old capacities - 16 13 10 12 4 14 9 7 20 B (Step #1) Use depth- or breadth-first search to find any path from s to t. C FROM D E B 0/12 D t 4/16 12 8/20 sink 12 12 t s 10 4 9 7 source 13 4 C E 14 TO s B C D E t What's left… s - 4 13 12 10 14 9 7 8 Residual capacities. B C FROM D and the red edges? E Backwards capacities! t
(Step #2) Continue with the remaining capacities until no path exists! TO Max Flow Old capacities s B C D E t s - 16 13 10 12 4 14 9 7 20 B (Step #1) Use depth- or breadth-first search to find any path from s to t. C FROM D E B D t 4 12 8 sink 12 12 t s 10 4 9 7 source 13 4 C E 14 TO s B C D E t Residual capacities. s - 4 13 12 10 14 9 7 8 Backwards capacities. B C FROM D (Step #2) Continue with the remaining capacities until no path exists! E t
Max Flow max flow: 23 B D s t C E (Step #1) Use depth- or breadth-first search to find any path from s to t. B 12/12 D 11/16 19/20 sink s 0/10 1/4 t 0/9 7/7 source 12/13 4/4 C E 11/14 (Step #2) Continue with the remaining capacities until no path exists! max flow: 23
And the code needed to run it… Setting up… And the code needed to run it… 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 Linked at the ACM website by the slides…
Get into the flow! edmonds_karp def max_flow(C, source, sink): A little bit of name contention… This is the algorithm. edmonds_karp 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 brief BFS algorithm using the Capacity matrix Useful alone, too A brief BFS algorithm using the Capacity matrix 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
But is max flow good for anything? that is, beyond solving "max flow" problems...
and some acceptable possibilities ... Matching! and some acceptable possibilities ... we have four brides and six grooms a bipartite graph
and some acceptable possibilities ... Matching! and some acceptable possibilities ... we have four brides and six grooms a maximal matching == no more matchings without rearrangement
and some acceptable possibilities ... Matching! and some acceptable possibilities ... we have four brides and six grooms a maximum matching == no rearrangements will yield more matchings
connect a source to the left side... Maximum matching is max flow... connect a source to the left side... all 1s s source
connect a source to the left side... Maximum matching is max flow... connect a source to the left side... make all capacities = 1 1 1 all 1s 1 s 1 source 1 1
Maximum matching is max flow... connect a source to the left side... make all capacities = 1 put a sink on the right 1 1 all 1s all 1s 1 sink s 1 t source 1 1 what do the source and sink constraints ensure?
Max flow thought experiment... Suppose this is the flow so far (3 units): 1 1 all 1s all 1s 1 sink s 1 t source 1 1 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... ... the path it finds ... 1 1 all 1s all 1s 1 sink s 1 t source 1 1 What's going on here?
Max flow thought experiment... Done! 1 1 all 1s all 1s 1 sink s 1 t source 1 1 Maximum matching == 4
This week's problems… dinner dining hardware muddy optmilk all can be done with maxflow…
is often setting up the graph The challenge: is often setting up the graph
can an assignment be made without putting teammates together? Input dinner number of teams number of tables # of people in each team 4 5 4 5 3 5 3 5 2 6 4 3 5 2 6 3 0 0 Output 1 capacity of each table again… can an assignment be made without putting teammates together? end… tables with capacities 3 teams with sizes 5 seating assignments! 4 3 no teammates 6 5 2 4 5
fully connected with edge weights of 1 dinner's maxflow graph How do these edge weights reflect the problem constraints? Team Table 2 4 Table 5 3 Team s 6 sink Table 5 fully connected with edge weights of 1 t source Team 3 Table 5 4 Team Table How does the maxflow here relate to whether the seating is possible or not?
4 tools & 4 tasks 4 4 42 189 10 1000 50 1 3 20 1 3 8 2 3 1 4 hardware hammer There are four tools available ~ at these costs TV coffee PC E4 each task requires some tools hammer 50 42 waking folks in east 20 TV There are four tasks available ~ with these rewards 189 sink source 8 PERL stories - use in ACM - creating scripts - LTS, BOT 10 coffee sleep 1000 3 task rewards tool costs PC coding What is flowing? Tools Jobs How do we use the results?
Try max flow!
Try max flow! Sophs JRs SRs Elderly Pomona slate 1 slate 3 slate 2 flair 0 flair 0 flair 1 flair 2 flair 2 stems 1 stems 3 stems 1 stems 2 stems 2 loser 1 loser 2 loser 3 loser 2 loser 3 stone 1 stone 3 stone 2 stone 1 stone 2 guppy 2 PERL stories - use in ACM - creating scripts - LTS, BOT guppy 1 guppy 0 guppy 1 guppy 0 lasso 1 lasso 1 lasso 3 lasso 1 lasso 2 pluot 1 pluot 1 pluot 2 pluot 1 pluot 0 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...
old years…
hardware 4 4 42 189 10 1000 50 1 3 20 1 3 8 0 3 1 4 Tools Jobs 50 42 20 189 sink source 8 PERL stories - use in ACM - creating scripts - LTS, BOT 10 3 1000 job rewards tool costs What is flowing? How can max flow help us here?
hardware 4 4 42 189 10 1000 50 1 3 20 1 3 8 0 3 1 4 Tools Jobs 50 42 20 189 sink source PERL stories - use in ACM - creating scripts - LTS, BOT 8 10 3 1000 job rewards tool costs What is flowing? How can max flow help us here?
number of cows dining Input total # of foods total # of drinks 4 3 3 2 2 1 2 3 1 2 2 2 3 1 2 2 2 1 3 1 2 2 1 1 3 3 1 2 2 3 1 3 Likes 3 1 1 2 3 # of foods cow[i] likes foods drinks 1 # of drinks cow[i] likes 2 Output 3 What is a cow-satisfying assignment here? 3 # of cows that can receive both a food and a drink they like… foods drinks each can be used only once
Jotto! Frosh Sophs Jrs Srs audio 2 audio 1 audio 2 audio 1 graze 2 alloy 2 alloy 1 alloy 1 alloy 1 fresh 1 fresh 2 fresh 2 fresh 2 armor 2 armor 2 armor 2 armor 1 brave 2 brave 3 brave 1 brave 1 wreak 2 wreak 3 wreak 1 wreak 2 PERL stories - use in ACM - creating scripts - LTS, BOT fjord 1 fjord 2 fjord 1 fjord 5 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...
Stake Height and width of the field Input 4 1 0 1 1 0 1 0 0 0 0 0 1 0 1 0 1 the pattern Output 3 Maximum number of cows such that no two share a column and no two share a row.
4 tools & 4 tasks 4 4 42 189 10 1000 50 1 3 20 1 3 8 2 3 1 4 hardware hammer There are four tools available ~ at these costs TV coffee PC each task requires some tools hammer E4 42 50 189 waking folks in east TV There are four tasks available ~ with these rewards 20 sink source 10 PERL stories - use in ACM - creating scripts - LTS, BOT 8 sleep coffee 3 1000 tool costs task rewards coding PC What is flowing? Tools Jobs How do we use mf to maximize our profit?
Stake as matching who are the brides? and the grooms? and the constraints?
Try one or more of this week's problems! this one is from last week... ? Try one or more of this week's problems!
Dijkstra, for single-source shortest paths shortest dist from S For all nk , track <nk, Inf> Put <S,0> into your queue Q. While Q not empty: Remove Q's nearest node <nc,dc> For each edge [nc, nk, dc2k]: Let dk be nk's distance: <nk,dk> If dc + dc2k < dk: set dk = dc + dc2k Put <nk,dk> into Q...
Dijkstra, for single-source shortest paths shortest dist from S For all nk , track <nk, Inf> Put <S,0> into your queue Q. While Q not empty: Remove Q's nearest node <nc,dc> For each edge [nc, nk, dc2k]: Let dk be nk's distance: <nk,dk> If dc + dc2k < dk: set dk = dc + dc2k Put <nk,dk> into Q...
Jotto! Frosh Sophs Jrs Srs audio 2 audio 1 audio 2 audio 1 graze 2 alloy 2 alloy 1 alloy 1 alloy 1 fresh 1 fresh 2 fresh 2 fresh 2 armor 2 armor 2 armor 2 armor 1 brave 2 brave 3 brave 1 brave 1 wreak 2 wreak 3 wreak 1 wreak 2 PERL stories - use in ACM - creating scripts - LTS, BOT fjord 1 fjord 2 fjord 1 fjord 5 taper 1 taper 2 taper 4 taper 1 tater 1 tater 2 tater 4 tater 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...