Recursion and Exhaustion - PowerPoint PPT Presentation

1 / 41
About This Presentation
Title:

Recursion and Exhaustion

Description:

Mathematically, it gives output(s) from input(s) In short, ... 2086 Storage Box. 4013 Mahjong. 20750 8 Queen Chess Problem. IOI. 94 Day 2 Problem 1 The Clocks ... – PowerPoint PPT presentation

Number of Views:115
Avg rating:3.0/5.0
Slides: 42
Provided by: Eri794
Category:

less

Transcript and Presenter's Notes

Title: Recursion and Exhaustion


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

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

3
Pre-requisite
  • Know something beforehand

4
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

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

6
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

I
Object
O
K
H
Container (Stack)
7
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

Function
int f()
int main()
System Stack
8
Recursion
  • Playing with functions!

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

10
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

11
Recursion
  • Example
  • Correct?

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

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

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

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

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

17
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

18
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(k1)
  • Call f(0)
  • Base case(s)...?

19
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

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

21
Recursion
  • Case Study 2
  • Calculate
  • Given
  • Solution 1
  • Efficient enough?
  • Consider Y can be as large as 240...

22
Recursion
  • Case Study 2
  • Calculate
  • Given
  • Solution 2
  • Efficient enough!

23
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!

24
Exhaustion
  • Try your ...... BEST

25
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

26
Exhaustion
  • Analogy
  • Consider you are selling Broadband service
  • You want to promote it in a fixed building
  • Known Facts
  • You dont know who live inside are interested in
    your service
  • You dont 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

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

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

29
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

30
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

31
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

32
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

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

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

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

36
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

37
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 3050), 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

38
Extras
  • Only for attended trainee

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

40
Tasks
  • Prove to me that you really learnt something

41
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
Write a Comment
User Comments (0)
About PowerShow.com