CSCE310 Data Structure & Algorithms Spring 2001

advertisement
CSCE310
Data Structure & Algorithms Spring 2001
Homework 2
Name:
Given: Monday, February 12, 2001.
Due: Monday, February 26, 2001.
Pen+paper: turn in class before lecture. Programming: use handin.
________________________________________________________________________
You are asked to do the homework by your own and may not receive any help. You may discuss with the
instructor, the TA, a colleague or a friend the content of the course, not the content of the homework.
Please check the applicable option and sign your statement. Honesty is expected from you. If you fail
to return this page with your homework, check a box, or sign, your homework will not be graded.
I hereby certify that:
I have done the homework completely by myself.
I have worked on the homework with another person.
Specify name:
I have received help but have written the homework independently.
Specify help:
I have copied the answers to the homework.
Specify source:
I refuse to answer this question.
Reason (optional):
Signature: _____________________________
________________________________________________________________________
1.
From the textbook, 4-1, page 72 parts a, c, e, and g (12 points).
Bonus: b, d, f, and h (3 points each).
2.
From the textbook, 11.1-6, page 203 (9 points).
3. What value is returned by the following function? Express your answer as a function of n. Give
the worst-case running time using the big-O notation. (8 points)
function mystery(n)
r = 0
for i =1 to n-1
for j = i+1 to n
r = r + 1
return r
4. For each of the following pairs of functions f(n) and g(n), give an appropriate positive constant c
such that f(n) ≤ c∙g(n) for all n > 1. (Total 6 points)
a. f(n) = n2 + n + 1, g(n) = 2n3
b. f(n) = n√n + n2, g(n) = n2
c. f(n) = n2 - n + 1, g(n) = n2/2
5. For each of the following pairs of functions f(n) and g(n), determine whether f(n) = O(g(n)), g(n) =
O(f(n)), or both. (Total 15 points)
a. f(n) = (n2 - n)/2, g(n) = 6n
b. f(n) = n+2
n , g(n) = n2
c. f(n) = n + logn, g(n) = n
d. f(n) = 2(log n)2, g(n) = log n +1
e. f(n) = 4nlogn + n, g(n) = (n 2 - n)/2
Programming assignment: Converting from infix to postfix
(50 points)
People in general are used to using infix notation for writing a mathematical expression.
For example a*(b+c)+d is an infix notation expression that is easy for us to
understand. However, it is sometimes easier for a computer to parse an expression in
postfix notation. In postfix notation, the same expression would be written abc+*d+.
As you can see, the operators are in order of precedence, with the first operation you
perform being the left most. The operators are applied to the first two operands or
expressions that precede them.
You are asked to write a program that converts infix notation to postfix notation. This
program does NOT have to do actual numerical calculations. It simply has to output the
postfix notation expression for a given infix notation expression. For simplicity, the only
operators you need to use are * and +. It should also understand the use of parenthesis.
Use normal conventions when deciding the precedence of operators. (Note:
multiplication has higher priority than addition.)
Your algorithm should do this using a stack. (Hint: for our purposes, only operators need
to be stored on the stack.) You can use an array or linked list that you treat as a stack or a
stack data structure if you find this more convenient.
Here are some sample inputs and outputs:
input: a+b
output: ab+
input: a*b
output: ab*
input: a+b*c
output: abc*+
input: (c+d)*b+a
output: cd+b*a+
input: c+d*b+a
output: cdb*+a+
Sources: textbooks and other courses on the topic.
Download