cmsc 104, l041 algorithms, part 1 of 3 topics definition of an algorithm example: the euclidean...
Post on 16-Dec-2015
221 Views
Preview:
TRANSCRIPT
CMSC 104, L04 1
Algorithms, Part 1 of 3
Topics
• Definition of an Algorithm• Example: The Euclidean Algorithm• Syntax versus Semantics
Reading
• Sections 1.4 – 1.5
The First step in the programming process
CMSC 104, L04 2
Problem Solving
• Problem solving is the process of transforming the description of a problem into the solution of that problem.
• We use our knowledge of the problem domain.
• We rely on our ability to select and use appropriate problem-solving strategies, techniques, and tools.
CMSC 104, L04 3
Algorithms
• An algorithm is a step by step solution to a problem.
• Why bother writing an algorithm ?o For your own use in the future. You won’t have
to rethink the problem.o So others can use it, even if they know very
little about the principles behind how the solution was derived.
CMSC 104, L04 4
Examples of Algorithms
• Washing machine instructions• Instructions for a ready-to-assemble piece
of furniture• A classic: finding the greatest common
divisor (GCD)o The Euclidean Algorithm
CMSC 104, L04 5
Washing Machine Instructions
• Separate clothes into white clothes and colored clothes.
• For white clothes:o Set water temperature knob to HOT.o Place white laundry in tub.
• For colored clothes:o Set water temperature knob to COLD.o Place colored laundry in tub.
• Add 1 cup of powdered laundry detergent to tub.• Close lid and press the start button.
CMSC 104, L04 6
Start
Separate Clothes into Light / Darkand Select Which one to wash
YES NO
Set Temp. to HOT Are Cloths Light Set Temp. to Cold?
Place Cloths in Tub
Add 1 cup detergent to Tub
Close Lid and Press Start Button
Stop
Flow Chart for Algorithm
If / else StructurePage 159 in
book.
CMSC 104, L04 7
Observations About the Washing Machine Instructions
• There are a finite number of steps.• We are capable of doing each of the
instructions.• When we have followed all of the steps,
the washing machine will wash the clothes and then will stop.
CMSC 104, L04 8
Refinement of the Definition
• Our old definition: o An algorithm is a step by step solution to a
problem.
• Adding our observations: o An algorithm is a finite set of executable
instructions that directs a terminating activity.
CMSC 104, L04 9
Instructions for a Ready-to-Assemble Piece of Furniture
• "Align the marks on side A with the grooves on Part F.“
• How could these instructions be hard to follow?o Which side is A? A & B look alike -- both
line up with Part F. This instruction is ambiguous.
o The steps are not sequential, do not follow any particular order.
CMSC 104, L04 10
Poor Example of an Algorithm
CMSC 104, L04 11
Poor Example of Algorithm
CMSC 104, L04 12
Poor example of an algorithm.
No Start, No Stop,No Steps
CarburetorRebuildingDiagram.
CMSC 104, L04 13
Final Version of the Definition
• Our old definition: o An algorithm is a finite set of executable
instructions that directs a terminating activity.
• Final version:o An algorithm is a finite set of unambiguous,
executable instructions that directs a terminating activity.
CMSC 104, L04 14
History of Algorithms
• The study of algorithms began as a subject in mathematics.
• The search for algorithms was a significant activity of early mathematicians.
• Goal: To find a single set of instructions that can be used to solve any problem of a particular type (a general solution).
CMSC 104, L04 15
The Euclidean Algorithm
Problem: Find the largest positive integer that divides evenly into two given positive integers (i.e., the greatest common divisor).
Algorithm: Assign M and N the values of the larger and smaller of
the two positive integers, respectively. Divide M by N and call the remainder R. If R is not 0, then assign M the value of N, assign N the
value of R, and return to Step 2. Otherwise, the greatest common divisor is the value currently assigned to N.
CMSC 104, L04 16
Finding the GCD of 24 and 9
M N R
24 9 6
9 6 3
6 3 0
So, 3 is the GCD of 24 and 9.
CMSC 104, L04 17
Euclidean Algorithm (con’t)
• Do we need to know the theory that Euclid used to come up with this algorithm in order to use it ?
• What intelligence is required to find the GCD using this algorithm ?
CMSC 104, L04 18
Greatest Com. Divisor Flow Chart Start
Assign M = LargerN = Smaller
Get Remainder R of M / N
Get Remainder R of M / N
Assign M value of N N Does R = 0Assign R value of N ?
Yes
N is GCD
Stop
While loop StructurePage 214 in book.
Assign M the value of NAssign N the value of R
CMSC 104, L04 19
The Idea Behind Algorithms
• Once an algorithm behind a task has been discovered
o We don't need to understand the principles.
o The task is reduced to following the instructions.
o The intelligence is "encoded into the algorithm."
CMSC 104, L04 20
Algorithm Representation
• Syntax and Semantics
o Syntax refers to the representation itself.
o Semantics refers to the concept represented. (i.e., the logic).
CMSC 104, L04 21
Contrasting Syntax and Semantics
• In the English language, we have both syntax and semantics.
• Syntax is the grammar of the language.• Semantics is the meaning.• Given the following sentence,
I walked to the corner grocery store.
o Is this sentence syntactically correct?o Is it semantically correct?
CMSC 104, L04 22
Contrasting Syntax and Semantics (con’t)
• Given the following sentence,
I talked to the circular grocery store.
o Is this sentence syntactically correct?o Is it semantically correct?
• How about
I grocery store walked corner the to.
CMSC 104, L04 23
Contrasting Syntax and Semantics (con’t)
• Conclusion: An English sentence may be syntactically correct, yet semantically (logically) incorrect.
• This is also true of algorithms.
• And it is also true of computer code.
CMSC 104, L04 24
Flowcharting
• Flowcharting is another technique used in designing and representing algorithms.
• A flowchart is a graph consisting of geometric shapes that are connected by flow lines.
• From the flowchart one can write the program code.
CMSC 104, L04 25
Symbols (I)
Terminal symbol
Process symbol
Input-Output symbol
or
Decision symbol
CMSC 104, L04 26
Symbols (II)
Flow Lines indicating the logical sequence of statements
One must follow the flow of thearrows direction, one cannot goin the opposite direction of thearrows flow.
top related