11 views

Uploaded by Aditya Mishra

2sol

- fibonaci_1
- Binary Trees Misc questions
- Threaded Binary Trees
- Datastructures in C++
- 1508.04872
- CS301 Notes
- BinomialHeaps New
- Tree Manager
- Pointer.f90
- Webyog Prep
- New Microsoft Word Document
- Augmented Data Structure
- Performance Analysis of BSTs in System Software
- Well Written Paper
- Data Structures
- eee qb
- Predictive Modeling Using Decision Trees
- L00-introduction_eng.pdf
- Multi Level Intro
- Evaluation of a New Incremental Classification Tree Algorithm for Mining High Speed Data Streams

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.

- fibonaci_1Uploaded byArockiaruby Ruby
- Binary Trees Misc questionsUploaded bymanan00
- Threaded Binary TreesUploaded byAnonymous V7P5fNQI
- Datastructures in C++Uploaded byVinoth Chandrasekaran
- 1508.04872Uploaded byErlander Spirit
- CS301 NotesUploaded byRizwanIlyas
- BinomialHeaps NewUploaded bysonu
- Tree ManagerUploaded bysudhascareer
- Pointer.f90Uploaded byvaskomane
- Webyog PrepUploaded byPriyatanu Bazinga Dey
- New Microsoft Word DocumentUploaded bySachin Sapkale
- Augmented Data StructureUploaded bySirc Ann Ares
- Performance Analysis of BSTs in System SoftwareUploaded byHoang Dao
- Well Written PaperUploaded byYuri Vishnevsky
- Data StructuresUploaded byMohammed Jeelan
- eee qbUploaded byindhu_giri2
- Predictive Modeling Using Decision TreesUploaded byTruely Male
- L00-introduction_eng.pdfUploaded bybalashivashree9593
- Multi Level IntroUploaded byPrem Rj
- Evaluation of a New Incremental Classification Tree Algorithm for Mining High Speed Data StreamsUploaded bymlaij
- Tulane AI finalprepUploaded byGustavo Eladio
- commref_mb3.2Uploaded byOtávio Marques
- CH10 Tree Structured IndexingUploaded byமனிதம்
- psr_0607_Chap10Uploaded bymiki1233
- tcs99Uploaded byAnonymous RrGVQj
- regressionUploaded byAshik Mahmud
- Data Structs 1Uploaded bybhar
- Induction as Model SelectionUploaded bymbush60
- BSTUploaded byNamrta Ratra
- Using Customer Churn Data to Analyse Customer Retention Project ReportUploaded bySunil Kamat

- cisb351Uploaded byAditya Mishra
- Exercise Set2 SolutionsUploaded byAditya Mishra
- comptia-cybersecurity-analyst-(cs0-001)Uploaded byAditya Mishra
- GS2011 QP ComputerUploaded by'Vinay Kumar Machamraj
- Remainder.docxUploaded byAditya Mishra
- CMI Maths.docxUploaded byAditya Mishra
- logic_problemSet.pdfUploaded byAditya Mishra
- Linux Important command.txtUploaded byAditya Mishra
- Economics_26072017Uploaded byAditya Mishra
- Cloud affairs - Probability.docxUploaded byAditya Mishra
- Gen English.pdfUploaded bysrinu09876
- Advt Eng Fraud Mgmt Chief Off Security 01122016Uploaded byAditya Mishra
- MATHS Exam Pape 2012Uploaded bysameer
- probabilityP.pdfUploaded byKarn Kumar
- Gen EnglishUploaded byAditya Mishra
- 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
- Other Geometry pUploaded byAditya Mishra
- s03nfUploaded byAditya Mishra
- e2 AnswersUploaded byAditya Mishra
- Gate New Syllabus Computer Science and Information Technology for 2016Uploaded byersayantan
- 15-1Uploaded byAditya Mishra
- 4 Regular ExpressionsUploaded byAditya Mishra
- 14-1UUploaded byAditya Mishra
- FinalUploaded byAditya Mishra
- probabilityP.pdfUploaded byKarn Kumar
- cs151_sq07_q4_solUploaded byAditya Mishra
- Solution 0Uploaded byAditya Mishra

- Clinically Significant Data Integration StudioUploaded byupendernathi
- University of New Hampshire Stormwater CenterUploaded byHRNERR
- 11409182 Term PaperUploaded byVinayRajAwasthi
- Closed-Loop Control of Hybrid Stepper Motor DrivesUploaded byChanthu C Nair
- LG Washer Service ManualUploaded byianalucy
- Samsung LE A656A Firmware UpgrateUploaded byJATG
- 200912516195110702Uploaded bydghfhh444t5566gfg
- Materials Performance January 2017Uploaded bySetiawan Wijayanto
- SISO MMSE-PIC detector in MIMO-OFDM systemsUploaded byIJMER
- Datapump11g Remapdata Twp-1Uploaded byJean-Christophe Dauchy
- Thyristor Switch ApplicationUploaded byRiyah_Rae
- slua143Uploaded byTonia Kata
- BSC_20160504Uploaded byOuhourou NA Kazi
- As NZS 1300-2009 Electrical Equipment for Mines and Quarries - Bolted Explosion-protected Three-phase Cable cUploaded bySAI Global - APAC
- Optoisolation for Intelligent Power ModulesUploaded byKiran Veesam
- AMKOR TECHNOLOGY INC 10-K (Annual Reports) 2009-02-24Uploaded byhttp://secwatch.com
- Press Kit Overall Redevelopment of the 1st Floor of the Eiffel TowerUploaded bytahanasr
- NITheP Bursary Title and Abstract 2012Uploaded byRene Kotze
- man8.pdfUploaded byCarel De Jager
- Project ObjectivesUploaded byडॉ. कनिष्क शर्मा
- Cutting Fat Adding MuscleUploaded byAndrea Di Maio
- Guide UsdEditor EnUploaded byslimamri
- Isobutylene gas.pdfUploaded byzhexiong_zeng
- bhelUploaded byabhishek kashyap
- Ibm Mainframe Ibm3172Uploaded byxsimio
- Sri Mittapalli College of EngineeringUploaded byandhracolleges
- Siiemens Controllers Um353Uploaded byGeorge Markas
- FR-E700 Product Details - Mitsubishi Electric InversorUploaded byGeoplace
- VAMUploaded byParmar Chandresh
- 9656776 EMC Academic Alliance ProgramUploaded bynish009