HW Solutions 7 and 8

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

def S(n,k): if (n,k) in dic: return dic[(n,k)] if n > 0 and k == 0: return 0 if n == k and k >= 0: return 1 if n > 1 and k == 1: return 1 if n < k: return 0 dic[n,k] = k*S(n-1, k) + S(n-1, k-1) return dic[n,k]

"top-down" DP solution

A graph pointing from F(5) to F(0)

Computation DAG

T/F: Dynamic programming is a technique which increases time complexity in order to decrease space complexity.

False

A palindrome is a string that is equal to itself when reversed. Formally, a string s1 s2 ⋯sn is a palindrome if s1 s2 ⋯sn−1 sn = sn sn−1 ⋯s2 s1 . According to wikipedia, the 19-letter Finnish word saippuakivikauppias is the longest palindromic word in everyday use. Given a string, S, you want to find the longest palindromic subsequence. That is, the longest subsequence of S where the subsequence is a palindrome. Remember, a subsequence is not necessarily continuous. Given your answers to the above questions, develop notation and write a formula the recurrence relation.

P(s,e) =if S(s)!=S(e): max(P(s+1, e), P(s,e-1)) if S(s)==S(e): 2 + P(s+1, e-1)

F(n) = F(n − 1) + F(n − 2)

Recurrence Relation

Fibonacci of n

Subproblem Instance

DP = {} def F(n): if n in DP: return DP[n] if f == 0: return 0 if f == 1: return 1 DP[n] = F(n-1) + F(n-2) return DP[n]

Top-Down DP

T/F: Dynamic programming is a technique to optimize certain types of recursive algorithms

True

Question: In how many ways can you glue the three types of smaller rods together to create a rod of length n? The table above enumerates all of the possibilities when n = 4 and giving an answer of 7. def R(n): # Base Cases arr = [0 for i in range (n + 1)] for i in range(0, n + 1): if i == 0: arr[0] = 1 if i == 1: arr[1] = 1 if i == 2: arr[2] = 2 if i == 3: arr[3] = 4 # Call the other cells for i in range(4, n + 1): arr[i] = arr[i - 1] + arr[i - 2] + arr[i - 3]

"bottom-up", iterative DP solution

Question: In how many ways can you glue the three types of smaller rods together to create a rod of length n? The table above enumerates all of the possibilities when n = 4 and giving an answer of 7. Which subproblem instances are the base cases?

Base Cases: If the length of the rod is 1, n = 1 will return 1 If the length of the rod is 2, n = 2 will return 2 If the length of the rod is 3, n = 3 will return 4

Question: In how many ways can you glue the three types of smaller rods together to create a rod of length n? The table above enumerates all of the possibilities when n = 4 and giving an answer of 7. Develop notation and describe a recurrence relation between the subproblem instances. R(n) = R(n-1) + R(

R(n) = R(n-1) + R(n-2) + R(n-3)

def F(n): if f == 0: return 0 if f == 1: return 1 return F(n-1) + F(n-2)

Recursive Algorithm

Question: In how many ways can you glue the three types of smaller rods together to create a rod of length n? The table above enumerates all of the possibilities when n = 4 and giving an answer of 7. What is the input to the subproblem instance?

The input is the length of the rod (n).

def S(n,k): arr = [[0]*(k+1) for n in range(n+1)] #Base Cases for i in range(0,n+1): for j in range(0,k+1): if i > 0 and j == 0: arr[i][j] = 0 if i == j and j >= 0: arr[i][j] = 1 if i > 1 and j == 1: arr[i][j] = 1 if i < j: arr[i][j] = 0 #Call the other cells for i in range (3,n+1): for j in range(2,k+1): if(j < i): arr[i][j] = j*arr[i-1][j] + arr[i-1][j-1]

"bottom-up", iterative DP solution

A palindrome is a string that is equal to itself when reversed. Formally, a string s1 s2 ⋯sn is a palindrome if s1 s2 ⋯sn−1 sn = sn sn−1 ⋯s2 s1 . According to wikipedia, the 19-letter Finnish word saippuakivikauppias is the longest palindromic word in everyday use. Given a string, S, you want to find the longest palindromic subsequence. That is, the longest subsequence of S where the subsequence is a palindrome. Remember, a subsequence is not necessarily continuous. Which subproblem instances are the base cases? What are their values?

1. When s is equal to e, the sub sequence is a single character that in itself is a palindrome. Length 1 2. if e is less than s, results in a length of 0.

The traditional world chess championship is a match of 24 games. The current champion retains the title in case the match is a tie1 . Each game ends in a win, loss, or draw (tie) where wins count as 1, losses as 0, and draws as 1 2 . The players take turns playing white and black with the champion playing white in the first game. Assume that W(c), D(c), and L(c) return the probabilities that the champion wins, draws, or loses one game while playing color c ∈ {w, b}, respectively2 . For example, D(b) is the probability that the champion draws while playing black. You won't specifically need to use this, but convince yourself that W(c) + D(c) + L(c) = 1.0. Given your answers to the above questions, develop notation and write a formula the recurrence relation. Assume that the function C(g) returns the color the champion is playing in game g.

C(g): The color the champion is playing during game g P(g,i) = W(C(g)) * P(g-1, i-1) + D(C(g)) * P(g-1, i + 0.5) + L(C(g)) * P(g-1, i)

T/F: When computing values in a dynamic programming table, we always start in the upper-left corner of the table.

False

A palindrome is a string that is equal to itself when reversed. Formally, a string s1 s2 ⋯sn is a palindrome if s1 s2 ⋯sn−1 sn = sn sn−1 ⋯s2 s1 . According to wikipedia, the 19-letter Finnish word saippuakivikauppias is the longest palindromic word in everyday use. Given a string, S, you want to find the longest palindromic subsequence. That is, the longest subsequence of S where the subsequence is a palindrome. Remember, a subsequence is not necessarily continuous. What are the inputs to the subproblem instance?

1. Starting index of the current sub problem, s 2. Ending index of the current subproblem, e

The traditional world chess championship is a match of 24 games. The current champion retains the title in case the match is a tie1 . Each game ends in a win, loss, or draw (tie) where wins count as 1, losses as 0, and draws as 1 2 . The players take turns playing white and black with the champion playing white in the first game. Assume that W(c), D(c), and L(c) return the probabilities that the champion wins, draws, or loses one game while playing color c ∈ {w, b}, respectively2 . For example, D(b) is the probability that the champion draws while playing black. You won't specifically need to use this, but convince yourself that W(c) + D(c) + L(c) = 1.0. Which subproblem instances are the base cases? What are their values? (Hint: think about subproblem instances where the probability of the champion retaining the title are not dependent on any of the win/draw/loss probabilities.)

1. if g less than i then champions chances of winning are zero 2. if i equal to or less than 0 the champion is guaranteed to win.

In terms of n and k, how many calls to the recursive algorithm are required to evaluate ( n | k )? ◻ k n ◻ n k ◻ ( n k ) ◻ 2( n k ) − 1 ◻ (n + k)k

2( n k ) − 1

A palindrome is a string that is equal to itself when reversed. Formally, a string s1 s2 ⋯sn is a palindrome if s1 s2 ⋯sn−1 sn = sn sn−1 ⋯s2 s1 . According to wikipedia, the 19-letter Finnish word saippuakivikauppias is the longest palindromic word in everyday use. Given a string, S, you want to find the longest palindromic subsequence. That is, the longest subsequence of S where the subsequence is a palindrome. Remember, a subsequence is not necessarily continuous. Provide two examples of longest palindromic subsequences for the fol- lowing string: COMPUTERSCIENCE.

ECECE ECICE

The traditional world chess championship is a match of 24 games. The current champion retains the title in case the match is a tie1 . Each game ends in a win, loss, or draw (tie) where wins count as 1, losses as 0, and draws as 1 2 . The players take turns playing white and black with the champion playing white in the first game. Assume that W(c), D(c), and L(c) return the probabilities that the champion wins, draws, or loses one game while playing color c ∈ {w, b}, respectively2 . For example, D(b) is the probability that the champion draws while playing black. You won't specifically need to use this, but convince yourself that W(c) + D(c) + L(c) = 1.0. Imagine that the championship match was a variable number of games n. What is the complexity of this bottom-up DP algorithm in terms of n?

O(n2 )

The traditional world chess championship is a match of 24 games. The current champion retains the title in case the match is a tie1 . Each game ends in a win, loss, or draw (tie) where wins count as 1, losses as 0, and draws as 1 2 . The players take turns playing white and black with the champion playing white in the first game. Assume that W(c), D(c), and L(c) return the probabilities that the champion wins, draws, or loses one game while playing color c ∈ {w, b}, respectively2 . For example, D(b) is the probability that the champion draws while playing black. You won't specifically need to use this, but convince yourself that W(c) + D(c) + L(c) = 1.0. How many dimensions will the dynamic programming table (used in bottom-up DP) have? What will each dimension of your table represent? What is the domain of each dimension?

The dynamic programming table will have 2 dimensions, one dimensions to rep- resent games g as the rows, one dimension to represent the number of points the champion needs to win represented as columns. The domain of i is -0.5 to 12 and increments by 0.5, this will result in 26 values. The domain of g is 0 to 24 and increments by 1 and will result in 25 values.

The traditional world chess championship is a match of 24 games. The current champion retains the title in case the match is a tie1 . Each game ends in a win, loss, or draw (tie) where wins count as 1, losses as 0, and draws as 1 2 . The players take turns playing white and black with the champion playing white in the first game. Assume that W(c), D(c), and L(c) return the probabilities that the champion wins, draws, or loses one game while playing color c ∈ {w, b}, respectively2 . For example, D(b) is the probability that the champion draws while playing black. You won't specifically need to use this, but convince yourself that W(c) + D(c) + L(c) = 1.0. What iteration order is required to compute the bottom-up DP table?

The iteration order required to compute the the probability when g represents rows and i represents columns is by computing an entire row of g before calculating the next row of g.

The traditional world chess championship is a match of 24 games. The current champion retains the title in case the match is a tie1 . Each game ends in a win, loss, or draw (tie) where wins count as 1, losses as 0, and draws as 1 2 . The players take turns playing white and black with the champion playing white in the first game. Assume that W(c), D(c), and L(c) return the probabilities that the champion wins, draws, or loses one game while playing color c ∈ {w, b}, respectively2 . For example, D(b) is the probability that the champion draws while playing black. You won't specifically need to use this, but convince yourself that W(c) + D(c) + L(c) = 1.0. In which cell of your bottom-up DP table will you find the answer to the posed question (the champion's probability of retaining the title)?

The probability of the champion retaining the title can be found in cell (24,12). The cell representing 24 games remaining in the match and 12 points left to win.

T/F: Dynamic programming can be applied to problems where there are multiple ways to arrive at the same subproblem instance via many branches of a recursion tree.

True

A palindrome is a string that is equal to itself when reversed. Formally, a string s1 s2 ⋯sn is a palindrome if s1 s2 ⋯sn−1 sn = sn sn−1 ⋯s2 s1 . According to wikipedia, the 19-letter Finnish word saippuakivikauppias is the longest palindromic word in everyday use. Given a string, S, you want to find the longest palindromic subsequence. That is, the longest subsequence of S where the subsequence is a palindrome. Remember, a subsequence is not necessarily continuous. "top-down" DP solution

dict = {} def P(s,e): if s == e: dict[(s,e)] = 1 elif s > e: dict[(s,e)] = 0 elif (s,e) not in dict.keys(): if S[s]!=S[e]: dict[(s,e)] = max(P(s+1, e), P(s,e-1)) if S[s]==S[e]: dict[(s,e)] = 2 + P(s+1, e-1) return dict[(s,e)]

The traditional world chess championship is a match of 24 games. The current champion retains the title in case the match is a tie1 . Each game ends in a win, loss, or draw (tie) where wins count as 1, losses as 0, and draws as 1 2 . The players take turns playing white and black with the champion playing white in the first game. Assume that W(c), D(c), and L(c) return the probabilities that the champion wins, draws, or loses one game while playing color c ∈ {w, b}, respectively2 . For example, D(b) is the probability that the champion draws while playing black. You won't specifically need to use this, but convince yourself that W(c) + D(c) + L(c) = 1.0. What are the inputs to the subproblem instance? (Hint: your an- swer will be in terms of the variables described above.)

g: The number of games remaining in the match i: number of points the champion need to retain the title

Question: In how many ways can you glue the three types of smaller rods together to create a rod of length n? The table above enumerates all of the possibilities when n = 4 and giving an answer of 7. def R(n): if n == 1: return 1 if n == 2: return 2 if n == 3: return 4 return R(n-1)+R(n-2)+R(n-3)

recursive algorithm

def S(n,k): if n > 0 and k == 0: return 0 if n == k and k >= 0: return 1 if n > 1 and k == 1: return 1 if n < k: return 0 return k*S(n-1,k) + S(n-1,k-1)

recursive algorithm


संबंधित स्टडी सेट्स

unit 4 Financial Accounting as Compared to Managerial Accounting I need help with this question

View Set

Intro to Physical Anthropology Chapter 14

View Set

multiple endocrine neoplasia (MEN types 1,2a and 2b)

View Set

NURS 405 Unit 1 Quiz (Weeks 1-3)

View Set

ELS - court structure & hierarchy

View Set

Consumer Math B - Unit 1 - Lesson 2

View Set

Thermodynamics Comment Questions (HW and Exams) & Equations

View Set

Chapter 15: Psychological Disorders

View Set