Data structures and Algorithms Recursive algorithm

39
Data structures and Algorithms Recursive algorithm Pham Quang Dung Hanoi, 2012 Pham Quang Dung () Data structures and Algorithms Recursive algorithm Hanoi, 2012 1 / 38

Transcript of Data structures and Algorithms Recursive algorithm

Data structures and AlgorithmsRecursive algorithm

Pham Quang Dung

Hanoi, 2012

Pham Quang Dung () Data structures and Algorithms Recursive algorithm Hanoi, 2012 1 / 38

Outline

1 Recursion concept

2 Recursive algorithm

3 Examples

4 Recursive algorithms analysis

5 Recursive algorithms with memory

6 Backtracking algorithms

Pham Quang Dung () Data structures and Algorithms Recursive algorithm Hanoi, 2012 2 / 38

Recursively defined functions

Recursive function f (n) is specified based on an integer number n ≥ 0 andthe following schema

Basic step: Specify f (0)

Recursive step: Specify f (n + 1) depending on f (k),∀k = 0, . . . , n

Example

f (0) = 3

f (n + 1) = 2f (n) + 3, n > 0

Example

f (0) = 1

f (n + 1) = (n + 1)× f (n), n > 0

Pham Quang Dung () Data structures and Algorithms Recursive algorithm Hanoi, 2012 3 / 38

Recursively defined sets

Basic step: define a basic set

Recursive step: specify rules for generating the set from existing sets

Example

Basic step: 3 is an element of the set S

Recursive step: if x ∈ S and y ∈ S then x + y ∈ S

Example

formula

Basic step: if x is a variable or constant, then x is a formula

Recursive step: if f and g are formula, then(f + g), (f − g), (f ∗ g), (f /g), (f g ) are formula

Pham Quang Dung () Data structures and Algorithms Recursive algorithm Hanoi, 2012 4 / 38

Outline

1 Recursion concept

2 Recursive algorithm

3 Examples

4 Recursive algorithms analysis

5 Recursive algorithms with memory

6 Backtracking algorithms

Pham Quang Dung () Data structures and Algorithms Recursive algorithm Hanoi, 2012 5 / 38

Recursive algorithm

An algorithm calls it-self with smaller input

Suitable to process recursively defined objects

High level programming languages allow users to design recursivefunctions and procedures

Algorithm 1: RecursiveAlgo(input)

1 if input has smallest size then2 process basic step;3 else4 RecursiveAlgo(input with smaller size);5 Combine results of subproblems for obtaining the result R;6 return R

Pham Quang Dung () Data structures and Algorithms Recursive algorithm Hanoi, 2012 6 / 38

Outline

1 Recursion concept

2 Recursive algorithm

3 Examples

4 Recursive algorithms analysis

5 Recursive algorithms with memory

6 Backtracking algorithms

Pham Quang Dung () Data structures and Algorithms Recursive algorithm Hanoi, 2012 7 / 38

Compute n!

1 i n t f a c t ( i n t n ) {i f ( n==0)

3 r e t u r n 1 ;e l s e

5 r e t u r n n∗ f a c t ( n−1) ;}

Listing 1: Compute n!

Pham Quang Dung () Data structures and Algorithms Recursive algorithm Hanoi, 2012 8 / 38

Compute Fibonacci number

i n t FibRec ( i n t n ) {2 i f ( n<=1)

r e t u r n 1 ;4 e l s e

r e t u r n FibRec ( n−1) + FibRec ( n−2) ;6 }

Listing 2: Compute the nth fibonacci number

Pham Quang Dung () Data structures and Algorithms Recursive algorithm Hanoi, 2012 9 / 38

Compute(

nk

)

i n t C( i n t n , i n t k ) {2 i f ( k==0 | | k==n )

r e t u r n 1 ;4 e l s e

r e t u r n C( n−1,k−1) + C( n−1,k ) ;6 }

Listing 3: Compute the nth fibonacci number

Pham Quang Dung () Data structures and Algorithms Recursive algorithm Hanoi, 2012 10 / 38

Binary search

An array of numbers A[0..n− 1] with A[i ] ≤ A[i + 1], ∀i = 0, . . . , n− 2

Given a value X , find the position i such that A[i ] = X

i n t bSearch ( i n t ∗ A, i n t s t a r t , i n t f i n i s h , i n t X) {2 i f ( s t a r t==f i n i s h ) {

i f (A [ s t a r t ]==X) r e t u r n s t a r t ;4 e l s e r e t u r n −1;/∗ not found ∗/

} e l s e {6 i n t mid = ( s t a r t+f i n i s h ) / 2 ;

i f (A [ mid]==X) r e t u r n mid ;8 e l s e i f (X < A [ mid ] ) r e t u r n bSearch (A, s t a r t , mid−1,X) ;

e l s e r e t u r n bSearch (A, mid+1, f i n i s h , X) ;10 }}

Listing 4: Binary search

Pham Quang Dung () Data structures and Algorithms Recursive algorithm Hanoi, 2012 11 / 38

Hanoi tower

1 v o i d move ( i n t n , c h a r s t a r t , c h a r f i n i s h , c h a r s p a r e ) {i f ( n==1){

3 p r i n t f ( ”Move d i s k from %c to %c\n” , s t a r t , f i n i s h ) ;} e l s e {

5 move ( n−1, s t a r t , s pa re , f i n i s h ) ;move ( 1 , s t a r t , f i n i s h , s p a r e ) ;

7 move ( n−1, sp ar e , f i n i s h , s t a r t ) ;}

9 }

11 i n t main ( ) {i n t n = 5 ;

13 move ( n , ’A ’ , ’B ’ , ’C ’ ) ;r e t u r n 0 ;

15 }

Listing 5: Hanoi tower

Pham Quang Dung () Data structures and Algorithms Recursive algorithm Hanoi, 2012 12 / 38

Outline

1 Recursion concept

2 Recursive algorithm

3 Examples

4 Recursive algorithms analysis

5 Recursive algorithms with memory

6 Backtracking algorithms

Pham Quang Dung () Data structures and Algorithms Recursive algorithm Hanoi, 2012 13 / 38

Recursive algorithms analysis

Algorithm 2: D-and-C(n)

1 if n ≤ n0 then2 Process directly;3 else4 Divide the problem into a subproblems with input size n/b;5 foreach subproblem do6 D-and-C(n/b);

7 Combine solutions of a subproblems for obtaining the result of initialproblem;

T (n): time complexity of the problem with input size nDivision takes D(n)Combination takes C (n)Recursive definition of T (n)

T (n) =

{Θ(1), n ≤ n0

aT (n/b) + D(n) + C (n), n > n0

Pham Quang Dung () Data structures and Algorithms Recursive algorithm Hanoi, 2012 14 / 38

Recursive algorithms analysis

Algorithm 3: D-and-C(n)

1 if n ≤ n0 then2 Process directly;3 else4 Divide the problem into a subproblems with input size n/b;5 foreach subproblem do6 D-and-C(n/b);

7 Combine solutions of a subproblems for obtaining the result of initialproblem;

T (n): time complexity of the problem with input size nDivision takes D(n)Combination takes C (n)Recursive definition of T (n)

T (n) =

{Θ(1), n ≤ n0

aT (n/b) + D(n) + C (n), n > n0

Pham Quang Dung () Data structures and Algorithms Recursive algorithm Hanoi, 2012 14 / 38

Master theorem

T (n) = aT (n/b) + cnk with a ≥ 1, b > 1, c > 0 are constant

If a > bk , then T (n) = Θ(nlogba)

If a = bk , then T (n) = Θ(nk logn) with logn = log2n

If a < bk , then T (n) = Θ(nk)

Example

T (n) = 3T (n/4) + cn2 ⇒ T (n) = Θ(n2)

T (n) = 2T (n/2) + n0.5 ⇒ T (n) = Θ(n)

T (n) = 16T (n/4) + n⇒ T (n) = Θ(n2)

T (n) = T (3n/7) + 1⇒ T (n) = Θ(logn)

Pham Quang Dung () Data structures and Algorithms Recursive algorithm Hanoi, 2012 15 / 38

Outline

1 Recursion concept

2 Recursive algorithm

3 Examples

4 Recursive algorithms analysis

5 Recursive algorithms with memory

6 Backtracking algorithms

Pham Quang Dung () Data structures and Algorithms Recursive algorithm Hanoi, 2012 16 / 38

Recursive algorithms with memory

In many cases, the identical subproblems appear very often

Use memory for storing results of subproblems solved to avoidresolving them

C(3,5)

C(2,4) C(3,4)

C(1,3) C(2,3)

C(0,2) C(1,2)

C(0,1) C(1,1)

C(1,2) C(2,2)

C(0,1) C(1,1)

C(2,3) C(3,3)

C(1,2) C(2,2)

C(0,1) C(1,1)

Pham Quang Dung () Data structures and Algorithms Recursive algorithm Hanoi, 2012 17 / 38

Recursive algorithms with memory

1 l o n g D[ 1 0 0 ] [ 1 0 0 ] ;l o n g C( i n t k , i n t n ) {

3 i f ( k == 0 | | k == n ) D[ k ] [ n ] = 1 ;e l s e {

5 i f (D[ k ] [ n ] <= 0)D[ k ] [ n ] = C( k−1,n−1) + C( k , n−1) ;

7 }r e t u r n D[ k ] [ n ] ;

9 }

Listing 6: recursive algorithms with memory

Pham Quang Dung () Data structures and Algorithms Recursive algorithm Hanoi, 2012 18 / 38

Outline

1 Recursion concept

2 Recursive algorithm

3 Examples

4 Recursive algorithms analysis

5 Recursive algorithms with memory

6 Backtracking algorithms

Pham Quang Dung () Data structures and Algorithms Recursive algorithm Hanoi, 2012 19 / 38

Introduction

List all configurations satisfying some given constraints

permutationssubsets of a given setetc.

A1, . . . ,An are finite sets and X = {(a1, . . . , an) | ai ∈ Ai , ∀1 ≤ i ≤ n}P is a property on X

Generate all configurations (a1, . . . , an) having P

Pham Quang Dung () Data structures and Algorithms Recursive algorithm Hanoi, 2012 20 / 38

Introduction

In many cases, listing is a final way for solving some combinatorialproblems

Two popular methods

Generating method (not consider)BackTracking algorithm

Pham Quang Dung () Data structures and Algorithms Recursive algorithm Hanoi, 2012 21 / 38

BackTracking algorithm

Construct elements of the configuration step-by-step

Initialization: Constructed configuration is null: ()

Step 1:

Compute (base on P) a set S1 of candidates for the first position ofthe configuration under constructionSelect an item of S1 and put it in the first position

Pham Quang Dung () Data structures and Algorithms Recursive algorithm Hanoi, 2012 22 / 38

BackTracking algorithm

At Step k: Suppose we have partial configuration a1, . . . , ak−1

Compute (base on P) a set Sk of candidates for the kth position ofthe configuration under construction

If Sk 6= ∅, then select an item of Sk and put it in the k th position andobtain (a1, . . . , ak−1, ak)

If k = n, then process the complete configuration a1, . . . , an)Otherwise, construct the k + 1th element of the partial configuration inthe same schema

If Sk = ∅, then backtrack for trying another item a′k−1 for the k − 1th

position

If a′k−1 exists, then put it in the k − 1th positionOtherwise, backtrack for trying another item for the k − 2th position, ...

Pham Quang Dung () Data structures and Algorithms Recursive algorithm Hanoi, 2012 23 / 38

BackTracking algorithm

Algorithm 4: BackTracking(k)

1 Construct a candidate set Sk ;2 foreach y ∈ Sk do3 ak ← y ;4 if (a1, . . . , ak) is a complete configuration then5 ProcessConfiguration(a1, . . . , ak);6 else7 BackTracking(k + 1);

Algorithm 5: Main()

1 BackTracking(1);

Pham Quang Dung () Data structures and Algorithms Recursive algorithm Hanoi, 2012 24 / 38

BackTracking algorithm - binary sequence

A configuration is represented by b1, b2, . . . , bn

Candidates for bi is {0, 1}

Pham Quang Dung () Data structures and Algorithms Recursive algorithm Hanoi, 2012 25 / 38

BackTracking algorithm - binary sequence

1 v o i d BackTrack ing ( i n t k ) {f o r ( i n t i = 0 ; i <= 1 ; i ++){

3 b [ k ] = i ;i f ( k == n )

5 p r i n t C o n f i g u r a t i o n ( ) ;e l s e

7 BackTrack ing ( k+1) ;}

9 }

11 v o i d main ( ) {BackTrack ing ( 1 ) ;

13 }

Pham Quang Dung () Data structures and Algorithms Recursive algorithm Hanoi, 2012 26 / 38

BackTracking algorithm - combination

A configuration is represented by (c1, c2, . . . , ck)

dummy c0 = 1Candidates for ci being aware of 〈c1, c2, . . . , ci−1〉:ci−1 + 1 ≤ ci ≤ n − k + i ,∀i = 1, 2, . . . , k

Pham Quang Dung () Data structures and Algorithms Recursive algorithm Hanoi, 2012 27 / 38

BackTracking algorithm - combination

1 v o i d BackTrack ing ( i n t i ) {f o r ( i n t j = c [ i −1]+1; j <= n−k+i ; j ++){

3 c [ i ] = j ;i f ( i == k ) {

5 p r i n t C o n f i g u r a t i o n ( ) ;} e l s e

7 BackTrack ing ( i +1) ;}

9 }

11 v o i d main ( ) {c [ 0 ] = 0 ;

13 BackTrack ing ( 1 ) ;}

Pham Quang Dung () Data structures and Algorithms Recursive algorithm Hanoi, 2012 28 / 38

BackTracking algorithm - permutation

A configuration: p1, p2, . . . , pk

Candidates for pi being aware of 〈p1, p2, . . . , pi−1〉:{1, 2, . . . , n} \ {p1, p2, . . . , pi−1}Use an array of booleans for making values used b1, b2, . . . , bn

bv = 1, if value v is already used (apprear in p1, p2, . . . , pi−1)bv = 0, otherwise

Pham Quang Dung () Data structures and Algorithms Recursive algorithm Hanoi, 2012 29 / 38

BackTracking algorithm - permutation

v o i d BackTrack ing ( i n t k ) {2 f o r ( i n t i = 1 ; i <= n ; i ++){

i f ( ! b [ i ] ) {4 p [ k ] = i ;

b [ i ] = 1 ;6 i f ( k == n ) {

p r i n t C o n f i g u r a t i o n ( ) ;8 } e l s e

BackTrack ing ( k+1) ;10 b [ i ] = 0 ;

}12 }}

14

v o i d main ( ) {16 f o r ( i n t i = 1 ; i <= n ; i ++)

b [ i ] = 0 ;18 BackTrack ing ( 1 ) ;}

Pham Quang Dung () Data structures and Algorithms Recursive algorithm Hanoi, 2012 30 / 38

BackTracking algorithm - Linear integer equation

Solve the linear equations in a set of positive integers

a1x1 + a2x2 + · · ·+ anxn = M

where (ai )1≤i≤n and M are positive integers

Partial solution (x1, x2, . . . , xk−1)

m =∑k−1

i=1 aixi

A =∑n

i=k+1 ai

M = M −m − A

Candidates of xk is {v ∈ Z | 1 ≤ v ≤ Mak}

Pham Quang Dung () Data structures and Algorithms Recursive algorithm Hanoi, 2012 31 / 38

BackTracking algorithm - Linear integer equation

1 v o i d TRY( i n t k ) { // t r y a v a l u e f o r v a r i a b l e x [ k ]f o r ( i n t v a l = 1 ; v a l <= (M−m−A) /a [ k ] ; v a l++){

3 x [ k ] = v a l ;m = m + a [ k ]∗ x [ k ] ;

5 A = A − a [ k ] ;i f ( k == n ) {

7 i f (m==M)p r i n t S o l u t i o n ( ) ;

9 } e l s eTRY( k+1) ;

11 m = m − a [ k ]∗ x [ k ] ;A = A + a [ k ] ;

13 }}

15 i n t main ( i n t argc , c h a r ∗∗ a r g v ) {m = 0 ;

17 A = 0 ;f o r ( i n t i = 2 ; i <= n ; i ++)

19 A = A + a [ i ] ;TRY( 1 ) ;

21 }Pham Quang Dung () Data structures and Algorithms Recursive algorithm Hanoi, 2012 32 / 38

BackTracking algorithm - n-queens problem

Problem: Place n queens on a chess board such that no two queensattack each other

Solution model: (x1, x2, . . . , xn) where xi represents the row on whichthe queen in column i is located

Constraints:

xi 6= xj ,∀1 ≤ i < j ≤ n|xi − xj | 6= |i − j |,∀1 ≤ i < j ≤ n

Pham Quang Dung () Data structures and Algorithms Recursive algorithm Hanoi, 2012 33 / 38

BackTracking algorithm - n-queens problem

i n t x [ 1 0 0 ] ;2 i n t n ;

i n t c a n d i d a t e ( i n t k , i n t v ) {4 f o r ( i n t i = 1 ; i <= k−1; i ++)

i f ( x [ i ] == v | | abs ( x [ i ]−v )==abs ( i−k ) ) r e t u r n 0 ;6 r e t u r n 1 ;}

8 v o i d BTrack ( i n t k ) {f o r ( i n t v = 1 ; v <= n ; v++)

10 i f ( c a n d i d a t e ( k , v ) == 1) {x [ k ] = v ;

12 i f ( k == n )p r i n t S o l u t i o n ( ) ;

14 e l s eBTrack ( k+1) ;

16 }}

18 i n t main ( i n t argc , c h a r ∗∗ a r g s ) {n = 8 ;

20 BTrack ( 1 ) ;}

Pham Quang Dung () Data structures and Algorithms Recursive algorithm Hanoi, 2012 34 / 38

BackTracking algorithm - n-queens problem - refinement

Use arrays for marking forbidden cells

r [1..n]: r [i ] = false if the cells on row i are forbiddend1[1− n..n− 1]: d1[q] = false if cells (r , c) s.t. c − r = q are forbiden

in C++, indices of elements of an array cannot be negative (i.e.,indices are 0, 1, ...). Hence making a deplacement: d1[q + n − 1]instead of d1[q]

d2[2..2n − 2]: d2[q] =false if cells (r , c) s.t. r + c = q are forbiden

Pham Quang Dung () Data structures and Algorithms Recursive algorithm Hanoi, 2012 35 / 38

BackTracking algorithm - n-queens problem

1 v o i d BTrack ( i n t i ) {// t r y v a l u e s f o r x [ i ]f o r ( i n t v a l = 1 ; v a l <= n ; v a l++){

3 i f ( r [ v a l ] == t r u e && d1 [ i−v a l+n−1] == t r u e && d2 [ i+v a l ]== t r u e ) {

x [ i ] = v a l ;5 r [ v a l ] = f a l s e ; / / marking f o r b i d e n c e l l s

d1 [ i−v a l+n−1] = f a l s e ; / / marking f o r b i d e n c e l l s7 d2 [ i+v a l ] = f a l s e ; / / marking f o r b i d e n c e l l s

i f ( i == n ) {9 p r i n t S o l u t i o n ( ) ;

} e l s e11 BTrack ( i +1) ;

r [ v a l ] = t r u e ; / / r e c o v e r i n g marking13 d1 [ i−v a l+n−1] = t r u e ; / / r e c o v e r i n g marking

d2 [ i+v a l ] = t r u e ; / / r e c o v e r i n g marking15 }

}17 }

Pham Quang Dung () Data structures and Algorithms Recursive algorithm Hanoi, 2012 36 / 38

BackTracking algorithm - n-queens problem

1 i n t main ( i n t argc , c h a r ∗∗ a r g v ) {n = a t o i ( a r g v [ 1 ] ) ;

3

f o r ( i n t i = 1 ; i <= n ; i ++)5 r [ i ] = t r u e ;

f o r ( i n t i = 0 ; i <= 2∗n ; i ++){7 d1 [ i ] = t r u e ;

d2 [ i ] = t r u e ;9 }

11 BTrack ( 1 ) ;}

Pham Quang Dung () Data structures and Algorithms Recursive algorithm Hanoi, 2012 37 / 38

BackTracking algorithm - Exercises

Sudoku problem

Subset Sum problem

List all the ways to decompose a positive integer N into a sum ofpositive integers

Pham Quang Dung () Data structures and Algorithms Recursive algorithm Hanoi, 2012 38 / 38