Bowen Yu Programming Practice Midterm, 7/30/2013.

Slides:



Advertisements
Similar presentations
Algorithm Analysis Input size Time I1 T1 I2 T2 …
Advertisements

TEST-TAKING STRATEGIES FOR THE OHIO ACHIEVEMENT READING ASSESSMENT
The Assembly Language Level
The Game of Algebra or The Other Side of Arithmetic The Game of Algebra or The Other Side of Arithmetic © 2007 Herbert I. Gross by Herbert I. Gross & Richard.
EE 553 Integer Programming
Complexity ©D Moshkovitz 1 Approximation Algorithms Is Close Enough Good Enough?
Introduction to C Programming
Algebra Problems… Solutions Algebra Problems… Solutions © 2007 Herbert I. Gross Set 22 By Herbert I. Gross and Richard A. Medeiros next.
1.2 Row Reduction and Echelon Forms
Linear Equations in Linear Algebra
Tirgul 9 Amortized analysis Graph representation.
Analysis of Algorithms. Time and space To analyze an algorithm means: –developing a formula for predicting how fast an algorithm is, based on the size.
Program Design and Development
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
ASC Program Example Part 3 of Associative Computing Examining the MST code in ASC Primer.
Systems of Linear Equations
The Game of Algebra or The Other Side of Arithmetic The Game of Algebra or The Other Side of Arithmetic © 2007 Herbert I. Gross by Herbert I. Gross & Richard.
1 MA 1128: Lecture 09 – 6/08/15 Solving Systems of Linear Equations.
Dynamic Programming Introduction to Algorithms Dynamic Programming CSE 680 Prof. Roger Crawfis.
Algebra Problems… Solutions Algebra Problems… Solutions © 2007 Herbert I. Gross Set 12 By Herbert I. Gross and Richard A. Medeiros next.
Algebra Problems… Solutions
While Loops and Do Loops. Suppose you wanted to repeat the same code over and over again? System.out.println(“text”); System.out.println(“text”); System.out.println(“text”);
1 1.1 © 2012 Pearson Education, Inc. Linear Equations in Linear Algebra SYSTEMS OF LINEAR EQUATIONS.
Methods of Proof & Proof Strategies
Lecture for Week Spring.  Numbers can be represented in many ways. We are familiar with the decimal system since it is most widely used in everyday.
Called as the Interval Scheduling Problem. A simpler version of a class of scheduling problems. – Can add weights. – Can add multiple resources – Can ask.
Key Stone Problem… Key Stone Problem… next Set 22 © 2007 Herbert I. Gross.
§ 4.3 Equations and Inequalities Involving Absolute Value.
Disclosure risk when responding to queries with deterministic guarantees Krish Muralidhar University of Kentucky Rathindra Sarathy Oklahoma State University.
1 Sections 1.5 & 3.1 Methods of Proof / Proof Strategy.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
Program Design (or Why C is really not difficult?) A/Prof Anthony Tung Department of Computer Science School of Computing NUS
30/10/ Iteration Loops Do While (condition is true) … Loop.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
Introduction to Problem Solving. Steps in Programming A Very Simplified Picture –Problem Definition & Analysis – High Level Strategy for a solution –Arriving.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Algorithm Analysis Data Structures and Algorithms (60-254)
Asking the USER for values to use in a software 1 Input.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Programming Logic and Design Fourth Edition, Comprehensive Chapter 8 Arrays.
Algorithm Design Methods (II) Fall 2003 CSE, POSTECH.
REVIEW No curveballs this time …. PROBLEM TYPE #1: EVALUATIONS.
Dynamic Programming1. 2 Outline and Reading Matrix Chain-Product (§5.3.1) The General Technique (§5.3.2) 0-1 Knapsack Problem (§5.3.3)
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
27-Jan-16 Analysis of Algorithms. 2 Time and space To analyze an algorithm means: developing a formula for predicting how fast an algorithm is, based.
CSE 311 Foundations of Computing I Lecture 28 Computability: Other Undecidable Problems Autumn 2011 CSE 3111.
CS6045: Advanced Algorithms Data Structures. Hashing Tables Motivation: symbol tables –A compiler uses a symbol table to relate symbols to associated.
CSCI 156: Lab 11 Paging. Our Simple Architecture Logical memory space for a process consists of 16 pages of 4k bytes each. Your program thinks it has.
Algebra Problems… Solutions Algebra Problems… Solutions © 2007 Herbert I. Gross Set 16 By Herbert I. Gross and Richard A. Medeiros next.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 6 Arrays.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
1 1.2 Linear Equations in Linear Algebra Row Reduction and Echelon Forms © 2016 Pearson Education, Ltd.
REVIEW A relation is a set of ordered pairs. {(2,3), (-1,5), (4,-2), (9,9), (0,-6)} This is a relation The domain is the set of all x values.
Precalculus Fifth Edition Mathematics for Calculus James Stewart Lothar Redlin Saleem Watson.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Systems of Linear Equations
Data Types Variables are used in programs to store items of data e.g a name, a high score, an exam mark. The data stored in a variable is entered from.
The Selection Structure
Lecture 2 Introduction to Programming
Writing Methods AP Computer Science A.
Instructor: Shengyu Zhang
Linear Equations in Linear Algebra
Coding Concepts (Basics)
Analysis of Algorithms
Programming Logic and Design Fifth Edition, Comprehensive
Analysis of Algorithms
Linear Equations in Linear Algebra
Analysis of Algorithms
Presentation transcript:

Bowen Yu Programming Practice Midterm, 7/30/2013

There is a ferry that has two sides (port/starboard). Both sides are L-unit long (L<=10000). You are given a sequence of cars to be loaded onto the ferry. Each car has an integer length in [100, 10000]. You can decide to which side of the ferry each car is loaded. But you have to load them in the input order and stop as soon as one car cannot be loaded to either side. Task: Maximize the number of cars you can load, and print a way of loading maximum number of cars (any will do). 2

L = 5000, Cars = {2500, 3000, 1000, 1000, 1500, 700, 800} 3 Answer: 6 port(2500) starboard (3000) starboard (1000) port(1500) port(700) Let’s verify: port = = ) starboard = = 5000 ( > 5000)

You may forget about the ferry-car story and switch to a more straightforward model instead. A simpler model: You are given some objects one by one. Pack as many of them as you can into two equal-size containers, so that the volume sum of the objects in either container does not exceed the capacity of the container. 4

Values are all integers, i.e. discrete. The size of the container is up to The size of one object is [100, 10000]. Unfortunately, the number of objects is not given (at first glance there is no bound). Can we determine its range? The container is at most large, and each object is at least 100, so we can pack at most 200 objects (don’t forget you have 2 containers!). 5 Small! Good for memo! YES!

Enumerating decisions for 200 objects is clearly TLE. So forget about complete search. There is no greedy strategy that makes sense. (Easy to show yourself some counterexamples) See that the input are integers, and limited in range (up to 10000). Also the objects are given in an order that we can not change (forward property). This recalls us directly to DP. 6

To use DP, we need to first choose the states we will maintain. Candidate 1: memorize our decision for each object Not feasible, that is 2 N Think… How does our previous choices affect the current decision? Only the volume used matters! We just need to know whether we can pack the previous objects into the two containers, so that we occupy V 1 unit of space in container 1, and V 2 unit of space in container 2 7

Candidate 2: memorize whether we can pack the previous objects (objects that appear before the i-th object we’re currently considering) so that the volume used are (V 1, V 2 ) respectively for the 2 containers. So the state is (i, V 1, V 2 ) = {true, false}. Not feasible :( This would require [200][10000][10000] for all the states, which is clearly MLE 8

We need to reduce the space requirement for our DP. Think… Can we drop one variable, and recover it from the others? YES! Suppose we know a valid state (i, V 1, V 2 ). Note that we also know the sum of volumes of objects we’ve already packed, say V sum. Then, V 2 = V sum – V 1. 9

Candidate 3: memorize whether we can pack the previous objects (before the i-th object we’re currently considering) so that the volume used are (V 1, V 2 ). But the state is (i, V 1 ) instead of (i, V 1, V 2 ) as we can recover V 2 from V 1. This gives us [200][10000] states. This candidate WORKS within the Memory Limit! :) 10

Suppose a previously achievable state is (i, V 1, V 2 ). (I write three parameters for clear explanation, though we do not store 3-parameter states physically) We are considering the i-th object of size V i. We have two choices with respect to this previously achievable state (i, V 1, V 2 ): 1)Put it into the first container (when V 1 +V i <= L) (i, V 1, V 2 ) -> (i+1, V 1 +V i, V 2 ) 2) Put it into the second container (when V 2 +V i (i+1, V 1, V 2 +V i ) 11

First, for each object we have to enumerate the previously achievable states, which is O(L). For each object, we need to consider two decisions, which is constant time. We have to do the above for each of the objects. We multiply the time by the number of objects. Therefore, we have finally an O(NL) time DP solution, where N<=200, L<= Now we know, this solution really WORKS, as both time and space requirements are satisfied. 12

Don’t forget that the problem asks us to print the actual solution in addition to the maximum number of objects we can pack. This is a DP solution memo requirement. We can do it without increasing our space requirement. When we reach a new state from a previous one, we can store a backward pointer of the new state to the previous state. In this way later we can trace back from the final state all the way to the initial state and figure out every decision, via the backward pointers. Each state of DP has exactly one backward pointer, so the backward pointers together take the same space as the dp states. 13

14 L = 5000, {2500, 3000, 1000, 1000, 1500, 700, 800} (2500) 2500 (0) 3000 (2500) 2500 (3000) 4000 (2500) 3000 (3500) 3500 (3000) 2500 (4000) 4000 (3500) 3000 (4500) 3500 (4000) 2500 (5000) 4500 (3000) 5000 (2500) i=0i= i= i= i=4 1000

15 L = 5000, {2500, 3000, 1000, 1000, 1500, 700, 800} 4000 (3500) 3000 (4500) 3500 (4000) 2500 (5000) 4500 (3000) 5000 (2500) i= i= (5000) 4500 (4500) 5000 (4000) 4700 (5000) 5000 (4700) i=6 700 i=7 800

16 L = 5000, {2500, 3000, 1000, 1000, 1500, 700, 800} 4000 (3500) 3000 (4500) 3500 (4000) 2500 (5000) 4500 (3000) 5000 (2500) i= i= (5000) 4500 (4500) 5000 (4000) 4700 (5000) 5000 (4700) i=6 700 i=7 800 Container 2 Container 1

17 L = 5000, {2500, 3000, 1000, 1000, 1500, 700, 800} (2500) 2500 (0) 3000 (2500) 2500 (3000) 4000 (2500) 3000 (3500) 3500 (3000) 2500 (4000) 4000 (3500) 3000 (4500) 3500 (4000) 2500 (5000) 4500 (3000) 5000 (2500) i=0i= i= i= i= Container 1 Container 2 Container 1

Pay attention to the input format. L is given in meters, but lengths of cars are in centimeters. So we shall do an easy conversion. As the number of cars is unknown beforehand, the input car length sequence of each case is terminated by 0. If one car cannot be loaded, all the following cars cannot be loaded either. But we still need to read in the other car lengths. Allocate arrays with redundant entries, or be careful with off-by-one errors. Note that a minimum of L+1 entries is required for each DP table row. 18

Test your code with predesigned test cases. For example: L = 1, {10000} Cannot load any car! output 0 L = 1, {100, 100} One car at each side … Good thing about DP: Usually if you have a clear thinking of your DP logic (time, space, state transition, etc.), you have a high chance of getting 1A (got Accepted using 1 submission). Finally, Submit and have fun! :D 19

I’ve written one C++ solution and one Java solution for this problem, which will be given out with this slide. (In fact for this problem, you can use space saving trick if you like for the DP table, but not for the backward pointers.) I’ve added exhaustive comments for each statement, and expand each variable to its full name (instead of contest-style short name) so as to minimize the chance you can confused. Hope they help! 20

21