1 CSE1301 Computer Programming Lecture 3: Introduction to Algorithms
Dec 17, 2015
1
CSE1301Computer Programming
Lecture 3:Introduction to Algorithms
2
Topics
• Solving problems
• Algorithms
• Values and variables
3
How do we solve problems?
• We "just do"
• Guesswork-and-luck
• Trial-and-error
• Experience (possibly someone else's)
• "Scientifically"
4
0100111010110010101010101001010101010100110010101010101001011010011101010101010010010111010011110101010111110101010001101…
sterilize(saw,alcohol);raise_hammer();lower hammer(fast);start(saw);/* etc. etc. */
Patient has elevated pressure in anterior parietal lobe
The Problem-solving Process
Problem
specification
Algorithm
Program
Executable (solution)
Design
Implementation
Compilation
"Doctor, my head hurts"
Analysis
1. Sterilize cranial saw2. Anaesthetize patient3. Remove top of skull4. Get the big spoon...5. etc., etc.
5
sterilize(saw,alcohol);raise_hammer();lower hammer(fast);start(saw);/* etc. etc. */
The Problem-solving Process
Problem specification
Algorithm
Program
Executable (solution)
Analysis
Design
Implementation
Compilation
"Doctor, my head hurts"
Patient has elevated pressure in anterior parietal lobe.
1. Sterilize cranial saw2. Anaesthetize patient3. Remove top of skull4. Get the big spoon...5. etc., etc.
010011101011001010101010100101010101010011001010101010100101101001110101010101001001011101001111010101011111010101000110100001101...
6
The Problem-solving Process
Problem specification
Algorithm
Program
Executable (solution)
Analysis
Design
Implementation
Compilation
7
Algorithm – Dictionary DefinitionFrom: Websters Revised Unabridged Dictionary, 1913:
Algorism (Al"go*rism Al"go*rithm) • n. [OE. algorism, algrim, augrim, OF. algorisme, F.
algorithme (cf. Sp. algoritmo, OSp. alguarismo, LL. algorismus)
• from the Arabic Al-Khowarezmi of Khowarezm, the modern Khiwa, surname of Abu Ja'far Mohammed ben Musa, author of a work on arithmetic early in the 9th century, which was translated into Latin, such books bearing the name algorismus.
8
• 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)
Algorithm
9Source: http://www.atlapedia.com/online/maps/political/Kazakh_etc.htm
10
Algorithm– HistoryMuhammad ibn Musa Al-Khwarizmi• Circa 160-230 A.H. (anno Hegirae)
in the year of the Hegira; of the Muslim era
• Circa 780-850 C.E. (Common Era)
11
Algorithm –History (cont)
Muhammad ibn Musa Al-Khwarizmihttp://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
12
• A sequence of instructions describing how to do a task
Algorithm – Working Definition
[As opposed to actually executing
the instructions]
13
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
14
Algorithm – Examples (cont)
• Recipe for Almond and honey slice
• Recipe for Arroz con pollo
15
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
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.
From: Stephanie Alexander, The Cook’s Companion, Viking/Penguin, Ringwood, Victoria, 1996, p. 349.
16
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
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.
From: Stephanie Alexander, The Cook’s Companion, Viking/Penguin, Ringwood, Victoria, 1996, p. 349.
Instructions are given in the order in which they are performed (“executed”)
17
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.
18
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.
Correct Algorithm?
19
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.
Correct Algorithm?
20
From Algorithms to Programs
Problem
C ProgramC Program
AlgorithmAlgorithm: A sequence of instructions describing how to do a task (or process)
21
Components of an Algorithm
• Variables and values
• Instructions
• Sequences
• Procedures
• Selections
• Repetitions
• Documentation
22
Values
• Represent quantities, amounts or measurements
• May be numerical or alphabetical (or other things)
• Often have a unit related to their purpose
• Example:– Recipe ingredients
23
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
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.
From: Stephanie Alexander, The Cook’s Companion, Viking/Penguin, Ringwood, Victoria, 1996, p. 349.
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
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.
From: Stephanie Alexander, The Cook’s Companion, Viking/Penguin, Ringwood, Victoria, 1996, p. 349.
25
Variables
This jarcan contain
10 cookies
50 grams of sugar
3 slices of cake
etc.
ValuesVariable
• Are containers for values – places to store values
• Example:
26
Restrictions on Variables
• Variables may be restricted to contain a specific type of value
27
Components of an AlgorithmValues and Variables
• Instruction (a.k.a. primitive)
• Sequence (of instructions)
• Procedure (involving instructions)
• Selection (between instructions)
• Repetition (of instructions)
• Documentation (beside instructions)
28
Instructions (Primitives)
• Some action that is simple...
• ...and unambiguous...
• ...that the system knows about...
• ...and should be able to actually do
29
Instructions – Examples
• Take off your shoes
• Count to 10
• Cut along dotted line
• Knit 1
• Purl 2
• Pull rip-cord firmly
• Sift 10 grams of arsenic
Directions to perform specific actions on values and variables.
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 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 the casserole dish.
32
Instruction (Primitives)• When writing an algorithm, make the
instructions simple and unambiguous.
• 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 the casserole dish.
A “sequence” of simple instructions
33
Summary• Problem Solving Process• Algorithms• Components of Algorithms
– Values and Variables– Instructions– to be continued...
Reading• Deitel & Deitel, C: How to program
– Chapter 3, Sections 3.1 to 3.3