brought to you by max (icq:31252512 tel:61337706) march 12, 2005 recursion and exhaustion

13
Brought to you by Max (ICQ:31252512 TEL:61337706) March 12, 2005 Recursion and Exhaustion

Upload: milton-harris

Post on 02-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Brought to you by Max (ICQ:31252512 TEL:61337706)March 12, 2005

Recursion and Exhaustion

Page 2

Why Exhaustion?

• Many problems can be solved by brute force in a reasonable amount of time if the problem size is small

• Some problems have no known “fast” algorithm Example: Traveling Salesman, Hamiltonian Path, Graph

Isomorphism…

• Estimating the time needed for brute force let us decide whether to use exhaustion or not

Example: O(2N) is okay for N~30, O(N!) is okay for N~13

• Knowing what types of problems are “hard” prevents you from wasting time in competitions

Page 3

Problem: Generating Permutations

• Given N, generate all permutations of integers 1..N• When N = 3:

123132213231312321

Page 4

Problem: Generating Permutations

• Nested For loop is out of the question• We need a more advanced method – recursion• Outline of algorithm:

varused : array[0..100] of boolean;n : integer;

procedure run(stage : integer);begin

if (level = n)do_something;

else for i = 1 to n doif not used[i] begin

used[i] = true;run(stage+1);used[i] = false;

end;end;

Page 5

Problem: Generating Permutations

• Search Tree

1

2 3

2

1 3

3

1 2

3 2 3 1 2 1

Page 6

General Idea of Recursion

• To solve a problem, break it into smaller, similar instances

• Suppose we can solve the smaller instances, then combining these results can solve the original problem

• For the algorithm to terminate, we also need to know how to solve the base case

Page 7

The N-Queens Problem

• In a NxN chessboard, place N queens so that no two of them can attack each other (this is wrong XD )

• One brute force algorithm is to find all permutations of 1..N

• Example for N=3: permutation is 132, then we place the queens at (1,1), (2,3) and (3,2)

• The do_something at base case is to check whether 2 queens are attacking

• Optimization: when placing each queen, we can immediately check if it has violated the constraint

• Cuts off useless branches quickly

Q

Q

Q

Q

Q

Q

Q

Q

Page 8

Estimating Time Complexity of Exhaustion

• The time complexity for the previous 2 algorithms are easy

First stage has N choices, second stage has N-1 choices, … Total time = N*(N-1)*(N-2)*…*2*1 = N! If still not convinced, look at the search tree

• How about other problems? Start at the top-left corner, move to the bottom-right

corner Each step can go down or right only Maximize sum of numbers in the path1 5 4 2 5

6 9 2 8 1

4 7 6 1 4

2 4 3 1 2

Page 9

Two useful mathematical concepts

• Permutations (n P r) : number of ways to arrange r out of n objects with ordering

5 books, but only 3 slots on a bookshelf, how many permutations?

The first slot has 5 choices The second slot has 4 choices The third slot has 3 choices Total number of permutations = 5*4*3

• Formula : n P r = n! / (n-r)!

Page 10

Two useful mathematical concepts

• Combinations (n C r) : number of ways to choose r out of n objects without ordering

A bag of 49 balls, draw 6 of them, how many combinations? The first ball has 49 choices The second ball has 48 choices … Total number of combinations = 49*48*47*46*45*44 However, 1 3 5 10 11 12 is just the same as 12 11 10 5 1 3, 10 5 1 3

11 12 or any of 6! permutations Therefore need to divide it by 6!

• Formula : n C r = n! / (n-r)! r!• Property : n C r = n C (n-r)

Choosing r items is equal to choosing which n-r items to leave out

Page 11

Estimating Time Complexity of Exhaustion

• For the equation x1 + x2 + … + xk = s, where xi>=0, how many possible solutions?

Answer = (s+k-1) C (k-1), why?

• N people sit around a round table, how many permutations? (rotations of a permutation are considered the same)

Answer = (N-1)!, why?

• Return to the previous problem To get from the top-left to bottom-right, we need to make

7 moves (4 right, 3 down) This is equivalent to choosing 4 out of 7 items Answer = 7 C 4 (or generally, (width + height – 2) C height

)

Page 12

Lexicographical Ordering

• The objects we permutate may have some “ordering” Integers : 1 < 2 < 3 < … < N Alphabets : a < b < c < … < z

• If such an ordering exists, we can extend it to an ordering between permutations

123 < 213 < 321 bdca < cabd < cbad < dcab

• This is called “lexicographical ordering” or “dictionary ordering”

Page 13

Lexicographical Ordering

• Three natural questions to ask……

• Given a permutation, determine its “next” permutation

231 -> 312

• Find the Nth permutation (Deranking) 3rd permutation of 1,2,3 = 213

• Given a permutation, determine its position (Ranking)

132 -> position = 2