ACM so far Sep 6 Welcome and DP

  • Slides: 40
Download presentation
ACM so far… Sep 6 Welcome! and DP problems ~ 5 problems Sep 13

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!

This code looks obfuscated!

Donut! International Obfuscated C Coding Contest http: //www. ioccc. org/ donut. c

Donut! International Obfuscated C Coding Contest http: //www. ioccc. org/ donut. c

Max Flow ! B 16 10 s 12 4 D 9 4 C 14

Max Flow ! B 16 10 s 12 4 D 9 4 C 14 E capacity What's the maximum flow possible, from src to sink? Ford-Fulkerson algorithm sink or target t 7 source 13 20

TO Capacity Graph Max Flow (Step #1) Use depth- or breadth-first search to find

TO Capacity Graph Max Flow (Step #1) Use depth- or breadth-first search to find any path from s to t. B 16 10 s 12 4 D 9 What's left ? B s B C FROM D - 16 13 - 10 12 4 - 14 - - 9 - - 20 E t - - - 7 - - 4 - 20 4 C 14 E sink t 7 source 13 s C D E t -

TO Old capacities Max Flow (Step #1) Use depth- or breadth-first search to find

TO Old capacities Max Flow (Step #1) Use depth- or breadth-first search to find any path from s to t. B 4/16 12 s 10 0/12 s B C FROM D - 16 13 - 10 12 4 - 14 - - 9 - - 20 E t - - - 7 - - 4 - D 12 8/20 12 4 9 13 D E t - sink t 7 source C 4 C 14 E TO s What's left… s B C FROM D E t Residual capacities. and the red edges? Backwards capacities! 12 - B C D E t 4 13 - 10 4 12 9 0 - - - 14 - 8 - - 7 12 - 4 -

TO Old capacities Max Flow (Step #1) Use depth- or breadth-first search to find

TO Old capacities Max Flow (Step #1) Use depth- or breadth-first search to find any path from s to t. B 4 12 s 10 0 s B C FROM D - 16 13 - 10 12 4 - 14 - - 9 - - 20 E t - - - 7 - - 4 - D 12 9 D E t - sink 12 4 t 7 source 13 8 C 4 C 14 E Residual capacities. s Backwards capacities. B C FROM D (Step #2) Continue with the remaining E capacities until no path exists! t TO s 12 - B C D E t 4 13 - 10 4 12 9 0 - - - 14 - 8 - - 7 12 - 4 -

Max Flow (Step #1) Use depth- or breadth-first search to find any path from

Max Flow (Step #1) Use depth- or breadth-first search to find any path from s to t. B 11/16 s 12/12 D 19/20 sink 0/10 1/4 0/9 t 7/7 source 12/13 4/4 C 11/14 (Step #2) Continue with the remaining capacities until no path exists! E max flow: 23

Setting up… And the code needed to run it… if __name__ == "__main__": #

Setting up… And the code needed to run it… if __name__ == "__main__": # make a capacity graph # node A B C D C = [ [ 00, 16, 13, 00, [ 00, 10, 12, [ 00, 04, 00, [ 00, 9, 00, [ 00, 00, 7, [ 00, 00, E 00, 14, 00, 00, F 00 00 00 20 4 00 ], ], ], ] ] # # # A B C D E 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! A little bit of name contention… This is the edmonds_karp

Get into the flow! A little bit of name contention… This is the edmonds_karp algorithm. 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

Useful alone, too A brief BFS algorithm using the Capacity matrix def BFS(C, F,

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) return None # go from v in the future

But is max flow good for anything? that is, beyond solving "max flow" problems.

But is max flow good for anything? that is, beyond solving "max flow" problems. . .

Matching! and some acceptable possibilities. . . we have four brides and six grooms

Matching! and some acceptable possibilities. . . we have four brides and six grooms a bipartite graph

Matching! and some acceptable possibilities. . . we have four brides a maximal matching

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

Matching! and some acceptable possibilities. . . we have four brides a maximum matching

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

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

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

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

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

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

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

Max flow thought experiment. . . Suppose this is the flow so far (3

Max flow thought experiment. . . Suppose this is the flow so far (3 units): 1 all 1 s 1 sink 1 s 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 all

Max flow thought experiment. . . the path it finds. . . 1 all 1 s 1 sink 1 s source 1 1 What's going on here? t

Max flow thought experiment. . . Done! 1 all 1 s 1 sink 1

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

This week's problems… dinner dining hardware muddy optmilk all can be done with maxflow…

This week's problems… dinner dining hardware muddy optmilk all can be done with maxflow…

The challenge: is often setting up the graph

The challenge: is often setting up the graph

Input number of teams dinner number of tables 4 4 3 0 5 5

Input number of teams dinner number of tables 4 4 3 0 5 5 5 0 3 5 2 6 4 3 5 2 6 3 # of people in each team Output 0 1 capacity of each table again… can an assignment be made without putting teammates together? end… tables with capacities 3 teams with sizes 4 3 5 5 seating assignments! no teammates 5 2 6 4

dinner's maxflow graph How do these edge weights reflect the problem constraints? Table Team

dinner's maxflow graph How do these edge weights reflect the problem constraints? Table Team 2 4 3 s 5 Table 6 Team 5 source Table Team 5 Team fully connected with edge weights of 1 sink t 3 Table 4 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

4 tools & 4 tasks 4 4 42 189 10 1000 50 1 3 20 1 3 8 2 3 1 4 hardware hammer TV coffee There are four tools available ~ at these costs PC each task requires some tools There are four tasks available ~ with these rewards hammer E 4 50 42 TV 189 source waking folks in east 8 10 coffee 3 tool costs PC How do we use the results? Tools sink sleep 1000 What is flowing? 20 coding Jobs task rewards

l f x a m y r T ! w o

l f x a m y r T ! w o

Try max flow! Sophs JRs SRs Elderly Pomona slate 1 slate 3 slate 2

Try max flow! Sophs JRs SRs Elderly Pomona slate 1 slate 3 slate 2 slate 1 slate 3 flair 0 flair 1 flair 2 stems 1 stems 3 stems 1 stems 2 loser 1 loser 2 loser 3 stone 1 stone 3 stone 2 stone 1 stone 2 guppy 1 guppy 0 lasso 1 lasso 3 lasso 1 lasso 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…

old years…

hardware 4 4 42 189 10 1000 50 1 3 20 1 3 8

hardware 4 4 42 189 10 1000 50 1 3 20 1 3 8 0 3 1 4 Tools Jobs 50 42 20 189 source 10 1000 tool costs What is flowing? How can max flow help us here? 8 sink 3 job rewards

hardware Tools Jobs 50 42 4 4 42 189 10 1000 50 1 3

hardware Tools Jobs 50 42 4 4 42 189 10 1000 50 1 3 20 1 3 8 0 3 1 4 20 189 source 10 1000 8 sink 3 job rewards tool costs What is flowing? How can max flow help us here?

dining number of cows Input Likes 0 12 31 # of foods cow[i] likes

dining number of cows Input Likes 0 12 31 # of foods cow[i] likes 1 2 3 total # of foods 433 221231 222312 221312 21133 foods 23 12 13 foods 3 drinks total # of drinks cow[i] likes Output What is a cow-satisfying assignment here? 3 # of cows that can receive both a food and a drink they like… each can be used only once

Jotto! Frosh Sophs Jrs Srs audio 2 audio 1 graze 2 graze 3 graze

Jotto! Frosh Sophs Jrs Srs audio 2 audio 1 graze 2 graze 3 graze 1 alloy 2 fresh 1 alloy 1 fresh 2 armor 2 brave 2 wreak 2 fjord 2 armor 1 brave 3 brave 1 wreak 3 wreak 1 wreak 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 4 1 0 0 1 0 1

Stake Height and width of the field 4 1 0 0 1 0 1 1 0 0 0 1 1 Output 3 the pattern Input 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

4 tools & 4 tasks 4 4 42 189 10 1000 50 1 3 20 1 3 8 2 3 1 4 hardware hammer TV coffee There are four tasks available ~ with these rewards There are four tools available ~ at these costs PC each task requires some tools E 4 hammer 42 50 waking folks in east 20 source TV coffee 3 1000 task rewards coding How do we use mf to maximize our profit? sink 10 8 sleep What is flowing? 189 Tools PC Jobs tool costs

Stake as matching who are the brides? and the grooms? and the constraints?

Stake as matching who are the brides? and the grooms? and the constraints?

This week's problems… this one is from last week. . . ? Try one

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 S shortest dist from S For all nk ,

Dijkstra, for single-source shortest paths S 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, dc 2 k]: Let dk be nk's distance: <nk, dk> If dc + dc 2 k < dk: set dk = dc + dc 2 k Put <nk, dk> into Q. . .

Dijkstra, for single-source shortest paths S shortest dist from S For all nk ,

Dijkstra, for single-source shortest paths S 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, dc 2 k]: Let dk be nk's distance: <nk, dk> If dc + dc 2 k < dk: set dk = dc + dc 2 k Put <nk, dk> into Q. . .

Jotto! Frosh Sophs Jrs Srs audio 2 audio 1 graze 2 graze 3 graze

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