Midterm Question and Solutions - Data Structures and Algorithms | COP 3530, Exams of Data Structures and Algorithms

Material Type: Exam; Professor: Bermudez; Class: DATA STRUC/ALGORITHMS; Subject: COMPUTER PROGRAMMING; University: University of Florida; Term: Fall 2005;

Typology: Exams

Pre 2010

Uploaded on 09/17/2009

koofers-user-jzv-1
koofers-user-jzv-1 🇺🇸

9 documents

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Given name:Family name:
Student number:Signature:
UNIVERSITY OF FLORIDA
Computer and Information Science and Engineering
COP3530 (Data Structures and Algorithms)
Instructor: Dr. Manuel Berm´udez
MIDTERM 1
October 14, 2005
Duration: 50 minutes
No aids allowed
This examination paper consists of 10 pages and 4questions. Please bring any discrepancy
to the attention of an invigilator. The number in brackets at the start of each question is
the number of points the question is worth.
Answer all questions.
To obtain credit, you must give arguments to support your answers.
For graders’ use:
Score
1 (25)
2 (0)
3 (30)
4 (25)
Total (100)
Page 1 of 10
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Midterm Question and Solutions - Data Structures and Algorithms | COP 3530 and more Exams Data Structures and Algorithms in PDF only on Docsity!

Given name: Family name:

Student number: Signature:

UNIVERSITY OF FLORIDA

Computer and Information Science and Engineering

COP3530 (Data Structures and Algorithms) Instructor: Dr. Manuel Berm´udez

MIDTERM 1 October 14, 2005

Duration: 50 minutes

No aids allowed

This examination paper consists of 10 pages and 4 questions. Please bring any discrepancy to the attention of an invigilator. The number in brackets at the start of each question is the number of points the question is worth.

Answer all questions.

To obtain credit, you must give arguments to support your answers.

For graders’ use:

Score

Total (100)

  1. (a) [10] Start with an LZW compression dictionary that has the entries (a,0) and (b,1) Compress the string sequence ”ababaaab”. For each character encountered, draw the code table following the encoding of each character. The code tables after the first ’a’ is compressed are shown below. Complete the remaining process. ”ababaaab”

a = 0 b = 1 ababaaab Compressed string = null

a = 0 b = 1 ab = 2 babaaab Compressed string = 0

Solution:

a 0 b 1 ab 2 ba 3 aba 4 aa 5 aab 6

Compressed string = 012051

  1. In a (2n − 1) × n K-matrix, all entries other than those in column 1, the diagonal from (n − 1 , 2) up to (1, n) and the diagonal from (n + 1, 2) down to (2n − 1 , n) are
    1. A K-matrix can be compactly stored in an one-dimensional array by first storing column 1 from (1, 1) to (2n − 1 , 1), followed by the diagonal from (n − 1 , 2) up to (1, n), followed by the diagonal from (n + 1, 2) down to (2n − 1 , n).

x x x x x x x x x zero x x x x x x x x

x denotes a possible non-zero all other terms are zero

(a) [5] Give a sample 5 × 3 K-matrix containing unique integers and the matrix’s compact representation as described above.

Solution:     

    

Suppose that we are defining a class K-Matrix that represents a (2n − 1) × n K-matrix in an one-dimensional array element as above.

class KMatrix { int [] element; int n; // the amount of rows in this matrix

public KMatrix(int cols) { element = new int[4*cols-3]; n = cols; } public void set(int i, int j, int newValue){ //your code goes here } }

(b) [15] Write Java code for the member method void set(int i, int j, int newValue) which stores newValue as the (i, j) element of the K-matrix repre- sented compactly as described above. Throw an IllegalArgumentException if either i or j is improper.

public void set(int i, int j, int newValue) { if (i > 2 * n - 1 || i < 1 || j > n || j < 1) throw new IllegalArgumentException(); else if (j == 1) element[i - 1] = newValue; else if (i + j == n + 1) element[2 * n - 3 + j] = newValue; else if (i - j == n + 1) element[3 * n - 4 + j] = newValue; throw new IllegalArgumentException(); }

public boolean circleChecking(){ //your code goes here public boolean circleChecking() { MyChainNode lastNode = firstNode; for (int i = 0; lastNode != null && i < size; i++) { lastNode = lastNode.next; } if (lastNode.next == null) return false;

int circleSize = 0; MyChainNode circle = lastNode.next; while (circle != lastNode) { circle = circle.next; circleSize++; } System.out.println(circleSize); return true; }

(b) [10] What is the time complexity of your code as a function of the size of MyChain? Explain. Solution: O(n) Since we consider every element at most twice. Hence O(2n) which is equal to O(n).

(b) [10] Complete the following the put method for the class PseudoQueue.

/*Add theElement to the rear of the queue/ public void put(Object theElement) { //your code goes here s1.push(theElement); }

(c) [5] What is the time and space complexity of the methods: get() and put()? Be as precise as possible.

Solution: get(), time = O(n), space = O(1) put(), time = O(1), space = O(1)

End of examination Total pages: 10 Total marks: 100