0% found this document useful (0 votes)
25 views

Asymptotic Notation 1 Introduction

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Asymptotic Notation 1 Introduction

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

End-of-First Semester Practical

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

Unordered list: 5,1,2,9,10 Ordered list: 1,2,5,9,10

• In general, an algorithm is thus a sequence of computational steps


that transform the input into the output.
Example: Sorting
Correct and incorrect
Algorithms
• An algorithm is said to be correct if, for every input instance, it ends
with the correct output. We say that a correct algorithm solves the
given computational problem.

• An incorrect algorithm might end with an answer


other than the desired one. Contrary to what you
might expect, incorrect algorithms can sometimes
be useful, if we can control their error rate.
• We shall be concerned only with correct
algorithms.
Algorithm - Definition
Multiple definitions:
• A simple version – “A sequence of computational steps that transform
the input into the output”.
Algorithm - Definition
• A more detailed version by Schneider and Gersting 1995:
“An algorithm is a well-ordered collection of unambiguous and
effectively computable operations that when executed produces a
result and halts in a finite amount of time”.
Some important characteristics
of Algorithms
• Should have well-ordered set of instructions
• Should have unambiguous operations
• Should have effectively computable operations
• Should produce result(s)
• Should complete execution in a finite amount of time
Some important characteristics
of Algorithms
• Should have well-ordered set of instructions
• We must know the correct order in which to execute the instructions.
• If the order is unclear, we may perform the wrong instruction or we may be
uncertain which instruction should be performed next.

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.

• By analyzing we can determine the needed


resources (e.g. memory, processing power,
etc.) and the computational time for
execution.

• Generally, by analyzing several candidate


algorithms for a problem, we can identify
the most efficient one.
Reasons for analyzing
Algorithms
• To estimate the computational time
It is necessary to estimate the time required to solve a
problem with a specified input size. This will help to
decide whether an algorithm is useful for real world use.

• 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.

• To estimate resource requirements


E.g. memory requirements
Reasons for analyzing
Algorithms
• To understand scalability
Scalability is the ability to continue to
function even with larger input sizes.

• Understand theoretical basis


Analyzing algorithms based on
running time
• Algorithms can be compared based on running time.
• A typical way to automate this is:
Analyzing algorithms based on
running time
Given two algorithms, we need to find which one is the best based on the
execution time.
• perform independent experiments on different inputs of various sizes.
• visualize the execution times:
• X axis – size of the input
• Y axis – execution time
• Such a visualization provides some intuition regarding the relationship
between problem size and execution time for the algorithm.
Analyzing algorithms based on
running time
Uses repeated concatenation to compose Uses StringBuilder to compose a string
a string with n copies of character c with n copies of character c

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.

You might also like