If fn and gn are both Ohn then
If f(n) and g(n) are both O(h(n)), then f(n) + g(n) is O(h(2 n)). 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 1000 nlogn operations, while algorithm B uses n 2 operations, we say that A is faster than B. True! A: O(nlogn) B: O(n 2) and O(nlogn) << O(n 2)
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. int. Value() + b;
Using Singly. Linked. Lists to implement the Stack ADT is not more efficient than using the Doubly. Linked. Lists. 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(), pop. Air. Bags(), and stop(). • class Sport. Utility extends Vehicle and defines instance variable “all. Wheel. Drive'‘ and method road. Trip().
Object Vehicle Car Yugo oil drip() burn() drive() stop() ride() move() Sport. Utility Honda Volvo fuel sip() crash() pop. Air. Bags() stop() all. Whell. Drive road. Trip()
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: Let A be a given array Of n integers. Primitive operations: 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 n n 1, 2, 3, … n
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(n 2)
Suppose you are asked to use two stacks, in. Stack and out. Stack, as your only instance variables to implement the Queue abstract data type. public class Queue. Object implements Queues{ Stack. Object in. Stack; Stack. Object out. Stack; Queue. Object() { // assumed } public int size(){ // assumed } public boolean is. Empty(){ // assumed } public void enqueue(Object an. Obj){ // your job } public Object dequeue() throws Empty. Queue. Exception{ // your job } }
enqueue(e 3) e 2 e 1 in. Stack out. Stack
1 2 3 dequeue() e 3 e 2 e 1 in. Stack e 1 e 2 e 3 out. Stack
public void enqueue(Object an. Obj){ in. Stack. push(an. Obj); } public Object dequeue() throws Empty. Queue. Exception{ if (in. Stack. is. Empty() && out. Stack. is. Empty()) throw new Empty. Queue. Exception(); if (out. Stack. is. Empty()) while (!in. Stack. is. Empty()) out. Stack. push(in. Stack. pop()); return out. Stack. 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 in. Stack 2) push to out. Stack, and 3) pop from out. Stack.
- Slides: 14