Title: Algorithm Discovery and Design
1Algorithm Discovery and Design
2Outline
- Representing Algorithms
- Examples of Algorithmic Problem Solving
3Representing Algorithm
4How to Express Algorithms Clear, Precise, and
Unambiguous
- Natural language
- Extremely verbose, with the resulting algorithms
ending up as rambling, unstructured paragraphs
that are hard to follow - Too rich in interpretation and meaning
- Formal programming language like C, C, Java
- During the initial phase of algorithm design, we
should be thinking and writing at a highly
abstract level - Using a programming language forces us to deal
immediately with such low-level language issues
as punctuation, grammar, and syntax - Pseudo Code
- Compromise between natural language and formal
programming language - Translation of algorithms to programming language
is relatively simple
5Use Natural Language to Express the Addition
Algorithm
6Use Java to Express the Addition Algorithm (Part)
7How to Write Pseudo Code
- Pseudo Code for
- Sequential operations
- Computation
- Input
- Output
- Conditional and iterative operations (control
operations)
8Computation
- Set the value of variable to arithmetic
expression - A named variable is simply a named storage
location that can hold a data value - A variable is like a mailbox into which one can
store a value and from which one can retrieve a
value - Examples
- Set the value of carry to 0
- Set the value of Area to (?r2)
- Add the two digits ai and bi to the current value
of carry to get ci - Set the value of ci to (ai bi carry)
- Add 1 to I, effectively moving one column to the
left - Set the value of I to (I1)
- Set cm to the value of carry
- Set the value of cm to carry
9Variable
carry
5
Set the value of carry to 0
carry
5
0
I
4
415
Set the value of I to I1(or II1)
I
4
5
10Input/Output
- Input receive from the outside world data values
that it may then use in later instructions - Get values for variable, variable,
- Get a value for r, the radius of the circle
- Output send results to outside world for display
- Print the values of variable, variable,
- Print the value of area
- The output operation can be used to display a
message rather than the desired results
(something wrong during the computation) - Print the message sorry, no answers were
computed
11Input/Output
Input
ComputingAgent
Outside World
Output
Input Equipments keyboard, mouse Output
Equipments screen, printer
12Average Miles Per Gallon Algorithm (Version 1)
13Conditional Operations
- Conditional operations question-asking
- Allow an algorithm to ask a question and, on the
basis of the answer to that question, to select
the next operation to perform - if a true/false condition is true then First
set of algorithm operationselse (or
otherwise) Second set of algorithm operations - Example
- if (ci gt 10) then set the value of ci to (ci -
10) set the value of carry to 1else set the
value of carry to 0
14Average Miles Per Gallon Algorithm (Version 2)
15Iteration (Loop) Operations
- Iteration operations implement the repetition of
a block of operations - The real power of a computer comes from doing a
calculation many, many times - Repeat step i to step j until a true/false
condition becomes true - Step i operation
- Step i1 operation
-
- Step j operation
Termination Condition
Loop Body
This iterative primitive assumes the loop body
will always be done at least once. The first test
of termination is not done until after the
completion of the first iteration
16Example of A Loop
- Set the value of count to 1
- Repeat step 3 to 5 until (count gt 100)
- set square to (count count)
- print the value of count and square
- add 1 to count
?Loop??????Termination Condition?????????,???Loop?
???
17Average Miles Per Gallon Algorithm (Version 3)
18Variation of Iterative Operations (I)
- Repeat until a true/false condition becomes
true Operation OperationEnd of the loop
19Variation of Iterative Operations (II)
- While a true/false condition remains
true Operation OperationEnd of the loop - The following three iterative operations are
nearly equal - Repeat until the sum is greater than 100
- While the sum is not greater than 100 do
- While the sum is less than or equal to 100 do
Continuation Condition
Loop Body
- A while loop can execute 0, 1, or more times (the
loop condition is checked immediately rather than
at the end of the first iteration) - A repeat loop must execute at least once
20(No Transcript)
21Examples of Algorithmic Problem Solving
- Trick 1?????,???,?????(?????)??????,??????
22Example 1 Looking, Looking, Looking (Sequential
Search)
- Purpose searching for a particular persons name
in a non-alphabetized telephone book - Name Telephone NumberN1 T1N2 T2 N1000
0 T10000 - Input A NAME to be searched
- Output
- If NAME is found at position j, then output the
telephone number of NAME, TJ - If not, then output I am sorry but this name is
not in the directory
10,000 (name, phone number) pair
23Example
24First Attempt at Designing a Sequential Search
Algorithm
- ??????????????(index)?
- Trick 2 ???Iteration?(1) ??????????(2)
???Iteration??? ????????,? ?????????? ??
No iteration is used
25Second Attempt at Designing a Sequential Search
Algorithm
What if a desired NAME does not appear anywhere
in the list
?????Iteration?????????,??????????
??Iteration?????????
A variable called i is used as an index (pointer)
to the list of all names.That is, Ni refers to
the ith name in the list
26Sequential Search Algorithm
27Notes about Looking, Looking, Looking
- How do you search a telephone book by hand?
- A telephone book is usually alphabetized (sorted)
- Binary Search may be better
- But the telephone book used here is
non-alphabetized - The selection of an algorithm to solve a problem
is greatly influenced by the way the data are
organized
28Example 2 Big, Bigger, Biggest
- Purpose search for the numerically largest value
in the entire list - Given a list of examinations, which student has
the highest score - Given a list of annual salaries, which person
earns the most money - Given a value n gt 2 and a list containing n
unique numbers called A1, A2,, An, find and
print out both the largest value in the list and
the position in the list where that largest value
occurred - Example list 19, 41, 12, 63, 22 (n5)
29Find the Largest by Hand (I)
19
- ???????,????????????
- ??????????,???????? (i.e. ??????????????)
The Pile
41
19
1
The largest so far
The largest position
The Pile
12
41
2
The largest so far
The largest position
The Pile
30Find the Largest by Hand (II)
63
4
63
The largest so far
The largest position
The Pile
4
63
22
The largest so far
The largest position
The Pile
31Find Largest Algorithm
32Example 3 Meeting Your Match (Pattern Match)
- You will be given some text composed of n
characters that will be referred to as T1T2Tn.
You will also be given a pattern of m characters,
mltn, that will be represented as P1P2Pm. The
algorithm must locate every occurrence of the
pattern within the text. The output of the
algorithm is the location in the text where each
match occurred. For this problem, the location of
a match is defined to be the index position in
the text where the match begins - Text to be or not to be, that is the
questionPattern toOutput matches starting at
position 1 and 14
33??????
Text We must band together and handle
adversityPattern and
We must band together and handle adversityand
We must band together and handle adversity and
We must band together and handle adversity and
We must band together and handle adversity and
We must band together and handle adversity
and
We must band together and handle adversity
and
We must band together and handle adversity
and
We must band together and handle adversity
and
We must band together and handle adversity
and
We must band together and handle adversity
and
We must band together and handle adversity
and Match!!!!
34Pattern Matching Algorithm
- The pattern-matching algorithm is composed of two
parts - The pattern is aligned under a specific position
of the text, and the algorithm sees whether there
is a match at that given position - T1T2T3T4T5T6P1P2P3
- The pattern is slided ahead one character
position - T1T2T3T4T5T6 P1P2P3
35First Draft of the Pattern Match Algorithm
- Abstraction Use of high-level instructions
during the initial design process - Top-down design
36Go to the Primitives(I)
- Attempt to match every character in the pattern
beginning at position K of the text - Text T1T2TkTk1Tk2Tk(m-1)Pattern
P1P2 P3 Pm - Compare P1 to TkCompare P2 to Tk1Compare P3 to
Tk2Compare Pm to Tk(m-1)
- If all the pairs are equal ? match
- Any one pair is not equal ? mismatch(No need to
compare further)
37Go to the Primitives(II)
- Repeat until we have fallen off the end of the
text - Text T1T2 Tn-m1 Tn-2Tn-1Tn
Pattern P1 Pm-2Pm-1Pm - Repeat until k gt (n-m1)
38Pattern Matching Algorithm
39Pattern-Match Tracing
assignment is equal to?
Text We must band together and handle
adversity Pattern and
n 42 m3 ? (n-m1) 40 k1
Repeat until k gt (n-m1) 1 gt 40 i1
MismatchNo Repeat until either (i gt m) or
(MismatchYes) (1 gt 3), MismatchNo Pi
Tk(i-1) ? p1T1(1-1)? ? a W? ?
MismatchYes Exit the Repeat Loop Mismatch
No? Mismatch Yes ? Do not execute the two
Print Increment k by 1 ? set k to 2 Go back to
test the termination condition of the
outer-repeat loop and re-enter the loop
40Pattern-Match Tracing (Cont.)
Suppose now k 10 (i.e., we start comparing
the and in band Repeat until k gt (n-m1) 10 gt
40 i1 MismatchNo Repeat until either (i
gt m) or (MismatchYes) (1 gt 3), MismatchNo
Pi Tk(i-1) ? p1T10(1-1)? ? a a ?
Increment i by 1 (i2) Test the termination
condition of the inner-repeat and re-enter the
body Pi Tk(i-1) ? p2T10(2-1)? ? n
n ? Increment i by 1 (i3) Test the
termination condition of the inner-repeat and
re-enter the body Pi Tk(i-1) ?
p3T10(3-1)? ? d d ? Increment i by 1
(i4) Test the termination condition of the
inner-repeat and find (4 gt 3). Do notre-enter
the body Mismatch No? Mismatch No ?
Execute the two Print (print k10) Increment
k by 1 ? set k to 11 Go back to test the
termination condition of the outer-repeat and
re-enter the body