140 likes | 286 Views
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!
E N D
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! 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).
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)
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;
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.
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().
Object ride() move() Vehicle allWhellDrive roadTrip() SportUtility drive() stop() Car Volvo Yugo Honda oil drip() burn() crash() popAirBags() stop() fuel sip()
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
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)
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 } }
enqueue(e3) e2 e1 inStack outStack
1 2 3 dequeue() e3 e1 e2 e2 e1 e3 inStack outStack
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(); }
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.