3. ❖ A step-by-step procedure to solve a problem
❖ Every program is the instantiation of some algorithm
❖ Algorithm can be presented in a pseudocode or
flowchart
ALGORITHM
4. ❖ sorting
❖ data retrieval
❖ network routing
❖ Games etc...
Study problems these techniques can be applied to
5. Pseudocode is an artificial and informal language that helps
programmers develop algorithms.
Pseudocode may be an informal english, combinations of
computer languages and spoken language.
Whatever works for you.
Pseudocode
6.
7. ❖ Algorithms are all around us in everyday life.
❖ In the recipe of a cook book.
❖ In assembling a toy.
❖ In setting the table.
❖ In preparing a cup of tea.
❖ In calling your friend on the phone.
8. Hard to design algorithms that are
❖ correct
❖ efficient
❖ implementable
Need to know about design and modeling techniques
resources - don't reinvent the wheel
Challenges
9. ❖ Determine the feasibility of solution w.r.t. memory requirements,
performance constraints … etc.
❖ Manually review and validate pseudo code
❖ Analyze the complexity of the algorithm using the big O notation to
determine the complexity w.r.t. to time and storage.
❖ Other criteria include: Clarity, Maintainability, Portability
Analyze Performance
10. ❖ Understand the Problem (requirement analysis)
❖ Select Data structure (data structure is a particular way of organizing data in a computer
so that it can be used efficiently)
❖ Write Pseudo Code
❖ Analyze Performance
❖ Implement using suitable programming language
Guidelines for Algorithm Designing and Analysis:
11. To understand their behavior, and (Job -- Selection,
performance, modify) improve them. (Research)
Why Analyze Performance
12. Correctness: Does the input/output relation match algorithm requirement?
Amount of work done (aka complexity):Basic operations to do task
Amount of space used: Memory used
Simplicity, clarity: Verification and implementation.
Optimality: Is it impossible to do better?
What do we analyze about them?
13. Testing is divided into 2 main parts:
❖ Trying to break the function of the program by entering unexpected data.
❖ Debugging: It is concerned with finding out what is what caused the
program to function in incorrectly.
Test to resolve syntax and logic errors
14. The complexity of an algorithm is simply the amount of work the algorithm
performs to complete its task.
Time complexity is a main issue in evaluating an algorithm.
It reflects how the algorithm responds to the increase in data size (n) it
handles, by measuring the corresponding increase in number of instructions
to be performed.
Time Complexity of an
Algorithm
15. ❖ How does the algorithm behave as the problem
size gets very large?
❖ Running time
❖ Memory/storage requirements
❖ Bandwidth/power requirements/logic gates/etc.
Analysis using Asymptotic Notation(Big-O)
16. Big O notation also called Landau's symbol, is a symbolism used in complexity
theory, computer science, and mathematics to describe the asymptotic behavior of functions.
Basically, it tells you how fast a function grows or declines.
Landau's symbol comes from the name of the German number theoretician Edmund
Landau who invented the notation.
The letter O is used because the rate of growth of a function is also called its order.
For example, when analyzing some algorithm, one might find that the time (or the
number of steps) it takes to complete a problem of size n is given by T(n) = 4 n2
- 2 n + 2
17. O(n2
): known as Quadratic complexity
● 1 item: 1 second
● 10 items: 100 seconds
● 100 items: 10000 seconds
Notice that the number of items increases by a factor of 10, but the time increases by a factor of 102
. Basically,
n=10 and so O(n2
) gives us the scaling factor n2
which is 102.
O(n): known as Linear complexity
● 1 item: 1 second
● 10 items: 10 seconds
● 100 items: 100 seconds
This time the number of items increases by a factor of 10, and so does the time. n=10 and so O(n)'s scaling
factor is 10.
Big-Oh Notation
18. Big-Oh Notation
O(1): known as Constant complexity
● 1 item: 1 second
● 10 items: 1 second
● 100 items: 1 second
The number of items is still increasing by a factor of 10, but the scaling factor of O(1) is always 1.
O(log n): known as Logarithmic complexity
● 1 item: 1 second
● 10 items: 2 seconds
● 100 items: 3 seconds
● 1000 items: 4 seconds
● 10000 items: 5 seconds
The number of computations is only increased by a log of the input value. So in this case, assuming each
computation takes 1 second, the log of the input n is the time required, hence log n.
19. Divide and conquer
Greedy method
Dynamic Programming
Basic Search and Traversal Technique
Graph Theory
Linear Programming
Approximation Algorithm
NP Problem
General approaches to algorithm design
20. ❖ Heart of computer
❖ Promote analytical skills
Donald Knuth:
“ A person does not understand something until after
teaching it to someone else.
Actually: A person does not understand something until
after he teaches it to a ……COMPUTER!”
Why study Algorithms?
21. ❖ Algorithms help us to understand scalability.
❖ Performance often draws the line between what is feasible and what is
impossible.
❖ Algorithmic mathematics provides a language for talking about program
behavior.
❖ Performance is the currency of computing.
❖ The lessons of program performance generalize to other computing
resources.
Why study algorithms and performance?
22. Write an algorithm that reads two values, determines the largest value and
prints the largest value with an identifying message.
ALGORITHM
Step 1: Input VALUE1, VALUE2
Step 2: if (VALUE1 > VALUE2) then
MAX VALUE1
else
MAX VALUE2
endif
Step 3: Print “The largest value is”, MAX
Algorithm e.g 1
23.
24. Write an algorithm that reads three numbers and prints the value of the largest
number.
Algorithm e.g 2
25. Step 1: Input N1, N2, N3
Step 2: if (N1>N2) then
if (N1>N3) then
MAX N1 [N1>N2, N1>N3]
else
MAX N3 [N3>N1>N2]
endif
else
if (N2>N3) then
MAX N2 [N2>N1, N2>N3]
else
MAX N3 [N3>N2>N1]
endif
Algorithm e.g 2
27. Write an algorithm and draw a flowchart to read an
employee name (NAME), overtime hours worked
(OVERTIME), hours absent (ABSENT) and determine the
bonus payment (PAYMENT).
Algorithm e.g 3