Docsity

Binary Search Algorithms: Solving Problems with Weights and Coins, Slides of Computer Science

Partial preview of the text

Download Binary Search Algorithms: Solving Problems with Weights and Coins and more Slides Computer Science in PDF only on Docsity!

Binary Search

Ten Puzzling Problems in Computer Engineering

Weighing with a Balance

A large container is known to hold 24 oz of nails.

The hardware store has a balance, but no weights.

Can you measure out 9 oz of nails for a customer?

Divide all nails into two equal piles: 12 oz 12 oz

Divide one pile into two equal piles: 12 oz 6 oz 6 oz

... and again: 12 oz 6 oz 3 oz 3 oz

A chemist has a balance and fixed weights of 1, 2, 4, and 8 grams.

Show that she can weigh any amount of material from 1 to 15 grams

by placing the weights on one side and the material on the other.

What is the best set of 4 fixed weights in the sense of maximizing the

range of measurable weights in increments of 1 gram? (e.g., 1, 4, 7, 12)

Weights of 1, 3, 9, and 27 grams allow us to measure up to 40 grams

12 Coins with 1 Counterfeit: Lighter or Heavier

We have 12 coins. Eleven are normal coins; one is a counterfeit coin that

weighs less or more than a normal coin. Identify the counterfeit coin and

its relative weight with a minimum number of weighings on a balance.

Hint: First do it for 3 coins, one of which is a counterfeit, using only two weighing,

If A = B, then C contains the counterfeit coin.

Weigh 3 coins from C against 3 good coins.

If equal, the lone remaining coin in C is

counterfeit and one more weighing is enough

to tell if it’s lighter or heavier than normal.

If the three C coins

are lighter, then...

If the three C coins

are heavier, then...

If A < B or A > B...

Group A Group B Group C

Compare

A & B

Another Solution to the 12-Coin Puzzle

We have 12 coins. Eleven are normal coins; one is a counterfeit coin

1 4 6 10 against 5 7 9 12

2 5 4 11 against 6 8 7 10

3 6 5 12 against 4 9 8 11

Each weighing has three

possible outcomes:

L -- Left heavier

R -- Right heavier

B -- Balance

Example: L L B -- Counterfeit coin is among 1, 2, 7, 10 → 7 is lighter

L L L np

L L B 7–

L L R

L B L

L B B

L B R

L R L

L R B

L R R

B L L

B L B

B L R

B B L

B B B np

B B R

B R L

B R B

B R R

R L L

R L B

R L R

R B L

R B B

R B R

R R L

R R B 7+

R R R np

Searching in an Alphabetized List

Possible range: [0, 20]

Middle of the range = (0 + 20)/2 = 10

Is “tomato paste” an ingredient?

tomato paste > olive or vegetable oil

tomato paste > sliced pitted ripe olives

Possible range: [11, 20]

Middle of the range = (11 + 20)/2 = 15

Possible range: [16, 20]

Middle of the range = (16 + 20)/2 = 18

tomato paste > thinly sliced pepperoni

Possible range: [19, 20]

Middle of the range = (19 + 20)/2 = 19

Tomato paste is indeed an ingredient!

The Binary Search Algorithm

Is the number 85 in the 63-entry list to the right?

First Last Middle Entry Outcome

Six probes are needed with a 63-entry list,

in the worst case

More generally, a (

n

  • 1)-entry list requires

n probes in the worst case

Searching in Dynamic Lists

If we use a binary search algorithm on a

dynamic list, its sorted order must be maintained

D

I

A dynamic list has entries inserted or deleted

Example: Delete 81 from the list

1. Search to find the entry 81

2. Move all entries beyond 81 one notch up

Addition/deletion takes O ( n ) steps on average.

So, if the number of additions/deletions

is comparable to the number of searches,

sorting the list does not buy us anything

Example: Insert 95 into the list

1. Search for 95, to see where it should go

2. Move all entries beyond there a notch down

3. Put 95 in the vacated location

Examples of Dynamic Lists 1 2 5 6 8 12 16 17 18 21 23 24 30 32 33 35 38 40 44 45 47

Students currently enrolled at UCSB:

This list is dynamic, but does not change often

Customers of a wireless phone company

currently having active connections:

This list may change 1000s of times per minute

UCSB graduates, class of 2000:

This list is nearly static, but may change to

include missing persons or to make corrections

Even “static” lists may change on occasion …

Question: How do we store a rapidly changing

dynamic list so that it is easy to search and to

update with insertions and deletions? Stud #s, customer IDs, etc.

Spell check dictionary for a word processor:

Changes as you add new words

Insertions and Deletions in Binary Search Trees 1 5

Example 1:

Insert 48

Example 2:

Delete 116

48 Deleted

Complications

to deal with:

Loss of

balance due

to repeated

insertions and

deletions

Deletion of

an inner or

nonleaf node

Example Unbalanced (Random) Binary Tree

Source: http://cg.scs.carleton.ca/~luc/trees.html

Game of 20 Questions as Binary Search

Animate?

Human?

Yes No Yes No 1 5

Yes No

Practical Multiway Search Trees

M

E H P T X

A C F G I K L N O Q S U V Y Z

B-tree, with 1-3 entries

(B = balanced, Boeing Labs,

or Bayer/McCreight)

Find A

Find B

Insert D

Insert J

Delete L

Delete T

Property at each node:

Subtree between x i and xi +

has all elements in [ x i , x i +1 ]

Creating Binary-Tree Mazes

Start with grid and outer walls

Divide the area in two parts,

with an opening between them

Repeat dividing process for

each of two parts, then for

four parts, etc., until no

further dividing is possible

You know you are done when

every rectangular area has

one side of length 1

Have fun with your maze!