{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Fibonacci sequence - -if its not a legal move return...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
Fibonacci sequence Int fib(int n) { if(n==1||n==2) return 1; else return fib(n-1)+fib(n-2); } Fibonacci – iterative Int fib(int n) { if(n==1||n==2) return 1; else{ int f1=1, f2=2, fnew; for(int I<3, I<=n; I++){ fnew=f1+f2; f2=f1; f1=fnew; } return fnew; } } Let T(n) be the time to compute fib(n) and that T(1)=T(2)=1 T(3)=T(1) + T(2) +k . . . T(n)=2fib(n)-1 How does backtracking work? -we attempt a move
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: -if its not a legal move, return false-if it is legal, make the move(asterisk) -see if it leads to a soltion-recursively try all possible “next” moves until-1 a solution is found-2 all moves are exhausted without a solution-in case of 2, this implies no solution from the current point, so. .-erase this move-return false...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online