The questions on this assignment are about timing analysis and ADT specification. There is no programming on this assignment.
Question 1 ():
For each of the following functions, give the tightest upper bound chosen from among the usual simple functions listed in Section 3.5 of the course readings. Answers should be expressed in big-O notation.
4 log280 n + 242n (a) (1 point) f1(n) = n log2 n + n
- (1 point) f3(n) = 4n4 + n2 log n2 + log2(2n)
- (1 point) f2(n) = 4n7 + 29n log2 n + 280
Question 2 ():
Suppose the exact time required for an algorithm A in both the best and worst cases is given by the function
TA n
- (2 points) For each of the following statements, indicate whether the statement istrue or false.
- Algorithm A is O(log n)
- Algoirthm A is O(n2)
- Algoirthm A is O(n3)
- Algoirthm A is O(2n)
- (1 point) What is the time complexity of algorithm A in big- notation.
Question 3 ():
If possible, simplify the following expressions. Hint: See slide 11 of topic 4 of the lecture slides!
- (1 point) O(n2)+ O(log n)+ O(n log n)
- (1 point) O(2n) O(n2)
- (1 point) 42O(n log n)+ 18O(n3)
- (1 point) O(n2 log2 n2)+ O(m) (yes, thats an m, not a typo; note that m is independent of n)
Question 4 ():
Consider the following Java code fragment:
// Print out all ordered pairs of numbers for(i = 1; i <= n; i++) { for(j = 1; j <= n; j++) {System.out.println( i + , + j) ;}} | between | 1 | and | n |
- (3 points) Use the statement counting approach to determine the exact number of statements that are executed when we run this code fragment as a function of n. Show all of your calculations.
- (1 point) Express the answer you obtained in part (a) in big- notation.
Question 5 ():
Consider the following pseudocode:
Algorithm roundRobinTournament(a)This algorithm generates the list of matches that must be played in a round-robin pirate-dueling tournament (a tournament where each pirate duels each other pirate exactly once).a is an array of strings containing names of pirates in the tournamentn = a.length for i = 0 to n-1 for j = i+1 to n-1 print a[i] + duels + a[j] + ,Yarrr! |
- (6 points) Use the statement counting approach to determine the exact number of statements that are executed by this pseudocode as a function of n. Show all of your calculations..
- (1 point) Express the answer you obtained in part a) in big- notation.
Question 6 (3 points):
Using the active operation approach, determine the time complexity of the pseudocode in question 5. Show all your work and express your final answer in Big- notation.
Question 7 (6 points):
Consider the following pseudocode.
Algorithm multiSearch( data, target ):data: an list of arrays of integers; in each array the integers are sorted in ascending order; the list data has a cursor.target: an integer// Iterate over the arrays in the list data using // its cursor: data.goFirst() found = false while( !data.after() and !found ) { // search for integer target in Afound = binarySearch(data.currentItem(), target) data.goForth() |
Using the active operation approach to timing analysis determine the time complexity of this pseudocode in the worst case. Assume that the list of arrays contains n arrays and that each array has exactly m items in it. Show all your work and express your final answer in Big-O notation.
Question 8 (17 points):
A priority queue is a queue where a numeric priority is associated with each element. Access to elements that have been inserted into the queue is limited to inspection and removal of the elements with smallest and largest priority only. A priority queue may have multiple items that are of equal priority.
Give the ADT specification for a bounded priority queue using the specification method described in Topic 7 of the lecture notes. By bounded, it is meant that the priority queue has a maximum capacity specified when it is created, and it can never contain more than that number of items.
Your specification must specify the following operations:
newPriorityQueue: make a new queue insert: inserts an element with a certain priority isEmpty: test if the queue is empty isFull: test if the queue is full maxItem: obtain the item in the queue with the highest priority minItem: obtain the item in the queue with the lowest priority deleteMax: remove from the queue the item with the highest priority deleteAllMax: remove from the queue all items that are tied for the highest priority deleteMin: remove from the queue the item with the lowest priority
frequency: obtain the number of times a certain item occurs in the queue (with any priority)
Reviews
There are no reviews yet.