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 }log_{280 }*n *+ ^{2}_{42}* ^{n }*(a) (1 point)

*f*

_{1}(

*n*) =

*n*log

_{2 }

*n*+

*n*

- (1 point)
*f*_{3}(*n*) = 4*n*^{4 }+*n*^{2 }log*n*^{2 }+ log_{2}(2)^{n} - (1 point)
*f*_{2}(*n*) = 4*n*^{7 }+ 29*n*log_{2 }*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

*T _{A}*

*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*(*n*^{2}) - Algoirthm A is
*O*(*n*^{3}) - Algoirthm A is
*O*(2)^{n}

- Algorithm A is
- (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*(*n*^{2})+*O*(log*n*)+*O*(*n*log*n*) - (1 point)
*O*(2) ·^{n}*O*(*n*^{2}) - (1 point) 42
*O*(*n*log*n*)+ 18*O*(*n*^{3}) - (1 point)
*O*(*n*^{2 }log_{2 }*n*^{2})+*O*(*m*)*(yes, that’s 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.