11 views

Uploaded by Aditya Mishra

2sol

- lab6ASD1
- Binary Decision Trees
- CAA
- POP Report
- Heap java
- bstx
- Multicriteria Decision Making Software French2005
- Midterm2 Solutions
- Trees
- Fibonacci Heaps and Binary Heaps
- Bachman-DataStructureDiagrams.pdf
- Itc
- CPE311 Trees
- 53 More Algorithms
- Design and Analysis of Algorithm Lab Work File
- Chap-BKT-BB
- Particle Gibbs for Bayesian Additive Regression Trees
- Batch Signature Concept
- Csci 4511w Artificial
- sda

You are on page 1of 3

For each question, please write up detailed answers carefully. Make sure that you use notation and terminology

correctly, and that you explain and justify what you are doing. Marks may be deducted for incorrect/ambiguous

use of notation and terminology, and for making incorrect, unjustified or vague claims in your solutions.

1. Let D be an ordered Dictionary with n items implemented with an AVL tree. Show how to implement the

following method for D in time O(log n):

countAllinRange(k1 , k2 ): return the number of items in D with key k such that k1 6 k 6 k2 .

note: you may assume that the AVL tree supports a data field at each node x which stores the number of

nodes in the subtree rooted at x.

Solution : We will show how to implement a method AllinRangefromNode(v, k1 , k2 ) where v is a node in D.

This is enough of course since AllinRange(k1 , k2 ) = AllinRangefromNode(root, k1 , k2 ). In addition we will implement

countAllBiggerThan(v,k) and countAllsmallerThan(v,k) that return the number of items in the subtree of D

rooted at node v, with keys bigger-than or smaller-than k respectively.

We denote by numnods(v) the data field supported by the AVL that stores the number of nodes in the subtree

rooted at v.

AllinRangefromNode(v, k1 , k2 ) {

if v = N IL return 0

if (v.key < k1 ) return AllinRangefromNode(rightchild(v), k1 , k2 )

if (v.key > k1 ) return AllinRangefromNode(lef tchild(v), k1 , k2 )

else return 1+ countAllBiggerThan(lef tchild(v), k1 ) + countAllSmallerThan(rightchild(v), k2 )

}

Why does this work? If v.key < k1 then we can safely ignore the elements in the left subtree of v and continue

with the same query to the right child of v. A similar argument justifies the third line. If, however,k1 6 v.key 6 k2

then v should be counted, and for the two subtrees of v we only need to count the elements that are bigger than k1

in the left subtree, and smaller than k2 in the right subtree.

How long does it take? We can bound the running time of this method by the height of the tree, that is

O(logn) plus the running time of the other two methods. This is since every time the conditions in line 2 or line

3 are satisfied in the recursion tree, we move to a deeper node. Hence there can be as many recursive calls to

AllinRangefromNode as the height of the tree.

Now we define countAllBiggerThan(v, k) and countAllSmallerThan(v, k).

countAllBiggerThan(v, k) {

if v = N IL return 0

if (v.key < k) return countAllBiggerThan(rightchild(v), k)

else return 1 + countAllBiggerThan(lef tchild(v), k) + numnodes(rightchild(v))

}

countAllSamllerThan(v, k) {

if v = N IL return 0

if (v.key > k) return countAllSmallerThan(lef ttchild(v), k)

else return countAllBiggerThan(rightchild(v), k) + numnodes(lef tchild(v))

}

CSC 263 H5S Homework Assignment # 2 solutuions Spring 2010

Why do they work? We analyze the first of the two methods (the other is proved similarly). If the condition

in line 2 holds then we can avoid looking at the left subtree as all elelemtns there are clearly too small. Otherwise,

all elements of the right subtree must be counted, the root must be counted and in addition all sufficiently large

elements in the right subtree should, hence the recursion.

How long do they take? As before its clear that the recursion can be no longer than the height of the tree,

as regardless of whether the condition in line 2 holds, we move to a child node of the original node.

2. Given two AVL trees T1 and T2 , where the largest key in T1 is less than the smallest key in T2 , Join(T1 , T2 )

returns an AVL tree containing the union of the elements in T1 and T2 .

Give an algorithm (in pseudocode) for Join that runs in time O(log n), where n is the size of the resulting AVL tree.

Justify the correctness and efficiency of your algorithm.

Solution:

Begin by computing the heights h1 of T1 and h2 of T2 . This takes time O(h1 + h2 ): You simply traverse a path

from the root, going to left child if the balance factor is -1, to the right child if it is positive, and to any of the

children if the balance factor is 0, until you reach a leaf. Assume that h1 > h2 ; the other case is symmetric.

Next, DELETE the smallest element x from T2 , leaving T20 of height h. This takes O(h2 ) time.

Find a node v on the rightmost path from the root of T1 , whose height is either h or h + 1, as follows:

v root(T1 )

h0 h1

while h0 > h + 1 do

if balance factor (v) = -1

then h0 h0 2

else h0 h0 1

v rightchild(v)

Let u denote the parent of v.

Form a new tree whose root contains the key x, whose left subtree is the subtree rooted at v and whose right subtree

is T20 .

Note that this is a valid binary search tree, since all the keys in the subtree rooted at v are in T1 and, hence, smaller

than x, and, by construction, x is smaller than or equal to all elements in T20 . The balance factor of the root of this

new tree is h h0 , which is either -1 or 0, so this new tree is a valid AVL tree. The height of this new tree is h0 + 1,

which is 1 bigger than vs height. Let the root of this new tree be the right child of u, in place of v. Again, since all

keys in this new tree are at least as big as u, this results in a valid binary search tree. This part of the construction

takes constant time.

Now, as in the INSERT algorithm, we go up the tree, starting at u, fixing balance factors and perhaps doing

a rotation. This takes O(h1 ) time. Note that the correctness follows from the condition at u before this process is

begun is a condition that can arise during the INSERT algorithm.

Since h1 , h2 O(log n), the total time taken by this algorithm is in O(log n).

3. Suppose we are using a priority queue Q to schedule jobs on a CPU. Job requests together with their priorities

are inserted into the queue. Whenever the CPU is free, the next job to execute is found by extracting the highest

priority job in Q. Using a heap implementation for Q the scheduler can both extract the next job from Q and add

a new job to Q in time O(log n) where n is the number of pending jobs in the queue.

CSC 263 H5S Homework Assignment # 2 solutuions Spring 2010

However, in practice, we want our scheduler to be able to do more than just insert jobs and extract the highest

priority job. In particular, we want our scheduler to be able to remove a job x from the job queue (Delete(x)) as

well as change the priority of a job x to some new value k (Change-Priority(x, k)). Show how to add the operations

Delete(x) and Change-Priority(x, k) to the heap data structure so that (a) they both run in time O(log n) and (b)

the resulting data structure after executing either these operations is still a heap. For both operations, you may

assume that parameter x gives you the index corresponding to job x in the array representation of the heap.

Solution:

For the delete operation, we start with taking the last item (lets say it has key k) and placing it in position

x. So now the original key of x is gone, and instead we have y there, and in addition the heap is of the

right size. But of course, its not necessarily a heap: y may agree or disagree with its parents/children. If y

is smaller than the key of the parent, we perform up-heap bubble. Otherwise, we perform down-heap bubble

(which, in the case that y is smaller than the keys of the children, will stop right away). Since there is either

up-heap or down-heap bubble, this will have ruinning timeof O(log n). The initial swap is of course O(1).

The change-priority is similar but even easier: All you have to do is change the key of poistion x to k, and

continue as the second phase of the delete(x) method above. In fact, you can even implement delete(x) by

performing the swap, and then using change-priority as a black box if you wish! Again, clearly this takes

O(log n).

4. [10 marks]

Consider two Binomial Heap H1 and H2 . You are required to create the union H of H1 and H2 and also to find the

minimum key in H. A standard way would be to perform

H Union(H1 , H2 )

m LookupMin(H).

m1 LookupMin(H1 )

m2 LookupMin(H2 )

m min(m1 , m2 )

H Union(H1 , H2 ).

Give an example of heaps H1 and H2 in which the first sequence of operations is faster, and one in which the

second is faster. Justify your answer.

Solution: Notice that the union operation will have the same inputs in both cases (the lookupMin operations

do not change the Binomial heaps). So the question boilds down to comparing the cost of the lookupMin operations.

For a Binomail heap J, denote by `(J) length of the linked list of Binomial trees in H. Recall that the cost of

lookupMin(J) is simply `(J). Hence, if we denote by u the running time of persorming the union of H1 and H2 we

get that the first method would run in time

u + `(H)

and the second in time

`(H1 ) + `(H2 ) + 1 + u.

An example in which the first method is faster is one where `(H) is bigger than `(H1 ) + `(H2 ) + 1 and vice versa.

If H1 and H2 contain list of binomial trees of all heights upto some k, then H will contains only one binomial tree

(of height k + 1, and so `(H) = 1 and `(H1 ) + `(H2 ) + 1 = 2k + 1. On the other hand, it is always the case that

`(H) 6 `(H1 ) + `(H2 ) (do you see why? equality will happen only when the binomial trees of H1 are all different

than those of H2 ) and so an example of the opposite kind does not exist.

- lab6ASD1Uploaded byZafarIqbal
- Binary Decision TreesUploaded bySazzad Hossain
- CAAUploaded byEDUARDOMOSTAJO
- POP ReportUploaded bydisguizedmagician
- Heap javaUploaded byaravind.arjun
- bstxUploaded bycarlone77
- Multicriteria Decision Making Software French2005Uploaded byKevinPatrónHernandez
- Midterm2 SolutionsUploaded byWang Chaoran
- TreesUploaded byjanurag1993
- Fibonacci Heaps and Binary HeapsUploaded byashishpatel_99
- Bachman-DataStructureDiagrams.pdfUploaded bymabderhakim_9570938
- ItcUploaded byImran Rahman
- CPE311 TreesUploaded byJaspher Jan Pasol
- 53 More AlgorithmsUploaded byMohammad Shahid Fazal
- Design and Analysis of Algorithm Lab Work FileUploaded byRaju
- Chap-BKT-BBUploaded byfirefly9x
- Particle Gibbs for Bayesian Additive Regression TreesUploaded byKevin P
- Batch Signature ConceptUploaded byArun Balaji
- Csci 4511w ArtificialUploaded byulairai
- sdaUploaded bybukhori
- ocaml-3.08-refmanUploaded byChiritoiu Tiberiu
- Open-pit Mine Production Scheduling Under Grade UncertaintyUploaded byzerg
- Adaptive Submodularity - Andreas KrausUploaded byLuciano Di Palma
- Edward de Bono- Water LogicUploaded byEyal Schneider
- Thesis Final VersionUploaded byowais800
- lecture22.pdfUploaded byMarien
- Estimation of Pore Water Pressure of Soil Using Genetic ProgrammingUploaded byMalik Asad Hameed
- Recursion Trees and Master MethodUploaded byHarsh Vardhan Jha
- Boris Ederov.bachelor.merkle Tree Traversal TechniquesUploaded byShiva prasad
- A P2P Platform for Real-Time Multicast Video Streaming Leveraging on Scalable Multiple Descriptions to Cope with Bandwidth FluctuationsUploaded byAIRCC - IJCNC

- Exercise Set2 SolutionsUploaded byAditya Mishra
- GS2011 QP ComputerUploaded by'Vinay Kumar Machamraj
- Remainder.docxUploaded byAditya Mishra
- comptia-cybersecurity-analyst-(cs0-001)Uploaded byAditya Mishra
- CMI Maths.docxUploaded byAditya Mishra
- Linux Important command.txtUploaded byAditya Mishra
- logic_problemSet.pdfUploaded byAditya Mishra
- cisb351Uploaded byAditya Mishra
- Advt Eng Fraud Mgmt Chief Off Security 01122016Uploaded byAditya Mishra
- MATHS Exam Pape 2012Uploaded bysameer
- probabilityP.pdfUploaded byKarn Kumar
- Gen EnglishUploaded byAditya Mishra
- Economics_26072017Uploaded byAditya Mishra
- Cloud affairs - Probability.docxUploaded byAditya Mishra
- Gen English.pdfUploaded bysrinu09876
- Other Geometry pUploaded byAditya Mishra
- e2 AnswersUploaded byAditya Mishra
- s03nfUploaded byAditya Mishra
- Gate New Syllabus Computer Science and Information Technology for 2016Uploaded byersayantan
- Aptitude Shortcuts and Mind Tricks for Boats and Streams Problems Type-I-www.ibpsguide.com (1)Uploaded byVikas Banpurkar
- Bharat Bill Payments SystemUploaded byAditya Mishra
- Midterm1 SolutionUploaded byAditya Mishra
- cs151_sq07_q4_solUploaded byAditya Mishra
- probabilityP.pdfUploaded byKarn Kumar
- Solution 0Uploaded byAditya Mishra
- 15-1Uploaded byAditya Mishra
- 14-1UUploaded byAditya Mishra
- 4 Regular ExpressionsUploaded byAditya Mishra
- FinalUploaded byAditya Mishra

- Artificial IntelligenceUploaded bySandeep Rao
- polynomial time primality test.pdfUploaded byvijay
- 2012-PP-DSE-MATH-EP(M1)Uploaded byTsoi Jason
- Lecture-8.pptxUploaded byNoor Ahmed
- Q3Uploaded byShaun Loo
- TRANSFER FUNCTIONS FROM BLOCK DIAGRAMS AND SIGNAL FLOW CHARTUploaded byYzza Veah Esquivel
- Steganalysis of Binary ImagesUploaded byKhaled Hassan Mostafa
- LabView Analysis ConceptsUploaded byRatna Giridhar Kavali
- Unit 2 UsefulUploaded byPrathap Vuyyuru
- fftw3Uploaded byRicardo Alonso Muñoz Canales
- Finite AutomataUploaded byAbhishekGupta
- Lab Report 01Uploaded byAmina Khalid
- SPM12 ManualUploaded byguy_prochilo6095
- Matlab Simplex TutorialUploaded byfabricio_monturil
- Cluster analysis using k mean algorithmUploaded bynooti
- The Portable Chris LanganUploaded bydylancatlow
- Multi-Biometric Cryptosystem using the Features of Fingerprint and Palmprint for Securing Biometric TemplatesUploaded byInternational Journal for Scientific Research and Development - IJSRD
- Convergence Hints (aspen)Uploaded bySaurabh Gupta
- FOPIDUploaded byrehan_sadiq1991
- Automata TheoryUploaded bypraveennallavelly
- Moore and Mearly MachineUploaded byJibran Elahi
- 46 CalculationUploaded byVimalMakadia
- ansysUploaded bySangbum Kim
- C2441ch4Uploaded byIvonne Prida
- Decision Making and AIUploaded byBhattarai CM
- Neraca Massa PPTUploaded byriris
- S06 ConvolutionUploaded byChakshuGrover
- Exam QuestionsUploaded byBob Ran
- Blanco Velasco 2008 Signal ProcessingUploaded byMidou Messa
- poly syUploaded byJomar Rabajante