Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Brought to you by Max (ICQ:31252512 TEL:61337706) March 12, 2005 Recursion and Exhaustion."— Presentation transcript:

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

2 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(2 N ) 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

3 Page 3 Problem: Generating Permutations Given N, generate all permutations of integers 1..N When N = 3: 123 132 213 231 312 321

4 Page 4 Problem: Generating Permutations Nested For loop is out of the question We need a more advanced method – recursion Outline of algorithm: var used: array[0..100] of boolean; n: integer; procedure run(stage : integer); begin if (level = n) do_something; else for i = 1 to n do if not used[i] begin used[i] = true; run(stage+1); used[i] = false; end;

5 Page 5 Problem: Generating Permutations Search Tree 1 23 2 13 3 12 323121

6 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

7 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

8 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 path 15425 69281 47614 24312

9 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)!

10 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

11 Page 11 Estimating Time Complexity of Exhaustion For the equation x 1 + x 2 + … + x k = s, where x i >=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 )

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

13 Page 13 Lexicographical Ordering Three natural questions to ask…… Given a permutation, determine its “next” permutation 231 -> 312 Find the Nth permutation (Deranking) 3 rd permutation of 1,2,3 = 213 Given a permutation, determine its position (Ranking) 132 -> position = 2


Download ppt "Brought to you by Max (ICQ:31252512 TEL:61337706) March 12, 2005 Recursion and Exhaustion."

Similar presentations


Ads by Google