0% found this document useful (0 votes)
24 views

ECE391 Ch6 Stacks Queues Recursions

Uploaded by

Thành Công Thi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

ECE391 Ch6 Stacks Queues Recursions

Uploaded by

Thành Công Thi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

9/4/

ECE391 Computer System Engineering


Chapter 6:
Stacks, Queues, and Recursion
1. Algorithm analysis
2. Recursion
3. Stacks
4. Queues
5. Double-Ended Queues

1. Algorithm Analysis
⚫Most algorithms
transform input objects
into output objects
⚫The running time of an
algorithm typically grows
with input size

2
9/4/

1. Algorithm Analysis
⚫Iterative summing of a list of numbers
float sum(float list[ ], int n)
{
float tempsum = 0; count++; /* for assignment */
int i;
for (i = 0; i < n; i++) {
count++; /*for the for loop */
tempsum += list[i]; /* for assignment */
}
count++; /* last execution of for */
count++; /* for return */
return tempsum;
} 2n + 3 steps

1. Algorithm Analysis
⚫Recursive summing of a list of numbers

float rsum(float list[ ], int n)


{
count++; /*for if conditional */
if (n) {
count++; /* for return and rsum invocation*/
return rsum(list, n-1) + list[n-1];
}
count++;
return list[0]; How many steps?
} 2n+2 steps
4
9/4/

1. Algorithm Analysis
⚫Example: Find the code number of a combination
lock

How many steps to find


the code?
10n steps

1. Algorithm Analysis
⚫Big-Oh Notation is used to characterize running
times and space bound in terms of some parameter n.
⚫Definition:
⚫ Let f(n) and g(n) be function mapping nonnegative
integers to real numbers
⚫ We say that f(n) is O(g(n)) if there is a real constant
c>0 and an integer constant n0 ≥ 1, such that f(n) <
cg(n) for every integer n≥n0 c g(n)
running time

f(n)

no Input size
6
9/4/

1. Algorithm Analysis
Definition: [Big “oh’’]
f(n) = O(g(n)) if there exist positive constants c and n0
such that f(n) ≤ cg(n) for all n, n ≥ n0.
⚫Examples:
⚫7n – 2 is O( )
⚫20n3 + 10n logn + 5 is O( )
⚫3 log n + log log n is O( )
⚫2100 is O( )
⚫5/n is O( )
⚫2n3 + 4n2 log n is O( )
⚫∑n (2i+3) is O( )

1. Algorithm Analysis
⚫Common terms used in algorithm analysis:
⚫Logarithmic O(log n)
⚫Linear O(n)
⚫Quadratic O(n2)
⚫Cubic O(n3)
⚫Polynomial O(nk) (k ≥ 1)
⚫Exponential O(an)
⚫Any algorithm running in O(n log n) time should be
considered efficient

8
9/4/

1. Algorithm Analysis
⚫Practical complexity
⚫To get a feel for how the various functions grow with n,
consider the table below.

1. Algorithm Analysis

⚫Linear O(n)
⚫Quadratic O(n2)
⚫Cubic O(n3)

10
9/4/

1. Algorithm Analysis
Relatives of Big-Oh
⚫big-Omega
⚫f(n) is Ω(g(n)) if there is a constant c > 0, and an integer
constant n0 ≥ 1 such that f(n) ≥ c•g(n) for n ≥ n0
⚫big-Theta
⚫f(n) is Θ(g(n)) if there are constants c’ > 0 and c’’ > 0
and an integer constant n0 ≥ 1 such that c’•g(n) ≤ f(n) ≤
c’’•g(n) for n ≥ n0
⚫little-oh
⚫f(n) is o(g(n)) if, for any constant c > 0, there is an
integer constant n0 ≥ 0 such that f(n) ≤ c•g(n) for n ≥
n0
⚫little-omega
⚫f(n) is ω(g(n)) if, for any constant c > 0, there is an
integer constant n0 ≥ 0 such that f(n) ≥ c•g(n) for n ≥
n0 11

1. Algorithm Analysis
⚫Big-Oh
⚫f(n) is O(g(n)) if f(n) is asymptotically less than or equal to
g(n)
⚫Big-Omega
⚫f(n) is Ω(g(n)) if f(n) is asymptotically greater than or
equal to g(n)
⚫Big-Theta
⚫f(n) is Θ(g(n)) if f(n) is asymptotically equal to g(n)
⚫Little-oh
⚫f(n) is o(g(n)) if f(n) is asymptotically strictly less than g(n)
⚫Little-omega
⚫f(n) is ω(g(n)) if is asymptotically strictly greater than g(n)

12
9/4/

Class Assignment
1. Algorithm A uses 10nlog n operations
Algorithm B uses n2 operations. Determine the
value n0 such that A is better than B for n≥n0
2. Show that log3 n is O(n1/3)
3. Show that O(max{f(n), g(n)}) = O(f(n)+g(n))
4. Show that if p(n) is polynomial in n, then log p(n) is
O(log n)
5. Show that (n+1)5 is O(n5)

6. Show that

13

2. Recursion
Definition: a function that can call itself as a subroutine.
1.1. Linear Recursion
⚫A function is defined so that it makes one recursive
call each time it is invoked
⚫Example: calculate sum of the first n integers in A.
int LinearSum(int A[],int n) {
if n = 1 then
return A[0];
else
return LinearSum(A, n-1) + A[n-1];
}

14
9/4/

2. Recursion
2.1. Linear Recursion
⚫Test for the base case:
⚫At least one
⚫should not use recursion
⚫Recursion
⚫Each recursive step makes the progress towards a base
case
A = {4, 3, 6, 2, 5} return 7 + A[2] = 7 + 6 = 13

LinearSum(A, 3)
return 4 + A[1] = 4+ 3 = 7

LinearSum(A, 2)
return A[0] = 4

LinearSum(A, 1)
15

2. Recursion
2.1. Linear Recursion
⚫Algorithm ReverseArray(A, i, n):
Input: An integer array A and integers i and n
Output: The reversal of the n integers in A starting at
index i
if n>1 then
Swap A[i] and A[i+n-1]
Call ReverseArray(A, i+1, n-2)
return

16
9/4/

2. Recursion
2.1. Linear Recursion
⚫Computing Power via Linear Recursion
⚫power(x, n) = xn

⚫The algorithm uses O(n) time

⚫The algorithm uses O(log n) time


17

2. Recursion
2.2. High order Recursion
⚫Binary recursion
⚫Example
Algorithm BinarySum(A,i,n):
Input: An integer array A and integer i and n
Output: The sum of the n integer in A starting at index i
If n = 1 then
return A[i]
return BinarySum (A,i, [n/2]) + BinarySum(A, i + [n/2],
n/2])

⚫The running time of the algorithm is O(log n)

18
9/4/

2. Recursion
2.2. High order Recursion
⚫Computing the kth Fibonacci number using
binary recursion
⚫Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, …
⚫Recursion relation:
⚫F0 = 0;
⚫F1 = 1;
⚫Fn = Fn-1 + Fn-2

19

3. Stack
⚫Stack is an abstract data type (ADT) that uses last-in-first-out
principle
⚫Support functions:
⚫push(o): insert object o at the top of the stack
Input: Object; Output: None
⚫pop(): remove from the stack and return the top on the
stack; an error occurs if the stack is empty
Input: None; Output: None
⚫size(): return the number of objects in the stack
Input: None; Output: Integer
⚫isEmpty(): return a Boolean indicating if the stack is empty
Input: None; Output: Boolean
⚫top(): Return the top object on the stack, without
removing it; an error occurs if the stack is empty
Input: None; Output: Object

⚫Note that STL stack may have different functions


20
9/4/

3. Stack
No. Operation Output Stack contents -> top
1 Push(5) - (5)
2 Push(3) - (5, 3)
3 Pop()
4 Push(7)
5 Pop()
6 Top()
7 Pop()
8 pop()
9 isEmpty()
10 Push(9)
11 Push(7)
12 Push(3)
13 Push(5)
14 Size()
21

3. Stack
⚫Example for STL stack:
#include <iostream>
#include <stack>
using namespace std;
int main() {
stack<int> myStack;
myStack.push(3);
myStack.push(4);
cout << myStack.size() << ‘\n’;
cout << myStack.top() << ‘\n’;
myStack.pop(); //pop() function is void type
cout << myStack.top() << ‘\n’;
cout << myStack.size() << ‘\n’;
}
⚫What shows on the output screen?

22
9/4/

4. Queues
⚫Queue is a data structure which uses first-in-first-out
principle
⚫Functions:
⚫enqueue(o): insert object o at the rear of the queue
Input: Object; Output: None
⚫dequeue(): remove the object at the front; an error occurs if the
queue is empty.
Input: None; Output: None
⚫size(): Return the number of the objects in the queue
Input: None; Output: Integer.
⚫isEmpty(): return a Boolean value indicating if the queue is
empty.
Input: None; Output: Boolean.
⚫front(): return, but do not remove, a reference to the front
element in the queue; an error occurs if the queue is empty.
Input: None; Output: Object.
⚫Note that STL queue may have different functions

23

4. Queues
No. Operation Output Front <- Queue <- rear
1 enqueue(5) - (5)
2 enqueue(3) - (5, 3)
3 dequeue()
4 enqueue(7)
5 dequeue()
6 front()
7 dequeue()
8 dequeue()
9 isEmpty()
10 enqueue(9)
11 enqueue(7)
12 size()
13 enqueue(3)
14 enqueue(5)
24
9/4/

4. Queues
⚫Application of Queues
⚫Direct applications
⚫waiting lists (e.g., stores, theaters, reservation centers)
⚫access to shared resources (e.g., printers, scanner)
⚫multiprogramming

⚫Indirect applications
⚫Auxiliarydata structure for algorithms (e.g.,
scheduling)
⚫Component of other data structures (e.g., pattern)

25

4. Queue
⚫Example for STL Queue:
#include <iostream>
#include <queue>
using namespace std;
void main() {
queue<int> myQueue;
myQueue.push(3);
myQueue.push(4);
myQueue.push(5);
myQueue.push(6);
cout << myQueue.size() << '\n';
cout << myQueue.front() << '\n';
myQueue.pop ();
cout << myQueue.empty() << '\n';
cout << myQueue.size() << '\n';
getchar();
}
⚫What shows on the output screen?

26
9/4/

5. Double-Ended Queues - Deque


⚫Deque, pronounced “deck”, is a queue-like data structure that
supports insertion and deletion at both the front and the rear
of the queue.
⚫Functions of the deque (D):
⚫insertFront(o): insert a new object o at the beginning of D
Input: Object; Output: None
⚫insertBack(o): insert a new object o at the end of D.
Input: Object; Output: None
⚫eraseFront(o): remove the first object of D; an error occurs if D is
empty
Input: None; Output: None
⚫eraseBack(): remove the last object of D; an error occurs if D is
empty
Input: None; Output: None

⚫Note that STL deque may have different functions

27

5. Double-Ended Queues - Deque


⚫Additional functions:
⚫front(): Return the first object of D; an error occurs if D is
empty
⚫back(): Return the last object of D; an error occurs if D is
empty.
⚫size(): Return the number of the objects in D
Input: None; Output: Integer.
⚫isEmpty(): return a Boolean value indicating if the queue is
empty.
Input: None; Output: Boolean.

⚫Note that STL deque may have different functions

28
9/4/

5. Double-Ended Queues - Deque


Operation Output D
insertFront(3) - (3)
insertFront(5)
front()
eraseFront()
insertBack(7)
back()
eraseFront()
eraseBack()

29

5. Double-Ended Queues - Deque


⚫Example for STL Deque
#include <iostream>
#include <deque>
using namespace std;
void main() {
deque<int> myDeque;
myDeque.push_front(3);
myDeque.push_back(4);
myDeque.push_back(5);
myDeque.push_front(6);
cout << myDeque.size() << '\n';
cout << myDeque.front() << '\n';
cout << myDeque.back() << '\n';
myDeque.pop_back();
cout << myDeque.empty() << '\n';
cout << myDeque.size() << '\n';
getchar();
}

30

You might also like