Algorithms - PowerPoint PPT Presentation

1 / 57
About This Presentation
Title:

Algorithms

Description:

11 (base five) = 6, 10 (base six) = 6, so 6 ... into fingers or squares. ... Take off your shoes. Count to 10. Cut along dotted line. Knit 1. Purl 2. Pull ... – PowerPoint PPT presentation

Number of Views:62
Avg rating:3.0/5.0
Slides: 58
Provided by: Preu
Category:

less

Transcript and Presenter's Notes

Title: Algorithms


1
Algorithms
  • "Perfection is achieved not when you have nothing
    more to add, but when you have nothing left to
    take away."   
  •   Antoine de Saint- Exupery

2
The Problem-solving Process
"Doctor, my head hurts"
Patient has elevated pressure in anterior
parietal lobe
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. /
01001110101100101010101010010101010101001100101010
10101001011010011101010101010010010111010011110101
010111110101010001101
3
Algorithm History
  • Persian author Abu Jafar Mohammed ibn Musa al
    Khwarizmi wrote a text book on mathematics
    (c.825 A.D.)
  • http//www-groups.dcs.st-andrews.ac.uk/history/Ma
    thematicians/Al-Khwarizmi.html
  • The word algorithm comes from his name.
  • al-Khwarizmi literally means from the town of
    Khowarazm this city is now called Khiva,
    located in Uzbekistan

4
Algorithm Working Definition
  • A sequence of instructions describing how to do a
    task
  • Algorithm is a finite set of instructions that,
    if followed, accomplishes a particular task.

As opposed to actually executing the
instructions
5
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

6
Algorithms Definition
  • An algorithm is an 1ordered set of 2unambiguous,
    3executable steps that define a 4terminating
    activity
  • Brookshear
  • The term Algorithm differs from terms like
    Method, or Technique as algorithm attempts to
    encode intelligence in some way.

7
Algorithms Definition
  • Ordered algorithms specify the sequencing of
    steps in a process
  • Unambiguous algorithms specify with clarity
    what activity or action should occur
  • Executable algorithms specify action that can,
    in fact, be done
  • Terminating there is a well-defined end point

8
Criteria for Algorithms
  • Input Zero or more quantities are externally
    supplied
  • Output At least one desired result is produced
  • Definiteness Each instruction must be clear and
    unambiguous
  • Finiteness Algorithm terminates after a finite
    number of steps
  • Effectiveness Each instruction must be feasible
    and very basic

9
The Problem
  • Algorithms are difficult to construct Problems
    are not represented in a manner directly
    understandable by the machine.
  • Provide paychecks for all employees
  • Simulate the effects of an earthquake
  • Track business sales and inventory
  • Humans (like you) must develop algorithms (using
    operations of the machine) to solve complex
    problems

10
Ambiguity
  • Ambiguity is inherent in the English Language
  • Understand the problem clearly and eliminate
    ambiguity when defining the problem
  • Computer Scientists have developed formal
    languages and methods to deal with ambiguity

11
Exercise Organize Yourselves!
  • Purpose Organize into Groups of 3
  • Who Everyone
  • Task Organize yourselves into 3-member groups
    with the condition that
  • Every member of the group must have day of birth
    within 3 days of every other member. Minimize
    the number of students that do not belong to a
    group.
  • Product Every member of the class is either in a
    group or is an orphan
  • Presentation Group member names and birthdays

12
Reflect
  • Was there ambiguity in the problem statement?
  • How did you recognize the ambiguity? How did you
    resolve the ambiguity?
  • What were the sources of difficulty in solving
    this problem?
  • How did you solve the problem?
  • Did the class have a method or an algorithm for
    deciding on the groups?
  • Could you write down that algorithm?
  • If you did this again, would you be able to
    discover a way to do it faster, and have fewer
    orphans?

13
Writing An Algorithm
  • Purpose To explore writing algorithms
  • Who Groups of 3 formed as a result of exercise 1
  • Task Write the instructions for decoding a given
    8-bit floating point encoding
  • Product A set of instructions on a single sheet
    of paper that can be handed to lesser minds who
    can then duplicate your design
  • Presentation Exchange your instructions with
    another group and see if you can follow their
    steps.

14
Reflect
  • How close were the instructions to what you wrote
    yourself?
  • What difficulties did you encounter following the
    other persons instructions?
  • What would have helped?
  • What worked well?

15
Problem Solving
  • Problem Fill in the blank in the sequence
  • 110, 20, 12, 11, 10, __, ...

16
One Possibility
  • Answer 6. The pattern is
  • 110 (base two) 6, 20 (base three) 6, 12
    (base four) 6
  • 11 (base five) 6, 10 (base six) 6, so 6
    (base seven) 6
  • The point is not whether you solve it but that
    you realize that solving a problem is a creative
    process that may not be achieved by following a
    prescribed methodology.
  • Actually, you could argue that there are many
    correct answers to this problem, but I think
    you'll agree that the one given below is a good
    one.

17
How do we solve problems?
  • We "just do"
  • Guesswork-and-luck
  • Trial-and-error
  • Experience (possibly someone else's)
  • "Scientifically"

18
Polyas Phases (c.1945)
  • Phase 1. Understand the problem
  • Phase 2. Devise a plan for solving the problem
  • Phase 3. Carry out the plan
  • Phase 4. Evaluate the solution for accuracy and
    for its potential as a tool for solving other
    problems

19
In program development
  • Phase 1. Understand the problem (ANALYSIS)
  • Phase 2. Get an idea of how an algorithmic
    procedure might solve the problem (DESIGN)
  • Phase 3. Formulate the algorithm and represent it
    as a program (IMPLEMENTATION)
  • Phase 4. Evaluate the program for accuracy and
    for its potential as a tool for solving other
    problems (COMPILATION, EXECUTION)

20
Algorithm Discovery
  • How are algorithms found in the first place?
  • The Art of Problem Solving
  • Can the process of problem-solving be reduced to
    an algorithm itself?
  • This has been shown to be impossible
  • (See Chapter 11)
  • Ability to solve problems remains more of an
    artistic skill to be developed rather than a
    precise science to be learned

21
The Art of Problem Solving
  • Mr. A is charged with the task of determining the
    ages of Mr. Xs three children.
  • 1. Mr. X tells Mr. A that the product of the
    three ages is 36
  • After considering this Mr. A decides he need
    another clue
  • 2. Mr. X tells Mr. A that the sum of his
    childrens ages.
  • Again, Mr. A notices that he needs one more clue
    to solve this problem.
  • 3. Mr. X tells Mr. A that his oldest child plays
    the piano.
  • After hearing the last clue, Mr. A tells Mr. X
    the ages of the three children.
  • How old are the three children?

22
Lets solve the problem!
  • First clue leads to the following triples whose
    product is 36
  • (1,1,36) (1,6,6)
  • (1,2,18) (2,2,9)
  • (1,3,12) (2,3,6)
  • (1,4,9) (3,3,4)

23
Lets solve the problem!
  • Second clue is about the sum of the triple, but
    we are not told what that is.
  • But, we know that sum of the three ages is still
    not sufficient to solve the problem. Why Not?
  • Sum of triples from previous slide
  • 113638 16613
  • 121821 22913
  • 131216 23611
  • 14914 33410

24
Lets solve the problem!
  • Notice that there are two triples (1,6,6) and
    (2,2,9) whose sum is 13, therefore, we know that
    the answer must be one of these two because Mr. A
    realizes that the two clues so far is not
    sufficient to isolate the answer.
  • The third clue was that the oldest child plays
    the piano. The significance of this clue is that
    there is an oldest child. This rules out (1,6,6).
    Therefore the ages are (2,2,9).

25
Getting a foot in the door
  • Working Backwards
  • Knowing what the output needs to be, work back up
    to the given input
  • Adapt an existing solution
  • Understand the solution to a related problem and
    apply it to the problem at hand
  • Stepwise Refinement
  • Decompose the given problem into several sub
    problems and solve them progressively, leading to
    refinement of solution

26
TD and BU Methodologies
  • Top Down Methodology
  • General to Specific
  • Example, Stepwise Refinement
  • Bottom Up Methodology
  • Specific to General
  • The two methodologies complement each other and
    are often used in conjunction.
  • TD Stepwise Refinement is usually guided by the
    problem solvers intuition, working in the BU
    mode.

27
From Algorithms to Programs
28
Algorithm Representation
  • Before learning to code a program in any specific
    language, we will use a less formal notational
    system for writing algorithms viz., Pseudocode
  • Pseudocode
  • A Consistent,
  • concise notation for representing
  • recurring semantic structures,
  • which in turn will become the primitives for
    expressing future ideas.

29
Components of an Algorithm
  • Variables and values
  • Instructions
  • Sequences
  • Procedures
  • Selections
  • Repetitions
  • Documentation

30
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

31
Almond and Honey Slice
  • 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
    caramelized evenly, about 15 minutes.
  • Cool before cutting into fingers or squares.
  • 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 Cooks Companion,
Viking/Penguin, Ringwood, Victoria, 1996, p. 349.
32
Variables
  • Are containers for values places to store
    values
  • Example

33
Restrictions on Variables
  • Variables may be restricted to contain a specific
    type of value.
  • Well learn what Data Types are and how they
    help organize and store data of specific types.

34
Components of an Algorithm
  • Values and Variables
  • Instruction (a.k.a. primitive)
  • Sequence (of instructions)
  • Procedure (involving instructions)
  • Selection (between instructions)
  • Repetition (of instructions)
  • Documentation (beside instructions)

35
Instructions (Primitives)
  • Some action that is simple...
  • ...and unambiguous...
  • ...that the system knows about...
  • ...and should be able to actually do

36
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.
37
Instructions -- Application
  • Some instructions can only be applied to a
    specific type of values or variables
  • In BSM, Op-code 6 is for floating point addition
    if your operands are not in FP notation, it is an
    Illegal Operation for that machine.

Chop
38
Instructions (Primitives) -- Recommendations
A sequence of simple instructions
  • When writing an algorithm, make each instruction
    simple and unambiguous
  • Example

Cut chicken into pieces. Heat olive oil in a
casserole dish. Brown the chicken pieces in the
casserole dish.
Cut chicken into pieces and brown the pieces on
all sides in a casserole dish in hot olive oil.
39
Instruction Primitives
A sequence of simple instructions
  • Another Example

Read the first nybble of string 1 and store it as
result1 Read the second nybble of second number
and store it as result2 Combine result1 and
result2 to get the final result
Combine nybbles by reading the first 4 bits of
first 8-bit binary string string1 and the last
four bits of the second binary string string2 and
put them together to get the final result.
40
Components of an Algorithm
  • Values and Variables
  • Instruction (a.k.a. primitives)
  • Sequence (of instructions)
  • Procedure (involving instructions)
  • Selection (between instructions)
  • Repetition (of instructions)
  • Documentation (beside instructions)

41
Sequence
  • A series of instructions
  • ...to be carried out one after the other...
  • ...without hesitation or question
  • If the sequence is not strictly followed, we can
    get unpredictable results.
  • So, designing the sequence is crucial to ensure
    correct results.

42
Sequence -- Example
  • 1. Open freezer door
  • 2. Take out Gourmet Meal
  • 3. Close freezer door
  • 4. Open microwave door
  • 5. Put Gourmet Meal on carousel
  • 6. Shut microwave door
  • 7. Set microwave on high for 5 minutes
  • 8. Start microwave
  • 9. Wait 5 minutes till microwave stops
  • 10. Open microwave door
  • 11. Remove Gourmet Meal
  • 12. Close microwave door

43
Components of an Algorithm
  • Values and Variables
  • Instruction (a.k.a. primitives)
  • Sequence (of instructions)
  • Procedure (involving instructions)
  • Selection (between instructions)
  • Repetition (of instructions)
  • Documentation (beside instructions)

44
Procedure
  • A named sequence of instructions
  • So that you can
  • Refer to it collectively (by name)
  • ...instead of individually (by each instruction
    in the sequence)
  • Example
  • Drive_To_PCC

45
Procedure -- Example
  • procedure Drive_To_PCC
  • 1. find car keys
  • 2. disable car alarm
  • 3. open car door
  • 4. get in car
  • 5. shut car door
  • 6. put keys in ignition
  • 7. start car
  • 8. back car out of driveway
  • 9. drive to end of street
  • 10. turn right
  • 11. drive to end of street
  • 12. turn left ...etc...etc...etc

...etc...etc...etc... 52. find parking
space 53. pull into parking space 54.
turn off engine 55. remove keys from
ignition 56. open car door 57. get out
58. shut car door 59. lock car door 60.
enable alarm
46
Procedure Example (cont) RE-USE!
  • procedure Do_Monday
  • Wake_up
  • Take_A_Shower
  • Eat_Breakfast
  • Drive_To_PCC
  • Attend_CS160_Lecture
  • ...etc...etc...etc...
  • Drive_From_PCC
  • ...etc...etc...etc...

procedure Do_Week Do_Monday Do_Tuesday
Do_Wednesday Do_Monday
Do_Thursday ...etc...etc...etc...
47
Procedure Example (cont)
  • procedure Do_Monday
  • Wake_up
  • Take_A_Shower
  • Eat_Breakfast
  • Drive_To_PCC
  • Attend_CS01_Lecture
  • ...etc...etc...etc...
  • Drive_From_PCC
  • ...etc...etc...etc...
  • In this context, we also use the following words
    to refer to a Procedure
  • Sub-routine
  • Module
  • Function

48
Procedure Example (cont)
We use brackets to mark the beginning and end of
a procedure. Also known as a BLOCK, ( of
instructions), to be carried out in sequence,
together as a set, when called for.
  • procedure Do_Monday
  • Wake_up
  • Have_Shower
  • Eat_Breakfast
  • Drive_To_Uni
  • Sit_1301_Lecture
  • ...etc...etc...etc...
  • Drive_From_Uni
  • ...etc...etc...etc...

49
Procedure Example (cont)
  • procedure Do_Monday
  • Wake_up
  • Take_A_Shower
  • Eat_Breakfast
  • Drive_To_PCC
  • Attend_CS160_Lecture
  • ...etc...etc...etc...
  • Drive_From_PCC
  • ...etc...etc...etc...

An instruction invoking a procedure is known as a
procedure call
50
Procedure
  • A procedure may have a set of parameters

procedure customerService (timeOfDay, myName
) say Good timeOfDay say My name is
myName say How can I help you?
customerService ( Morning, Ann
) customerService (Afternoon, Ann
) customerService ( Evening, Jeff )
51
Components of an Algorithm
  • Values and Variables
  • Instruction (a.k.a. primitives)
  • Sequence (of instructions)
  • Procedure (involving instructions)
  • Selection (between instructions)
  • Repetition (of instructions)
  • Documentation (beside instructions)

52
Selection
  • An instruction that decides which of two possible
    sequences is executed
  • The decision is based on a single true/false
    condition
  • Examples
  • Odd/Even test
  • Letter grade based on points earned
  • Reciprocals

53
Instruction Structures Selection
Should be a true or false condition.
  • Selection
  • If (the condition is true)
  • Do Something
  • Else
  • Do Something Else
  • Example
  • If (value in register 0 value of cell 45)
  • write CC in cell 88
  • Else
  • write DD in cell 88

54
Selection If Else
Should be a true or false condition.
  • If a given number n is odd, write ODD, else
    write EVEN.
  • If (n mod 2 not equal to zero)
  • write ODD
  • else
  • write EVEN

The operator mod computes the remainder of a
division operation. 16 mod 4 gives 0 13 mod 5
gives 3 3 mod 2 gives 1
55
Selection Multiple (Nested)
Example Charging Admission Ticket at a Theater
based on age
  • If (condition1 is true)
  • Do Action1
  • else
  • if (condition2 is true)
  • Do Action2
  • else
  • Do Action3
  • If( age lt 10)
  • Charge nothing
  • else
  • if (age gt 64)
  • charge senior rate
  • else
  • charge regular rate

56
Selection Example Letter Grade. Nested
Conditions
  • if (points gt 89 and points lt 101)
  • Assign letter grade A
  • else
  • if (points gt 79 and points lt 91)
  • Assign letter grade B
  • else
  • Assign letter grade C

Each block is enclosed in brackets.
Each nested block is properly indented
57
Summary(Acknowledgement Many slides in this
presentation are courtesy of Monash University
courseware repository at http//www.csse.monash.ed
u.au/courseware/cse1301/lectures/)
  • Problem Solving Process
  • Algorithms
  • Components of Algorithms
  • Values and Variables
  • Instructions
  • Procedure
  • Selection
  • To be continued
Write a Comment
User Comments (0)
About PowerShow.com