Presentation is loading. Please wait.

Presentation is loading. Please wait.

19.1 CompSci 102 Today’s topics Decision TreesDecision Trees Reading: Sections 9.1Reading: Sections 9.1.

Similar presentations


Presentation on theme: "19.1 CompSci 102 Today’s topics Decision TreesDecision Trees Reading: Sections 9.1Reading: Sections 9.1."— Presentation transcript:

1 19.1 CompSci 102 Today’s topics Decision TreesDecision Trees Reading: Sections 9.1Reading: Sections 9.1

2 19.2 CompSci 102 Tree and Forest Examples A Tree:A Tree:A Forest: Leaves in green, internal nodes in brown.

3 19.3 CompSci 102 Coin-Weighing Problem Imagine you have 8 coins, one of which is a lighter counterfeit, and a free-beam balance.Imagine you have 8 coins, one of which is a lighter counterfeit, and a free-beam balance. –No scale of weight markings is required for this problem! How many weighings are needed to guarantee that the counterfeit coin will be found?How many weighings are needed to guarantee that the counterfeit coin will be found? ?

4 19.4 CompSci 102 As a Decision-Tree Problem In each situation, we pick two disjoint and equal- size subsets of coins to put on the scale.In each situation, we pick two disjoint and equal- size subsets of coins to put on the scale. The balance then “decides” whether to tip left, tip right, or stay balanced. A given sequence of weighings thus yields a decision tree with branching factor 3.

5 19.5 CompSci 102 Tree Height Theorem The decision tree must have at least 8 leaf nodes, since there are 8 possible outcomes.The decision tree must have at least 8 leaf nodes, since there are 8 possible outcomes. –In terms of which coin is the counterfeit one. Theorem: There are at most m h leaves in an m- ary tree of height h.Theorem: There are at most m h leaves in an m- ary tree of height h. –Corollary: An m-ary tree with leaves has height h≥  log m . If m is full and balanced then h=  log m . What is this decision tree’s height?What is this decision tree’s height? How many weighings will be necessary?How many weighings will be necessary?

6 19.6 CompSci 102 General Solution Strategy The problem is an example of searching for 1 unique particular item, from among a list of n otherwise identical items.The problem is an example of searching for 1 unique particular item, from among a list of n otherwise identical items. –Somewhat analogous to the adage of “searching for a needle in haystack.” Armed with our balance, we can attack the problem using a divide- and-conquer strategy, like what’s done in binary search.Armed with our balance, we can attack the problem using a divide- and-conquer strategy, like what’s done in binary search. –We want to narrow down the set of possible locations where the desired item (coin) could be found down from n to just 1, in a logarithmic fashion. Each weighing has 3 possible outcomes.Each weighing has 3 possible outcomes. –Thus, we should use it to partition the search space into 3 pieces that are as close to equal-sized as possible. This strategy will lead to the minimum possible worst-case number of weighings required.This strategy will lead to the minimum possible worst-case number of weighings required.

7 19.7 CompSci 102 General Balance Strategy On each step, put  n/3  of the n coins to be searched on each side of the scale.On each step, put  n/3  of the n coins to be searched on each side of the scale. –If the scale tips to the left, then: The lightweight fake is in the right set of  n/3  ≈ n/3 coins.The lightweight fake is in the right set of  n/3  ≈ n/3 coins. –If the scale tips to the right, then: The lightweight fake is in the left set of  n/3  ≈ n/3 coins.The lightweight fake is in the left set of  n/3  ≈ n/3 coins. –If the scale stays balanced, then: The fake is in the remaining set of n − 2  n/3  ≈ n/3 coins that were not weighed!The fake is in the remaining set of n − 2  n/3  ≈ n/3 coins that were not weighed! Except if n mod 3 = 1 then we can do a little better by weighing  n/3  of the coins on each side.Except if n mod 3 = 1 then we can do a little better by weighing  n/3  of the coins on each side. You can prove that this strategy always leads to a balanced 3-ary tree.

8 19.8 CompSci 102 Coin Balancing Decision Tree Here’s what the tree looks like in our case:Here’s what the tree looks like in our case: 123 vs vs. 2 left: 123 balanced: 78 right: vs. 8 4 vs. 5 L:1 R:2B:3 L:4 R:5B:6 L:7 R:8

9 19.9 CompSci 102 Problem 1 How many weighings of a balance scale are needed to find a counterfeit coin among 12 coins if the counterfeit coin is lighter than the others?How many weighings of a balance scale are needed to find a counterfeit coin among 12 coins if the counterfeit coin is lighter than the others? –Describe your algorithm to find this coin

10 19.10 CompSci 102 Problem 2 1 of 4 coins may be counterfeit1 of 4 coins may be counterfeit –Counterfeit coin should be lighter or heavier than others How many weighings are needed toHow many weighings are needed to –Determine whether there is a counterfeit coin –If so, is it lighter or heavier than others What is your algorithm?What is your algorithm?


Download ppt "19.1 CompSci 102 Today’s topics Decision TreesDecision Trees Reading: Sections 9.1Reading: Sections 9.1."

Similar presentations


Ads by Google