代写CSCI 4041 Algorithms and Data Structures - Spring 2025 Homework 4 - Trees and Optimization代做留学生SQL

CSCI 4041 Algorithms and Data Structures - Spring 2025

Homework 4 - Trees and Optimization

Due Date: Friday, April 11, 2025 by 11:59pm.

Instructions: This is an individual homework assignment. You may work together to discuss con-cepts, but the solutions must be your own work. Submit your answers on Canvas, which is linked to Gradescope (be sure to correctly map the page for each problem). We will not grade *Practice Problems*, however, you are responsible for knowing how to solve them in order to prepare for quizzes and exams.

Problem H4.1: BSTs and Rotations (20 points)

Consider the following algorithm.

SUCCESSIVE-RIGHT-ROTATES(z)

1 if z != NIL

2 while z.left != NIL

3 RIGHT-ROTATE(T, z)

4 z = z.p

5 return z

(a) Draw the tree that results from applying SUCCESSIVE-RIGHT-ROTATES where the input z is the root of the tree below.

(b) Let T be any BST, and n the number of nodes. What is best case asymptotic runtime of SUCCESSIVE-RIGHT-ROTATES over all BSTs of size n? What is the worst case? (Hints: (i) the runtime will be related to the number of right rotations. (ii) Note another way to think about this question is what type of BST with n nodes will result in the fewest rotations, and what type will will result in the most rotations?)

(c) Consider the algorithm below.

ALG-HW4(T)

1 z = T.root

2 while z != NIL

3 z = SUCCESSIVE-RIGHT-ROTATES(z)

4 z = z.right

Draw the tree that results from applying ALG-HW4 to the (original) BST is part (a).

(d) What is worst case asymptotic number of right rotations (over all BSTs of size n) done by ALG-HW4? State the runtime and provide a brief justification, although you do not need to provide a rigorous proof.

Problem H4.2: AVL Trees (20 points)

Recall that to handle the LR case in an AVL tree tree for a node x with ∣balance factor∣ > 1, we first LEFT-ROTATE(T, x.left) and then RIGHT-ROTATE(T, x). We call two successive rotations like this a double rotation.

For each of the double rotation algorithms described below, do the following:

1. Draw the the double rotation in a figure similar to Figure 13.2. Your diagrams should (i) show the relationship changes between nodes x, y, z and the parent; (ii) include subtrees α, β, γ, and ω; (iii) show how the height of the nodes x, y, and z have changed from their previous heights (since for an AVL tree we will need to calculate the balance factor at each node).

2. In pseudocode or another language of your choice, write the following double rotation functions as direct changes to the pointers and any other needed information, e.g., the height at each node (x.height). Do this directly, that is, do not call other rotation functions.

(a) RL-ROTATE(T, x):

Let y = x.right and z = y.left

Double Rotation: RIGHT-ROTATE(T,y) and then LEFT-ROTATE(T,x)

(b) DOUBLE-RIGHT-ROTATE(T, x):

Let y = x.left and z = y.left

Double Rotation: RIGHT-ROTATE(T,x) and then RIGHT-ROTATE(T,y)

(c) REVERT-LR(T, x) (*Practice Problem*):

Let x = y.right and z = y.left

Double Rotation: LEFT-ROTATE(T,y) and then RIGHT-ROTATE(T,y)

Problems H4.3: Red-Black Trees (20 points)

Let T be a red-black tree and define

Φ(T) = (number of black nodes with no red children)

+2 (number of black nodes with two red children).

What is the change in Φ(T) in each of the following cases? For each case give a justification of your answer.

[Note: for all parts below there might not be a single answer. That is, Φ might change by different amounts depending upon the whether certain nodes involved are red or black. In such cases list all possible potential changes that might result (e.g., change = 1, 0, −1) and explain when each such change will occur.]

(a) After inserting a new node, but before applying RB-INSERT-FIXUP (regardless of whether the fixup would be needed after the insertion or not). Note: RB-INSERT-FIXUP, described in Section 13.3, fixes the tree if any of the red-black properties are violated after insert.

(b) After case 3 of RB-INSERT-FIXUP.

(c) After case 1 of RB-INSERT-FIXUP. (*Practice Problem*)

Problems H4.4: B-Trees (20 points)

Consider the 2-3-4 tree below

(a) Show the resulting tree after A is inserted using B-TREE-INSERT.

(b) Show the resulting tree after B is inserted using B-TREE-INSERT (into the tree from (a)).

(c) Show the resulting tree after Q is inserted using B-TREE-INSERT (into the tree from (b)).

(d) Show the resulting tree after D is inserted using B-TREE-INSERT (into the tree from (c)).

Problems H4.5: Dynamic Programming (20 points)

Consider the subsequence matching problem. Suppose that X and Y are both sequences of n char-acters. Suppose further than sometimes a character in one sequence will be replaced by a single character in the other sequence, but sometimes it will be replaced by a couple of characters in the other sequence (e.g., the letter ‘f’ may be replaced by ‘ph’ in a misspelling). Moreover, suppose that for each character α in X you have a probability function πα which can take either 1 or 2 arguments.

More specifically,

πα(β) = the probability that α in X is replaced by β in Y

πα(β, γ) = the probability that α in X is replaced by the pair of consecutive characters β, γ in Y .

As an example, consider

with the subsequences consisting of the underlined terms and the matches given by the arrows. The total matching score in this case would be

πb(b) + πc(c, c) + πd(b) + πa(d, a) + πb(b).

Note if a character in X matches a pair in Y those characters must be consecutive in Y — it is not sufficient for them to be consecutive only in the subsequence.

Write down a recurrence for computing the largest possible matching score in O(n 2 ) time. Justify the recurrence and the O(n 2 ) time. Your justification does not need to be rigorous, but should include enough detail that it is clear what your recurrence measures, how you derived it, and why computing the largest matching score takes O(n 2 ) time. Note here you just need to write the recurrence and the justifications. You do not need to actually write the algorithm, nor do you need to be concerned with storing or printing the optimal subsequences.



热门主题

课程名

mktg2509 csci 2600 38170 lng302 csse3010 phas3226 77938 arch1162 engn4536/engn6536 acx5903 comp151101 phl245 cse12 comp9312 stat3016/6016 phas0038 comp2140 6qqmb312 xjco3011 rest0005 ematm0051 5qqmn219 lubs5062m eee8155 cege0100 eap033 artd1109 mat246 etc3430 ecmm462 mis102 inft6800 ddes9903 comp6521 comp9517 comp3331/9331 comp4337 comp6008 comp9414 bu.231.790.81 man00150m csb352h math1041 eengm4100 isys1002 08 6057cem mktg3504 mthm036 mtrx1701 mth3241 eeee3086 cmp-7038b cmp-7000a ints4010 econ2151 infs5710 fins5516 fin3309 fins5510 gsoe9340 math2007 math2036 soee5010 mark3088 infs3605 elec9714 comp2271 ma214 comp2211 infs3604 600426 sit254 acct3091 bbt405 msin0116 com107/com113 mark5826 sit120 comp9021 eco2101 eeen40700 cs253 ece3114 ecmm447 chns3000 math377 itd102 comp9444 comp(2041|9044) econ0060 econ7230 mgt001371 ecs-323 cs6250 mgdi60012 mdia2012 comm221001 comm5000 ma1008 engl642 econ241 com333 math367 mis201 nbs-7041x meek16104 econ2003 comm1190 mbas902 comp-1027 dpst1091 comp7315 eppd1033 m06 ee3025 msci231 bb113/bbs1063 fc709 comp3425 comp9417 econ42915 cb9101 math1102e chme0017 fc307 mkt60104 5522usst litr1-uc6201.200 ee1102 cosc2803 math39512 omp9727 int2067/int5051 bsb151 mgt253 fc021 babs2202 mis2002s phya21 18-213 cege0012 mdia1002 math38032 mech5125 07 cisc102 mgx3110 cs240 11175 fin3020s eco3420 ictten622 comp9727 cpt111 de114102d mgm320h5s bafi1019 math21112 efim20036 mn-3503 fins5568 110.807 bcpm000028 info6030 bma0092 bcpm0054 math20212 ce335 cs365 cenv6141 ftec5580 math2010 ec3450 comm1170 ecmt1010 csci-ua.0480-003 econ12-200 ib3960 ectb60h3f cs247—assignment tk3163 ics3u ib3j80 comp20008 comp9334 eppd1063 acct2343 cct109 isys1055/3412 math350-real math2014 eec180 stat141b econ2101 msinm014/msing014/msing014b fit2004 comp643 bu1002 cm2030
联系我们
EMail: 99515681@qq.com
QQ: 99515681
留学生作业帮-留学生的知心伴侣!
工作时间:08:00-21:00
python代写
微信客服:codinghelp
站长地图