Upload
ewurama-minka
View
51
Download
0
Embed Size (px)
Citation preview
❖ 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
❖ sorting
❖ data retrieval
❖ network routing
❖ Games etc...
Study problems these techniques can be applied to
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
❖ 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.
❖ …. There are countless examples!
Hard to design algorithms that are❖ correct❖ efficient❖ implementable
Need to know about design and modeling techniquesresources - don't reinvent the wheel
Challenges
❖ 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
❖ 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
❖ Test to resolve syntax and logic errors
Guidelines for Algorithm Designing and Analysis:
To understand their behavior, and (Job -- Selection, performance, modify) improve them. (Research)
Why Analyze Performance
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?
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
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
❖ 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)
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
If we ignore constants (which makes sense because those depend on the particular
hardware the program is run on) and slower growing terms, we could say
"T(n) grows at the order of n2 " and write:T(n) = O(n2)
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
Big-Oh NotationO(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.
That's the gist of it. They reduce the maths down so it might not be exactly n2 or whatever they say it is, but that'll be the dominating factor in the scaling.
Divide and conquer Greedy method Dynamic ProgrammingBasic Search and Traversal TechniqueGraph TheoryLinear ProgrammingApproximation AlgorithmNP Problem
General approaches to algorithm design
❖ Heart of computer
❖ Promote analytical skillsDonald 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?
❖ 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.
❖ Speed is fun!
Why study algorithms and performance?
Write an algorithm that reads two values, determines the largest value and prints the largest value with an identifying message.
ALGORITHMStep 1: Input VALUE1, VALUE2Step 2: if (VALUE1 > VALUE2) then
MAX VALUE1else
MAX VALUE2endif
Step 3: Print “The largest value is”, MAX
Algorithm e.g 1
Write an algorithm that reads three numbers and prints the value of the largest number.
Algorithm e.g 2
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
endif
Step 3: Print “The largest number is”, MAX
Algorithm e.g 2
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
Step 1: Input NAME,OVERTIME,ABSENT
Step 2: if (OVERTIME–(2/3)*ABSENT > 40) then
PAYMENT 50
else if (OVERTIME–(2/3)*ABSENT > 30) then
PAYMENT 40
else if (OVERTIME–(2/3)*ABSENT > 20) then
PAYMENT 30
else if (OVERTIME–(2/3)*ABSENT > 10) then
PAYMENT 20
else
PAYMENT 10
endif
Step 3: Print “Bonus for”, NAME “is $”, PAYMENT
Algorithm e.g 3