Chapter 7. Algorithmic Thinking

advertisement
Chapter 7
What's The Plan?:
Algorithmic Thinking
Learning Objectives
1. List the five essential properties of an algorithm
2. Consider other properties of algorithms
3. Review examples of algorithms
4. Explain similarities and differences among
algorithms, programs, and heuristic solutions
5. Review Algorithmic Thinking
Algorithm
• A precise, systematic method for producing
a specified result. Purposeful. Directed.
• A (step-by-step) plan for a solution to a
problem using a finite amount of time and a
finite amount of resources (including data)
• We have already seen several:
– Procedure to submit Projects
– Drawing a shape or letter using Python
– Hex to bits (binary) (Ch.4) and reverse
Algorithms in Everyday Life
• Some algorithms are learned —
arithmetic, shoelaces, dressing.
• Some we (can) figure out ourselves —
looking up a phone number
• Others require written instructions —
recipe, assembly instructions, driving
directions, etc.
Is it an Algorithm?
• The process of doing something step-bystep may not be not an algorithm
– An algorithm is a “systematic method for
producing a specified result”
– Searching is purposeful, and provides
direction…with no guarantee of a result!
– Algorithms ALWAYS work!
– Heuristic might describe the search.
Heuristics are helpful procedures for
finding a result
Five Essential Properties of Algorithms
1. Input specified (Finite)
– Data to be transformed during the
computation to produce (any) output
– Must specify type, amount, and form of
data
2. Output specified (Finite or not?)
– Information resulting from the computation
– The intended result (type, amount, form)
– It is possible to have no output
Five Essential Properties (cont'd)
3. Definiteness
– Specify the sequence of steps
– Details of each step, including how to
handle errors
4. Effectiveness
– The operations are doable. Possible.
5. Finiteness
– Must eventually stop
– Computer algorithms often repeat instructions with
different data and finiteness may be a problem
Language in Algorithms
• Natural languages
– For people, we use a natural language
like English
– Ambiguity is common in natural language
– Algorithms Should Be “ Clear and Precise”
despite the language used
• Programming Language
– Formal languages designed to
express algorithms
– Precisely defined; no ambiguity
The Context of a Program
• A Program can fulfill the five properties of an
algorithm, be unambiguous, and still not work right
because it is executed in the wrong context!
– Context example: Last name in Western countries
is typically the family name;
in Asian countries it may mean given name
• Context matters: Driving instructions
– "From the Limmat River go to Bahnhof Strasse and
turn right."
– Assumes you are traveling in a specific direction.
If you are not, the directions will fail
– Don’t assume! Be specific. Be precise and as
detailed as needed.
Program vs. Algorithm
• A program is one or more algorithms,
customized to solve a specific task under
a specific set of circumstances and
expressed in a specific language. (A
Programming Language.)
• An Algorithm is a general method; A
Program is a specific method
• This is a relative comparison !
Analyzing An Algorithm
• Illustrate the five basic properties of algorithms using
an example. Class example?
– Inputs and Outputs listed
– Each instruction defined precisely (definiteness)
– Operations are effective if they are simple and
mechanically doable
– Finiteness is satisfied if there are a finite number
possibilities, so instructions will not be repeated
indefinitely
Other Algorithmic Characteristics
• Algorithms Should Be General and be
applicable to several cases
• Algorithms Should Use Resources
Efficiently:
– Fast Speed
– Minimal RAM or Disk Space
• Algorithms Should Be Understandable
so that the operations are clear to
readers
More Algorithmic Characteristics
• Exhaustive. Considers all the input/output
(as needed)
• Non-redundant. Does not repeat steps or
do work more than necessary
• Progressive. Steadily solves the problem
(some algorithms backtrack)
• Complete. Covers all required possibilities
• Goal-achieving. Solves the problem, or
sub-problems
Algorithm Facts
1. Algorithms can be specified at different levels
of detail
– Functions to simplify the algorithmic description
– These may have their own algorithms
2. Algorithms should build on functionality
previously defined and known to the user
3. Different algorithms can solve the same
problem differently, and the different
solutions can take different amounts of time
(or space)
How Do We Know it Works?
• Suppose an algorithmic solution is clear and
simple and efficient
• Then, how do we know it works?
• If there is no loop, the program runs, gets to
an end, and we can check the result
• What if there is a loop?
– Programs with loops cannot be absolutely verified
to work…there are too many possible cases
– A loop usually includes a test to determine
whether the instructions should be repeated one
more time
Then, what?
• One way to know that an algorithm works is
to know why it works…
• Informal Strategy for knowing why it works:
– Find one or more properties that ensure
the algorithm works
– Explain, using the program, why these
properties make it work.
Summary
• We use algorithms daily, and we often
create them as we instruct other people
in how to do something.
• Everyday algorithms can be sometimes
be unclear because natural language is
imprecise.
• Algorithms have five fundamental
properties.
• Good ones have other properties as well
Summary
• Algorithms can be given at different
levels of detail depending on the abilities
of the agent performing the algorithm.
• Problems can be solved by different
algorithms in different ways.
• Algorithms always work—either they
give the answer, or say no answer is
possible—and they are evaluated on
their use of resources such as space
and time..
Download