Recursion and Exhaustion Hong Kong Olympiad in Informatics 2009 Hackson Leung 2009-01-24.

Slides:



Advertisements
Similar presentations
Heuristic Search techniques
Advertisements

Recursion CS 367 – Introduction to Data Structures.
CS252: Systems Programming Ninghui Li Program Interview Questions.
Reference :Understanding Computers
Recursion. Recursion is a powerful technique for thinking about a process It can be used to simulate a loop, or for many other kinds of applications In.
Recursion, Divide and Conquer Lam Chi Kit (George) HKOI2007.
Fundamentals of Computer Science Lecture 14: Recursion Instructor: Evan Korth New York University.
1 Introduction to Computability Theory Lecture12: Reductions Prof. Amos Israeli.
1 Module 2: Fundamental Concepts Problems Programs –Programming languages.
Fundamentals of Computer Science Lecture 14: Recursion Instructor: Evan Korth New York University.
Lecture 2: Fundamental Concepts
1 Module 2: Fundamental Concepts Problems Programs –Programming languages.
Unit 181 Recursion Definition Recursive Methods Example 1 How does Recursion work? Example 2 Problems with Recursion Infinite Recursion Exercises.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
1 Lecture 10 Proving more specific problems are not recursive Reduction technique –Use subroutine theme to show that if one problem is unsolvable, so is.
CSE 830: Design and Theory of Algorithms
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Abstract Data Types (ADT)
CS5371 Theory of Computation Lecture 8: Automata Theory VI (PDA, PDA = CFG)
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.
19-Aug-15 Simple Recursive Algorithms. 2 A short list of categories Algorithm types we will consider include: Simple recursive algorithms Backtracking.
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Recursion. Review  Recursive solutions, by definition, are built off solutions to sub-problems.  Many times, this will mean simply to compute f(n) by.
Introduction Algorithms and Conventions The design and analysis of algorithms is the core subject matter of Computer Science. Given a problem, we want.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
1 CS 177 Week 16 Recitation Recursion. 2 Objective To understand and be able to program recursively by breaking down a problem into sub problems and joining.
CIS 068 Welcome to CIS 068 ! Stacks and Recursion.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Advanced Data Structure Hackson Leung
Recursion When to use it and when not to use it. Basics of Recursion Recursion uses a method Recursion uses a method Within that method a call is made.
Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
1 FUNCTIONS - I Chapter 5 Functions help us write more complex programs.
CSE373: Data Structures & Algorithms Lecture 22: The P vs. NP question, NP-Completeness Lauren Milne Summer 2015.
Exhaustion, Branch and Bound, Divide and Conquer.
Recursion Unit 15. Recursion: Recursion is defined as the process of a subprogram calling itself as part of the solution to a problem. It is a problem.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Functions. Motivation What is a function? A function is a self-contained unit of program code designed to accomplish a particular task. We already used.
1/32 This Lecture Substitution model An example using the substitution model Designing recursive procedures Designing iterative procedures Proving that.
1 Lecture 3 Part 2 Storage Classes, Scope, and Recursion.
Recursion A recursive definition is one which uses the word or concept being defined in the definition itself Example: “A computer is a machine.
Introduction to Computer Programming - Project 2 Intro to Digital Technology.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
Efficiently Solving Computer Programming Problems Doncho Minkov Telerik Corporation Technical Trainer.
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
Recursion.
Recursion Data Structure Submitted By:- Dheeraj Kataria.
Problem Solving: Brute Force Approaches
Recursion 5/4/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Introduction to Recursion
Functional Programming: Lists, Pattern Matching, Recursive Programming (CTM Sections , 3.2, , 4.7.2) Carlos Varela RPI September 12,
Recursion DRILL: Please take out your notes on Recursion
More Simple Recursive Problems
Data Structures and Algorithms
Java 4/4/2017 Recursion.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Problem Solving: Brute Force Approaches
Applied Algorithms (Lecture 17) Recursion Fall-23
Dynamic Programming.
CS201: Data Structures and Discrete Mathematics I
Data Structures Review Session
This Lecture Substitution model
Programming with Recursion
Recursive Thinking.
Presentation transcript:

Recursion and Exhaustion Hong Kong Olympiad in Informatics 2009 Hackson Leung

Agenda Pre-requisite Recursion Exhaustion More...?

Pre-requisite Know something beforehand

Pre-requisite Function Mathematically, it gives output(s) from input(s) In short, y is the output, aka function of x x is the parameter of the function f gives what x can be related to y In programming, function can give nothing void in C/C++ Procedure in Pascal

Pre-requisite Function Simple exercise Write a function f such that

Pre-requisite Stack First In, Last Out (FILO) Supported operations Push to the bottom Pop from the top Learn more in future training H H K K O O I I Container (Stack) Object

Pre-requisite In the computer Each function is an object Start a new function Push Return from a function Pop Container is the system stack int main() int f() System Stack Function Running

Recursion Playing with functions!

Recursion Warmup Mark Six 6 integers, ranged from 1 to 49 inclusive Numbers are not repeated Write a program to generate all possible Mark Six results is not the same as for loops!? OK. There is a lottery called Mark Twelve….

Recursion To recur means to happen again In computer science, we say that a subroutine (function or procedure) is recursive when it calls itself one or more times We call the programming technique by using recursive subroutine(s) as recursion The correctness, time complexity and space complexity can be proved by mathematical induction

Recursion Example Correct?

Recursion Example Problematic It does not stop! When to stop? Everybody knows that So we do not recur on ! Base case(s) / Terminating condition(s)

Recursion Recursion requires two components Recurrence relation(s) (by how f relates to itself) Base case(s) (by when should f not to recur)

Recursion Common recurrence relations Factorial Combinations More on Combinations Permutations Integral powers

Recursion Case Study 1 Give all permutations of the string ``ABC ’’ ABC, ACB, BCA, BAC, CAB, CBA Before deriving... Parameter(s)? Stage k What does f(k) mean? f(k) depends on...?

Recursion Case Study 1 Give all permutations of the string ``ABC ’’ ABC, ACB, BCA, BAC, CAB, CBA Solution 1 f(k) depends on f(k-1) for sure! f(k) will add for each results from f(k-1) an unused character from ``ABC’’ and generate a new result e.g. In f(2), place ``B’’ to ``A’’ and ``C’’, which are generated from f(1) Call f(3) to finish the task Base case(s)?

Recursion Case Study 1 Give all permutations of the string ``ABC ’’ ABC, ACB, BCA, BAC, CAB, CBA Solution 1 f(k) will add for each results from f(k-1) an unused character from ``ABC’’ and generate a new result Not easy to implement, because... You need to remember all results from f(k-1) As well as the occurrence from each of them

Recursion Case Study 1 Give all permutations of the string ``ABC ’’ ABC, ACB, BCA, BAC, CAB, CBA Solution 2 In f(k), add an unused character into the current result, and directly go to f(k+1) Call f(0) Base case(s)...?

Recursion Case Study 1 Give all permutations of the string ``ABC ’’ ABC, ACB, BCA, BAC, CAB, CBA Solution 2 A more intuitive approach, in coding No extra memory for storing result strings and occurence (``Grow-on-fly’’) Note that the general idea is identical to solution 1

Recursion Case Study 2 Calculate Given: Solution 1 Simple! What does f(k) mean? Recurrence relation(s)? Base case(s)? Call f(?)?

Recursion Case Study 2 Calculate Given: Solution 1 Efficient enough? Consider Y can be as large as

Recursion Case Study 2 Calculate Given: Solution 2 Efficient enough!

Recursion Summary Usually a (nice and) well defined function can be easily implemented by recursion Different thinking can also lead to different complexity in coding Different thinking can even lead to different complexity in time NOTE: Recursion does not mean SLOW!

Exhaustion Try your BEST

Exhaustion 窮舉 / 窮尋 / 暴力法 Also called Brute Force Anyway it is not about violence... Sometimes for finding the answers, you need to try all possible candidates and see if they are the answers

Exhaustion Analogy Consider you are selling Broadband service You want to promote it in a fixed building Known Facts You don’t know who live inside are interested in your service You don’t want to promote to the same person twice Still, you want to find a way to promote your service and want all potential users to subscribe

Exhaustion You don’t know who live inside are interested in your service Yes, you don’t know the direct answers in the problem You don’t want to promote to the same person twice You don’t waste time on checking same candidate Still, you want to find a way to promote your service and want all potential users to subscribe That’s what describes exhaustion

Exhaustion Exhaustion related problem Constraints Satisfaction Problem (CSP) Given all constraints, give any/all solution(s) that satisfy the constraint(s) E.g. Sudoku

Exhaustion Case Study 1 Irreversible Transform Given a transform H, you can calculate y = H(x) But given y, you cannot easily calculate x such that y = H(x), we call H is irreversible Given y, tell me how many x can be transformed to y Suppose x and y are 32bit signed integers

Exhaustion Case Study 1 There is no explicit information about H, you can only try all possible x values If the transformation is not complicated, the time complexity is still acceptable Example transformation: Game of Life

Exhaustion Case Study 2 Narrow Range Broadband Given all clients’ positions as well as the profits that can be made from each of them You can only setup one server station with limited transmission distance Give the best possible position and the profit for the company

Exhaustion Case Study 2 Give the best possible position and the profit for the company Any definite answer, first? The more the clients it covers, the better? Map size: at most 100 x 100 (w x h) Number of clients: at most 1000 (n) Maximum distance: 200 (d) Manhatten Distance:

Exhaustion Case Study 2 Give the best possible position and the profit for the company Solution 1 Each position can be the best For each position Find from its reachable distance Add profit if that position is a client Complexity?

Exhaustion Case Study 2 Give the best possible position and the profit for the company Solution 1 Complexity? A bit slow Any definite NON answer?

Exhaustion Case Study 2 Give the best possible position and the profit for the company Solution 2 A client is served if a server can reach it within d units Similarly, if a client can reach the server d units, it is served By exploiting the fact that distance is symmetric, one can achieve an algorithm Any faster algorithm?

Exhaustion Case Study 3 You have a lock with password of length N Each character is A to Z inclusive You only know that the password contains distinct characters Target: Unlock it Discussion

Exhaustion Summary If you cannot figure out fast way to solve a given problem (may or may not exist), try brute force For small case (usually 30%~50%), they are designed for brute force purposes Unless you proved that the only way to solve is to try all possible cases (HKOI2009 Dictionary), this method is bound to fail most data intensive cases

Extras Only for attended trainee

Extras Recursion Know the complexity – Master Theorem Interesting problems that are solved by recursion Solving some simple recurrence relations Exhaustion More classic examples Not really slow – Prunning Can be even faster – A.I. Thinking (Heuristics)

Tasks Prove to me that you really learnt something

Tasks HKOJ 2021 Lovely String 2031 Narrow Range Broadband 2062 Sudoku 2076 SOS 2086 Storage Box 4013 Mahjong Queen Chess Problem IOI 94 Day 2 Problem 1 The Clocks