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

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

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

Recursion and ExhaustionHong Kong Olympiad in Informatics 2009

Hackson Leung2009-01-24

Agenda

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

Pre-requisiteKnow 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• 2009-4-25

H

K

O

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 stackint main()

int f()

System Stack

Function

Running

RecursionPlaying 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• 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….

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

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

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!

ExhaustionTry 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

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

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

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

TasksProve 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• 20750 8 Queen Chess Problem

• IOI• 94 Day 2 Problem 1 The Clocks