Assignment5

.pdf
School
Pomona College**We aren't endorsed by this school
Course
COMS CS140
Subject
Computer Science
Date
Dec 18, 2024
Pages
4
Uploaded by AgentJellyfishPerson2658
CS140 - Assignment 5Due: Sunday, 2/25 at10pm 11:59pmhttps://xkcd.com/2584/For this assignment, you may (and are encouraged to) work with a partner.For the dynamic programming solutions, in addition to the algorithm, make sure to explicitly state:- What the table looks like (size and range of values).- How you initialize the table, i.e., any starting values- How you fill the table in, i.e., what indices you start at and how you proceed.- Where the final answer is.This can either be done with pseudocode or in plain language.1. CS Snack[10 points]The CS liaisons have asked for your help with shopping for the CS snack. There arenpossiblesnack options that you can choose from and you haveddollars to spend. Each snack optionhas been voted on with how excited the students are about the snack:e1, e2, ..., en, withhigher being better.Each snack also has a price,p1, p2, ..., pn.Your job is to pick whichsnacks so as to optimize the sum of the excitement of the snacks chosen while spendingddollars. Note that you can choose a given snack option multiple times.(a)[8 points]Give a dynamic programming solution that determines the maximum excite-ment sum, given the budget constraints (you don’t need to explicitly state what snacksare chosen, just the overall sum of the excitement).Make sure to explicitly state thesize of the table, which elements you fill in first, how you fill in the table, and where thesolution is found.Solution:A dynamic programming solution to the CS snack problem could look similar to the rodsplitting example where the rod is now the amount of money the liaisons have to workwith. Each split is now considered purchasing a snack and the remaining rod is now theremaining amount of the money. To determine the best solution spendingddollars.To do this we will build a table with two three rows. Row one will be the count for how1
Background image
excited students are about the said split. Row two will be the amount of money. Rowthree will be the split. Given the table ofe1, e2, ..., enandp1, p2, ..., pn, we can fill in thetable with three rows. At each value of d, we can make a mini table with the possibleoptions of the splits. Given a split, add the split’s value with how much is left followingthat split.For example, say we are given 18 dollars.Cheetos have an excitement ofvalue 5 with a cost of 5 while fruit loops have an excitement of value 7 with a cost of8. We can make splits with the given 18 with either Cheetos or fruit loops. Followingthe splits we will have either 13 or 10 dollars left. Added those excitements value withCheeto’s excitement value and fruit loops respectively. The max of the values is the oneto be placed in the 18 dollar excitement slot within the table in row 1. In row 3 for the18 dollar value is the dollar cost of which split we decided to choose. Continuing thistype of table filling in until we reach d. To find the answer we simply find the max inrow 1.(b)[2 points]State the running time of your algorithm in terms ofnthe number of snackoptions anddthe budget.Solution:Similar to the rod splitting solution, the running time will Θ(nd)2. Words[12 points]You are given a string of charactersS=s1, s2, ..., snwhere all non-alphabetic charactershave been removed (e.g.“thisisasentencewithoutanyspacesorpunctuation”) and a functiondict(w, i, j), which takes as input a stringwand two indicesiandjand returnstrueif thestringwi...jis a dictionary word andfalseotherwise.(a)[10 points]Give a dynamic programming solution that determines whether the stringSconsists of a sequence of valid dictionary words. Make sure to explicitly state the sizeof the table, which elements you fill in first, how you fill in the table, and where thesolution is found.Solution:Similar to the problem for the LCS function, we are looking within a given string whetherthe the subsiding at the inputted indices are a match with a word in the dictionary. Wecould create a table with the x axis being values from 1 to len(S) and the y axis beingeach character of the string S. How this table will be filled is when at say position 3,4 intable, we will get the 3rd character in the string and go from that character 4 charactersover and input that sub string into the DICT function. The table will look somethinglike this below.2
Background image
As can be seen in the table, I used the example case of ‘thisisasentencewithoutanys-pacesorpunctuation’. I only included the first couple of characters to present how myalgorithm would work. In the first row of the character ’T’, we can see that 3 charactersafter T is a true as the call to DICT() was DICT(S, 1, 1+3). This returns true as theword is ’this’. Once the word is confirmed, continue to the next word. The next wordshould start after ’this’ would would be the 5th character (1 + 3 + 1). Continue thisfor the whole substring. If there is ever a point where a row does not have a true value,go back a step to the next possible true in the prevouisly checked row. For example, ifwe were checking the word ”Wordsapple”, it will return false if it only checks the firsttrue in each word. But if we went back after using the sub-string word ’word’ and thenchecking ’sapple’ which is not a word. But if we went back to ’words’ and then ’apple’we would see that this case is actually true. Another way to view this, is there a possiblepathway that will use all the letters. If we go down a path and it runs to a deadend,take a step back and make a different path.(b)[2 points]State the running time of your algorithm assuming calls todictareO(1).My runtime of my algorthim would be Θ(n2) where n is the length of S as it calls dict foreach character’s sub string to fill in the table. N for the amount of rows in the table andlog n for the columns as on average it will have n/2 size sub-strings.NN/2 = 0.5N23. Party[15 Points]You’ve been asked to design an algorithm for deciding who to invite to a company party. Thestructure of the company can be described by a tree as follows: the CEO is at the root, belowthe root are VPs, below them are directors, below them are manages, etc., etc., until you getdown to the leaves (summer interns). The tree is not necessarily binary; some non-leaf nodesmay have one “child”, others two, and others even more.To make the party fun, we won’t invite an employee along with their immediate supervisor(their parent in the tree). In addition, each person has been assigned a positive real numbercalled theircoefficient of fun.The goal is to invite employees so as to maximize the total3
Background image
sum of the coefficients of fun of all invited guests, while not inviting an employee with theirimmediate supervisor.(a)[4 points]Describe a recursive algorithm for this problem (i.e. non-dynamic program-ming).Assume that the tree is represented as a collection of nodes with links fromparents to children and also from children to parents.The tree is passed to you bygiving you a reference to the root.(b)[8 points]Describe a DP algorithm for this problem. You may assume that each of thennodes in the tree has a unique number between 1 andnassociated with it. You mayalso assume that you have a function that will give you a list (array or linked list) of allof the leaves in the tree in timeO(n).(c)[3 points]State the running time of your approach with respect tonthe number ofemployees andkthe maximum number of children any node has.4
Background image