121 views

Uploaded by siddharth1k

- Aisciences Data Science With r v0
- hw05-solution.pdf
- hw04-solution.pdf
- Hw01 Solution(1)
- Code
- hw06-solution.pdf
- hw08-solution.pdf
- hw02-solution.pdf
- hw10-solution.pdf
- hw07-solution.pdf
- hw09-solution.pdf
- Homework 2 Solutions
- Following Are the Multiple Choice Questions
- Datastructure Questions
- Cublas Library
- 06 DS Linked Lists 1
- ExpertQGen : an Expert Automated Question Paper Generation System
- Syllabus_compe260_Fall2016
- 100 Top Data Structures and Algorithms m
- Array

You are on page 1of 7

CSCI-GA.1170-001/Summer 2016

Solution to Homework 3

Problem 1. (1 point) Illustrate the operation of heapsort on the array:

A = (19, 2, 11, 14, 7, 17, 4, 3, 5, 15)

By showing the values in array A after initial heapification and after each call to max-heapify.

Solution:

A

A

A

A

A

A

A

A

A

A

=

=

=

=

=

=

=

=

=

=

[19 , 15 , 17 , 14 , 7, 11 , 4, 3, 5, 2]

[17 , 15 , 11 , 14 , 7, 2, 4, 3, 5, 19]

[15 , 14 , 11 , 5, 7, 2, 4, 3, 17 , 19]

[14 , 7, 11 , 5, 3, 2, 4, 15 , 17 , 19]

[11 , 7, 4, 5, 3, 2, 14 , 15 , 17 , 19]

[7 , 5, 4, 2, 3, 11 , 14 , 15 , 17 , 19]

[5 , 3, 4, 2, 7, 11 , 14 , 15 , 17 , 19]

[4 , 3, 2, 5, 7, 11 , 14 , 15 , 17 , 19]

[3 , 2, 4, 5, 7, 11 , 14 , 15 , 17 , 19]

[2 , 3, 4, 5, 7, 11 , 14 , 15 , 17 , 19]

#

#

#

#

#

#

#

#

#

#

After

After

After

After

After

After

After

After

After

After

initial heapification

extracting 19

extracting 17

extracting 15

extracting 14

extracting 11

extracting 7

extracting 5

extracting 4

extracting 3

A = (4, 6, 3, 5, 0, 5, 1, 3, 5, 5)

By showing the values in array C after each loop, and the final array B.

Solution:

C

C

C

C

=

=

=

=

0

[0 ,

[1 ,

[1 ,

[0 ,

1

0,

1,

2,

1,

2

0,

0,

2,

2,

3

0,

2,

4,

2,

4

0,

1,

5,

4,

5

0,

4,

9,

5,

6

0]

1]

10]

9]

#

#

#

#

After

After

After

After

initialization

counting elements

computing running sum

producing sorted B

1 2 3 4 5 6 7 8 9 10

B = [0 , 1, 3, 3, 4, 5, 5, 5, 5, 6] # Sorted B

Problem 3. (1 point) Illustrate the operation of radix sort on the array:

A = (392, 517, 364, 931, 726, 912, 299, 250, 600, 185)

By showing the values in array A after each intermediate sort.

Solution:

A = [392 ,

517 ,

364 ,

[250 ,

600 ,

931 ,

[600 ,

912 ,

517 ,

[185 ,

250 ,

299 ,

1

931 ,

726 ,

912 ,

299 ,

250 ,

600 ,

185]

->

392 ,

912 ,

364 ,

185 ,

726 ,

517 ,

299]

->

726 ,

931 ,

250 ,

364 ,

185 ,

392 ,

299]

->

364 ,

392 ,

517 ,

600 ,

726 ,

912 ,

931]

A = (0.88, 0.23, 0.25, 0.74, 0.18, 0.02, 0.69, 0.56, 0.57, 0.49)

By showing the final array B of sorted buckets.

Solution:

B = {0:

1:

2:

3:

4:

5:

6:

7:

8:

9:

[0.02]

[0.18]

[0.23 , 0.25]

[]

[0.49]

[0.56 , 0.57]

[0.69]

[0.74]

[0.88]

[]}

Problem 5. (3 points) Consider a d-ary heap a generalization of a binary heap, in which all

internal nodes (with at most one exception) have d children. Design a method to store a d-ary

heap in an array and derive the expressions for:

(a) Parent of i-th node in a d-ary heap.

(b) j-th child of i-th node in a d-ary heap.

(c) Maximum number of nodes of height h in any n-element d-ary heap.

(d) Maximum height of any n-element d-ary heap.

Solution:

(a) Parent of i-th node in a d-ary heap.

(b) j-th child of i-th node in a d-ary heap.

Assuming 1-based indexing, we can extend the standard level-wise method of storing

binary heaps to d-ary heaps by taking:

i1

,

par ent(i) =

d

child(i, j) = d(i 1) + j + 1.

2

n1

As par ent(i) = i1

,

the

parent

of

the

last

node

is

par

ent(n)

=

. Defining nh as

d

d

the number of nodes of height h, and observing that the parent of the last node is the

last non-leaf node, we can express the number of leaf nodes as:

n1

.

n0 = n

d

Removing all leaf nodes from the heap of size n would result in a heap of size n0 :

n1

0

n = n n0 =

d

0

with n0 leaves:

0

n 1

0

0

n0 = n

d

n1

1

n1

d

=

d

d

n1

n1d

=

.

d

d2

Removing all leaf nodes from the heap of size n0 would result in a heap of size n00 :

n00 = n0 n00

n1

n1d

n1

+

=

d

d

d2

n1d

=

d2

with n000 leaves:

n00 1

=n

d

n1d

1

n1d

d2

=

d2

d

n1d

n 1 d d2

,

=

d2

d3

suggesting a general expression for a number of leaves in a heap obtained from a heap

of size n by removing all leaf nodes k times:

n 1 d ... d k1

n 1 d ... d k

(k)

n0 =

dk

d k+1

Pk

Pk1 i

n i=0 d

n i=0 d i

=

dk

d k+1

k

k+1

1

n dd1

n d d11

=

for d > 1.

dk

d k+1

n000

00

We now observe that the leaves in heap H 0 , obtained from heap H by removing all leaf

nodes, had height 1 in the original heap H. The leaves in heap H 00 , obtained from heap

H 0 by removing all leaf nodes, had height 1 in H 0 and height 2 in H. In general, nh the

number of nodes of height h in an n-element d-ary heap H is the number of leaves in

H (h) , obtained from H by removing all leaf nodes h times:

h+1

h

1

n d d11

n dd1

for d > 1.

nh =

dh

d h+1

(d) Maximum height of any n-element d-ary heap.

We notice that a maximum number of nodes in a d-ary heap of height h is observed when

the last level is full, and a minimum number of nodes when the last level has only one

node:

d h+1 1

for d 6= 1,

d 1

dh 1

nmin (h) = d 0 + d 1 + ... + d h1 + 1 =

+ 1 for d 6= 1.

d 1

This allows us to provide a tight bound on the size of a d-ary heap of height h (we assume

d 6= 1 for the rest of this problem):

d h+1 1

dh 1

+1 n

.

d 1

d 1

With integer n we can get rewrite and then reduce the inequality as follows:

dh 1

d h+1 1

<n

,

d 1

d 1

d h 1 < n(d 1) d h+1 1,

d h < n(d 1) + 1 d h+1 ,

h < logd (n(d 1) + 1) h + 1.

For integer n and real x:

n 1 < x n iff

n = dxe,

and so:

h = logd (n(d 1) + 1) 1.

Problem 6. (4 points) Consider a min-priority queue representing a set of integers and supporting the following operations: insert(k) to insert element with value k, get-min() to return

the minimum element, and extract-min() to remove and return the minimum element.

Give pseudocode and worst-case running time for each operation, assuming the priority queue

is implemented with the following data structures:

4

(b) Ordered array.

(c) Unordered linked list.

(d) Ordered linked list.

(e) Min-heap.

Solution:

(a) Unordered array of size n referenced by variable array:

insert(k): Without the need to maintain order, we can insert in O(1) by adding

the new element to the end of the array.

def insert (k ):

array . append (k )

We note that the true worst case is O(n) and is observed when the array needs to

be reallocated (though amortized running time is still constant).

get-min(): With no order to rely upon, we have to resort to a O(n) linear scan.

assert len ( array ) > 0

index = find - min ( array ) # O(n)

return array [ index ]

extract-min(): As with get-min(), we have no way around a O(n) linear scan.

We dont, however, need to spend another O(n) closing the gap left after removing

the element, as with no order to worry about we can just fill the hole with the last

element.

assert len ( array ) > 0

index = find - min ( array ) # O(n)

value = array [ index ]

exchange ( array [ index ], array [ len ( array ) - 1])

array . delete ( len ( array ) - 1) # O(1)

return value

(b) Ordered array of size n referenced by variable array:

insert(k): One way to insert the element into a sorted array is to use modified

binary search to find correct position in O(lg n) time, then insert and shift the elements in O(n). An alternative is to "drag" the element into its correct position by

successive exchanges (not unlike insertion sort). Both ways are O(n) in the worst

case.

def insert (k ):

index = find - insert - position ( array , k) # O(lg n)

5

get-min(): It is more convenient to represent a min-heap with an array sorted

in non-increasing order (so we can delete the minimum element in O(1)). The

minimum element is then in the last position and can be accessed in O(1).

assert len ( array ) > 0

return array [ len ( array ) - 1] # O(1)

extract-min(): Deleting the last element does not require closing any gaps and

can be done in O(1):

assert len ( array ) > 0

value = array [ len ( array ) - 1] # O(1)

array . delete ( len ( array ) - 1) # O(1)

return value

(c) Unordered linked list of size n referenced by variable list:

insert(k): Without the need to maintain order, we can insert in O(1) by adding

the new element to the head of the list.

def insert (k ):

list . insert (k , list . head ())

We note that the linked list data does not have to be contiguous and we are unaffected by the arrays "true worst case" when the storage needs to be reallocated.

get-min(): As with the unordered array, we have to resort to a O(n) linear scan.

return find - min ( list ) # O(1)

extract-min(): As with get-min(), we have no way around a O(n) linear scan.

There are, however, no gaps to be closed we can delete from any position in O(1).

# O(n) to find , O(1) to remove

return find - and - remove - min ( list )

(d) Ordered linked list of size n referenced by variable list:

insert(k): Linked lists do not support indexing, so we cannot use binary search to

find the insertion position and have to resort to a linear scan. Once the position is

found, however, we can insert in O(1) (assuming the position is given as a reference,

not an index):

def insert (k ):

ref = find - insert - position ( list , k) # O(n)

list . insert (k , ref ) # O(1)

6

element is the head of the list and can be accessed in O(1):

return list . head () # O(1)

extract-min(): We can delete at any position in O(1):

min = list . head () # O(1)

remove - head ( list ) # O(1)

return min

(e) Min-heap referenced by variable heap:

insert(k): We can insert an element into the heap in O(lg n) by adding it as the last

node and then percolating it up until the min-heap property is no longer broken:

def insert (k ):

++ heap . size

i = heap . size

heap [i] = k

while i > 0 and heap [ parent (i )] > heap [i ]: # O(lg n)

exchange ( heap [ parent (i )] , heap [i ])

i = parent (i)

get-min(): The minimum element in a min-heap is at the root and can be accessed

in O(1):

return heap [0] # O(1)

extract-min(): We can remove the minimum element from the heap by replacing

the root element with the last element, excluding the last element from the heap,

and rebuilding the heap, all in O(lg n):

min = heap [0]

heap [0] = heap [ heap . size - 1]

-- heap . size

min - heapify ( heap , 0) # O(lg n)

return min

- Aisciences Data Science With r v0Uploaded byVăn Hoàng Long
- hw05-solution.pdfUploaded bysiddharth1k
- hw04-solution.pdfUploaded bysiddharth1k
- Hw01 Solution(1)Uploaded bysiddharth1k
- CodeUploaded bywootwoooowhooooyeah
- hw06-solution.pdfUploaded bysiddharth1k
- hw08-solution.pdfUploaded bysiddharth1k
- hw02-solution.pdfUploaded bysiddharth1k
- hw10-solution.pdfUploaded bysiddharth1k
- hw07-solution.pdfUploaded bysiddharth1k
- hw09-solution.pdfUploaded bysiddharth1k
- Homework 2 SolutionsUploaded bysiddharth1k
- Following Are the Multiple Choice QuestionsUploaded bymax4one
- Datastructure QuestionsUploaded by7killers4u
- Cublas LibraryUploaded byserkano77
- 06 DS Linked Lists 1Uploaded byMin Min
- ExpertQGen : an Expert Automated Question Paper Generation SystemUploaded byInternational Journal of Innovative Science and Research Technology
- Syllabus_compe260_Fall2016Uploaded byjoemama123123123
- 100 Top Data Structures and Algorithms mUploaded byYashika Varshney
- ArrayUploaded bysiti_ismail_61
- List as y Generic OsUploaded byAndrea Lizeth Arriaga Garcia
- Chap 01-1Uploaded byAidaMustapha
- vivavoceUploaded byManu Kothari
- 2010 CSC Placement Paper - 1Uploaded byAbhisek Mukherjee
- mcqlistcsUploaded bykusum
- jwbn hackerrankUploaded byUrratul Aqyuni
- DataStructuresUsing c++Uploaded bya.vidyanand
- data structure HW2Uploaded byjitenderthakur490
- BeapUploaded bynarendra
- Program c#Uploaded byShanmuga Sundaram Chellam

- Data_Extract_From_World_Development_Indicators_revised.xlsxUploaded bysiddharth1k
- 02_06Uploaded bysiddharth1k
- hw10-solution.pdfUploaded bysiddharth1k
- hw07-solution.pdfUploaded bysiddharth1k
- Database assignment 2 solution NYUUploaded bysiddharth1k
- Assignment 2Uploaded byshashank
- hw06-solution.pdfUploaded bysiddharth1k
- Working of Communication DepartmentUploaded bysiddharth1k
- hw02-solution.pdfUploaded bysiddharth1k
- hw09-solution.pdfUploaded bysiddharth1k
- hw08-solution.pdfUploaded bysiddharth1k
- hw01Uploaded bysiddharth1k
- Homework 2 SolutionsUploaded bysiddharth1k
- Speed MathematicsUploaded bytarang_tushar
- Letter of ComplianceUploaded bysiddharth1k

- wls_enUploaded bymr3478
- Finite fields overviewUploaded byjohndoe314
- DIALux Setup InformationUploaded byHamza Agaplex
- CUPTI LibraryUploaded byde7yT3iz
- C Programming TutorialsUploaded byanioli
- Chaos FractalsUploaded byMiguel Angel
- 4 Characteristics and Kinds of Qualitative Research (3)Uploaded bymaygracedigol
- civic leadership workshop curriculum lesson plansUploaded byapi-377028089
- Bill Stewart Essay 3Uploaded byDavid Sugden
- Sheikh Nasir KabaraUploaded byMaghili Shehu Kabara
- ReadmeUploaded byDon Cote
- The Neurobiology of SorceryUploaded byandre magela
- AnItalianGrammarWithExercises_10010190Uploaded bysoltero2
- Client InstallUploaded byJuan Miguel Kattan
- Key to the Understanding of the ScripturesUploaded bySheranikally
- The logical structure of the naya method of the Jainas* - Part 7Uploaded byp.b3992
- Sample Information Technology Lecturer ResumeUploaded byRekhaa Kale
- Mod BusUploaded bycarlosprivitera
- PUPC 2016 Online-Entropy&StatMechUploaded byNguyễn Quang Dũng
- Body Idioms.docUploaded bySuzana
- 12 Watson Gender DefUploaded byobladi05
- 2778A ENU CompanionUploaded byСаша Ивић
- i2c_for1200Uploaded byfreris
- Slides_Ch_1_3Uploaded byfirefly_light20068718
- Evaluation of Discrete Event Wireless Sensor Network SimulatorsUploaded byijcsn
- Plex_856_v04010_04_2015Uploaded byAmit Garag
- 10.1.1.85Uploaded byAbhilash Mohapatra
- C++ TambolaUploaded byVishal Madan
- Reductions Yesdil Yes Grammar AlintiUploaded byottomana
- SAP-QUERYUploaded byCarlos