CM0551 Exam Prep. What are an algorithm’s time and space complexity? (2 marks) Answer: The growth...

18
CM0551 Exam Prep

Transcript of CM0551 Exam Prep. What are an algorithm’s time and space complexity? (2 marks) Answer: The growth...

Page 1: CM0551 Exam Prep. What are an algorithm’s time and space complexity? (2 marks) Answer: The growth rate of the algorithm’s time requirement and the computer.

CM0551

Exam Prep

Page 2: CM0551 Exam Prep. What are an algorithm’s time and space complexity? (2 marks) Answer: The growth rate of the algorithm’s time requirement and the computer.

What are an algorithm’s time and space complexity? (2 marks)

Answer:The growth rate of the algorithm’s time requirement and

the computer memory growth rate, requirement of the algorithm.

Determine the time complexity of the following algorithm. Your analysis should count comparisons.

To sort a[left…right] into ascending order:1. For r = left+1, …, right, repeat:

1.1. Let val = a[r].1.2. Insert val into its correct sorted position in

a[left…r].2. Terminate.

AnswerStep 1.2 involves an iteration (loop) down or up the array

and this is in a loop (step 1) so the time complexity is O(n2)

Page 3: CM0551 Exam Prep. What are an algorithm’s time and space complexity? (2 marks) Answer: The growth rate of the algorithm’s time requirement and the computer.

Complete the algorithm below for a method to return the position of the smallest item in array data with bounds 0..limit.

positionOfSmallest(data[0..limit])1. set smallest = data[0].2. set indexOfSmallest = 0.3. set index = 1.

(6 marks)

4 while index is less than or equal limit, repeat: 4.1 if data[index] is less than smallest: 4.1.1 set smallest = data[index]. 4.1.2 set indexOfSmallest = index. 4.1.3 set index = index + 1.5. Terminate with answer indexOfSmallest.

Page 4: CM0551 Exam Prep. What are an algorithm’s time and space complexity? (2 marks) Answer: The growth rate of the algorithm’s time requirement and the computer.

2a)The following list gives the requirements of a

Stack ADT:It must be possible to make a stack empty.It must be possible to add (‘push’) an element to the

top of a stack.It must be possible to remove (‘pop’) the topmost

element from a stack.It must be possible to test whether a stack is empty.It should be possible to access the topmost element

in a stack without removing it.Write these requirements as a contract, expressed

as a Java interface (include suitable header comments).

(6 marks)

Page 5: CM0551 Exam Prep. What are an algorithm’s time and space complexity? (2 marks) Answer: The growth rate of the algorithm’s time requirement and the computer.

Answerpublic interface Stack { // Each Stack object is a stack whose elements are objects.

/////////////// Accessors ///////////////public boolean isEmpty ();

// Returns true if and only if this stack is empty.public Object getLast ();

// Returns the element at the top of this stack./////////////// Transformers ///////////////public void clear ();

// Make this stack empty.public void push (Object elem);

// Add elem as the top element of this stack.public Object pop ();

// Removes and return the element at the top of this stack.

}

Page 6: CM0551 Exam Prep. What are an algorithm’s time and space complexity? (2 marks) Answer: The growth rate of the algorithm’s time requirement and the computer.

The following Java code gives the data structure and constructor of an ArrayStack.

public class ArrayStack {

private Object[] elems;private int depth;/////////////// Constructor ///////////////public ArrayStack (int maxDepth)

{elems = new Object[maxDepth];depth = 0;

}Write additional Java code to implement the methods to

clear and add an element to the stack. What is the time complexity of these operations?

(6 marks)

Page 7: CM0551 Exam Prep. What are an algorithm’s time and space complexity? (2 marks) Answer: The growth rate of the algorithm’s time requirement and the computer.

Answerpublic void clear () {

for (int i = 0; i < depth; i++)elems[i] = null;

depth = 0;} 2public void push (Object elem) {

elems[depth++] = elem;} 2

Operation push has time complexity O(1).Operation clear has time complexity O(n).

2

Could draw dias to help

Page 8: CM0551 Exam Prep. What are an algorithm’s time and space complexity? (2 marks) Answer: The growth rate of the algorithm’s time requirement and the computer.

3 a) Given the following queue contract and the SLL declaration giving the data structure and constructor.

public interface Queue {

/////////////// Accessors ///////////////public boolean isEmpty ();

// Return true if and only if this queue is empty.public int size ();

// Return this queue’s length.public Object getFirst ();

// Return the element at the front of this queue./////////////// Transformers ///////////////

public void clear ();// Make this queue empty.

public void addLast (Object elem);// Add elem as the rear element of this queue.

Page 9: CM0551 Exam Prep. What are an algorithm’s time and space complexity? (2 marks) Answer: The growth rate of the algorithm’s time requirement and the computer.

public Object removeFirst ();// Remove and return the front element of this queue.

public String toString ();// Returns a String of all elements in the queue.

}public class LinkedQueue implements Queue {

private SLLNode front, rear;

/////////////// Constructor ///////////////public LinkedQueue ()

{ front = rear = null;

}

Write Java code to implement the isEmpty and removeFirst methods (include error checking). (5 marks)

Page 10: CM0551 Exam Prep. What are an algorithm’s time and space complexity? (2 marks) Answer: The growth rate of the algorithm’s time requirement and the computer.

public boolean isEmpty ()

{return (front == null);

} 2

public Object removeFirst ()

{if (front == null) throw …;Object frontElem = front.element;front = front.succ;if (front == null) rear = null;return frontElem;

} 3

Could draw dia. to help

Page 11: CM0551 Exam Prep. What are an algorithm’s time and space complexity? (2 marks) Answer: The growth rate of the algorithm’s time requirement and the computer.

Write a Java main method to create a nameQueue and add three names, then remove a name, then add another name. Output the contents of the queue after each of these three steps.

(7 marks)

Page 12: CM0551 Exam Prep. What are an algorithm’s time and space complexity? (2 marks) Answer: The growth rate of the algorithm’s time requirement and the computer.

Answerpublic static void main (String[] args){

//set up the queueLinkedList nameQueue = new LinkedList();// add three elements to the queuequeue.addLast("Homer"); queue.addLast("Marge");queue.addLast("Bart"); System.out.println(queue.toString());// remove an elementSystem.out.println(queue.removeFirst()+

" has been removed");System.out.println(queue.toString());// add one more elementqueue.addLast("Lisa");System.out.println(queue.toString());

}

Page 13: CM0551 Exam Prep. What are an algorithm’s time and space complexity? (2 marks) Answer: The growth rate of the algorithm’s time requirement and the computer.

4a) Define the terms ‘binary tree’ and ‘binary search tree’.

(6 marks)AnswerA binary tree consists of a header, plus a number of nodes

connected by links in a hierarchical data structure:Each node contains an element (value or object), plus links

to at most two other nodes (its left child and right child).The header contains a link to a node designated as the root

node.

A binary search tree (or BST) is a binary tree with the following property. For any node in the binary tree, if that node contains element elem:

Its left subtree (if nonempty) contains only elements less than elem.

Its right subtree (if nonempty) contains only elements greater than elem.

Page 14: CM0551 Exam Prep. What are an algorithm’s time and space complexity? (2 marks) Answer: The growth rate of the algorithm’s time requirement and the computer.

b) Implement the following algorithm in Java.

To find which if any node of a BST contains an element equal to target:

1. Set curr to the BST’s root.2. Repeat:

2.1. If curr is null:2.1.1. Terminate with answer none.

2.2. Otherwise, if target is equal to curr’s element:2.2.1. Terminate with answer curr.

2.3. Otherwise, if target is less than curr’s element:2.3.1. Set curr to curr’s left child.

2.4. Otherwise, if target is greater than curr’s

element:2.4.1. Set curr to curr’s right child.

Page 15: CM0551 Exam Prep. What are an algorithm’s time and space complexity? (2 marks) Answer: The growth rate of the algorithm’s time requirement and the computer.

Given this definition of a BST.public class BSTNode

{protected Comparable element;protected BSTNode left, right;

protected BSTNode (Comparable elem) {

element = elem;left = null;

right = null;}

public int compareTo(Comarable elem) …}

(8 marks)

Page 16: CM0551 Exam Prep. What are an algorithm’s time and space complexity? (2 marks) Answer: The growth rate of the algorithm’s time requirement and the computer.

Answerpublic BSTNode search (Comparable target)

{ int direction = 0; BSTNode curr = root; while(true)

{if (curr == null) return null;direction = target.compareTo(curr.element);if (direction == 0) return curr;else if (direction < 0) curr = curr.left; else curr = curr.right;

}}

Could draw dia to help

Page 17: CM0551 Exam Prep. What are an algorithm’s time and space complexity? (2 marks) Answer: The growth rate of the algorithm’s time requirement and the computer.

c)Draw the resulting structure after inserting the following elements into a BST: mouse, screen, keyboard, modem, network, battery, floppy, and speaker. Write down the order in which elements are visited by an in-order and pre-order transversals.

(6 marks)

Page 18: CM0551 Exam Prep. What are an algorithm’s time and space complexity? (2 marks) Answer: The growth rate of the algorithm’s time requirement and the computer.

• In-order: Battery, floppy, modem, mouse, network, screen and speaker

• Pre-order: mouse, modem, battery, floppy, screen, network and speaker

floppy

battery

modem

mouse

network

screen

speaker