Lecture 1 Algorithms
Lecture 1 Algorithms
Analysis of
Algorithms
Lecture 1
CHAPTER 1: INTRODUCTION
What is an Algorithm
Algorithm is more than a branch of computer science. It is
the core of computer science, and, in all fairness, can be
said to be relevant to most science, business, and
technology.
An algorithm is a sequence of unambiguous instructions
for solving a problem, i.e., for obtaining a required output
for any legitimate input in a finite amount of time.
2
What the Difference Between Algorithm and
Program?
3
Problems vs Algorithms vs Programs
For each problem or class of problems,
there may be many different algorithms.
For each algorithm, there may be many
different implementations (programs).
Difference between Algorithm,
Pseudocode and Program
Algorithm : Systematic logical approach which is a
well-defined, step-by-step procedure that allows a
computer to solve a problem.
Pseudocode : It is a simpler version of a programming
code in plain English which uses short phrases to write
code for a program before it is implemented in a specific
programming language.
Program : It is exact code written for problem following
all the rules of the programming language.
5
Algorithmic Representation of
Computer Functions
Input
Get information Get (input command)
Storage
Store information Given/Result
Intermediates/Set
Process
Arithmetic Let (assignment command)
Repeat instructions Loop
Branch conditionals If
Output
Give information Give (output command)
Properties of an Algorithm
An algorithm must possess the following
properties:
finiteness: The algorithm must always
terminate after a finite number of steps.
definiteness: Each step must be precisely
defined; the actions to be carried out must
be rigorously and unambiguously specified
for each case.
input: An algorithm has zero or more inputs,
taken from a specified set of objects.
output: An algorithm has one or more
outputs, which have a specified relation to
the inputs.
effectiveness: All operations to be
performed must be sufficiently basic that
they can be done exactly and in finite
length
Expressing Algorithms
An algorithm may be expressed in a number of ways,
including:
natural language: usually verbose and ambiguous
flow charts: avoid most (if not all) issues of ambiguity;
difficult to modify w/o specialized tools; largely
standardized
pseudo-code: also avoids most issues of ambiguity;
vaguely resembles common elements of programming
languages; no particular agreement on syntax
programming language: tend to require expressing low-
level details that are not necessary for a high-level
understanding
Common Elements of Algorithms
acquire data (input): some means of reading
values from an external source; most algorithms
require data values to define the specific problem
(e.g., coefficients of a polynomial)
Computation: some means of performing
arithmetic computations, comparisons, testing
logical conditions, and so forth...
Selection: some means of choosing among two
or more possible courses of action, based upon
initial data, user input and/or computed results
Iteration: some means of repeatedly
executing a collection of instructions, for a
fixed number of times or until some logical
condition holds
report results (output): some means of
reporting computed results to the user, or
requesting additional data from the user
Understand the problem
Design an algorithm
Prove correctness
12
What does it mean to understand
the problem?
What are the problem objects?
What are the operations applied to the objects?
13
Design an algorithm
Prove correctness
• Correct output for every
legitimate input in finite time
• Based on correct math formula
• By induction 14
Analyze the algorithm
Efficiency: time and space
Simplicity
Generality: range of inputs, special
cases
Optimality:
no other algorithm can do
better
Coding
How the objects and operations in the 15
pseudo-Language
See the posted notes on pseudo-language notation.
Testing Correctness
How do we know whether an algorithm is actually
correct?
First, the logical analysis of the problem we
performed in order to design the algorithm should
give us confidence that we have identified a valid
procedure for finding a solution.
Second, we can test the algorithm by choosing
different sets of input values, carrying out the
algorithm, and checking to see if the resulting
solution does, in fact, work.
Proving Correctness
We can attempt to construct a formal,
mathematical proof that, if the algorithm is given
valid input values then the results obtained from
the algorithm must be a solution to the problem.
We should expect that such a proof be provided
for every algorithm.
In the absence of such a proof, we should view the
purported algorithm as nothing more than a
heuristic procedure, which may or may not yield
the expected results
gcd(m,n)
3. if n % t = 0 return
t,
else goto
4
20
21
Figure 8-6
Three constructs
Figure 8-7
Solution
See Algorithm on the next slide.
Algorithm : Average of two
AverageOfTwo
Input: Two numbers
1.Add the two numbers
2.Divide the result by 2
3.Return the result by step 2
End
Example 2
Solution
See Algorithm on the next slide.
Algorithm :Pass/no pass Grade
Pass/NoPassGrade
Input: One number
1. if (the number is greater than or equal to 70)
then
1.1 Set the grade to “pass”
else
1.2 Set the grade to “no pass”
End if
2. Return the grade
End
Example 3
Solution
Solution
Solution
37
Tasks
38