(1) Describe a divide-and-conquer algorithm for the candy testing task. You could simply describe it or show pseudocode. (2) Prove that when you have a constant number c of bad candies, the total number of tests you need to do is O(log n). If all the candies are bad, how many tests does your algorithm need? Present your answer in big-O. (3)

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Do not copy paste answer from Chegg I know it is from there because it's the same wording and same mispelling of the word candy into candle. I only need 1 2 and 3

You work at a candy factory. It’s not always the case that all the candies produced are perfect. There will be some bad ones (say, not that tasty). Your task is to identify these bad candies from n candies and discard them. Fortunately, you have a device to help you with this. If you put a batch of candies in the device, as long as there exists any bad candy, the device will return “BAD!” to you. Otherwise (i.e., all the candies are good) it returns “GOOD!”. Of course, if you put only one candy into the device, you can directly know if it is good or bad. However, every time you use the device, you need to pay 1 dollar, no matter how many candies you put in the tested batch. You want to use the device fewer times to save money.

Note that the device will not tell you how many bad candies are there in the tested batch or which ones are bad, it only tells you whether there’s any bad candy. Also, you don’t know in advance how many bad candies are there in the n candies.

You can put all the candies one by one into the device to check which ones are bad (that’s n dollars in total), but you want to do better. In fact, you know that the number of bad candies is very small (otherwise the candy factory will go bankrupt).

Again, every test costs 1 dollar, and all the other operations are free. Design a divide-and-conquer algorithm for this task, such that, when the number of bad candies is small, your solution is much cheaper than testing all of them one by one. Given n candies in total, when there is only a constant number of bad candies, your algorithm should cost \(O(\log n)\) dollars. For simplicity, you can assume the number of candies n is a power of 2.
Transcribed Image Text:You work at a candy factory. It’s not always the case that all the candies produced are perfect. There will be some bad ones (say, not that tasty). Your task is to identify these bad candies from n candies and discard them. Fortunately, you have a device to help you with this. If you put a batch of candies in the device, as long as there exists any bad candy, the device will return “BAD!” to you. Otherwise (i.e., all the candies are good) it returns “GOOD!”. Of course, if you put only one candy into the device, you can directly know if it is good or bad. However, every time you use the device, you need to pay 1 dollar, no matter how many candies you put in the tested batch. You want to use the device fewer times to save money. Note that the device will not tell you how many bad candies are there in the tested batch or which ones are bad, it only tells you whether there’s any bad candy. Also, you don’t know in advance how many bad candies are there in the n candies. You can put all the candies one by one into the device to check which ones are bad (that’s n dollars in total), but you want to do better. In fact, you know that the number of bad candies is very small (otherwise the candy factory will go bankrupt). Again, every test costs 1 dollar, and all the other operations are free. Design a divide-and-conquer algorithm for this task, such that, when the number of bad candies is small, your solution is much cheaper than testing all of them one by one. Given n candies in total, when there is only a constant number of bad candies, your algorithm should cost \(O(\log n)\) dollars. For simplicity, you can assume the number of candies n is a power of 2.
1. Describe a divide-and-conquer algorithm for the candy testing task. You could simply describe it or show pseudocode.

2. Prove that when you have a constant number \( c \) of bad candies, the total number of tests you need to do is \( O(\log n) \).

3. If all the candies are bad, how many tests does your algorithm need? Present your answer in big-O.

4. If there are \( m \) bad candies, how many tests does your algorithm need? You should present your bound in big-O using \( n \) and \( m \). You should present the tightest bound you could get.

Good luck!
Transcribed Image Text:1. Describe a divide-and-conquer algorithm for the candy testing task. You could simply describe it or show pseudocode. 2. Prove that when you have a constant number \( c \) of bad candies, the total number of tests you need to do is \( O(\log n) \). 3. If all the candies are bad, how many tests does your algorithm need? Present your answer in big-O. 4. If there are \( m \) bad candies, how many tests does your algorithm need? You should present your bound in big-O using \( n \) and \( m \). You should present the tightest bound you could get. Good luck!
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Computational Systems
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education