Asymptotic Notation 1 Introduction
Asymptotic Notation 1 Introduction
Assessment - Supplementary
Exam
for Computer Programming
19/05/2023 2.00 – 5.00 PM
Design of
Algorithms
CSC108S2 / CSC104G2
Contents
Assessment Strategy
References
Some well known computational
problems
• Sorting
• Searching
• Shortest path in a graph
• Travelling salesman problem
• Knapsack problem
• Chess
• Tower of Hanoi
What is an Algorithm?
• Informally, an algorithm is any well-defined computational procedure
that takes some value, or set of values, as input and produces some
value, or set of values, as output.
ALGORITHM
a=1 a=1
b=2 b=2
a=a+1 b=a*b
b=a*b a=a+1
print(b) print(b)
Some important characteristics
of Algorithms
• Should have unambiguous operations
• Each operation in an algorithm must be sufficiently clear so that it does not need
to be simplified.
Some important characteristics
of Algorithms
• Should have effectively computable operations
• Each operation in an algorithm must be doable, i.e., the
operation must be something that is possible to do.
• For computers, many mathematical operations such as
division by zero or finding the square root of a negative
number are impossible.
• These operations are not effectively computable so they
cannot be used in algorithms.
Some important characteristics
of Algorithms
• Should produce result(s)
• Unless an algorithm produces some result, we can
never be certain whether our solution is correct.
• Have you ever given a command to a computer and
discovered that nothing changed? What was your
response? You probably thought that the computer
was malfunctioning because your command did
not produce any type of result.
• Without some visible change, you have no way of
determining the effect of your command. The same
is true with algorithms.
• Only algorithms which produce results can be
verified as either right or wrong.
Some important characteristics
of Algorithms
• Should complete the execution in a finite amount of time
Algorithms should be composed of a finite number of operations and they should
complete their execution in a finite amount of time.
Algorithm vs. Pseudo-code vs.
Program
• Algorithm: Expressed using natural language, Independent of programming language.
• Pseudo-code: Gives a high-level description of an algorithm without the ambiguity
associated with plain text but also without the need to know the syntax of a
particular programming language.
Program
Analysis of Algorithms
The theoretical study of performance and resource usage of algorithms.
• Compare algorithms
It is possible to develop different algorithms to solve a
problem. Hence the analysis of algorithms is needed to
select a best performing algorithm for a given problem.
Algorithm 1 Algorithm 2
Analyzing algorithms based on
running time
Algorithm 1
Algorithm 2
• Figure shows how the running times of the algorithms depend on the size of n.
• As the value of n is doubled, the running time of Algorithm 1 typically increases
more than fourfold, while the running time of Algorithm 2 approximately doubles.
Challenges when comparing
execution times
While experimental studies of running times are valuable, this approach has many
drawbacks and depends on various factors.
• Different implementation: An algorithm can be implemented differently by different
programmers. In addition, algorithms can be implemented in different programming
languages as well.
• Machine dependent: Running time depends on hardware (CPU, memory, cache, etc.) as
well as software (OS, compiler, etc.). So the experiments should be done on the same
computer under same setting.
• An algorithm must be fully implemented in order to execute it to study its running time
experimentally.
• Background processes: These also consume resources.
Due to these reasons we can’t just rely on “execution/running time” to compare
different algorithms.