Harbin, China 2010 finals ACM today… ! (2009) Knapsack DP for NP! Floyd-Warshall DP for APSP! Max Flow.

Slides:



Advertisements
Similar presentations
§3 Shortest Path Algorithms Given a digraph G = ( V, E ), and a cost function c( e ) for e  E( G ). The length of a path P from source to destination.
Advertisements

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.
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.
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)
CSE 326: Data Structures Network Flow and APSP Ben Lerner Summer 2007.
The Maximum Network Flow Problem. CSE Network Flows.
Chapter 10: Iterative Improvement The Maximum Flow Problem The Design and Analysis of Algorithms.
1 Chapter 7 Network Flow Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
Advanced Algorithm Design and Analysis (Lecture 8) SW5 fall 2004 Simonas Šaltenis E1-215b
1 Maximum Flow Networks Suppose G = (V, E) is a directed network. Each edge (i,j) in E has an associated ‘capacity’ u ij. Goal: Determine the maximum amount.
1 Chapter 7 Network Flow Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
1 COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf.
All Pairs Shortest Paths and Floyd-Warshall Algorithm CLRS 25.2
CS138A Network Flows Peter Schröder. CS138A Flow Networks Definitions a flow network G=(V,E) is a directed graph in which each edge (u,v)
CSC 2300 Data Structures & Algorithms April 17, 2007 Chapter 9. Graph Algorithms.
1 Maximum flow problems. 2 - Introduction of: network, max-flow problem capacity, flow - Ford-Fulkerson method pseudo code, residual networks, augmenting.
1 8a-ShortestPathsMore Shortest Paths in a Graph (cont’d) Fundamental Algorithms.
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.
Odds and Ends HP ≤ p HC (again) –Turing reductions Strong NP-completeness versus Weak NP-completeness Vertex Cover to Hamiltonian Cycle.
Flows sourcesink s t Flows sourcesink edge-weights = capacities.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2004 Lecture 5 Wednesday, 10/6/04 Graph Algorithms: Part 2.
The Knapsack Problem Input –Capacity K –n items with weights w i and values v i Goal –Output a set of items S such that the sum of weights of items in.
1 Ford-Fulkerson method Ford-Fulkerson(G) f = 0 while( 9 simple path p from s to t in G f ) f := f + f p output f Runs in time O(|f max | |E|) where f.
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.
MAX FLOW CS302, Spring 2013 David Kauchak. Admin.
More Dynamic Programming Floyd-Warshall Algorithm.
Maximum Flow Chapter 26.
SPANNING TREES Lecture 21 CS2110 – Spring
Final Exam Review Final exam will have the similar format and requirements as Mid-term exam: Closed book, no computer, no smartphone Calculator is Ok Final.
Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).
DP TSP Have some numbering of the vertices We will think of all our tours as beginning and ending at 1 C(S, j) = length of shortest path visiting each.
1 COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf.
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…
1 WEEK 11 Graphs III Network Flow Problems A Simple Maximum-Flow Algorithm Izmir University of Economics.
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.
Announcements Finish up Network Flow today Then Review for Final on Monday ◦ HW#5 is due on Monday, let me or the TA’s know if you have trouble starting.
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.
CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 23.
Theory of Computing Lecture 12 MAS 714 Hartmut Klauck.
TU/e Algorithms (2IL15) – Lecture 12 1 Linear Programming.
A directed graph G consists of a set V of vertices and a set E of arcs where each arc in E is associated with an ordered pair of vertices from V. V={0,
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.
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)
Network Flow.
Maximum Flow Chapter 26.
ACM so far… Sep 6 Welcome! and DP problems ~ 5 problems
Network Flow.
Jotto! Frosh Sophs Jrs Srs audio 2 audio 1 audio 2 audio 1 graze 2
CS330 Discussion 6.
Instructor: Shengyu Zhang
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
Algorithms (2IL15) – Lecture 7
EE5900 Advanced Embedded System For Smart Infrastructure
Network Flow.
Maximum Flow Neil Tang 4/8/2008
Special Graphs: Modeling and Algorithms
Network Flow.
All Pairs Shortest Path Examples While the illustrations which follow only show solutions from vertex A (or 1) for simplicity, students should note that.
Presentation transcript:

Harbin, China 2010 finals ACM today… ! (2009) Knapsack DP for NP! Floyd-Warshall DP for APSP! Max Flow

Jotto! A word-guessing game similar to mastermind… next guesses? SophsJrsSrsProfs Chalk 1Chalk 0Chalk 1 Quine 1 Quine 2 ????

Candy! 1800's 1950's 1970's 1920's 1940's

Maximizing Candy! i = 1 i = 2 i = 3 i = 4 i = 5 Suppose you can consume 13 candywt units of candy. What choice will maximize your candy-value experience? (1) if you can take fractional parts of candy packages…? (2) if you can take any number of whole candy packages…? (3) if you can take 0 or 1 of each whole candy package…? w 1 = 2 v 1 = 100 w 2 = 3 v 2 = 120 w 3 = 5 v 3 = 230 w 4 = 7 v 4 = 560 w 5 = 9 v 5 = 675

Maximizing Candy! i = 1 i = 2 i = 3 i = 4 i = 5 Suppose you can consume 13 candywt units of candy. What choice will maximize your candy-value experience? (1) if you can take fractional parts of candy packages…? (2) if you can take any number of whole candy packages…? (3) if you can take 0 or 1 of each whole candy package…? GO WITH GREED! w 1 = 2 v 1 = 100 vpw 1 = 50 w 2 = 3 v 2 = 120 vpw 2 = 40 w 3 = 5 v 3 = 230 vpw 3 = 46 w 4 = 7 v 4 = 560 vpw 4 = 80 w 5 = 9 v 5 = 675 vpw 5 = 75

Knapsack problem i = 1 i = 2 i = 3 i = 4 i = 5 Suppose you can consume 13 candywt units of candy. (2) if you can take any number of whole candy packages…? "unbounded" IDEA: Consider all possible weights (integers) from 0 up to the weight you can carry - For each one choose the best from all N items w 1 = 2 v 1 = 100 vpw 1 = 50 w 2 = 3 v 2 = 120 vpw 2 = 40 w 3 = 5 v 3 = 230 vpw 3 = 46 w 4 = 7 v 4 = 560 vpw 4 = 80 w 5 = 9 v 5 = 675 vpw 5 = 75

Knapsack problem i = 1 i = 2 i = 3 i = 4 i = 5 Suppose you can consume 13 candywt units of candy. (2) if you can take any number of whole candy packages…? "unbounded" TOTAL WEIGHT max total value V(w) = w 1 = 2 v 1 = 100 vpw 1 = 50 w 2 = 3 v 2 = 120 vpw 2 = 40 w 3 = 5 v 3 = 230 vpw 3 = 46 w 4 = 7 v 4 = 560 vpw 4 = 80 w 5 = 9 v 5 = 675 vpw 5 = 75 max i V(w-w i ) + v i

Knapsack problem i = 1 i = 2 i = 3 i = 4 i = 5 Suppose you can consume 13 candywt units of candy. (3) if you can take 0 or 1 of each whole candy package…? w 1 = 2 v 1 = 100 vpw 1 = 50 w 2 = 3 v 2 = 120 vpw 2 = 40 w 3 = 5 v 3 = 230 vpw 3 = 46 w 4 = 7 v 4 = 560 vpw 4 = 80 w 5 = 9 "0-1 problem" IDEA: Do the same thing as before, but consider sublists of items that grow one-at-a-time v 5 = 675 vpw 5 = 75

Knapsack problem w 1 = 2 v 1 = 100 i = 1 i = 2 i = 3 i = 4 i = 5 vpw 1 = 50 w 2 = 3 v 2 = 120 vpw 2 = 40 w 3 = 5 v 3 = 230 vpw 3 = 46 w 4 = 7 v 4 = 560 vpw 4 = 80 w 5 = 9 v 5 = 675 vpw 5 = 75 Suppose you can consume 13 candywt units of candy. "0-1 problem" TOTAL WEIGHT max total value V(i,w) = (3) if you can take 0 or 1 of each whole candy package…? Total Items max V(i-1,w) V(i,w-w i ) + v i i

elite 3 30 the number of different subsets of cows satisfying that constraint. Input exact number of cows we need, indexed from but the sum-of-squares of their indices must be < 30 Input total

elite 3 30 the number of different subsets of cows satisfying that constraint. Input exact number of cows we need, indexed from but the sum-of-squares of their indices must be < 30 Input total "use it or lose it" analysis You're either going to USE the first cow - or you're not. Sum up both cases!

elite 3 30 the number of different subsets of cows satisfying that constraint. Input exact number of cows we need, indexed from but the sum-of-squares of their indices must be < 30 Input "use it or lose it" N(3,30,1) = N(2,29,2) + N(3,30,2) # of len-3 subsets, totaling under 30, starting from 1 # of len-3 subsets, totaling under 30, starting from 2 # of len-2 subsets, totaling under 29, starting from 2 total

Thanks to Allie Russell for this…

elite but it's much too slow!

Python function decorators plain

Python function decorators

Python function decorators implemented fib = dec42(fib)

DP for APSP… Directed graph as adjacency matrix: Directed graph: src "from" dst "to" 100inf inf50 14 inf intermediate nodes all-pairs-shortest-paths

Floyd-Warshall! src "from" dst "to" 100inf inf50 14 inf intermediate nodes src "from" dst "to" 100inf inf50 14 inf intermediate node(s)! 1

Floyd-Warshall! src "from" dst "to" 100inf inf50 14 inf src "from" dst "to" 64inf inf50 14 inf intermediate node(s)! 1 1 intermediate node(s) 1 2

Floyd-Warshall! src "from" dst "to" inf28 14 inf src "from" dst "to" intermediate node(s) ~ done! 1 3 intermediate node(s) All-pairs shortest paths:

Floyd-Warshall! src "from" dst "to" inf28 14 inf intermediate node(s) minimum distance from src to dst using intermediate nodes 1..k min T[src][dst][k] = T[src][dst][k-1] T[src][k][k-1] + T[k][dst][k-1] inf

Tracking the paths… Floyd Warshall… not a huge amount of extra work to keep an extra table ( pred ) of the first intermediate vertex on the path from i to j

This week's problems… These two are available if you haven't submitted them successfully before…

This week's problems… !

hurdles What is the minimum cost along any one of the edges required to get from start to end ? Input Output number of nodes number of edges number of "tasks" from node 1 to node 2 the cost ("height") is not possible!

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 What is a cow-satisfying assignment here? dining

Max Flow A B E D C 13 F source capacity sink The problem how much traffic (flow) can get from the source to the sink ? Ford-Fulkerson algorithm

Max Flow The problem how much traffic can get from the source to the sink ? A B E D C 13 A B C D E FROM ABCDE F F F TO “Capacity Graph” source sink capacity C

Find a path in C via BFS The problem how much traffic can get from the source to the sink ? A B E D C 13 FROM F TO source sink capacity C A B C D E ABCDE F F Need to subtract this flow from C !

Create F Create a FLOW GRAPH with the minimum weight from that path A B E D C 13 A B C D E FROM ABCDE F F F TO “Flow Graph” source sink capacity F 12 flow in one direction is negative flow in the other direction

R = C - F Use the RESIDUAL GRAPH with the rest of the capacity after that flow A B E D C 13 A B C D E FROM ABCDE F F F TO “Residual Graph” source sink capacity R 12 reverse edges allow the "undoing" of previous flow!

Keep going! Continue running this on the residual capacity until BFS fails… A B E D C 12/13 F 11/16 0/101/4 0/9 12/12 11/14 7/7 19/20 4/4 source sink Continue until there is no longer a path from A to F ! max flow: 23

The max flow algorithm A B E D C 13 F “Residual Graph” source sink capacity flow Don't need to keep R around explicity: Keep only the current flow (F) and the original capacity (C). A B C D E F residual Floyd-Fulkerson 1. Set F to all 0 2. Compute R = C - F 3. BFS in R from source to sink. 3a. If no path, you’re done. 3b. If  a path, add the available capacity to F; goto (2).

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

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…

Jotto! A word-guessing game similar to mastermind… next guesses? SophsJrsSrsProfs Chalk 1Chalk 0Chalk 1 Quine 1 Quine 2 ????

elite eating problem how does the solution depend on subproblems? use it or lose it!

Knapsack problem w 1 = 2 v 1 = 100 i = 1 i = 2 i = 3 i = 4 i = 5 vpw 1 = 50 w 2 = 3 v 2 = 120 vpw 2 = 40 w 3 = 5 v 3 = 230 vpw 3 = 46 w 4 = 7 v 4 = 560 vpw 4 = 80 w 5 = 9 Suppose you can consume 13 candywt units of candy. "0-1 problem" TOTAL WEIGHT max total value V(i,w) = (3) if you can take 0 or 1 of each whole candy package…? Total Items v 5 = 675 vpw 5 = 75

Candy! 1800's 1950's 1970's 1920's 1940's