Comments on the “Three Piles” Problem

Slides:



Advertisements
Similar presentations
Introduction to Recursion and Recursive Algorithms
Advertisements

The Singleton Pattern II Recursive Linked Structures.
Recursion - see Recursion. RHS – SOC 2 Recursion We know that: –We can define classes –We can define methods on classes –Mehtods can call other methods.
Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
29-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
Recursion. Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example: A list.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Complex objects How might you design a class called NestedRects of graphical objects that look like this? Requirements for the constructor: –Like many.
Iteration. Adding CDs to Vic Stack In many of the programs you write, you would like to have a CD on the stack before the program runs. To do this, you.
Backtracking and Games Eric Roberts CS 106B January 28, 2013.
5.2: Solving Systems of Equations using Substitution
Lecture 7 February 24, Javadoc version and author Tags These go in the comments before named classes. –Put your SS# on a separate line from the.
Recursion A recursive definition is one which uses the word or concept being defined in the definition itself Example: “A computer is a machine.
Question of the Day  What three letter word completes the first word and starts the second one: DON???CAR.
CSE 143 Lecture 13 Recursive Backtracking slides created by Ethan Apter
Recursion. Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example: A list.
1 Tirgul 11: Recursion & Backtracking. 2 Elements of a recursive solution (Reminder) A base case that is so simple we need no computation to solve it.
April 13, 1998CS102-02Lecture 3-1 Data Types in Java CS Lecture 3-1 Java’s Central Casting.
Identify the Appropriate Method for Handling Repetition
Recursion Data Structure Submitted By:- Dheeraj Kataria.
Recursion - see Recursion
Searching.
Computer Science 4 Mr. Gerb
Introduction to Python
Recursion 4-Jun-18.
Class 11: Two-argument recursion
CSC 222: Object-Oriented Programming
CSE 374 Programming Concepts & Tools
Recursion CSC 202.
Week 3 - Friday CS221.
Solving Systems Using Substitution
Java for Beginners.
CS/ENGRD 2110 Fall 2017 Lecture 6: Consequence of type, casting; function equals
Section 10.3b Quick Sort.
University of Washington Computer Programming I
Quicksort 1.
Design by Contract Fall 2016 Version.
Recursion 12-Nov-18.
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Lesson 2: Building Blocks of Programming
Phil Tayco Slide version 1.0 Created Oct 2, 2017
Dynamic Programming.
null, true, and false are also reserved.
Object-oriented design for multiple classes
Analysis and design of algorithm
Recursion - see Recursion
Recursion 2-Dec-18.
Recursion 2-Dec-18.
Coding Concepts (Sub- Programs)
searching Concept: Linear search Binary search
Recursion 29-Dec-18.
Coding Concepts (Basics)
Example: LinkedSet<T>
adapted from Recursive Backtracking by Mike Scott, UT Austin
Barb Ericson Georgia Institute of Technology Oct 2005
slides created by Marty Stepp and Alyssa Harding
Problem Solving Designing Algorithms.
Stacks.
Searching, Sorting, and Asymptotic Complexity
Basics of Recursion Programming with Recursion
Building Java Programs
Quicksort.
Java for Beginners University Greenwich Computing At School DASCO
Recursion 10-Apr-19.
CSC 172 DATA STRUCTURES.
CSC 143 Binary Search Trees.
Recursion 23-Apr-19.
CS2013 Lecture 7 John Hurley Cal State LA.
Bell work Describe what the following graphs may look like 2x^7 +3x^2
Quicksort.
Presentation transcript:

Comments on the “Three Piles” Problem 29-May-18

My timings Solved 10000 problems of size 5 in 15 milliseconds. Average time per problem: 0.0015 ms. Solved 200 problems of size 10 in 16 milliseconds. Average time per problem: 0.08 ms. Solved 200 problems of size 15 in 718 milliseconds. Average time per problem: 3.59 ms. Solved 200 problems of size 20 in 34719 milliseconds. Average time per problem: 173.595 ms. Solved 100 problems of size 25 in 43797 milliseconds. Average time per problem: 437.97 ms.

Timing graph

General approach The “Three Piles” problem can be solved by a backtracking algorithm Roughly, the algorithm goes like this: boolean solve(int numbersPlaced) { if (numbersPlaced== all the numbers) return true; for (each pile p) { if can put the next number, numbersPlaced+1, into pile p { put it in pile p if (solve(numbersPlaced+1)) return true else take the number out again } return false }

Problems with this algorithm It’s much easier for the programmer if the method returns a boolean, but the user demands a solution The method requires some data structures (say, arrays or stacks) to keep working information in We can’t set this information up inside a recursive method We don’t want to hassle the user with extra junk that is only needed to make the algorithm work Solution: Provide a facade method to: Provide a simplified interface to the user Initialize any needed data structures Call the “real” recursive method with whatever parameters are convenient for the programmer, not the user

The facade method The user wants to do this: So you do this: solution = solve(problem) where solution is some data structure containing your results In the code below, I called this data structure a “Solution,” but it might be implemented by (for example) an int[][] So you do this: Solution solve(int[] problem) { Solution solution; // maybe an int[][] // Create the “Solution” data structure and any other data // structures and initializations that you need put problem[0] into your solution pile 0 if (reallySolve(problem, solution, 0, other stuff)) { return solution; } else return null; // Much better than returning a bad solution! }

The real recursive method boolean reallySolve(problem, solution, nextNumber, ...) { if nextNumber == problem size, return true** for each pile p, if we can put nextNumber into pile p { put it in the pile if (reallySolve(problem, solution, nextNumber+1, ...)) return true; else take the number back out again } } return false } ** If some problems may be unsolvable , you should check the “solution” at this point and return false if it doesn’t work

Backtracking in general Solution solve(Problem p) { create object to hold solution; do any other initializations you need; if (solve(p, solution, other stuff)) return solution; else return null; } private boolean solve(Problem p, Solution s, other stuff) { if problem is already solved, return true; for each of the choices available to you { make a choice; if (solve(simpler version of p, s, other stuff)) return true; else undo this choice; // can sometimes be implicit } return false; }

Summary The notion of a facade is this: The code has a complex interface and requires lots of initializations and global structures The user wants a simple and easy to use interface Create a facade to stand between the user and the ugly code and present a simpler interface

The End