1b-2a-Algorithms

advertisement
Objectives
• By the end of this lecture, students should:
• understand what an algorithms is
• appreciate the role of algorithm definitions in the
problem solving process
• know the basic components of algorithms
• understand the difference between algorithms
and programs
• be able to formulate algorithms for simple
problems
• Reading: Walter Savitch. An Introduction to Problem
Solving and Programming, Pearson, 2004. Sec. 1.1-1.3
1
How do we solve problems?
•
•
•
•
•
We "just do"
Guesswork-and-luck
Trial-and-error
Experience (possibly someone else's)
Systematically !
2
The Problem-solving Process
"Doctor, my head hurts"
Patient has elevated
pressure in anterior
parietal lobe.
Analysis
Problem
specification
Design
Algorithm
Implementation
Program
Compilation
Executable
(solution)
1. Sterilize cranial saw
2. Anaesthetize patient
3. Remove top of skull
4. Get the big spoon...
5. etc., etc.
sterilize(saw,alcohol);
raise_hammer();
lower hammer(fast);
start(saw);
/* etc. etc. */
01001110101100101010101010010
10101010100110010101010101001
011010011101010101010010010111
010011110101010111110101010001
10100001101...
3
...it’s not a linear process
Idea
Problem
Informal
Description
Algorithm
Coding
Refinement
Program
Testing & Debugging
4
Algorithm
• A sequence of instructions specifying the
steps required to accomplish some task
• Named after:
Muhammad ibn Musa al-Khwarizmi
of Khowarezm (now Khiva in Uzbekistan)
Circa 780-850 C.E. (Common Era)
5
Algorithm –History
Muhammad ibn Musa Al-Khwarizmi
http://www-groups.dcs.st-andrews.ac.uk/~history/Mathematicians/Al-Khwarizmi.html
• Book on arithmetic:
– Hindu numeration, decimal numbers, use of zero,
method for finding square root
– Latin translation (c.1120 CE): “Algoritmi de
numero Indorum”
• Book on algebra
– Hisab al-jabr w’al-muqabala
6
Algorithm – Working Definition
• A sequence of instructions describing how
to do a task
[As opposed to actually executing
the instructions]
7
Algorithm -- Examples
•
•
•
•
•
•
•
•
A cooking recipe
Assembly instructions for a model
The rules of how to play a game
VCR instructions
Description of a martial arts technique
Directions for driving from A to B
A knitting pattern
A car repair manual
8
Algorithm -- Examples
•
•
•
•
•
•
•
•
A cooking recipe
Assembly instructions for a model
The rules of how to play a game
VCR instructions
Description of a martial arts technique
Directions for driving from A to B
A knitting pattern
A car repair manual
9
Algorithms are not Programs
• Algorithms are well-defined sequence of
unambiguous instructions
• must terminate (to produce a result)
• Algorithm description relies on a well-defined
“instruction language”
• Example: Manual Addition
Describe the method!
123456
+
789001
912457
10
Algorithm – Examples (cont)
• Recipe for Almond and honey slice
• Recipe for Arroz con pollo
11
Almond and Honey Slice
1/2 quantity Shortcrust
Pastry
185 g unsalted butter
100 g castor sugar
5 tablespoons honey
50 ml cream
50 ml brandy or any other
liqueur or spirit
300 g flaked almonds
From: Stephanie Alexander, The
Cook’s Companion, Viking/Penguin,
Ringwood, Victoria, 1996, p. 349.
Preheat oven for 200° C
Line a 30 cm  20 cm baking
tray with baking paper, and
then with pastry
Bake blind for 20 minutes, then
remove weights and foil
Turn oven up to 220° C.
Bring remaining ingredients to a
boil, stirring.
Spread evenly over pastry.
Bake until topping is bubbling
and has caramelised evenly,
about 15 minutes.
Cool before cutting into fingers
or squares.
12
Almond and Honey Slice
1/2 quantity Shortcrust
Pastry
185 g unsalted butter
100 gInstructions
castor sugar are given
in the order in which
5 tablespoons honey
they are performed
50 ml(“executed”)
cream
50 ml brandy or any other
liqueur or spirit
300 g flaked almonds
From: Stephanie Alexander, The
Cook’s Companion, Viking/Penguin,
Ringwood, Victoria, 1996, p. 349.
Preheat oven for 200° C
Line a 30 cm  20 cm baking
tray with baking paper, and
then with pastry
Bake blind for 20 minutes, then
remove weights and foil
Turn oven up to 220° C.
Bring remaining ingredients to a
boil, stirring.
Spread evenly over pastry.
Bake until topping is bubbling
and has caramelised evenly,
about 15 minutes.
Cool before cutting into fingers
or squares.
13
Correct Algorithm?
Cut chicken into pieces and
brown the pieces on all
sides in a casserole dish in
hot olive oil.
Remove the chicken and to
the juices in the casserole
add garlic, onions and
green peppers, and sauté
until onion is golden.
Add bay leaf, whole
tomatoes, and chicken
broth.
When the broth boils add
salt, saffron and rice.
Arrange chicken on rice,
cover casserole and bake
in a moderate oven
(350°F) for 20 minutes or
until the rice is tender.
Add beans and artichokes
during last 10 minutes of
cooking.
From: “Arroz Con Pollo” in The
Margaret Fulton Cookbook, Hamlyn,
Sydney, 1968.
14
Correct Algorithm?
Cut chicken into pieces and
brown the pieces on all
sides in a casserole dish in
hot olive oil.
Remove the chicken and to
the juices in the casserole
add garlic, onions and
green peppers, and sauté
until onion is golden.
Add bay leaf, whole
tomatoes, and chicken
broth.
When the broth boils add
salt, saffron and rice.
Arrange chicken on rice,
cover casserole and bake
in a moderate oven
(350°F) for 20 minutes or
until the rice is tender.
Add beans and artichokes
during last 10 minutes of
cooking.
From: “Arroz Con Pollo” in The
Margaret Fulton Cookbook, Hamlyn,
Sydney, 1968.
15
Correct Algorithm?
Cut chicken into pieces and
brown the pieces on all
sides in a casserole dish in
hot olive oil.
Remove the chicken and to
the juices in the casserole
add garlic, onions and
green peppers, and sauté
until onion is golden.
Add bay leaf, whole
tomatoes, and chicken
broth.
When the broth boils add
salt, saffron and rice.
Arrange chicken on rice,
cover casserole and bake
in a moderate oven
(350°F) for 10 minutes.
Add beans and artichokes.
Cover, and bake for another
10 minutes or until rice is
tender.
16
From Algorithms to Programs
Problem
Algorithm: A sequence
of instructions describing
how to do a task (or
process)
Program
17
Algorithms are not Programs
• always design the algorithm before you start
to program,
• the idea comes before the coding.
18
Example
How to
get out?
19
Example
“run through the maze always sticking to the wall on your left”
repeat
if “no wall left”
then turn left, step
ahead
else if “no wall ahead”
then step ahead
else turn right
until “at exit”
20
Algorithms and Languages
• the very same Algorithm may look very different when
described (or implemented) in different (programming-)
languages, e.g.
“Sum up the numbers between 1 and 10
by adding each of them to a total starting with 0”
s  +/( 10)
APL-style
int s:=0; for i = 0 to 10 do s:= s+i; end
Pascal-style
21
Components of an Algorithm
• Input and Output Specification
• Variables and values
• Instructions
– Sequences
– Selections
– Repetitions
• Abstraction Mechanisms
– Objects
– Methods
Also required: Documentation
22
Values
• Represent quantities, amounts or
measurements
• May be numerical or alphabetical
(or of some other defined “type”)
• Example:
– Recipe ingredients
23
Almond and Honey Slice
1/2 quantity Shortcrust
Pastry
185 g unsalted butter
100 g castor sugar
5 tablespoons honey
50 ml cream
50 ml brandy or any other
liqueur or spirit
300 g flaked almonds
From: Stephanie Alexander, The
Cook’s Companion, Viking/Penguin,
Ringwood, Victoria, 1996, p. 349.
Preheat oven for 200° C
Line a 30 cm  20 cm baking
tray with baking paper, and
then with pastry
Bake blind for 20 minutes, then
remove weights and foil
Turn oven up to 220° C.
Bring remaining ingredients to a
boil, stirring.
Spread evenly over pastry.
Bake until topping is bubbling
and has caramelised evenly,
about 15 minutes.
Cool before cutting into fingers
or squares.
24
Almond and Honey Slice
1/2 quantity Shotcrust Pastry
185 g unsalted butter
100 g castor sugar
5 tablespoons honey
50 ml cream
50 ml brandy or any other
liqueur or spirit
300 g flaked almonds
From: Stephanie Alexander, The
Cook’s Companion, Viking/Penguin,
Ringwood, Victoria, 1996, p. 349.
Preheat oven for 200° C
Line a 30 cm  20 cm baking
tray with baking paper, and
then with pastry
Bake blind for 20 minutes, then
remove weights and foil
Turn oven up to 220° C.
Bring remaining ingredients to a
boil, stirring.
Spread evenly over pastry.
Bake until topping is bubbling
and has caramelised evenly,
about 15 minutes.
Cool before cutting into fingers
or squares.
25
Variables
• Are containers for values – places to store
values
• Example:
Variable
Values
This jar
can contain
10 cookies
50 grams of sugar
3 slices of cake
etc.
26
Restrictions on Variables
• Variables may be restricted to contain a
specific type of value
27
Components of an Algorithm
Values and Variables
• Instruction (a.k.a. primitive)
• Sequence (of instructions)
• Selection (between instructions)
• Repetition (of instructions)
• Abstractions (with Objects and Methods)
• Documentation (beside instructions)
28
Instructions (Primitives)
• Some action that is
–
–
–
–
simple
unambiguous
that the system knows about...
...and should be able to actually do
29
Instructions – Examples
•
•
•
•
•
•
•
Take off your shoes
Directions to perform
Count to 10
specific actions on values
Cut along dotted line and variables.
Knit 1
Purl 2
Pull rip-cord firmly
Sift 10 grams of arsenic
30
Instructions -- Application
• Some instructions can only be applied to a
specific type of values or variables
• Examples:
31
Instructions (Primitives) -Recommendations
• When writing an algorithm, make each
instruction simple and unambiguous
• Example:
Cut chicken into pieces and Cut chicken into pieces.
brown the pieces on all
Heat olive oil in a casserole
sides in a casserole dish
dish.
in hot olive oil.
Brown the chicken pieces in
the casserole dish.
32
Components of an Algorithm
Values and Variables
Instruction (a.k.a. primitive)
• Sequence (of instructions)
• Selection (between instructions)
• Repetition (of instructions)
• Abstractions (with Objects and Methods)
• Documentation (beside instructions)
33
Instruction (Primitives)
• When
an algorithm,
make the
A writing
“sequence”
of
instructions
and unambiguous.
simplesimple
instructions
• Example:
Cut chicken into pieces and
brown the pieces on all
sides in a casserole dish in
hot olive oil.
Cut chicken into pieces.
Heat olive oil in a casserole
dish.
Brown the chicken pieces in
34
the casserole dish.
Sequence
•
•
•
•
A series of instructions
...to be carried out one after the other...
...without hesitation or question
Example:
– How to cook a Gourmet MealTM
35
Sequence -- Example
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
Open freezer door
Take out Gourmet Meal™
Close freezer door
Open microwave door
Put Gourmet Meal™ on carousel
Shut microwave door
Set microwave on high for 5 minutes
Start microwave
Wait 5 minutes
Open microwave door
Remove Gourmet Meal™
Close microwave door
36
Components of an Algorithm
Values and Variables
Instruction (a.k.a. primitive)
Sequence (of instructions)
• Selection (between instructions)
• Repetition (of instructions)
• Abstractions (with Objects and Methods)
• Documentation (beside instructions)
37
Selection
• An instruction that decides which of two
possible sequences is executed
• The decision is based on a single true/false
condition
• Examples:
– Car repair
– Reciprocals
38
Selection Example -- Car Repair
if (motor turns)
then
{
CheckFuel
CheckSparkPlugs
CheckCarburator
}
else
{
CheckStarterMotor
CheckEngineBlock
}
39
Selection Example –
Car Repair (cont)
if (motor turns)
then
{
CheckFuel
CheckSparkPlugs
CheckCarburator
}
else
{
CheckStarterMotor
CheckEngineBlock
}
Should be a
true or false
condition.
40
Selection Example -Car Repair (cont)
if (motor turns)
then
{
CheckFuel
CheckSparkPlugs
CheckCarburator
}
else
{
CheckStarterMotor
CheckEngineBlock
}
Sequence if
the condition
is true.
41
Selection Example -Car Repair (cont)
if (motor turns)
then
{
CheckFuel
CheckSparkPlugs
CheckCarburator
}
else
{
CheckStarterMotor
CheckEngineBlock
}
Sequence if the
condition is
false.
42
Selection Example -- Reciprocals
Q. Give an
Examples:
algorithm for
computing the
reciprocal of a
number.
Reciprocal of 2:
1/2
Reciprocal of -3/4:
1/(-3/4) = -4/3
Reciprocal of 0:
“undefined”
43
Selection Example – Reciprocals (cont)
Algorithm:
Q. Give an
algorithm for
computing the
reciprocal of a
number.
input Num
if (Num is not equal 0)
then
{
output 1/Num
}
else
{
output "infinity"
}
44
Selection Example-- Reciprocals
Algorithm:
Num is a variable
whose value
depends on the
actual number the
user provides.
input Num
if (Num is not equal 0)
then
{
output 1/Num
}
else
{
output "infinity"
}
45
Selection Example – Reciprocals
(cont)
Algorithm:
Condition depends
on the value of
Num
input Num
if (Num is not equal 0)
then
{
output 1/Num
}
else
{
output "infinity"
}
46
Selection Example – Reciprocals
(cont)
Algorithm:
For a given value
of Num, only one
of these two
sequences can be
executed
input Num
if (Num is not equal 0)
then
{
output 1/Num
}
else
{
output "infinity"
}
47
Selection Example – Reciprocals
(cont)
Algorithm:
Executed if Num
is not equal to 0
input Num
if (Num is not equal 0)
then
{
output 1/Num
}
else
{
output "infinity"
}
48
Selection Example – Reciprocals
(cont)
Algorithm:
Executed if Num
is equal to 0
input Num
if (Num is not equal 0)
then
{
output 1/Num
}
else
{
output "infinity"
}
49
Selection -- Exercise
Will the following algorithms produce the same output?
Algorithm 1:
input Num
if (Num is not equal 0)
then
{
output 1/Num
}
else
{
output "infinity"
}
Algorithm 2:
input Num
if (Num is not equal 0)
then
{
output 1/Num
}
output "infinity"
50
Selection – Several Conditions
• What if several conditions need to be satisfied?
if ( today is Wednesday and the time is 10.00am )
then
{
Go to CSE1301 Lecture
}
else
{
Go to Library
}
Solution 1
51
Selection – Several Conditions (cont)
if ( today is Wednesday )
then
{
if ( the time is 10.00am )
then
{
Go to CSE1301 Lecture
}
}
else
...etc...etc...etc...
Often called a
“nested selection”
Solution 2
52
Selection – At Least One of
Several Conditions
• What if at least one of several conditions
needs to be satisfied?
if ( I feel hungry or the time is 1.00pm or my
mate has his eye on my lunch )
then
{
Eat my lunch now
}
53
Components of an Algorithm
Values and Variables
Instruction (a.k.a. primitive)
Sequence (of instructions)
Selection (between instructions)
• Repetition (of instructions)
• Abstractions (with Objects and Methods)
• Documentation (beside instructions)
54
Repetition
• Repeat an instruction...
– ...while (or maybe until) some condition
occurs
– Test the condition each time before
repeating the instruction
• Also known as iteration or loop
• Example:
– Algorithm for leaving the maze
55
Example
“run through the maze always sticking to the wall on your left”
repeat
if “no wall left”
then turn left, step
ahead
else if “no wall ahead”
then step ahead
else turn right
until “at exit”
Danger: endless loop if termination test not correct
56
Components of an Algorithm
Values and Variables
Instruction (a.k.a. primitive)
Sequence (of instructions)
Selection (between instructions)
Repetition (of instructions)
• Abstractions (with Objects and Methods)
=> Object-orientation (next lectures)
• Documentation (beside instructions)
57
Documentation
• Records what the algorithm does
• Describes how it does it
• Explains the purpose of each component of
the algorithm
• Notes restrictions or expectations
• Example:
– Getting a date
58
Documentation -- Example
Think of something romantic to do
decide on time and location
Work through address book to look for a person
initialise booking to “unsuccessful”
until (successfully booked)
{
get next Name in little black book
AskOnDate(Name, Time, Location)
DetermineBookingSuccess
}
Assumes that I will find someone in the book before it runs out
SighWithRelief
59
Components of an Algorithm
Values and Variables
Instruction (a.k.a. primitive)
Sequence (of instructions)
Selection (between instructions)
Repetition (of instructions)
• Abstractions (with Objects and Methods)
=> Object-orientation (next week)
Documentation (beside instructions)
60
Analysis of Algorithms
we are interested in...
• Well-definedness (otherwise it’s not an algorithm)
• Correctness (does it fulfill the specification?)
• Termination (does it always end?)
• Resource consumption (time and memory)
61
Example
picking the lightest item...
1.
2.
3.
4.
5.
6.
7.
8.
put first item on scale
memorize weight
take item from scale in hand
repeat
put next item on scale
compare to memorized weight
take lighter item in hand; put other item away
until no more items left
...is this well-defined?
...can we be sure that this always terminates with the correct item in hand?
62
The Software Development Process
•
•
•
•
•
•
Define the problem clearly
Analyse the problem thoroughly
Design an algorithm carefully
Code the algorithm efficiently
Test the code thoroughly
Document the system lucidly
63
Summary
• Problem Solving Process
• Algorithms
• Components of Algorithms
– Values and Variables
– Instructions
– to be continued…(with Abstractions)
• Documentation
• Analysis of Algorithms
64
Download