Solution for Midterm Exam 1 - Data Structure | COP 3530, Exams of Data Structures and Algorithms

Material Type: Exam; Class: DATA STRUC/ALGORITHMS; Subject: COMPUTER PROGRAMMING; University: University of Florida; Term: Unknown 1989;

Typology: Exams

Pre 2010

Uploaded on 03/18/2009

koofers-user-ruh
koofers-user-ruh 🇺🇸

10 documents

1 / 11

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Solution for Midterm Exam 1
PROBLEM 1 (10 points)
X Matrix” is an nxn sparse matrix in which all non-zero terms are on two diagonals
of the square Matrix. That means the xi,j is part of “X Matrix” iff i=j or i=n-j+1. For
example, following are a 4x4 “X Matrix” and a 5x5 “X Matrix”.
1 0 0 1 1 0 0 0 1
0 1 1 0 0 1 0 1 0
0 1 1 0 0 0 1 0 0
1 0 0 1 0 1 0 1 0
1 0 0 0 1
Assuming that the 2D index of the first element of “X Matrix” starts from (1, 1) and the index of
1D array start from 1. We store an nxn “X Matrix” into a 1D array, in the form as follows:
x1,1 x1,n x2,2 x2,n-1 …… xn-1,2 xn-1,n-1 xn,1 xn,n
a) How many elements in such a 1D array?
b) What is the corresponding 1D index of “X Matrix” element xi, j in the 1D array?
Solution:
a) When n is even, it needs 2n elements. When n is odd, it needs 2n-1 elements.
b) When n is even, the index is
2i-1 when j<=n/2
2i when j>n/2
When n is odd, the index is
2i-1 when i=j
2i when i=n-j+1
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Solution for Midterm Exam 1 - Data Structure | COP 3530 and more Exams Data Structures and Algorithms in PDF only on Docsity!

Solution for Midterm Exam 1 PROBLEM 1 (10 points)X Matrix ” is an nxn sparse matrix in which all non-zero terms are on two diagonals of the square Matrix. That means the xi,j is part of “ X Matrix ” iff i=j or i=n-j+1. For example, following are a 4x4 “ X Matrix ” and a 5x5 “ X Matrix ”. 1 0 0 1 1 0 0 0 1 0 1 1 0 0 1 0 1 0 0 1 1 0 0 0 1 0 0 1 0 0 1 0 1 0 1 0 1 0 0 0 1 Assuming that the 2D index of the first element of “X Matrix” starts from (1, 1) and the index of 1D array start from 1. We store an nxn “X Matrix” into a 1D array, in the form as follows: x1,1 x1,n x2,2 x2,n-1 …… xn-1,2 xn-1,n-1 xn,1 xn,n a) How many elements in such a 1D array? b) What is the corresponding 1D index of “X Matrix” element xi, j in the 1D array? Solution: a) When n is even, it needs 2n elements. When n is odd, it needs 2n-1 elements. b) When n is even, the index is 2i-1 when j<=n/ 2i when j>n/ When n is odd, the index is 2i-1 when i=j 2i when i=n-j+

PROBLEM 2 (20 points) Given a hash table with 26 buckets (b = 26), and a hash function F(x) = ord(first character of x) - ord(‘A’). where ord(x) means the ASCII value of x. The ASCII value of each letter from ‘A’ to ‘Z’ increases by 1 for adjacent letters. For example, F(Burke)= ord(‘B’) - ord(‘A’)=1. Insert the elements whose keys are: Burke, Ekers, Broad, Blum, Attlee, Alton, Hecht and Ederly in this order. Use linear probing to resolve collisions. a) Draw the hash table following each insertion. b) What is the loading factor of your table after the last insert? c) What is the maximum and average number of buckets examined in a successful search of your hash table? d) What is the maximum and average number of buckets examined in an unsuccessful search of your hash table? (Assuming the hash function is uniform.) Solution: a) 0 1 2 3 4 5 6 7 8 … Attlee Burke Broad Blum Ekers Alton Ederly Hecht ………… b) 8/ c) Attlee 1, Burke1, Broad 2, Blum 3, Ekers 1, Alton 6, Ederly 3, Hecht 1 maximum: Alton 6 average:(1+1+2+3+1+6+3+1)/8=18/ d) maximum: 9 average(9+8+7+6+5+4+3+2+18)/26=62/

3: code 0 1 2 3 key a b aa aab aaabaabab Compressed string = 02 4: code 0 1 2 3 4 key a b aa aab ba aaabaabab Compressed string = 021 5: code 0 1 2 3 4 5 key a b aa aab ba aaba aaabaabab Compressed string = 0213 6: code 0 1 2 3 4 5 6 key a b aa aab ba aaba ab aaabaabab Compressed string = 02130 7: code 0 1 2 3 4 5 6 key a b aa aab ba aaba ab aaabaabab Compressed string = 021301 b) Decompress: 1: Initial Configuration code 0 1 key a b 010242 Decompressed string = null 2: code 0 1 2 key a b ab 010242 Decompressed string = a 3: code 0 1 2 3 key a b ab ba 010242 Decompressed string = ab

4: code 0 1 2 3 4 key a b ab ba aa 010242 Decompressed string = aba 5: code 0 1 2 3 4 5 key a b ab ba aa aba 010242 Decompressed string = abaab 6: code 0 1 2 3 4 5 6 key a b ab ba aa aba aaa 010242 Decompressed string = abaabaa 7: code 0 1 2 3 4 5 6 key a b ab ba aa aba aaa 010242 Decompressed string = abaabaaab

W 14 13 W 15 16 W F

W 12 W 14 15 W W 21

W 11 W 13 14 W W W W W 20

W 10 W 12 W W 23 22 21 20 19

W 8 9 W 11 12 W 22 21 20 19 18

6 7 W 9 10 11 12 W W W W 17

5 6 W 8 9 10 11 W 13 14 15 16

4 5 6 7 W 11 12 W 12 13 14 W

3 4 5 6 W W W W 11 12 W W

2 3 4 5 W 7 8 9 10 11 W W

1 2 3 4 W 6 7 8 9 10 11 12

S 1 2 3 4 5 6 7 8 W W 13

b) Simulate the (queue-based) search for the shortest path from the Start to Finish in the maze. Mark each square visited with the length of the shortest path from the Start to that square. Remember, the rat can only move horizontally and vertically. There is no need to depict the queue. What is the length of the shortest path from the Start to Finish? Determine the least accessible square, i.e. the square that takes the longest to get to. This is the one whose shortest path from Start is longer than that of any other square.

PROBLEM 5 (15 points) Assuming that all the nodes in the chain are of type Integer. Let firstNode be the head of the chain. The node of the chain is define as follows: class ChianNode{ public Object element; public ChainNode next; } class Chain{ protected ChainNode firstNode; int MaxKey(ChainNode p); int NumberOfNodes(ChainNode p); float AverageValue (ChainNode p); } Please give the recursive algorithms of the following functions: a) The maximum value of element in the chain MaxKey(firstNode) ; b) The number of nodes in the chain NumberOfNodes(firstNode) ; c) The average value of all element’s key AverageValue (firstNode). Solution a) int MaxKey(ChainNode p){ if (p==null) return -1; int result= (Integer)(p.element); if (p.next==null) return result; return result >= MaxKey(p.next)? result, MaxKey(p.next); }

PROBLEM 6 (20 points) In this problem we are trying to sort the elements in the queue in ascending order. You can only use only one stack to assist the sorting. Other data structures such as arrays or lists are not allowed. The template for the class is given below: package dataStructures; public class MyClass { protected ArrayStack stack; // NO other data members allowed // constructor public MyClass() { stack = new ArrayStack(); } /** sort the queue / public void sortTheQueue (ArrayQueue que) { // your code goes here } /* test code */ public static void main(String [] args) { ArrayQueue que = new ArrayQueue(); que.put(new Integer(6)); que.put(new Integer(5)); que.put(new Integer(9)); que.put(new Integer(4)); que.put(new Integer(3)); MyClass test = new MyClass(); test.sortTheQueue(que); } } a) Write Java code for the function sortTheQueue () in the class MyClass. You can only use the put() and remove() methods of the ArrayQueue. You can use all the methods of the ArrayStack. b) What is the time and space complexity of the method sortTheQueue ()?

Solution: a) /** sort the queue */ public void sortTheQueue(ArrayQueue que) { Integer temp = (Integer) que.remove(); if (temp == null ) return ; stack.push(temp); temp = (Integer) que.remove(); while (temp != null ) { while (!stack.empty() && temp > (Integer) stack.peek()) { Object topOfStack = stack.pop(); que.put(topOfStack); } stack.push(temp); temp = (Integer) que.remove(); } while (!stack.empty()) { System. out .println(stack.peek().toString()); que.put(stack.pop()); } } b)O(n^2 )