1 / 14

If f(n) and g(n) are both O(h(n)), then f(n) + g(n) is O(h(2n)).

If f(n) and g(n) are both O(h(n)), then f(n) + g(n) is O(h(2n)). False! From the Rule of Sum, f(n) + g(n) => max(O(h(n)), O(h(n))) => O(h(n)). A class is an abstract class if it fails to implement all methods declared in the interface which the class implements. True!

nura
Download Presentation

If f(n) and g(n) are both O(h(n)), then f(n) + g(n) is O(h(2n)).

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. If f(n) and g(n) are both O(h(n)), then f(n) + g(n) is O(h(2n)). False! From the Rule of Sum, f(n) + g(n) => max(O(h(n)), O(h(n))) => O(h(n))

  2. A class is an abstract class if it fails to implement all methods declared in the interface which the class implements. True! An abstract class lies between an interface and a complete concrete class which may contain empty method declarations (that is, declarations of methods without bodies, such as methods declared in an interface).

  3. Algorithm A uses 1000nlogn operations, while algorithm B uses n2 operations, we say that A is faster than B. True! A: O(nlogn) B: O(n2) and O(nlogn) << O(n2)

  4. The following Java code is correct:int b = 10; Integer a = new Integer(20); int sum = a + b; False! Variable a is an Integer object, which does not have addition arithmetic defined. Correction: int sum = a.intValue() + b;

  5. Using SinglyLinkedLists to implement the Stack ADT is not more efficient than using the DoublyLinkedLists. False! It is more efficient because for Stack ADT, the efforts on reverse links (memory and maintenance) are completely wasted.

  6. Draw a single class inheritance diagram for the following set of classes: class Vehicle extends Object and defines methods ride() and move(). class Car extends Vehicle and defines methods drive() and stop(). class Yugo extends Car and defines instance variable “oil'' and methods drip() and burn(). class Honda extends Car and defines instance variable “fuel'‘ and method sip(). class Volvo extends Car and defines methods crash(), popAirBags(), and stop(). class SportUtility extends Vehicle and defines instance variable “allWheelDrive'‘ and method roadTrip().

  7. Object ride() move() Vehicle allWhellDrive roadTrip() SportUtility drive() stop() Car Volvo Yugo Honda oil drip() burn() crash() popAirBags() stop() fuel sip()

  8. Counting all primitive operations for each statement, characterize the worst-case running time of the following algorithm using the big-Oh notation, please explain your answer: Primitive operations: n n 1, 2, 3, … n 1, 2, 3, … n Let A be a given array Of n integers. for i  0 to n-1 do if (A[i] = 0) then for j  0 to i do let A[i]  A[j] + A[i]. end for end if end for

  9. Worse case happens when the input array A[] is initialized with all 0’s. The worse case running time in terms of n in Big_O is: O(n2)

  10. Suppose you are asked to use two stacks, inStack and outStack, as your only instance variables to implement the Queue abstract data type. public class QueueObject implements Queues{ StackObject inStack; StackObject outStack; QueueObject() { // assumed } public int size(){ // assumed } public boolean isEmpty(){ // assumed } public void enqueue(Object anObj){ // your job } public Object dequeue() throws EmptyQueueException{ // your job } }

  11. enqueue(e3) e2 e1 inStack outStack

  12. 1 2 3 dequeue() e3 e1 e2 e2 e1 e3 inStack outStack

  13. public void enqueue(Object anObj){ inStack.push(anObj); } public Object dequeue() throws EmptyQueueException{ if (inStack.isEmpty() && outStack.isEmpty()) throw new EmptyQueueException(); if (outStack.isEmpty()) while (!inStack.isEmpty()) outStack.push(inStack.pop()); return outStack.pop(); }

  14. Complexity Analysis: enqueue(): O(1). dequeue(): O(1). Because for each element in the queue, the dequeue operation requires three primitive operations: 1) pop from inStack 2) push to outStack, and 3) pop from outStack.

More Related