Upcoming SlideShare
Loading in...5
×

# 03 stacks and_queues_using_arrays

4,499

Published on

2 Comments
1 Like
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
Your message goes here
• double ended queue is known as Deque
deletion on queue is known as Dequeue.
So please make the correction.

Are you sure you want to  Yes  No
Your message goes here
• good ppts of stack

Are you sure you want to  Yes  No
Your message goes here
No Downloads
Views
Total Views
4,499
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
288
Comments
2
Likes
1
Embeds 0
No embeds

No notes for slide
• All the operations on the stack are synchronized with each other.
• ### 03 stacks and_queues_using_arrays

1. 1. Stacks <ul><li>Stack is a data structure that can be used to store data which can later be retrieved in the reverse or last in first out (LIFO) order. </li></ul><ul><li>Stack is an ordered-list in which all the insertions and deletions are made at one end to maintain the LIFO order. (Stack is LIFO data structure)‏ </li></ul>
2. 3. Stacks <ul><li>The operations defined on a stack are: </li></ul><ul><ul><ul><li>Push - Store onto a stack </li></ul></ul></ul><ul><ul><ul><li>Pop - retrieve from stack </li></ul></ul></ul><ul><ul><ul><li>Top - examine the top element in the stack </li></ul></ul></ul><ul><ul><ul><li>Is_empty - check if the stack is empty </li></ul></ul></ul><ul><ul><ul><li>Is_Full - check if the stack is full </li></ul></ul></ul><ul><li>A stack can be very easily implemented using arrays. </li></ul><ul><li>Can also be implemented using linked list </li></ul><ul><li>Stack is implemented by maintaining a pointer to the top element in the stack. This pointer is called the stack pointer. </li></ul><ul><li>Linked implementation will be discussed later. </li></ul>
3. 4. Stacks – Array Implementation <ul><li>If a stack is implemented using arrays, the following two conventions can be used: </li></ul><ul><ul><li>A stack can grow upwards, i.e., from index 0 to the maximum index, or it can grow downwards, i.e., from the maximum index to index 0. </li></ul></ul><ul><ul><li>Stack pointer can point to the last element inserted into the stack or it can point to the next available position. </li></ul></ul>
4. 5. Growing Downwards Initial state: stk_ptr = MAX - 1 6 5 4 3 2 1 0 7 8 9 <ul><li>stk_ptr points to the next empty location </li></ul><ul><li>Pop – first increment stk_ptr and then take data out </li></ul>6 8 3 12 <ul><li>Push – first add data to the stack, then decrement stk_ptr </li></ul>
5. 6. Growing Downwards Initial state: stk_ptr = MAX 6 8 3 12 6 5 4 3 2 1 0 7 8 9 <ul><li>stk_ptr points to the last element added to the stack </li></ul><ul><li>Pop – first take data out and then increment stk_ptr </li></ul><ul><li>Push – first decrement the stk_ptr and then add data </li></ul>
6. 7. Growing Upwards Initial state: stk_ptr = 0 6 4 5 2 7 6 5 4 3 2 1 0 7 8 9 <ul><li>Push – first add data to the stack then increment stk_ptr </li></ul><ul><li>Pop – first decrement stk_ptr and then take data out </li></ul><ul><li>stk_ptr points to the next empty location </li></ul>
7. 8. Growing Upwards Initial state: stk_ptr = -1 6 4 5 2 7 6 5 4 3 2 1 0 7 8 9 <ul><li>Push – first increment the stk_ptr and then add data </li></ul><ul><li>Pop – first take data out and then decrement stk_ptr </li></ul><ul><li>stk_ptr points to the last element added to the stack </li></ul>
8. 9. Stacks – Array Implementation <ul><li>class Stack { </li></ul><ul><li>private: </li></ul><ul><li>int maxSize; // maximum storage </li></ul><ul><ul><ul><ul><ul><li>capacity </li></ul></ul></ul></ul></ul><ul><li>int stk_ptr; // stack pointer </li></ul><ul><li>int *stackArray; // array used to implement stack </li></ul><ul><li>public: </li></ul><ul><li>Stack(int s ); // constructor </li></ul><ul><li>~Stack() {delete [ ] stackArray; } // destructor </li></ul><ul><li>bool push (int); // add an element to the stack </li></ul><ul><li>bool pop(int &); // remove an element from stack </li></ul><ul><li>bool isFull(); // check if the stack is full </li></ul><ul><li>bool isEmpty(); // check if the stack is empty </li></ul><ul><li>}; </li></ul>
9. 10. bool Stack::push(int n)‏ { if (! isFull() ) { stackArray[stk_ptr] = n; stk_ptr = stk_ptr + 1; return true; } else return false; } bool Stack::pop(int &n)‏ { if (! IsEmpty ()) { stk_ptr = stk_ptr – 1; n = stackArray[stk_ptr]; return true; } else return false; } bool Stack::isEmpty()‏ { return (stk_ptr == 0); } Stack::Stack(int s)‏ { maxSize = s; stk_ptr = 0; stackArray = new int[maxSize]; } bool Stack::isFull()‏ { return (stk_ptr == maxSize); }
10. 11. Applications of stack <ul><li>Is used in recursion </li></ul><ul><li>Used in expression evaluation </li></ul><ul><li>Used for string comparisons </li></ul><ul><li>Used in function calling </li></ul><ul><li>Used in memory manipulation to store addresses </li></ul><ul><li>Used in tree manipulation </li></ul><ul><li>etc. </li></ul>
11. 12. Multiple Stack <ul><li>More than one stacks can be implemented on a single one dimensional array </li></ul><ul><li>Size of each array can be equal or different </li></ul><ul><li>Number of arrays can be fixed or varying </li></ul><ul><li>Simple implementation consists of two arrays of . Size of each array can not be predicted </li></ul>
12. 13. Queues <ul><li>Queue is a data structure that can be used to store data which can later be retrieved in the first in first out (FIFO) order. </li></ul><ul><li>Queue is an ordered-list in which all the insertions and deletions are made at two different ends to maintain the FIFO order. </li></ul><ul><li>The operations defined on a Queue are: </li></ul><ul><ul><ul><li>Add/ Insert - Store onto a Queue </li></ul></ul></ul><ul><ul><ul><li>Remove/ Delete - retrieve (delete) from Queue </li></ul></ul></ul><ul><ul><ul><li>isEmpty - check if the Queue is empty </li></ul></ul></ul><ul><ul><ul><li>isFull - check if the Queue is full </li></ul></ul></ul><ul><li>A Queue can be very easily implemented using arrays. </li></ul><ul><li>Queue is implemented by maintaining one pointer to the front element in the Queue and another pointer pointing to the rear of the Queue. </li></ul><ul><li>Insertions are made at the rear and deletions are made from the front . </li></ul>
13. 14. Uses of Queue <ul><li>Many real life applications ( banks, bill paying in shopping malls, etc ) </li></ul><ul><li>Process Scheduling </li></ul><ul><li>Memory Management </li></ul><ul><li>Trees traversing </li></ul><ul><li>etc </li></ul>
14. 15. Queues – Array Implementation <ul><li>class Queue { </li></ul><ul><li>public: </li></ul><ul><li>Queue(int s = 10); // constructor - default size = 10 </li></ul><ul><li>~Queue() {delete [ ] QueueArray; } // destructor </li></ul><ul><li>bool add (int n); </li></ul><ul><li>bool remove (int &n); </li></ul><ul><li>bool isFull() {return size == MaxSize;} </li></ul><ul><li>bool isEmpty() {return size == 0; } </li></ul><ul><li>private: </li></ul><ul><li>int maxSize; // max Queue size </li></ul><ul><li>int front, rear; </li></ul><ul><li>int *QueueArray; </li></ul><ul><li>int size; // no. of elements in the Queue </li></ul><ul><li>}; </li></ul>
15. 16. <ul><li>Queue::Queue(int s)‏ </li></ul><ul><li>{ </li></ul><ul><li>if (s <= 0) maxSize = 10; else maxSize = s; </li></ul><ul><li>QueueArray = new int[maxSize]; </li></ul><ul><li>size = 0; </li></ul><ul><li>rear = 0; // points to the last element </li></ul><ul><li>front = 0; // points to first element </li></ul><ul><li>} </li></ul>
16. 17. <ul><li>bool Queue::add(int n)‏ </li></ul><ul><li>{ </li></ul><ul><li>if (! isFull() ) { </li></ul><ul><li>QueueArray[rear] = n; </li></ul><ul><li>rear++; </li></ul><ul><li>size++; </li></ul><ul><li>return true; </li></ul><ul><li>} </li></ul><ul><li>else return false; </li></ul><ul><li>} </li></ul>
17. 18. <ul><li>bool Queue::remove(int &n)‏ </li></ul><ul><li>{ </li></ul><ul><li>if (! isEmpty() { </li></ul><ul><li>n = QueueArray[front]; </li></ul><ul><li>front++; </li></ul><ul><li>size--; </li></ul><ul><li>return true; </li></ul><ul><li>} </li></ul><ul><li>else return false; </li></ul><ul><li>} </li></ul>
18. 19. <ul><li>Assume maxSize = 5 </li></ul><ul><li>Initial condition </li></ul><ul><ul><li>size = 0; front = 0; rear = 0; </li></ul></ul><ul><li>Add 3, remove 1 </li></ul><ul><ul><li>size = 2; front = 1; rear = 3; </li></ul></ul>
19. 20. <ul><li>Question: Is the Queue Full? </li></ul><ul><li>Add 2 more, remove 1 more </li></ul><ul><ul><li>size = 3; front = 2; rear = 5; </li></ul></ul><ul><li>Where to add the next element? </li></ul><ul><li>Push everything back </li></ul><ul><ul><li>size = 3; front = 0; rear = 3; </li></ul></ul><ul><li>Cost? </li></ul><ul><li>O (size)‏ </li></ul>
20. 21. Circular Implementation <ul><li>bool Queue::add(int n)‏ </li></ul><ul><li>{ </li></ul><ul><li>if (! isFull() ) { </li></ul><ul><li>QueueArray[rear] = n; </li></ul><ul><li>rear++; </li></ul><ul><li>if (rear == maxSize) </li></ul><ul><li>rear = 0; </li></ul><ul><li>size++; </li></ul><ul><li>return true; </li></ul><ul><li>} </li></ul><ul><li>else return false; </li></ul><ul><li>} </li></ul>0 1 2 3 MS-1
21. 22. Circular Implementation bool Queue::remove(int &n)‏ { if (! isEmpty() { n = QueueArray[front]; front++; if (front == maxSize) front = 0; size--; return true; } else return false; } 0 1 2 3 MS-1
22. 23. <ul><li>bool Queue::add(int n)‏ </li></ul><ul><li>{ </li></ul><ul><li>if (! isFull() ) { </li></ul><ul><li>QueueArray[rear] = n; </li></ul><ul><li>rear++; </li></ul><ul><li>if (rear == maxSize) </li></ul><ul><li>rear = 0; </li></ul><ul><li>size++; </li></ul><ul><li>return true; </li></ul><ul><li>} </li></ul><ul><li>else return false; </li></ul><ul><li>} </li></ul>bool Queue::isFull() {return size == MaxSize;} bool Queue::isEmpty() {return size == 0; } Queue::Queue(int s)‏ { if (s <= 0) MaxSize = 10; else MaxSize = s; QueueArray = new int[MaxSize]; size = 0; rear = 0; front = 0; } bool Queue::remove(int &n)‏ { if (! isEmpty() ) { n = QueueArray[rear]; front++; if (front == maxSize) front = 0; size--; return true; } else return false; } 0 1 2 3 MS-1
23. 24. <ul><li>bool Queue::add(int n)‏ </li></ul><ul><li>{ </li></ul><ul><li>if (! isFull() ) { </li></ul><ul><li>QueueArray[rear] = n; </li></ul><ul><li>rear++; </li></ul><ul><li>if (rear == maxSize) </li></ul><ul><li>rear = 0; </li></ul><ul><li>size++; </li></ul><ul><li>return true; </li></ul><ul><li>} </li></ul><ul><li>else return false; </li></ul><ul><li>} </li></ul>bool Queue::remove(int &n)‏ { if (! isEmpty() { n = QueueArray[front]; front++; if (front == maxSize) front = 0; size--; return true; } else return false; } Add: rear = (rear + 1) % maxSize; Remove: front = (front + 1) % maxSize; What happens if we try to do it clockwise? 0 1 2 3 MS-1
24. 25. Double Ended Queue (Dequeue) <ul><li>We can insert at or delete from queue at either end </li></ul><ul><li>Can be used as queue as well as stack </li></ul><ul><li>Two types - Input restricted dequeue </li></ul><ul><li>- output restricted dequeue </li></ul>
25. 26. Input restricted dequeue (algorithm) <ul><li>remove (queue, n, f, r, side) </li></ul><ul><li>If (f= -1 or r= -1) then </li></ul><ul><li>print (“queue is empty”) </li></ul><ul><li>return (dummy value) </li></ul><ul><li>If (f = r) then </li></ul><ul><li>n = queue [f] </li></ul><ul><li>f = -1, r = -1 </li></ul><ul><li>If (side = ‘front) then </li></ul><ul><li>n = queue [f] </li></ul><ul><li>f = f + 1 </li></ul><ul><li>If (side = ‘rear’) then </li></ul><ul><li>n = queue [r] </li></ul><ul><li>r = r – 1 </li></ul><ul><li>5. Return (n) </li></ul>
26. 27. Output restricted dequeue (algorithm) <ul><li>Add( queue, f, r, n, size, side) </li></ul><ul><li>1. If (side = ‘front’ and f = 0) or (side = ‘rear’ and r = size-1) </li></ul><ul><li>print (“ queue full”) </li></ul><ul><li>return </li></ul><ul><li>2. If (side = ‘front’) then </li></ul><ul><li>if ( f = -1) then </li></ul><ul><li>f = 0, r = 0 </li></ul><ul><li>else </li></ul><ul><li>f = f – 1 </li></ul><ul><li>queue [f] = n </li></ul><ul><li>3. If (side = ‘rear’) then </li></ul><ul><li>r = r + 1 </li></ul><ul><li>queue [r] = n </li></ul><ul><li>if (f = -1) then </li></ul><ul><li>f = 0, r = 0 </li></ul><ul><li>4. return </li></ul>
27. 28. Priority Queue <ul><li>Items are inserted according to priority </li></ul><ul><li>Priority determines the order in which items exists in the queue </li></ul><ul><li>Simple example is mail sorting, process control systems </li></ul><ul><li>Ascending Priority Queue ( queue which remove lowest priority item first) </li></ul><ul><li>Descending priority Queue (queue which remove highest priority item first) </li></ul><ul><li>Implementation is like multiple stack i.e more than one queues can exist on a single one dimensional array </li></ul>
28. 29. Applications of Stacks Evaluation of Expression <ul><li>Expressions are of three types 1. infix 2. prefix 3. postfix </li></ul><ul><li>Infix ( a + b * c - d ) </li></ul><ul><li>Prefix ( - + a * b c d ) </li></ul><ul><li>Postfix ( a b c * + d - ) </li></ul><ul><li>Evaluation of expression like a+b/c*(e-g)+h-f*i was a challenging task for compiler writers. </li></ul><ul><li>It is a problem of parenthesization of the expression according to operator precedence rule. </li></ul><ul><li>A fully parenthesized expression can be evaluated with the help of a stack. </li></ul>
29. 30. Algorithm to Evaluate fully Parenthesized Expressions <ul><li>while (not end of expression) do </li></ul><ul><ul><li>get next input symbol </li></ul></ul><ul><ul><li>if input symbol is not “)” </li></ul></ul><ul><ul><ul><li>push it into the stack </li></ul></ul></ul><ul><ul><li>else </li></ul></ul><ul><ul><ul><li>repeat </li></ul></ul></ul><ul><ul><ul><ul><li>pop the symbol from the stack </li></ul></ul></ul></ul><ul><ul><ul><li>until you get “(“ </li></ul></ul></ul><ul><ul><ul><li>apply operators on the operands </li></ul></ul></ul><ul><ul><ul><li>push the result back into stack </li></ul></ul></ul><ul><li>end while </li></ul><ul><li>the top of stack is the answer </li></ul>
30. 31. Evaluation of Fully Parenthesized Expression (a+(b/c)) Assuming a=2, b=6, c=3 Pop”(a+2” and evaluate and push the result back 4 )‏ Pop”(b/c” and evaluate and push the result back (a+2 )‏ Push (a+(b/c c push (a+(b/ / push (a+(b b push (a+( ( push (a+ + push (a a Push ( ( Remarks Stack Input Symbol
31. 32. Evaluation of Expressions <ul><li>The normal way of writing expressions i’.e., by placing a binary operator in-between its two operands, is called the infix notation. </li></ul><ul><li>It is not easy to evaluate arithmetic and logic expressions written in infix notation since they must be evaluated according to operator precedence rules. E.g., a+b*c must be evaluated as (a+(b*c)) and not ((a+b)*c) . </li></ul><ul><li>The postfix or Reverse Polish Notation (RPN) is used by the compliers for expression evaluation. </li></ul><ul><li>In RPN, each operator appears after the operands on which it is applied. This is a parenthesis-free notation. </li></ul><ul><li>Stacks can be used to convert an expression from its infix form to RPN and then evaluate the expression. </li></ul>
32. 33. INFIX and POSTFIX abc/eg+*+h+fi*- a+b/c*(e+g)+h-f*i ab/c-de*+ac*- a/b-c+d*e-a*c ab+cd+*e/f- (a+b)*(c+d)/e-f ab*cd*+ a*b+c*d abc*+ a+b*c Postfix Infix
33. 34. Algorithm to Evaluate Expressions in RPN <ul><li>while (not end of expression) do </li></ul><ul><ul><li>get next input symbol </li></ul></ul><ul><ul><li>if input symbol is an operand then </li></ul></ul><ul><ul><ul><li>push it into the stack </li></ul></ul></ul><ul><ul><li>else if it is an operator then </li></ul></ul><ul><ul><ul><li>pop the operands from the stack </li></ul></ul></ul><ul><ul><ul><li>apply operator on operands </li></ul></ul></ul><ul><ul><ul><li>push the result back onto the stack </li></ul></ul></ul><ul><li>End while </li></ul><ul><li>the top of stack is answer. </li></ul>
34. 35. Algorithm to Evaluate Expressions in RPN (a+b)*(c+d)  ab+cd+* Assuming a=2, b=6, c=3, d=-1 Pop 8 and 2 from the stack, multiply, and push the result back. Since this is end of the expression, hence it is the final result. 16 * Pop c and d from the stack, add, and push the result back 8 2 + Push 8 c d d Push 8 c c Pop a and b from the stack, add, and push the result back 8 + Push a b b Push a a Remarks Stack Input Symbol
35. 36. <ul><li>Initialize an empty stack of operators </li></ul><ul><li>While not end of expression do </li></ul><ul><ul><li>Get the next input token </li></ul></ul><ul><ul><li>If token is </li></ul></ul><ul><ul><ul><li>“ (” push </li></ul></ul></ul><ul><ul><ul><li>“ )” pop and display stack element until a </li></ul></ul></ul><ul><ul><li>left parenthesis is encountered, but do </li></ul></ul><ul><ul><li>not display it. </li></ul></ul><ul><ul><ul><li>. An operator: if stack is empty or token has higher </li></ul></ul></ul><ul><ul><ul><li>precedence than the element at TOS, push </li></ul></ul></ul><ul><ul><ul><li>Note: “(” has the lowest precedence </li></ul></ul></ul><ul><ul><ul><li>else then pop from the stack all the operators with precedence higher than or equal to the scanned symbol and display until “(“ is found or an operator with less precedence is found. </li></ul></ul></ul><ul><ul><ul><li>. An Operand: Display </li></ul></ul></ul>Algorithm for Infix to RPN Conversion
36. 37. Converting Infix to RPN (a+b)*(c+d)  ab+cd+* Pop remaining symbols from the stack and display RPN  a b + c d + * End of input Pop till “(” is found and display – RPN  a b + c d + * )‏ Operand – display – RPN  a b + c d * ( + d Push as + has higher precedence than ( * ( + + Operand – display – RPN  a b + c * ( c Push * ( ( Push as stack is empty * * Pop till “(” is found and display – RPN  a b + )‏ Operand – display – RPN  a b ( + b Push as + has higher precedence than ( ( + + Operand – display – RPN  a ( a Push ( ( Remarks Stack Input Symbol
37. 38. a+b*c/(d+e)  a b c * d e + / + Pop till “(” is found – RPN  a b c * d e + + / )‏ Pop remaining symbols from the stack and display RPN  a b c * d e + / + End of input Operand – display – RPN  a b c * d e + / ( + e Push as + has higher precedence than ( + / ( + + Operand – display – RPN  a b c * d + / ( d Push + / ( ( Pop * and push / as * and / have the same precedence but / has higher precedence than + – RPN  a b c * + / / Operand – display – RPN  a b c + * c Push as * has higher precedence than + + * * Operand – display – RPN  a b + b Push as stack is empty + + Operand – display – RPN  a a Remarks Stack Input Symbol
38. 39. Use of Stack in the Implementation of Subprograms <ul><li>In a typical program, the subprogram calls are nested. </li></ul><ul><li>Some of these subprogram calls may be recursive. </li></ul><ul><li>Address of the next instruction in the calling program must be saved in order to resume the execution from the point of subprogram call. </li></ul><ul><li>Since the subprogram calls are nested to an arbitrary depth, use of stack is a natural choice to preserve the return address. </li></ul>
39. 40. Activation Records <ul><li>An activation record is a data structure which keeps important information about a sub program. </li></ul><ul><li>In modern languages, whenever a subprogram is called, a new activation record corresponding to the subprogram call is created, and pushed into the stack. </li></ul><ul><li>The information stored in an activation record includes the address of the next instruction to be executed, and current value of all the local variables and parameters. i.e. the context of a subprogram is stored in the activation record. </li></ul><ul><li>When the subprogram finishes its execution and returns back to the calling function, its activation record is popped from the stack and destroyed-restoring the context of the calling function. </li></ul>
40. 41. Activation Records
41. 42. Recursive Functions <ul><li>Recursion is a very powerful algorithm design tool. </li></ul><ul><li>A subprogram is called recursive if it directly or indirectly calls itself. </li></ul><ul><li>A recursive program has two parts: </li></ul><ul><ul><ul><li>The End Condition. </li></ul></ul></ul><ul><ul><ul><li>The Recursive Step. </li></ul></ul></ul><ul><li>The end condition specifies where to stop. </li></ul><ul><li>With each recursive step you should come closer to the end condition. </li></ul><ul><li>In other words, with a recursive step, you apply the same algorithm on a scaled down problem and its process is repeated until the end of condition is reached. </li></ul>
42. 43. Recursion – Some Examples <ul><li>int linear_search (int a[], int from, int to, int key)‏ </li></ul><ul><li>{ </li></ul><ul><ul><li>if (from <= to) { //end condition </li></ul></ul><ul><ul><ul><li>if (key == a[from]) return from; </li></ul></ul></ul><ul><ul><ul><li>//end condition </li></ul></ul></ul><ul><ul><ul><li>else </li></ul></ul></ul><ul><ul><ul><ul><li>return linear_search(a, from+1, to, key) </li></ul></ul></ul></ul><ul><ul><ul><ul><li>//recursive step </li></ul></ul></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>else </li></ul></ul><ul><ul><ul><li>return -1; </li></ul></ul></ul><ul><li>} </li></ul>
43. 44. <ul><li>int binary_search (int a[], int high, int low, int key)‏ </li></ul><ul><li>{ </li></ul><ul><li>int mid = (high + low)/2; </li></ul><ul><li>if (high >=low) { </li></ul><ul><li>if (key == a[mid]) return mid; //end condition </li></ul><ul><li>else if (key < a[mid])‏ </li></ul><ul><li>return binary_search(a, mid -1, low, key); //recursive step </li></ul><ul><li>else </li></ul><ul><li>return binary_search(a, high, mid + 1, key); //recursive step </li></ul><ul><li>} </li></ul><ul><li>else </li></ul><ul><li>return-1; // end condition </li></ul><ul><li>} </li></ul>
44. 45. How Does Recursion Work Some More Recursive Functions and Their Simulation <ul><li>int factorial (int n)‏ </li></ul><ul><li>{ </li></ul><ul><li>int temp; </li></ul><ul><ul><li>if (n<0) return 0; //end condition </li></ul></ul><ul><ul><li>else if (n <=1) return 1; //end condition </li></ul></ul><ul><ul><li>else { </li></ul></ul><ul><ul><li>temp = factorial(n-1); //recursive step </li></ul></ul><ul><ul><li>return n*temp; </li></ul></ul><ul><li>} </li></ul><ul><li>} </li></ul>
45. 46. Simulation of factorial(4)‏ <ul><li>if (n<0) return 0; </li></ul><ul><li>else if (n <=1) return 1; </li></ul><ul><li>else { </li></ul><ul><li>temp = factorial (n-1); </li></ul><ul><li>return n*temp; </li></ul><ul><ul><li>} </li></ul></ul>n = 4 1. 2. 3. 4. n = 3 1. 2. 3. 4. n = 2 1. 2. 3. 4. n = 1 1. 2. return 1 temp = 1 5. return 2*1 1. 2. return 1 temp = 2 5. return 3*2 1. 2. return 1 temp = 6 5. return 4*6 1. 2. return 1
46. 47. Fibonacci Sequence 0,1,1,2,3,5,8,13,… <ul><li>int fibonacci (int n)‏ </li></ul><ul><li>{ </li></ul><ul><li>int temp1, temp2; </li></ul><ul><li>if (n<=0) return 0; //end condition </li></ul><ul><li>else if (n<=2) return 1; //end condition </li></ul><ul><li>else{ </li></ul><ul><ul><li>temp1 = fibonacci(n - 1); // recursive step </li></ul></ul><ul><li> temp2 = fibonacci(n - 2); //recursive step </li></ul><ul><li> return temp1 + temp2; // same as return fib(N-1)+fib(N-2)‏ </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
47. 48. Simulation of fibonacci(4)‏ <ul><li>if (n<=0) return 0; </li></ul><ul><li>else if (n<=2) return 1; </li></ul><ul><li>else { </li></ul><ul><li>temp1 = fibonacci(n - 1); </li></ul><ul><li> temp2 = fibonacci(n - 2); </li></ul><ul><li> return temp1 + temp2; </li></ul><ul><li>} </li></ul>n = 2 <ul><li>1. </li></ul><ul><li>return 1 </li></ul>n = 4 1. 2. 3. n = 3 1. 2. 3. temp1 = 1 4. 1. 2. return 1 n = 1 1. 2. return 1 temp2 = 1 5. return 2 1. 2. return 1 temp1 = 2 4. 1. 2. return 1 n = 2 1. 2. return 1 temp2 = 1 5. return 3 1. 2. return 1
48. 49. Tower of Honoi <ul><li>Invented by French mathematician Lucas in 1880s. </li></ul><ul><li>In the town of Honoi, monks are playing a game with: </li></ul><ul><ul><li>3 diamond needles fixed on a brass plate. </li></ul></ul><ul><ul><li>One needle contains 64 pure gold disks of different diameters. </li></ul></ul><ul><ul><li>Plates are put on top of each other in the order of their diameters with the largest plate lying at the bottom on the brass plate. </li></ul></ul><ul><li>Priests are supposed to transfer all the disks from one needle to the other such that: </li></ul><ul><ul><li>Only one disk can be moved at a time. </li></ul></ul><ul><ul><li>At no time a disk of larger diameter should be put on a disk of smaller diameter. </li></ul></ul>
49. 50. From Using To 3 Disks <ul><li>How much time would it take? Estimate….. </li></ul><ul><li>According to the legend, that will mark the end of time! </li></ul>
50. 51. Tower of Honoi Recursive Solution <ul><li>End Condition: </li></ul><ul><li>Recursive Step: </li></ul>
51. 52. Tower of Honoi <ul><li>void TOH (int from, int to, int using, int n)‏ </li></ul><ul><li>{ </li></ul><ul><li>if (n>0) // end condition – stop when there is // nothing to be moved </li></ul><ul><li>{ </li></ul><ul><li>TOH (from, using, to, n-1); </li></ul><ul><li>// recursive step </li></ul><ul><li>// move n-1 plates from the starting </li></ul><ul><li>// disk to the auxiliary disk </li></ul><ul><li>move (to, from); </li></ul><ul><li>// move the nth disk to the destination </li></ul><ul><li>TOH (using, to, from, n-1); </li></ul><ul><li>// recursive step </li></ul><ul><li>// move n-1 plates from the auxiliary </li></ul><ul><li>// disk to the destination disk </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
52. 53. Simulation of TOH(1, 2, 3, 3)‏ <ul><li>if (n>0) </li></ul><ul><li>{ </li></ul><ul><li>TOH (from, using, to, n-1); </li></ul><ul><li>move (to, from); </li></ul><ul><li>TOH (using, to, from, n-1); </li></ul><ul><li>} </li></ul>1 2 3 from:1, to:2, using:3, n:3 f = 1, t = 2, u = 3, n = 3 statement: 3 f = 1, t = 2, u = 3, n = 1 statement: 3 f = 1, t = 3, u = 2, n = 2 statement: 3 f = 1, t = 3, u = 2, n = 0 statement: 1 f = 1, t = 2, u = 3, n = 1 statement: 4 f = 1, t = 2, u = 3, n = 1 statement: 5 f = 3, t = 2, u = 1, n = 0 statement: 1 f = 1, t = 3, u = 2, n = 2 statement: 4 f = 1, t = 3, u = 2, n = 2 statement: 5 f = 2, t = 3, u = 1, n = 1 statement: 3 f = 2, t = 1, u = 3, n = 0 statement: 1 f = 2, t = 3, u = 1, n = 1 statement: 4 f = 2, t = 3, u = 1, n = 1 statement: 5 f = 1, t = 3, u = 2, n = 0 statement: 1 f = 1, t = 2, u = 3, n = 3 statement: 4 f = 1, t = 2, u = 3, n = 3 statement: 5 f = 3, t = 2, u = 1, n = 2 statement: 3 f = 3, t = 1, u = 2, n = 1 statement: 3 f = 3, t = 2, u = 1, n = 0 statement: 3 f = 3, t = 1, u = 2, n = 1 statement: 4 f = 3, t = 1, u = 2, n = 1 statement: 5 f = 2, t = 1, u = 3, n = 0 statement: 1 f = 3, t = 2, u = 1, n = 2 statement: 4 f = 3, t = 2, u = 1, n = 2 statement: 5 f = 1, t = 2, u = 3, n = 1 statement: 3 f = 1, t = 3, u = 2, n = 0 statement: 1 f = 1, t = 2, u = 3, n = 1 statement: 4 f = 3, t = 2, u = 1, n = 1 statement: 5 f = 1, t = 2, u = 3, n = 0 statement: 5
1. #### A particular slide catching your eye?

Clipping is a handy way to collect important slides you want to go back to later.