2022-Algorithms Lecture 5
2022-Algorithms Lecture 5
Dr Ghada Deghedi
Lecture 5 2022-2023
1
Introduction
• In problem solving, algorithm development and coding we
tend to view problems at different levels of abstraction
2
Introduction
• In Part 1, we illustrated the working of problem
solving from a general perspective.
• In computing, we focus on the type of problems
categorically known as algorithmic problems,
where their solutions are expressible in the form
of well-defined procedure consisting of a set of
instructions, that takes some value or set of
values, as input, and produces some value or set
of values, as output.
Heuristic solutions: require reasoning built on knowledge and experience, and
a process of trial and error. (not guaranteed to be optimal put satisfactory)
3
Introduction
• A cooker, for instance, follows a procedure commonly known
as a recipe that converts the ingredients (input) into some
culinary dish (output), after a certain number of steps.
4
Introduction
• An algorithm, is a form that embeds the complete
logic of the solution. Its formal written version is
called a computer program, or code.
• Thus, algorithmic problem solving actually
comes in two phases:
Derivation of an algorithm that solves the
problem
5
Introduction
• Computer programming can be divided into
three distinct areas of activity:
• 1. Problem definition and problem solving
• 2. Creating a structured solution (or algorithm)
• 3. Coding (e.g. Java, Python, C++)
6
• Has no intelligence or thinking power.
• The Intelligence Quotient (I.Q) of a computer is zero.
• A computer performs many tasks exactly in the same manner as it is
told to do.
• People are better than computers at developing heuristic solutions.
• Computers are built to deal with algorithmic solutions, which are
often difficult or very time consuming for humans.
• The field of computers that deals with heuristic types of problems is
7
called artificial intelligence.
Introduction
• In order to instruct a computer program
correctly, the user must have clear
understanding of the problem to be solved.
Apart from this he should be able to develop a
method, in the form of series of sequential
steps, to solve it.
• Once the problem is well-defined and a
method of solving it is developed, then
instructing the computer to solve the problem
becomes relatively easier task.
8
Algorithms
• The word algorithm comes from the name of a
mathematician Abu Ja’far Mohammed ibn-i
Musa al Khowarizmi.
9
Algorithms: Real Life Examples
◼ Many Real-Life Examples
Cooking: Recipe for preparing a dish
Origami: The Art of Paper Folding
Directions: How to go to Cairo Airport
1. Boil kettle
2. Put tea in cup
3. Pour boiling water
into cup
10
Informal definition of an algorithm
Computer Algorithms
A set of sequential steps usually written in Ordinary Language to
solve a given problem is called Algorithm. It is used for
obtaining a required output for any legitimate input in a finite
amount of time.
problem
algorithm
12
Algorithm Definition
• An algorithm can be defined as “a complete,
unambiguous, finite number of logical steps
for solving a specific problem “
13
Algorithms
• It may be possible to solve to problem in more than
one way, resulting in more than one algorithm.
• The choice of various algorithms depends on the
factors like reliability, accuracy and easy to modify.
• The most important factor in the choice of algorithm
is the time requirement to execute it, after writing
code in High-level language with the help of a
computer. The algorithm which will need the least
time when executed is considered the best.
14
Steps involved in algorithm development
• Step1: Identification of input: For an algorithm, there are
quantities to be supplied called input and these are fed
externally. The input is to be identified first for any specified
problem.
• Step2: Identification of output: From an algorithm, at least
one quantity is produced, called for any specified problem.
• Step3: Identification the processing operations: All the
calculations to be performed in order to lead to output from the
input are to be identified in an orderly manner.
15
Properties of an efficient algorithm
16
Properties of an efficient algorithm
• Finiteness: An algorithm must terminate in a finite number of steps
• Definiteness: Each step of the algorithm must be clear and unambiguous.
• Effectiveness: Each step must be effective, in the sense that it should be
primitive easily convertible into program statement , can be performed
exactly in a finite amount of time.(steps must be doable).
• Input/output: Each algorithm must take zero, one or more quantities as
input data and produce one or more output values.
• Generality: The algorithm must be complete in itself so that it can be used
to solve problems of a specific type for any input data.
17
Three constructs
Example 1
Find the average of three numbers
20
Example 3
Write an algorithm to find the area of the
triangle. Let b, c be the sides of the triangle ABC and A
the included angle between the given sides.