recursion and exhaustion

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

Upload: raja

Post on 27-Jan-2016

40 views

Category:

Documents


1 download

DESCRIPTION

Recursion and Exhaustion. Hong Kong Olympiad in Informatics 2009 Hackson Leung 2009-01-24. Agenda. Pre-requisite Recursion Exhaustion More...?. Pre-requisite. Know something beforehand. Pre-requisite. Function Mathematically, it gives output(s) from input(s) - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Recursion and Exhaustion

Recursion and ExhaustionHong Kong Olympiad in Informatics 2009

Hackson Leung2009-01-24

Page 2: Recursion and Exhaustion

Agenda

• Pre-requisite• Recursion• Exhaustion• More...?

Page 3: Recursion and Exhaustion

Pre-requisiteKnow something beforehand

Page 4: Recursion and Exhaustion

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

Page 5: Recursion and Exhaustion

Pre-requisite

• Function• Simple exercise

• Write a function f such that

Page 6: Recursion and Exhaustion

Pre-requisite

• Stack• First In, Last Out (FILO)• Supported operations

• Push to the bottom• Pop from the top

• Learn more in future training• 2009-4-25

H

K

O

I

Container (Stack)

Object

Page 7: Recursion and Exhaustion

Pre-requisite

• In the computer• Each function is an object• Start a new function

• Push

• Return from a function• Pop

• Container is the system stackint main()

int f()

System Stack

Function

Running

Page 8: Recursion and Exhaustion

RecursionPlaying with functions!

Page 9: Recursion and Exhaustion

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• 1 2 3 4 5 6 is not the same as 6 5 4 3 2 1• 6 for loops!?

• OK. There is a lottery called Mark Twelve….

Page 10: Recursion and Exhaustion

Recursion

• 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

Page 11: Recursion and Exhaustion

Recursion

• Example

• Correct?

Page 12: Recursion and Exhaustion

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)

Page 13: Recursion and Exhaustion

Recursion

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

Page 14: Recursion and Exhaustion

Recursion

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

Page 15: Recursion and Exhaustion

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

Page 16: Recursion and Exhaustion

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)?

Page 17: Recursion and Exhaustion

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

Page 18: Recursion and Exhaustion

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

Page 19: Recursion and Exhaustion

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

Page 20: Recursion and Exhaustion

Recursion

• Case Study 2• Calculate • Given:

• Solution 1• Simple!• What does f(k) mean?• Recurrence relation(s)?• Base case(s)?• Call f(?)?

Page 21: Recursion and Exhaustion

Recursion

• Case Study 2• Calculate • Given:

• Solution 1

• Efficient enough?• Consider Y can be as large as 240...

Page 22: Recursion and Exhaustion

Recursion

• Case Study 2• Calculate • Given:

• Solution 2

• Efficient enough!

Page 23: Recursion and Exhaustion

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!

Page 24: Recursion and Exhaustion

ExhaustionTry your ...... BEST

Page 25: Recursion and Exhaustion

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

Page 26: Recursion and Exhaustion

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

Page 27: Recursion and Exhaustion

Exhaustion

• 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

Page 28: Recursion and 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

Page 29: Recursion and Exhaustion

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

Page 30: Recursion and Exhaustion

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

Page 31: Recursion and Exhaustion

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

Page 32: Recursion and Exhaustion

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:

Page 33: Recursion and Exhaustion

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?

Page 34: Recursion and Exhaustion

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?

Page 35: Recursion and Exhaustion

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 reachit 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?

Page 36: Recursion and Exhaustion

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

Page 37: Recursion and Exhaustion

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

Page 38: Recursion and Exhaustion

ExtrasOnly for attended trainee

Page 39: Recursion and Exhaustion

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)

Page 40: Recursion and Exhaustion

TasksProve to me that you really learnt

something

Page 41: Recursion and Exhaustion

Tasks

• HKOJ• 2021 Lovely String• 2031 Narrow Range Broadband• 2062 Sudoku• 2076 SOS• 2086 Storage Box• 4013 Mahjong• 20750 8 Queen Chess Problem

• IOI• 94 Day 2 Problem 1 The Clocks