• 沒有找到結果。

6.1SortingandHashing(1)(20%)Considerthefollowingmodi cationofrandomizedquicksortto ndthe

N/A
N/A
Protected

Academic year: 2022

Share "6.1SortingandHashing(1)(20%)Considerthefollowingmodi cationofrandomizedquicksortto ndthe"

Copied!
4
0
0

加載中.... (立即查看全文)

全文

(1)

Data Structures and Algorithms (NTU, Class 01/02, Spring 2011) instructor: Hsuan-Tien Lin

Homework #6

TA in charge: Ming-Yuan Hsu RELEASE DATE: 05/27/2011

DUE DATE CHANGED TO: 06/17/2011, 17:20 in R217

As directed below, you need to submit your code to the designated place on the course website.

Any form of cheating, lying or plagiarism will not be tolerated. Students can get zero scores and/or get negative scores and/or fail the class and/or be kicked out of school and/or receive other punishments for those kinds of misconducts.

Discussions on course materials and homework solutions are encouraged. But you should write the nal solutions alone and understand them fully. Books, notes, and Internet resources can be consulted, but not copied from.

Since everyone needs to write the nal solutions alone, there is absolutely no need to lend your homework solutions and/or source codes to your classmates at any time. In order to maximize the level of fairness in this class, lending and borrowing homework solutions are both regarded as dishonest behaviors and will be punished according to the honesty policy.

Both English and Traditional Chinese are allowed for writing any part of your homework (if the compiler recognizes Traditional Chinese, of course). We do not accept any other languages. As for coding, either C or C++ or a mixture of them is allowed.

This homework set comes with 200 points and 10 bonus points. In general, every home- work set of ours would come with a full credit of 200 points.

6.1 Sorting and Hashing

(1) (20%) Consider the following modi cation of randomized quick sort to nd the k-th smallest element. When k is chosen uniformly between f1; 2;    ; Ng at random, denote C(N) as the expected number of comparisons made by the algorithm. Try your best to derive a closed-form solution for C(N). You can use the harmonic function H(N) = PN

n=11

n, which is O(log N), in your closed-form solution.

Quick-k(array a, int N, int k) /* such that 1  k  N */

 pick an element pivot within a[0], a[1],    , a[N 1] at random uniformly

 compare pivot with every element a[0], a[1],    , a[N 1] except the pivot (i.e., a total of N 1 comparisons). Randomly store those elements less than pivot in an array left of size M 1; randomly store those elements greater than pivot in an array right of size N M.

if k = M then return pivot else if k < M then

return Quick-k(left, M 1, k) elsereturn Quick-k(right, N M, k M) end if

Hint: Let T (N; k) be the number of comparisons when Quick-k is called with N and k. From the algorithm,

T (N; k) = 1 N

k 1X

M=1

T (N M; k M) + XN

M=k+1

T (M 1; k)

!

+ (N 1) and T (1; 1) = 0:

Our goal is to compute C(N) = N1 PN

k=1T (N; k).

1 of 4

(2)

Data Structures and Algorithms (NTU, Class 01/02, Spring 2011) instructor: Hsuan-Tien Lin

(2) (20%) Consider the problem of sorting N boolean elements in an array using comparison sort.

Prove that (N) comparisons are needed.

(3) (20%) Suppose two arrays A and B, both of length N, are readily sorted. Use either C or any pseudo-code to give a O(log N) time algorithm to nd the median of A [ B. Brie y explain why your algorithm is O(log N)-time.

(4) Suppose you are given a sorted array of N elements followed by M unsorted elements. How would you sort the whole array if

(a) (10%) M = O(N)?

(b) (10%) M = O(log N)?

(c) (10%) M = O(1)?

Please give convincing reasons of your choice in each case.

(5) (10%) Hash functions are used not only for ecient storage, but also for designing ecient algorithms. Use any search engine to study the \Rabin-Karp String Matching Algorithm." Explain the di erence between the algorithm and KMP String Matching clearly to the grading TA in your own words. Also, cite the website that you learn the algorithm from.

(6) (20%) Consider an alternative quadratic probing scheme that visits h0(key), h1(key), h2(key),

   , hm(key) where

hi(key) = h(key) + 16i2 mod P

When P is an odd prime, let m = P 12 and i 6= j with both i and j within f0; 1; 2;    ; mg. Prove that hi(key) 6= hj(key) for all i; j that are between 0 and m.

(7) (Bonus 10%) Construct a perfect hash function that is eciently computable for the following 32 standard keywords in C. You need to explain why the hash function is perfect and why it is eciently computable to get the full bonus.

auto, break, case, char, const, continue, default, do, double, else, enum, extern, float, for, goto, if, int, long, register, return, short, signed, sizeof, static, struct, switch, typedef, union, unsigned, void, volatile, while

6.2 Quick Sort

(1) (30%) Implement any TWO of the following sorting algorithms: shell sort, heap sort, merge sort, radix-16 sort (with counting sort). You can choose any implementation details but please state them clearly to the TAs. Then, write a program hw6 2 to compare your implementations with either qsort in C or std::sort in C++ using random arrays within the range of unsigned 32-bit integers. Plot a gure like Figure 7.18 on page 372 of the textbook using your two algorithms and qsort or std::sort with the size of the array being f16; 64; 256; 1024; 4096; 16384g. Brie y state your ndings.

6.3 Balanced Binary Search Trees

libavl (http://www.stanford.edu/~blp/avl/) is a useful library for binary search trees. For instance, the following short code constructs an AVL tree of 16 integers and print it out.

#include <s t d i o . h>

#include <s t d l i b . h>

#include " avl . h"

void p o s t o r d e r i n t e g e r a v l ( const struct avl node node )f i f ( node == NULL)

return ;

2 of 4

(3)

Data Structures and Algorithms (NTU, Class 01/02, Spring 2011) instructor: Hsuan-Tien Lin

p r i n t f ( "%d " ,  ( ( int ) node >a v l d a t a ) ) ;

i f ( node >a v l l i n k [ 0 ] != NULL j j node >a v l l i n k [ 1 ] != NULL)f putchar ( ' ( ' ) ;

p o s t o r d e r i n t e g e r a v l ( node >a v l l i n k [ 0 ] ) ; putchar ( ' , ' ) ;

putchar ( ' ' ) ;

p o s t o r d e r i n t e g e r a v l ( node >a v l l i n k [ 1 ] ) ; putchar ( ' ) ' ) ;

g g

int int compare ( const void pa , const void pb , void param ) f int a = ( const int ) pa ;

int b = ( const int ) pb ; i f ( a < b) return 1;

else i f ( a > b) return +1;

else return 0 ; g

int main ( ) f

struct a v l t a b l e  t r e e ;

t r e e = a v l c r e a t e ( int compare , NULL, NULL) ; int i ;

for ( i =0; i <16; i ++)f

int  element = ( int ) malloc ( sizeof ( int ) ) ;

 element = i ;

void p = avl probe ( tree , element ) ; g

p o s t o r d e r i n t e g e r a v l ( tree >a v l r o o t ) ; puts ( "" ) ;

return 0 ; g

Note that the manual of libavl may be dicult to read; the code above comes from modifying the avl-test.c in libavl. When the code is compiled with avl.c, it correctly outputs an AVL tree.

7 (3 (1 (0 , 2 ), 5 (4 , 6 )), 11 (9 (8 , 10 ), 13 (12 , 14 (, 15 ))))

In this problem, you are asked to download libavl to play with three di erent trees: height-bounded binary search tree (bst.c), AVL tree (avl.c) and Red-Black tree (rb.c).

(1) (20%) Write a program hw6 3 1 that inserts the 32 standard keywords in C in Problem 6.1(7) using the order they are provided to the three di erent trees. Output the resulting trees with a format similar to the output above.

(2) (30%) Write a program hw6 3 2 that randomly permutes the order of the 32 standard keywords before they are inserted to the three di erent trees. For 10000 di erent random rounds, record the height of the trees that you get. Then, ll in the following the table and brie y state your ndings.

tree type maximum height minimum height average height height-bounded binary search tree

AVL tree Red-Black tree

You are strongly encouraged to to talk to the TAs if you encounter problem using/compiling libavl.

3 of 4

(4)

Data Structures and Algorithms (NTU, Class 01/02, Spring 2011) instructor: Hsuan-Tien Lin

Submission File

Please submit your written part of the homework on all problems together before the deadline (06/17/2011) to the TA at CSIE R217. Also, you need to upload your coding part as a single ZIP compressed le to CEIBA. The zip le should be like b86506054.zip, where the le name should be changed to your own school ID. The ZIP le should contain the following items:

 hw6 2.c or hw6 2.cpp

 hw6 3 1.c or hw6 3 1.cpp

 hw6 3 2.c or hw6 3 2.cpp

The TAs will use the Makefile provided on the course website to test your code. Please make sure that your code can be compiled with the Makefile on CSIE R217 linux machines.

4 of 4

參考文獻

相關文件

The bit rate which has more than 4 fail packets to transmission will not be sample, so if all bit rate have fail packets more than 4 or all predict time are too long, we can

In addition, some appropriate and widely used joint models of a latent variable and the cluster measurements are proposed to find the most possible occurring value of a latent

Students can get zero scores and/or get negative scores and/or fail the class and/or be kicked out of school and/or receive other punishments for those kinds of misconducts..

Two optimization problems (P 1 ) and (P 2 ) are equivalent if the optimal solution of one problem can be transformed to that of the other so that solving one the problems also gives

Let the coefficient of x 1 be reduced to 1 and use the primal feasibility, dual feasibility and complementary slackness to verify the optimal solution does not change.. (b) What if

Each cabinet requires 250 hours of labor (this is 6 weeks of full time work) and uses 50 board feet of lumber.. Suppose that the company can sell a cabinet for $200, would it

 whole-day face-to-face classes for individual class level(s) - (i) all teachers and school staff (including teaching and non-teaching staff) directly employed by the school have

First, among the eight dimensions in the instrument of Quality of School Life (QSL), three have consistently produced significant differences between EMI schools and CMI schools