Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Final Examination for E&CE 250 - Algorithms and Data Structures, Exams of Data Structures and Algorithms

A final examination for the course E&CE 250 - Algorithms and Data Structures at the University of Waterloo. The exam consists of two problems, each with two parts. The first problem covers algorithm analysis and solving recurrences, while the second problem covers queues and primary clustering. The exam is closed book and no calculators are allowed. Java code fragments and figures to aid in problem-solving.

Typology: Exams

Pre 2010

Uploaded on 05/11/2023

photon
photon 🇺🇸

4.6

(5)

223 documents

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
UNIVERSITY OF WATERLOO
DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING
E&CE 250 – ALGORITHMS AND DATA STRUCTURES
Final Examination Instructors: R.E.Seviora and L.Tahvildari 3 hrs, Apr. 11, 2001
Name:
Student ID:
1. 2. 3. 4. 5. 6. Total:
Do all problems. The number in brackets denotes the relative weight of the problem (out of 100). If
information appears to be missing from a problem, make a reasonable assumption, state it and proceed. If
the space to answer a question is not sufficient, use the last (overflow) page.
Closed book. No calculators allowed.
PROBLEM 1 [16]
A. Algorithm Analysis
1.Consider the algorithm represented by the following Java program fragment. What value does bar
compute? Derive a tight, big oh expression for the running time of the method bar.
static int bar (int x, int n)
{
int sum = 0;
for (int i = 1; i <= n; ++i)
sum = sum + i;
return {x + sum};
}
2.Consider the algorithm represented by the following Java program fragment. The method bar1
computes the same function as bar above. However, bar1 is based on a different algorithm and its tight
big oh bound is O(n). What value does foo compute? Give a tight, big oh expression for the worst case
running time of the method foo.
static int foo (int x, int n)
{
int sum = 0;
for (int i = 1; i <= n; ++i)
sum = sum + bar1 (i, n);
return {x + sum};
}
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Final Examination for E&CE 250 - Algorithms and Data Structures and more Exams Data Structures and Algorithms in PDF only on Docsity!

UNIVERSITY OF WATERLOO

DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING

E&CE 250 – ALGORITHMS AND DATA STRUCTURES

Final Examination Instructors: R.E.Seviora and L.Tahvildari 3 hrs, Apr. 11, 2001

Name: Student ID:

            1. Total:

Do all problems. The number in brackets denotes the relative weight of the problem (out of 100). If information appears to be missing from a problem, make a reasonable assumption, state it and proceed. If the space to answer a question is not sufficient, use the last (overflow) page. Closed book. No calculators allowed.

PROBLEM 1 [16]

A. Algorithm Analysis

1.Consider the algorithm represented by the following Java program fragment. What value does bar compute? Derive a tight, big oh expression for the running time of the method bar.

static int bar (int x, int n) { int sum = 0; for (int i = 1; i <= n; ++i) sum = sum + i; return {x + sum}; }

2.Consider the algorithm represented by the following Java program fragment. The method bar computes the same function as bar above. However, bar1 is based on a different algorithm and its tight big oh bound is O(n). What value does foo compute? Give a tight, big oh expression for the worst case running time of the method foo.

static int foo (int x, int n) { int sum = 0; for (int i = 1; i <= n; ++i) sum = sum + bar1 (i, n); return {x + sum}; }

B. Solving Recurrences

Solve the following recurrence. You may assume that n is a power of 2. Show all your work.

n k a

n

n

n

aT

T n k

PROBLEM 2 [16]

A. Queues

The interface Queue discussed in the lectures contained only the operations enqueue , dequeue and getHead. In some applications, the method reverse() is required. This method will reverse the order of items in the queue. For example, if the original queue contained a, b, c, d (in this order), reverse() would reorder its contents to d, c, b, a. In this problem, you are asked to devise an algorithm for reverse() , for the case of linked list implementation of the queue. Note that you may use any of the methods of the LinkedList class in your answer.

  1. Explain the basic approach to reversing you used in reverse().
  2. Given the algorithm for reverse() in Java.

public class QueueAsLinkedList extends AbstractContainer implements Queue { protected LinkedList list;

// standard methods of Queue interface … // reverse () : reverses the order of items on the queue public void reverse () {

PROBLEM 3 [16]

A. Hash/Scatter Table

1.Consider a hash table with separate chaining with ten hash locations. Using the hash function h(x) = x mod 10, insert the keys {33, 54, 69, 74, 18, 19} (in the order given) into the hash table. Draw the resulting hash table. (Note: to keep this example simple, we use table size that is not prime.)

0 1 2 3 4 5 6 7 8 9

  1. Consider an open addressing scatter table with ten slots. For the hash function h(x) = x mod 10, insert the keys {33, 54, 69, 74, 18, 19} (in the order given) into the table. Use linear probing for collision resolution. Show the result in the table (2) below.
  2. Consider an open addressing scatter table with ten slots. For the hash function h(x) = x mod 10, insert the keys {33, 54, 69, 74, 18, 19} (in the order given) into the table. Use quadratic probing for collision resolution. Show the result in the table (3) below.
  3. Consider an open addressing scatter table with ten slots. For the hash function h(x) = x mod 10, insert the keys {33, 54, 69, 74, 18, 19} (in the order given) into the table. Use the secondary hash function

h '^ ( x )= 1 +( x mod 9 ) for collision resolution. Show the result in the table (4) below.

  1. Consider your answer to (3) above. What happens when you attempt to insert the key 94 into the final hash table obtained after inserting the keys given in (3). Explain.

B. Primary Clustering

Figure 1 illustrates an open addressing scatter table with linear probing, as it is being filled, at increments of 10 percent of table capacity. The black bands in this figure represent clusters. A cluster is a set of adjacent occupied entries in the table. Figure 2 illustrates the table being filled under the same scenario. However, in this case, double hashing is used.

Figure 1 – Linear Probing Clusters

Figure 2 – Double Hashing Clusters

  1. State what the ‘primary clustering’ phenomenon is.
  2. Various open addressing schemes differ in their susceptibility to primary clustering. How is this reflected in the two figures above?

B. Space Requirements of Trees

Derive an expression for the total space needed to represent a tree of n internal nodes using each of the following classes. Assume four-byte integers and references. The expression should include the space requirement of the tree only, and not include the space needed to store the objects referred to by the nodes of the tree.

  1. GeneralTree public class GeneralTree extends AbstractTree { protected Object key; protected int degree; protected LinkedList list;

// … }

  1. NaryTree public class NaryTree extends AbstractTree { protected Object key; protected int degree; protected NaryTree[] subtree;

// … }

PROBLEM 5 [20]

A. Search Trees

  1. For each node shown in the binary tree below, show its depth, height, and AVL balance factor. Write your answers in the following table.

node depth height balance factor A B C D E F G

C

A E

B D G

F

  1. Draw the sequence of AVL trees obtained when the following keys are inserted one-by-one, in the order given into an initially empty AVL search tree: {F, E, A, B, D, C, G}. Identified rotations, if there is any.
  1. What is the main advantage of an AVL Tree over a Binary Search Tree (BST)?

B. Binary Trees and Heaps

  1. The method isHeap determines whether a particular binary tree is a heap. The implementation this method given below contains a few bugs. Find these bugs and correct them.

public class BinaryTree extends AbstractTree { public Boolean isHeap () { if (isEmpty ()) return true; if (!getLeft ().isEmpty () || getLeft ().getKey ().isLT (getKey ())) return false; if (!getRight ().isEmpty () || getRight ().getLey ().isLT (getKey ())) retuen false; return getLeft ().isHeap () && getRight ().isHeap (); } }

  1. What is the big Oh bound on the running time of this algorithm after modification?

3. (a) How many internal nodes are there in a perfect binary tree of height h ≥ 0?

(b) What is the height of a perfect binary tree with n internal nodes?

OVERFLOW SHEET [Please identify the question(s) being answered.]