Lecture 4 Read S&G ch. 3 for Tuesday Algorithm Discovery Search Problem

advertisement
Lecture 4
1/21/04
Lecture 4
Read S&G ch. 3 for Tuesday
Examples of Algorithmic Problem
Solving
(S&G, §2.3)
1/21/04
CS 100 - Lecture 4
1
1/21/04
Algorithm Discovery
• We have a list of names: N1, N2, …, N10000
• We have a corresponding list of telephone
numbers: T1, T2, …, T10000
intelligence
hard work
past experience
artistic skill
plain good luck
– That is, Ti is the phone number of the person
whose name is Ni
• We have a Name and want to print this
person’s number
• Practice is essential
1/21/04
2
Search Problem
• Discovering correct & efficient algorithm
for a problem requires:
–
–
–
–
–
CS 100 - Lecture 4
CS 100 - Lecture 4
3
1/21/04
CS 100 - Lecture 4
4
Straight-Line Algorithm
Automation Principle
1
2
3
4
Get Name, N1, …, N10000, T1, …, T10000
If Name = N1 then print the value of T1
If Name = N2 then print the value of T2
If Name = N3 then print the value of T3
…
10000 If Name = N9999 then print the value of T9999
10001 If Name = N10000 then print the value of T10000
10002 Stop
1/21/04
CS 100
CS 100 - Lecture 4
5
Automate mechanical, tedious, or
error-prone activities
1/21/04
CS 100 - Lecture 4
6
1
Lecture 4
1/21/04
Application to Searching
Problem
Abstraction Principle
• In this case the recurring pattern is:
If Name = Ni then print the value of Ti
• We want to execute this command for
successive values of i = 1, 2, …, 10000
• Until one of them succeeds
Avoid requiring something to be
stated more than once;
factor out the recurring pattern
1/21/04
CS 100 - Lecture 4
7
1/21/04
8
Sequential Search Algorithm
Importance of Data Organization
1. Get values for Name, N1, …, N10000 and for T1,
…, T10000
2. Set the value of i to 1 and set the value of Found
to no
3. Repeat steps 4 through 8 until Found = yes
4.
If Name = Ni then
5.
Print the value of Ti
6.
Set the value of Found to yes
7.
Else (Name is not equal to Ni)
8.
Add 1 to i
9. Stop
• Straight-line algorithm tested all 10000
• Sequential search tests 5000 on average
• Could do much better by alphabetizing the
list of names
• “The selection of an algorithm to solve a
problem is greatly influenced by the way the
data are organized.” — S&G
• “Pick the right data structure and the
algorithm will design itself.” — Anon.
1/21/04
1/21/04
CS 100 - Lecture 4
9
10
• Do not always have to start from the
primitive operations
• Purpose:
– given a list of numbers
– find the largest number & its position in list
– programming in the small
• Can make use of already developed
algorithms
• These can be collected in libraries
• Programming in the large
• Abstraction Principle
• Useful in itself
• Can also be used in building other
algorithms (e.g., sorting algorithm)
1/21/04
CS 100 - Lecture 4
Programming in the Large
Another Example: Find Largest
CS 100
CS 100 - Lecture 4
CS 100 - Lecture 4
11
1/21/04
CS 100 - Lecture 4
12
2
Lecture 4
1/21/04
Doing It By Hand
Find-Largest Problem
• Given n ≥ 2
• A list of n unique numbers A1, A2, …, An
• Find and print out:
19
41
12
63
22
A1
A2
A3
A4
A5
– the largest number in the list
– the position in the list of the largest number
• For example:
– input: n = 5, list 19, 41, 12, 63, 22
– output: 63, 4
1/21/04
CS 100 - Lecture 4
13
1/21/04
Find Largest Algorithm:
Initialization
CS 100 - Lecture 4
15
CS 100
i
location
largest
so far
CS 100 - Lecture 4
14
1/21/04
CS 100 - Lecture 4
16
Pattern Matching Problem
• Given a text of n characters T1T2…Tn
• Given a pattern of m characters P1P2…Pm
• Goal: locate every occurrence of the pattern
in the text
• Output the location of the beginning of each
match
Print out the values of largest so far and
location
Stop
CS 100 - Lecture 4
19
41
63
Repeat until i > n
If Ai > largest so far then
Set largest so far to Ai
Set location to i
Add 1 to the value of i
End of the loop
Find Largest Algorithm:
Wrap-up
1/21/04
1
2
4
Find Largest Algorithm:
Main Loop
Get a value for n, the size of the list
Get values for A1, A2, …, An, the list to be
searched
Set the value of largest so far to A1
Set the value of location to 1
Set the value of i to 2
1/21/04
2
4
3
5
17
1/21/04
CS 100 - Lecture 4
18
3
Lecture 4
1/21/04
Doing It By Hand
First Draft: Initialization
Get values for n and m, the sizes of the text
and the pattern, respectively
Get values for both the text T1T2…Tn and the
pattern P1P2…Pm
Set k, the starting location for the attempted
match, to 1
E.g., search for the pattern ‘the’ in the text:
tobeornottobe,thatisthequestion
the
the
the
the
the
the
the
Match!
1/21/04
CS 100 - Lecture 4
19
1/21/04
CS 100 - Lecture 4
20
Top-Down Design
First Draft: Main Loop
• First draft uses high-level statements:
– until we have fallen off the end of the text
– Attempt to match every character in the pattern,
beginning at position k
Repeat until we have fallen off the end of the text
Attempt to match every character in the pattern,
beginning at position k of the text
If there was a match then
Print the value of k, the beginning location
Add 1 to k, which slides the pattern forward
End of the loop
1/21/04
CS 100 - Lecture 4
• These are not primitive operations
• Must eventually be defined, but definition
can be deferred
• “Divide and conquer” strategy
21
Process of Top-Down Design
1/21/04
CS 100
CS 100 - Lecture 4
22
Attempting the Pattern Match
• Divide problem to be solved into a small
number of simpler subproblems
• Further divide the subproblems into simpler
subproblems
• Continue until the simplest subproblems can
be easily implemented in terms of the
primitive operations
1/21/04
CS 100 - Lecture 4
23
T1 T2 … Tk Tk+1 Tk+2 … Tk+(m–1) … Tn
P 1 P2 P3 … P m
Tk+(i–1)
Pi
1/21/04
CS 100 - Lecture 4
24
4
Lecture 4
1/21/04
Attempt to match every character
in the pattern, beginning at k
Set the value of i to 1
Set the value of Mismatch to no
Repeat until either (i > m) or (Mismatch = yes)
If Pi ≠ Tk+(i–1) then
Set Mismatch to yes
Else
Increment i by 1 (to move to next character)
End of the loop
1/21/04
CS 100 - Lecture 4
Print Successful Matches
If Mismatch = no then
Print the message ‘There is a match at
position’
Print the value of k
25
1/21/04
Repeat until we have fallen off of
end of text
T1 T2 … Tn–m+1 Tn–m+2 …
P1
P2
Tn–1 Tn
… Pm–1 Pm
€
€
Suppose i = m
and k + i −1 = n
Then, k + m −1 = n
€
Examples and Analysis
– miss match in last position
– try to look beyond end of list
So, k = n − m + 1
CS 100 - Lecture 4
26
• We may work out a couple examples to
figure out the stopping condition
• But we should do the algebra to make sure
• There is a danger of “off by one” errors
Recall Tk +i−1 ⇔ Pi
• Typical program “bugs”
Repeat until k > (n – €m + 1)
1/21/04
CS 100 - Lecture 4
27
1/21/04
CS 100 - Lecture 4
28
What is a “Good” Algorithm
• First (and above all), it must be correct
• Second, it should be efficient enough
– S&G, ch. 3
1/21/04
CS 100
CS 100 - Lecture 4
29
5
Download