# 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.

## Presentation on theme: "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."— Presentation 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 C 13 t 16 104 9 12 14 7 20 4 source capacity Max Flow ! sink or target

s B E D C 13 s B C D E FROM sBCDE t -1613-- --1012- -4--14 --9-- ---7- - - - 20 4 ------ t t 16 104 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 C 13 s B C D E FROM sBCDE t -1613-- --1012- -4--14 --9-- ---7- - - - 20 4 ------ t t 4/16 104 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… s B C D E FROM sBCDE -413-- 12-100- -4--14 -129-- ---7- - - - 8 4 --- -- t t TO Residual capacities. and the red edges? Backwards capacities! 12

s B E D C 13 s B C D E FROM sBCDE t -1613-- --1012- -4--14 --9-- ---7- - - - 20 4 ------ t t 4 104 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 s B C D E FROM sBCDE -413-- 12-100- -4--14 -129-- ---7- - - - 8 4 --- -- t t TO 12 (Step #2) Continue with the remaining capacities until no path exists! Old capacities Residual capacities. Backwards capacities. New capacities

B E D C 12/13 11/16 0/101/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! s source 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_karp This 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 bridesand six grooms Matching! and some acceptable possibilities... a bipartite graph

we have four bridesand six grooms Matching! and some acceptable possibilities... a maximal matching == no more matchings without rearrangement

we have four bridesand six grooms Matching! and some acceptable possibilities... a maximum matching == no rearrangements will yield more matchings

Maximum matching is max flow... s source connect a source to the left side... all 1s

Maximum matching is max flow... s source connect a source to the left side... make all capacities = 1 1 1 1 1 1 1 all 1s

Maximum matching is max flow... s source t sink connect a source to the left side... put a sink on the right make all capacities = 1 1 1 1 1 1 1 all 1s what do the source and sink constraints ensure?

Max flow thought experiment... s source t sink 1 1 1 1 1 1 all 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... s source t sink 1 1 1 1 1 1 all 1s... the path it finds... What's going on here?

Max flow thought experiment... s source t sink 1 1 1 1 1 1 all 1s Done! Maximum matching == 4

general problems:max-flow problems: cowcarn feeding optimilk sandcas timecards tswift difficult-to-classify problems: This week's problems Try max flow! soda (last week) tour (last week)

The challenge: is sometimes setting up the graph

4 42 189 10 1000 50 1 3 20 1 3 8 2 3 1 4 How do we use the results? What is flowing? tswift There 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 42 189 10 1000 50 1 3 20 1 3 8 2 3 1 4 source 42 189 10 1000 ingredient costs How do we use the results? What is flowing? tswift There 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 4 ingredients

4 42 189 10 1000 50 1 3 20 1 3 8 2 3 1 4 source sink 42 189 10 1000 ingredient costs recipe rewards 50 20 8 3 How do we use the results? What is flowing? tswift There 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 ingredients recipes

4 42 189 10 1000 50 1 3 20 1 3 8 2 3 1 4 source sink 42 189 10 1000 ingredient costs recipe rewards 50 20 8 3 How do we use the results? What is flowing? tswift There 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 sandcas timecards tswift difficult-to-classify problems: This week's problems Try 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 42 189 10 1000 50 1 3 20 1 3 8 2 3 1 4 source Jobs sink 42 189 10 1000 tool costs task rewards 50 20 8 3 How do we use the results? What is flowing? hardware There 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 5 4 5 3 5 3 5 2 6 4 4 5 4 5 3 5 3 5 2 6 3 0 number of teams Input Output number of tables # of people in each team can an assignment be made without putting teammates together? 0101 capacity of each table again… end… 3 5 2 6 4 tables with capacities teams with sizes 5 3 4 5 seating assignments! no teammates

dinner's maxflow graph s source t sink How does the maxflow here relate to whether the seating is possible or not? Team 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?

JRsSRsElderly slate 3 This term's first class to guess another's word earns 1 problem... slate 2slate 1 This term's last class to have its word guessed earns 1 problem... Sophs slate 1 flair 0flair 1flair 2flair 0 Pomona slate 3 flair 2 stems 3stems 1stems 2stems 1stems 2 loser 2loser 3loser 2loser 1 loser 3 stone 3stone 2stone 1 stone 2 guppy 1guppy 0guppy 1guppy 2guppy 0 Try max flow! lasso 1lasso 3lasso 1 lasso 2 pluot 1pluot 2pluot 1 pluot 0

old years…

hardware Tools 4 42 189 10 1000 50 1 3 20 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 42 189 10 1000 50 1 3 20 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 3 2 2 1 2 3 1 2 2 2 3 1 2 2 2 1 3 1 2 2 1 1 3 3 number of cows Input total # of foods total # of drinks # of foods cow[i] likes # of drinks cow[i] likes foodsdrinks 0 Output # of cows that can receive both a food and a drink they like… 3 each can be used only once Likes foodsdrinks 1 2 3 1 2 2 3 1 3 3 1 1 2 3 What is a cow-satisfying assignment here? dining

Jotto! SophsJrsSrs audio 1audio 2audio 1 Frosh audio 2 graze 3graze 1 graze 2 alloy 1 alloy 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 1armor 2 brave 3brave 1 brave 2 wreak 3wreak 1wreak 2 fjord 1fjord 5 fjord 1 fjord 2

Stake 4 1 0 1 1 0 1 0 0 0 0 0 1 0 1 Input Output 3 Height and width of the field the pattern Maximum number of cows such that no two share a column and no two share a row.

Tools 4 42 189 10 1000 50 1 3 20 1 3 8 2 3 1 4 source Jobs sink 50 20 8 3 task rewards tool costs 42 189 10 1000 How do we use mf to maximize our profit? What is flowing? hardware There 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 matching who 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 into your queue Q. While Q not empty: Remove Q's nearest node For each edge [n c, n k, d c2k ]: For all n k, track Let d k be n k 's distance: If d c + d c2k < d k : set d k = d c + d c2k Put into Q... S shortest dist from S

Dijkstra, for single-source shortest paths Put into your queue Q. While Q not empty: Remove Q's nearest node For each edge [n c, n k, d c2k ]: For all n k, track Let d k be n k 's distance: If d c + d c2k < d k : set d k = d c + d c2k Put into Q... S shortest dist from S

Jotto! SophsJrsSrs audio 1audio 2audio 1 Frosh audio 2 graze 3graze 1 graze 2 alloy 1 alloy 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 1armor 2 brave 3brave 1 brave 2 wreak 3wreak 1wreak 2 fjord 1fjord 5 fjord 1 fjord 2 taper 4taper 1 taper 2 tater 4tater 1 tater 2

Download ppt "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."

Similar presentations