Solution for Midterm Exam 1

Similar documents
CSE373 Fall 2013, Second Midterm Examination November 15, 2013

CS171 Final Practice Exam

COP 3530 HW6 - Solutions

COP 3530 Assignment 5 Due: 7 th Oct In this problem you will solve the Rat in a maze problem (Lecture 12), using Stacks and Queues.

CS171 Final Practice Exam

CSCE 2014 Final Exam Spring Version A

CSE 373 Autumn 2012: Midterm #2 (closed book, closed notes, NO calculators allowed)

n 2 ( ) ( ) Ο f ( n) ( ) Ω B. n logn Ο

DS ata Structures Aptitude

Computing Science 115 Final Examination April 23, 2002 Section: B2 BASU. Please put student id on last page. Instructions:

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

CS/ENGRD2110: Final Exam

York University AK/ITEC INTRODUCTION TO DATA STRUCTURES. Final Sample II. Examiner: S. Chen Duration: Three hours

HASH TABLES.

BRONX COMMUNITY COLLEGE of the City University of New York DEPARTMENT OF MATHEMATICS AND COMPUTER SCIENCE. Sample Final Exam

Second Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

: Fundamental Data Structures and Algorithms. June 06, 2011 SOLUTIONS

Introducing Hashing. Chapter 21. Copyright 2012 by Pearson Education, Inc. All rights reserved

About this exam review

CIS 121 Data Structures and Algorithms Midterm 3 Review Solution Sketches Fall 2018

Final Examination CSE 100 UCSD (Practice)

Hashing. Manolis Koubarakis. Data Structures and Programming Techniques

Visit ::: Original Website For Placement Papers. ::: Data Structure

Points off Total off Net Score. CS 314 Final Exam Spring Your Name Your UTEID

CSE 332 Spring 2014: Midterm Exam (closed book, closed notes, no calculators)

9. The expected time for insertion sort for n keys is in which set? (All n! input permutations are equally likely.)

CS201 Discussion 7 MARKOV AND RECURSION

Multiple Choice. Write your answer to the LEFT of each problem. 3 points each

Data Structures and Algorithms 2018

Standard ADTs. Lecture 19 CS2110 Summer 2009

) $ f ( n) " %( g( n)

CMPS 11 Introduction to Programming Midterm 1 Review Problems

Dictionaries. Application

Hashing as a Dictionary Implementation

COMPUTER SCIENCE Paper 1

EXAMINATIONS 2006 SUMMER TRIMESTER. COMP103 Introduction to Data Structures and Algorithms

CS 216 Fall 2007 Final Exam Page 1 of 10 Name: ID:

Problem 1. Which of the following is true of functions =100 +log and = + log? Problem 2. Which of the following is true of functions = 2 and =3?

Topic HashTable and Table ADT

CSE030 Fall 2012 Final Exam Friday, December 14, PM

I. True/False: (2 points each) On your bubble form fill out a for true and b for false.

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

NATIONAL UNIVERSITY OF SINGAPORE

D. Θ nlogn ( ) D. Ο. ). Which of the following is not necessarily true? . Which of the following cannot be shown as an improvement? D.

CS61B Spring 2016 Guerrilla Section 6 Worksheet

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators)

Student number: Datenstrukturen & Algorithmen page 1

CIS 120 Midterm II November 8, Name (printed): Pennkey (login id):

Dictionary. Dictionary. stores key-value pairs. Find(k) Insert(k, v) Delete(k) List O(n) O(1) O(n) Sorted Array O(log n) O(n) O(n)

5. Hashing. 5.1 General Idea. 5.2 Hash Function. 5.3 Separate Chaining. 5.4 Open Addressing. 5.5 Rehashing. 5.6 Extendible Hashing. 5.

7 Hashing: chaining. Summer Term 2010

CSE373 Fall 2013, Final Examination December 10, 2013 Please do not turn the page until the bell rings.

University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination

Algorithms: Design & Practice

COMPUTER APPLICATIONS

Solutions to Exam Data structures (X and NV)

Test 1 Last 4 Digits of Mav ID # Multiple Choice. Write your answer to the LEFT of each problem. 2 points each t 1

BINARY HEAP cs2420 Introduction to Algorithms and Data Structures Spring 2015

CS 110 Practice Final Exam originally from Winter, Instructions: closed books, closed notes, open minds, 3 hour time limit.

CSCE 210/2201 Data Structures and Algorithms. Prof. Amr Goneid

Midterm Exam 2 CS 455, Spring 2015

PROBLEM 1 : (And the winner is...(12 points)) Assume you are considering the implementation of a priority queue that will always give you the smallest

CSCE 210/2201 Data Structures and Algorithms. Prof. Amr Goneid. Fall 2018

( ) n 3. n 2 ( ) D. Ο

Therefore, after becoming familiar with the Matrix Method, you will be able to solve a system of two linear equations in four different ways.

( ) 1 B. 1. Suppose f x

CS 112 Final May 8, 2008 (Lightly edited for 2011 Practice) Name: BU ID: Instructions GOOD LUCK!

( ) D. Θ ( ) ( ) Ο f ( n) ( ) Ω. C. T n C. Θ. B. n logn Ο

INSTITUTE OF AERONAUTICAL ENGINEERING

TIME: 3 HOURS MARKS: a. Develop a java program to sort an array of N numbers in ascending order. (40)

APJ ABDUL KALAM TECHNOLOGICAL UNIVERSITY THIRD SEMESTER B.TECH DEGREE EXAMINATION, JULY 2017 CS205: DATA STRUCTURES (CS, IT)

( ) + n. ( ) = n "1) + n. ( ) = T n 2. ( ) = 2T n 2. ( ) = T( n 2 ) +1

HASH TABLES. Hash Tables Page 1

EXAMINATIONS 2017 TRIMESTER 2

Midterm Exam 2A Principles of Computing Fall November 10, 2014

ASCII American Standard Code for Information Interchange. Text file is a sequence of binary digits which represent the codes for each character.

MIDTERM EXAM THURSDAY MARCH

CS 251, LE 2 Fall MIDTERM 2 Tuesday, November 1, 2016 Version 00 - KEY

COMP 250 Midterm #2 March 11 th 2013

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1

Summer Final Exam Review Session August 5, 2009

Midterm Exam 2B Answer key

CSE 332 Winter 2015: Midterm Exam (closed book, closed notes, no calculators)

CSE 373 Spring 2010: Midterm #2 (closed book, closed notes, NO calculators allowed)

Tutorial 4 Algorithms and Data Structures

Final Exam Solutions

MLR Institute of Technology

COMP 103. Data Structures and Algorithms

CSE wi: Practice Midterm

CPSC 211, Sections : Data Structures and Implementations, Honors Final Exam May 4, 2001

CS165 Practice Midterm Problems Fall 2018

Prelim 2. CS 2110, 16 November 2017, 7:30 PM Total Question Name Short Heaps Tree Collections Sorting Graph

University of Illinois at Urbana-Champaign Department of Computer Science. Final Examination

HASH TABLES cs2420 Introduction to Algorithms and Data Structures Spring 2015

ECE 242 Data Structures and Algorithms. Hash Tables II. Lecture 25. Prof.

1) What is the primary purpose of template functions? 2) Suppose bag is a template class, what is the syntax for declaring a bag b of integers?

Dynamic Dictionaries. Operations: create insert find remove max/ min write out in sorted order. Only defined for object classes that are Comparable

COMP 103 Introduction to Data Structures and Algorithms

Bachelor Level/ First Year/ Second Semester/ Science Full Marks: 60 Computer Science and Information Technology (CSc. 154) Pass Marks: 24

No Aids Allowed. Do not turn this page until you have received the signal to start. Read this entire page or you ll miss the bonus question.

Transcription:

PROBLEM 1 (10 points) Solution for Midterm Exam 1 X Matrix is an nxn sparse matrix in which all non-zero terms are on two diagonals of the square Matrix. That means the x i,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: x 1,1 x 1,n x 2,2 x 2,n-1 x n-1,2 x n-1,n-1 x n,1 x n,n a) How many elements in such a 1D array? b) What is the corresponding 1D index of X Matrix element x i, 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

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/26 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/8 d) maximum: 9 average(9+8+7+6+5+4+3+2+18)/26=62/26

PROBLEM 3 (20 points) Start with an LZW compression dictionary that has the entries (a,0) and (b,1) a)compress the string sequence "". For each character encountered, draw the code table following the encoding of each character. The initial configuration is shown below. Complete the remaining process. Initial Configuration code 0 1 key a b Compressed string = null b) Decompress the code sequence "". For each code encountered, draw the decode table following the decoding of each code. The initial configuration is shown below. Complete the remaining process. Initial Configuration code 0 1 key a b Decompressed string = null Sloution a) Compress: 1: Initial Configuration code 0 1 key a b Compressed string = null 2: code 0 1 2 key a b aa Compressed string = 0

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

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

PROBLEM 4 (15 points) Consider the rat in a maze problem, depicted below W X X W X X W F W X W X X W W O W X W X X W W W W W O W X W X W W O W X X W X X W O O O W X X X X W W W W O O O W X X X X W O O O O O O O O W X X W O W O O W W W W O W W O O W O O O O W W O O W O S O O O W W a) The start location for the rat is S, and the finish is at F. The locations marked W contain impenetrable walls. Simulate the stack-based search for the Finish. The rat attempts to move up, right, down, and left (in that order!). There s no need to depict the stack. As you arrive on a square, place a circle in it. As you retreat from a square, add an X to it. You may find it useful to draw a line from the circle to the next, in certain places.

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);

b) int NumberOfNodes(ChainNode p) { if (p==null) return 0; if (p.next==null) return 1; return 1+ NumberOfNodes(p.next); c) float AverageValue (ChainNode p) { if (p==null) return 0; int result= (Integer)(p.element); if (p.next==null) return result; int numberofnodes= NumberOfNodes(p.next); return (result+ AverageValue(p.next)* numberofnodes)/( numberofnodes+1);

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 )