Stacks New
Stacks New
Stacks
Akashdeep
Stack
Stack is an ordered collection of items into which new items may be inserted and from
which items may be deleted from one end, called top of stack.
Two basic operations: Push and Pop
push
push
depth
pop (returned)
pop (returned)
1
9/24/21
2
9/24/21
push
push
pop (returned)
peek (returned)
3
9/24/21
Stack Implementation in C
datatype list[ ];
void push (datatype element);
datatype pop( );
datatype peek( );
boolean isEmpty( );
main
{
call different functions;
}
4
9/24/21
class Stack
{
Private:
datatype list[];
int top;
Public:
void push (datatype element);
datatype pop( );
datatype peek( );
boolean isEmpty( );
}
9
10
10
5
9/24/21
Push(Stack,Top,Maxstk,Item)
1. [Stack already filled?]
If Top=Maxstk then Print Overflow and Return;
2. Set Top=Top+1 [Increase top by 1]
3. Set Stack[Top]:= Item [Insert Item in new Top
position]
4. Return
11
12
12
6
9/24/21
13
13
14
7
9/24/21
Item = LIST[FirstFree-1].
Firstfree = Firstfree-1
return (Item)
15
15
16
16
8
9/24/21
17
17
18
9
9/24/21
Header Node
19
19
20
20
10
9/24/21
21
PUSH_LINKSTACK(INFO,LINK,TOP,AVAIL,ITEM)
22
11
9/24/21
POP_LINKSTACK(INFO,LINK,TOP,AVAIL,ITEM)
23
APPLICATIONS OF STACK
24
12
9/24/21
25
26
13
9/24/21
27
28
14
9/24/21
10 } b
a=3
29
c = 15
b=5
a=3
Return to LINE 3 c = 15
Clear Stack
b b b
a=3 a=3 a=3
CIS 068
30
15
9/24/21
c = 15
b b = 15
a=3 a=3
Temporary storage
CIS 068
31
32
16
9/24/21
Recursion
33
Recursion
When you turn that into a program, you end up with functions
that call themselves:
Recursive Functions
34
17
9/24/21
RECURSION
Suppose P is a procedure containing either a Call
statement to itself. Then P is called a recursive
procedure.
Recursive procedure must have the following
properties:
1. There must be certain criteria, called base criteria,
for which the procedure does not call itself.
2. Each time the procedure does call itself, it must be
closer to the base criteria.
35
Space
occupied
36
18
9/24/21
Time Complexity
• T(n) = 1 if n ==0
• T(n) = T(n-1) + 1 if n >=1
• T(n-1) = T (n-2) +1
T(n-2) = T (n-3) +1
Backward Substituting:
• T(n)= T ((n-2) +1 ) + 1
= T ( n-2) + 2
= T (( n-3) +1) +2
= T (n-3) + 3
……
= T ( n-k) + k
• We need to omit term of T . We know that the T ‘s term will vanish when recursion will
stops and it will stop when n-k=0 or n=k
• Substituting value of k in the equation we get
= T(n-n) + n
= T(0) + n
=1+n
Or O(n)
37
Another Example
39
19
9/24/21
40
41
20
9/24/21
Iterative Factorial
Iterative definition
N! = N * (N-1) * (N-2) * … * 3 * 2 * 1
42
42
Recursive Factorial
Recursive N! definition
N! = 1 if N = 0
= N * (N-1)! Otherwise
int factorial ( int n)
{
if (n == 0)
return (1);
return (n * factorial(n - 1) );
}
43
43
21
9/24/21
Iterative Recursive
1. 2 local variables 1. No local variables
2. 3 statements 2. One statement
3. Saves solution in an 3. Returns result in
intermediate variable single expression
44
44
45
45
22
9/24/21
Recursion
What’s behind this function ?
46
Factorial
Factorial:
a! = 1 * 2 * 3 * ... * (a-1) * a
Note:
a! = a * (a-1)!
remember:
...splitting up the problem into a smaller problem of the same type...
a!
a * (a-1)!
a-1 a-2!
47
23
9/24/21
48
a=1
Return to L4
a=2
Return to L4
a=3
Return to L4
a=4 a=4
Return to L4 Return to L4
a=5 a=5
… a=5
Initial After 1 recursion After 4th recursion
24
9/24/21
a=1
Return to L4
a=2 a = 2*1 = 2
Return to L4 Return to L4
a=3 a=3 a = 3*2 = 6
Return to L4 Return to L4 Return to L4
a=4 a=4 a=4 a = 4*6 = 24
Return to L4 Return to L4 Return to L4 Return to L4
a=5 a=5 a=5 a=5 a = 5*24 = 120
After 4 recursion
th Result
50
51
25
9/24/21
Solution
The recursive algorithms we write generally consist of an if statement:
IF
the stopping case is reached solve it
ELSE
split the problem into simpler cases using recursion
Solution on stack
Solution on stack
Solution on stack
52
53
26
9/24/21
54
54
55
55
27
9/24/21
56
56
57
57
28
9/24/21
For example,
((())())
58
58
59
59
29
9/24/21
60
61
30
9/24/21
Applications of STACK
• If Q is any arithmetic expression, find the value of Q by using
reverse polish notation.
• Polish Notation
- infix notation is A + B, C- D
- Polish Notation is + A B, - C D
62
63
31
9/24/21
Applications of Stacks 64
64
65
32
9/24/21
66
66
67
33
9/24/21
STACK
5
5,6
30
30,10
20
68
68
69
69
34
9/24/21
70
Example : Q:- A + ( B * C – ( D / E ^ F ) * G ) * H
71
71
35
9/24/21
1 A ( A Q:- A + ( B * C – ( D / E ^ F ) * G ) * H )
2 + ( + A
3 ( ( + ( A
4 B ( + ( A B
5 * ( + (* A B
6 C ( + ( * A B C
7 - ( + ( - A B C *
8 ( ( + ( -- ( A B C *
9 D ( + ( -- ( A B C * D
10 / ( + ( -- ( / A B C * D
11 E ( + ( -- ( / A B C * D E
12 ^ ( + ( -- ( / ^ A B C * D E
13 F ( + ( -- ( / ^ A B C * D E F
14 ) ( + ( -- A B C * D E F ^ /
15 * ( + ( -- * A B C * D E F ^ /
16 G ( + ( -- * A B C * D E F ^ / G
17 ) ( + A B C * D E F G * --
18 * ( + * A B C * D E F ^ / G * --
19 H ( + * A B C * D E F ^ / G * -- H
20 ) A B C * D E F ^ / G * -- H * +
72
MERGE SORT
74
74
36
9/24/21
Merging
• Merging is the
process of
combining two or
more sorted
arrays into a third
sorted array.
75
75
Merging
76
76
37
9/24/21
Merging
77
77
Merging
78
78
38
9/24/21
Merging
79
79
Merging
merge( A,p,q,r) // merge procedure
{
n1=q-p+1;
n2=r-q;
//Let L[1,2,...n+1] and R[1 to n2+1] be new arrays
for(i=1 to n1)
L[i]=A[p+i-1]
for (j=1 to n2)
R[j]=A[q+j]
L[n1+1]= INFY
R[n2+1]= INFY
i=1, j=1
for ( k= p to r)
{ if (L[i]<=R[j])
A[k]=L[i]
i=i+1;
else
A[k]=R[j]
j=j+1;
}
}
80
39
9/24/21
81
Algorithm:
– Divide: If S has at least two elements (nothing needs to be
done if S has zero or one elements, just return S),
– remove all the elements from S and put them into two
sequences, S1 and S2, each containing about half of the
elements of S. (i.e. S1 contains the first én/2ù elements and
S2 contains the remaining ën/2û elements.
– Recur: Recursively sort sequences S1 and S2.
– Conquer: Put back the elements into S by merging the
sorted sequences S1 and S2 into a sorted sequence.
82
82
40
9/24/21
5 1 4 2 10 3 9 15
5 1 4 2 10 3 9 15
5 1 4 2 10 3 9 15
5 1 4 2 10 3 9 15
83
83
1 2 3 4 5 9 10 15
1 2 4 5 3 9 10 15
1 5 2 4 3 10 9 15
5 1 4 2 10 3 9 15
84
84
41
9/24/21
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2
85
18 26 32 6 43 15 9 1 6 18 26 32 1 9 15 43
43
18 26 32 6 43 15 9 1 18 26 6 32 15 43 1 9
18 26 32 6 43 15 9 1 18 26 32 6 43 15 9 1
18 26 32 6 43 15 9 1
Comp 122
86
42
9/24/21
merge_sort(A,p,r)
{
if p<r
{
q=(p+r)/2;
merge_sort(A,p,q)
merge_sort(A,q+1,r)
merge(A,p,q,r)
}
}
87
88
43
9/24/21
First Step
89
Second Step
90
44
9/24/21
91
92
45
9/24/21
Space required
• Extra space is required for merging as merge will need
extra space.
• Once merge is active no other copy of merge is active
simultaneously.
• Extra space is equal to number of elements = O(n)
• Space required for function calls.
• Total function calls = 16 in last case but only (4) equal to
height of tree calls are active
• Genrally number of levels in stack is log(n)+1
• Size of stack is log(n) +1
• Total space = O(n) + O(log(n)) ~ O(n)
93
94
46
9/24/21
95
97
97
47
9/24/21
98
98
99
99
48
9/24/21
100
100
101
101
49
9/24/21
QuickSort- Recursive
102
QuickSort
The Pseudo-Code
103
50
9/24/21
104
105
51
9/24/21
106
107
52
9/24/21
• = log n10/9
108
Towers of Hanoi
• Move n (4) disks from pole A to pole C
• such that a disk is never put on a smaller disk
AA B
B C
C
109
53
9/24/21
110
A B C
111
54
9/24/21
Figure a and b
a) The initial state; b) move n - 1 disks from A to C
112
Figure c and d
c) move one disk from A to B; d) move n - 1 disks from C to B
113
55
9/24/21
Hanoi towers
void solveTowers(int count, char source,
char destination, char spare)
{
if (count == 1)
{
cout<< "Move top disk from pole " <<source<< " \t---->"<<destination<<"\n";
}
else
{
solveTowers(count-1, source, spare, destination); // X
solveTowers(1, source, destination, spare); // Y
solveTowers(count-1, spare, destination, source); // Z
} // end if
114
} // end solveTowers
Recursion tree:
The order of recursive calls that results from solveTowers(3,A,B,C)
115
56
9/24/21
Figure 2.21a
Box trace of solveTowers(3, ‘A’, ‘B’, ‘C’)
A B C
A B C
A B C
A B C
116
Figure 2.21b
Box trace of solveTowers(3, ‘A’, ‘B’, ‘C’)
A B C
A B C
A B C
A B C
117
57
9/24/21
Figure 2.21c
Box trace of solveTowers(3, ‘A’, ‘B’, ‘C’)
A B C
A B C
A B C
A B C
118
Figure 2.21d
Box trace of solveTowers(3, ‘A’, ‘B’, ‘C’)
A B C
119
58
9/24/21
Figure 2.21e
Box trace of solveTowers(3, ‘A’, ‘B’, ‘C’)
120
121
59
9/24/21
T(n) = 2*T(n-1) + 1
• T(n) = 2 * ( 2 * T(n-2) + 1) + 1
• T(n) = (2 ^ 2) * T(n-2) + 2^1 + 2^0
• T(n) = (2^k) * T(n-k) + 2^(k-1) + 2^(k-2) + ... +
2^0
• Solving this the closed from comes out to be
• T(n) = (2^n) - 1 with T(0) = 0
122
Space Complexity:
• Space for parameter for each call is independent of n
i.e., constant. Let it be k . When we do the 2nd
recursive call 1st recursive call is over . So, we can
reuse the space of 1st call for 2nd call . Hence ,
• T(n) = T(n-1) + k
• T(0) = k
• T(1) = 2k
• T(2) = 3k
• T(3) = 4k
• So the space complexity is O(n).
124
60