1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Rufina [12.5K]
3 years ago
10

You are given a list of n positive integers a1, a2, . . . an and a positive integer t. Use dynamic programming to design an algo

rithm that examines whether a subset of these n numbers add up to exactly t, under the constraint that you use each ai at most once. If there is a solution, your program should output the subset of selected numbers. Recurrence Relation (and base cases)
Computers and Technology
1 answer:
Anna11 [10]3 years ago
6 0

Answer:

See explaination for the program code

Explanation:

The code below

Pseudo-code:

//each item ai is used at most once

isSubsetSum(A[],n,t)//takes array of items of size n, and sum t

{

boolean subset[n+1][t+1];//creating a boolean mtraix

for i=1 to n+1

subset[i][1] = true; //initially setting all first column values as true

for i = 2 to t+1

subset[1][i] = false; //initialy setting all first row values as false

for i=2 to n

{

for j=2 to t

{

if(j<A[i-1])

subset[i][j] = subset[i-1][j];

if (j >= A[i-1])

subset[i][j] = subset[i-1][j] ||

subset[i - 1][j-set[i-1]];

}

}

//returns true if there is a subset with given sum t

//other wise returns false

return subset[n][t];

}

Recurrence relation:

T(n) =T(n-1)+ t//here t is runtime of inner loop, and innner loop will run n times

T(1)=1

solving recurrence:

T(n)=T(n-1)+t

T(n)=T(n-2)+t+t

T(n)=T(n-2)+2t

T(n)=T(n-3)+3t

,,

,

T(n)=T(n-n-1)+(n-1)t

T(n)=T(1)+(n-1)t

T(n)=1+(n-1)t = O(nt)

//so complexity is :O(nt)//where n is number of element, t is given sum

You might be interested in
The process of analyzing data to extract information not offered by the raw data alone; it uncovers trends and patterns using te
anygoal [31]

Answer:

The correct answer to the following question is "Data Mining".

Explanation:

It is indeed a method that businesses that are used to transform the raw information towards valuable data. Corporations might learn something about their clients instead of using technology to analyze trends in large quantities of content to create more efficient campaigns, boost revenue as well as reduce costs.

  • It encompasses investigating as well as analyzing huge blocks of documentation to extrapolate useful behavioral patterns.
  • It constructs discernment about what actually happens in certain details, and is already teensy bit mathematical than scripting, but utilizes both.
5 0
3 years ago
What does the word tolerance mean in textiles?
Firdavs [7]
It means the willingness to respect or except thecustoms, beliefs, or opinions of others
4 0
3 years ago
Technology is (BLANK.1 ) science that makes (blank.2) science useful and practical.
Hoochie [10]

Answer:

Example

Explanation:

Example

5 0
3 years ago
Read 2 more answers
Differences between unions and structures
vovikov84 [41]

Answer:

A structure is a user-defined data type available in C that allows to combining data items of different kinds. Structures are used to represent a record. A union is a special data type available in C that allows storing different data types in the same memory location.

3 0
3 years ago
Suppose that you have just bought a new computer and you want to install soft- ware on that. Specifically, two companies, which
Gennadij [26K]

Answer:

#importing the time module

import time

#welcoming the user

name = raw_input("What is your name? ")

print "Hello, " + name, "Time to play hangman!"

print "

"

#wait for 1 second

time.sleep(1)

print "Start guessing..."

time.sleep(0.5)

#here we set the secret

word = "secret"

#creates an variable with an empty value

guesses = ''

#determine the number of turns

turns = 10

# Create a while loop

#check if the turns are more than zero

while turns > 0:          

   # make a counter that starts with zero

   failed = 0              

   # for every character in secret_word    

   for char in word:      

   # see if the character is in the players guess

       if char in guesses:    

   

       # print then out the character

           print char,    

       else:

   

       # if not found, print a dash

           print "_",      

       

       # and increase the failed counter with one

           failed += 1    

   # if failed is equal to zero

   # print You Won

   if failed == 0:        

       print "

You won"  

   # exit the script

       break              

   print

   # ask the user go guess a character

   guess = raw_input("guess a character:")  

   # set the players guess to guesses

   guesses += guess                    

   # if the guess is not found in the secret word

   if guess not in word:  

 

    # turns counter decreases with 1 (now 9)

       turns -= 1        

 

   # print wrong

       print "Wrong

"    

 

   # how many turns are left

       print "You have", + turns, 'more guesses'  

 

   # if the turns are equal to zero

       if turns == 0:            

   

       # print "You Lose"

           print "You Lose

"  

7 0
3 years ago
Other questions:
  • True or false. Every word has only one correct spelling and pronunciation.
    6·1 answer
  • ________ is a type of attack in which the attacker takes control of a session between two machines and masquerades as one of the
    5·1 answer
  • Consider the following two code segments. In both, assume that n is an integer variable that has been declared and initialized.
    10·1 answer
  • Explain the four misconceptions about entrepreneurship.
    7·1 answer
  • System.out.print();
    10·1 answer
  • Finding values in an array
    5·1 answer
  • Fill in the blanks
    7·1 answer
  • Egovernment involves the use of strategies and technologies to transform government by improving the delivery of services and en
    7·1 answer
  • Plz plz plz help me with me
    14·2 answers
  • What could be done to make sure that people follow copy right laws?
    13·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!