ACM so far… Sep 6 Welcome! and DP problems ~ 5 problems

Slides:



Advertisements
Similar presentations
Jotto! 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.
Advertisements

Lecture 5: Network Flow Algorithms Max-Flow Min-Cut Single-Source Shortest-Path (SSSP) Job Sequencing.
Jotto! JRsSRsElderly A word-guessing game similar to mastermind… slate 3 This term's first class to guess another's word earns 1 problem... slate 2slate.
1 EE5900 Advanced Embedded System For Smart Infrastructure Static Scheduling.
1 Maximum flow sender receiver Capacity constraint Lecture 6: Jan 25.
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.
1 Review of some graph algorithms Graph G(V,E) (Chapter 22) –Directed, undirected –Representation Adjacency-list, adjacency-matrix Breadth-first search.
Jotto! SophsJrsSrs audio 1audio 2audio 1 Frosh audio 2 graze 3graze 1 graze 2 alloy 1 alloy 2 fresh 2fresh 1fresh 2fresh 1 This term's first class to guess.
Introduction To Algorithms CS 445 Discussion Session 8 Instructor: Dr Alon Efrat TA : Pooja Vaswani 04/04/2005.
MAXIMUM FLOW Max-Flow Min-Cut Theorem (Ford Fukerson’s Algorithm)
CSCI 256 Data Structures and Algorithm Analysis Lecture 18 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some.
Chapter 10: Iterative Improvement The Maximum Flow Problem The Design and Analysis of Algorithms.
Advanced Algorithm Design and Analysis (Lecture 8) SW5 fall 2004 Simonas Šaltenis E1-215b
Nick McKeown Spring 2012 Maximum Matching Algorithms EE384x Packet Switch Architectures.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2001 Lecture 4 Tuesday, 2/19/02 Graph Algorithms: Part 2 Network.
1 Maximum flow problems. 2 - Introduction of: network, max-flow problem capacity, flow - Ford-Fulkerson method pseudo code, residual networks, augmenting.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Lecture 4 Tuesday, 10/2/01 Graph Algorithms: Part 2 Network.
Maximum Flows Lecture 4: Jan 19. Network transmission Given a directed graph G A source node s A sink node t Goal: To send as much information from s.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2004 Lecture 5 Wednesday, 10/6/04 Graph Algorithms: Part 2.
Maximum Flow Maximum Flow Problem The Ford-Fulkerson method
CS 189 today Open-ended project option… A few more problems…… and one more algorithm! Jotto game finale?! Rest of the term No meeting on Tuesday, November.
ACM Welcome! Sep 11 Welcome! and DP problems ~ 6 problems Sep 18 Lab session ~ 6 problems Sep 25 Discussion session on graph problems ~ 6 problems Oct.
9/23/2015ACM-ICPC1 Maximum Flow Problem Source t fg e 42 Target (sink) flow capacity b s c a de 2/3/ actual flow 1/2/ 1/ 2/ 1.Actual flow  capacity.
Curricularizing the ACM... ? Zach Dodds ~ November 13, 2010.
CS 473Lecture ?1 CS473-Algorithms I Lecture ? Network Flows Finding Max Flow.
ACM today This week's problems… New and improved :-) ACM table slacking! not slacking… missing entries? let me know…
Maximum Flow Chapter 26. Flow Concepts Source vertex s – where material is produced Sink vertex t – where material is consumed For all other vertices.
CS223 Advanced Data Structures and Algorithms 1 Maximum Flow Neil Tang 3/30/2010.
Algorithm Design and Analysis (ADA)
Chapter 7 May 3 Ford-Fulkerson algorithm Step-by-step walk through of an example Worst-case number of augmentations Edmunds-Karp modification Time complexity.
1 EE5900 Advanced Embedded System For Smart Infrastructure Static Scheduling.
Theory of Computing Lecture 12 MAS 714 Hartmut Klauck.
1 Chapter 7 Network Flow Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
Maximum Flow Problem Definitions and notations The Ford-Fulkerson method.
Harbin, China 2010 finals ACM today… ! (2009) Knapsack DP for NP! Floyd-Warshall DP for APSP! Max Flow.
Instructor Neelima Gupta Edited by Divya Gaur(39, MCS '09) Thanks to: Bhavya(9), Deepika(10), Deepika Bisht(11) (MCS '09)
Contest Algorithms January 2016 Describe the maxflow problem, explain the Ford-Fulkerson, Edmonds-Karp algorithms. Look at the mincut problem, bipartite.
ACM today The Fall competition is getting organized… it's going to be lonely…
TU/e Algorithms (2IL15) – Lecture 8 1 MAXIMUM FLOW (part II)
The Maximum Network Flow Problem
Maximum Flow Chapter 26.
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
The Steepest Ascent Hill Climbing Algorithm
Graph Algorithms Minimum Spanning Tree (Chap 23)
CS4234 Optimiz(s)ation Algorithms
CSCI 3160 Design and Analysis of Algorithms Tutorial 8
JinJu Lee & Beatrice Seifert CSE 5311 Fall 2005 Week 10 (Nov 1 & 3)
ADVANCED ALGORITHMS GRAPH ALGORITHMS (UNIT-2).
Jotto! Frosh Sophs Jrs Srs audio 2 audio 1 audio 2 audio 1 graze 2
Topological Sort (topological order)
Chapter 7 Network Flow Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
Maximum Flow Problem flow capacity Actual flow  capacity
Instructor: Shengyu Zhang
Edmonds-Karp Algorithm
Lecture 10 Network flow Max-flow and Min-cut Ford-Fulkerson method
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
Jotto! Sophs Jrs Srs Profs pluot 1 pluot 2 pluot 1 pluot 2 squid 2
ACM today… ! DP for NP! DP for APSP! Knapsack Floyd-Warshall
Flow Networks General Characteristics Applications
Flow Networks and Bipartite Matching
Algorithms (2IL15) – Lecture 7
Network Flow CSE 373 Data Structures.
EE5900 Advanced Embedded System For Smart Infrastructure
Scheduling Crossbar Switches
Text Book: Introduction to algorithms By C L R S
MAXIMUM flow by Eric Wengert.
Maximum Flow Neil Tang 4/8/2008
Maximum Bipartite Matching
Advanced Graph Homer Lee 2013/10/31.
Presentation transcript:

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