Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

Data Structures and Algorithm

Lecture 2 - Algorithm Analysis


Introduction
Objectives
• Algorithm
• Algorithm Analysis
• Problem Solving
Algorithm
Photosynthesis
Algorithm is a set of executable steps.

• They describe the solution to a problem in terms of the data needed to


represent the problem instance and the set of steps necessary to produce
the intended result.
• Programming languages must provide a way to represent both the
process and the data. They include:
• Words/statement
• Formula
• Flow chart
• Pseudocode
Algorithm…
Algorithm Representation
• Words/Statements
• To calculate the area A, of a rectangle
1. Get the length, L
2. Get the width, W
3. Area is found by getting the product of L and W
4. If the area is greater or equals to 100m2, stop otherwise;
5. Get new values of L and W by repeating the same process.

• Formula
• Uses mathematical expressions/theorems.
• To calculate the area A, of a rectangle
Area = Length x Width
Algorithm…
Algorithm Representation…
• Flow Chart
• The use of connected shapes with lines.
Algorithm…
Algorithm Representation…
• Pseudocode
• They are notations that resemble programming language representation.
• It is not executable by computing devices.
• Intended for human reading and not machine reading.

// Calculate Area of a rectangle


Length=L
Width=W
Area=LxW
Begin
Area>=100m2 , Stop
Else, repeat
End if
Stop
Algorithm…
Algorithm Properties
• Unambiguous
• Not open to different interpretations.
• Correct
• If the steps are followed, desired results will be achieved.
• Terminating
• The executable steps should be able to end to allow other steps and processes to be
executed.
• It should not tie the computing resources such as memory or processor time in unending
loop.
• Precise
• The steps should guide the implementers clearly.
Algorithm…
Asymptotic Analysis
• Measuring the cost or complexity of an algorithm, is basically
performing an analysis of the algorithm when the input sets are
increasing.
• Analyzing what happens as the number of inputs becomes very
large is referred to as asymptotic analysis.
• The programmer should figure it out before the client.
Algorithm…
Rate of Growth
• Rate of growth describes how an algorithm's complexity changes
as the input size grows.
• This is commonly represented using Big-O notation.
• Big-O notation uses a capital letter O (for "order") and a formula
that expresses the complexity of the algorithm.
• The formula may have a variable n, which represents the size of
the input.
Algorithm…
Rate of Growth…
• Constant – O(1)
• An O(1) algorithm is one whose complexity is constant
regardless of how large the input size is.
• The 1 does not mean that there is only one operation or that
the operation takes a small amount of time.
• The size of the input does not influence the time the
operation takes.
Algorithm…
Rate of Growth…
• Linear – O(n)
• An O(n) algorithm is one whose complexity grows linearly
with the size of the input.
• If an input size of one takes ten milliseconds, an input with
one thousand items will take ten seconds.
Algorithm…
Rate of Growth…
• Linearithmic – O(n log n)
• A linearithmic algorithm, or loglinear.
• An algorithm that has a complexity of O(n log n).
• Some divide and conquer algorithms fall into this category.
Example:
• Merge sort and quick sort.
Algorithm…
Rate of Growth…
• Quadratic – O(n2)
• The complexity is quadratic to its size.
• Quadratic algorithm is a potential sign that you need to reconsider
your algorithm or the choice of data structures.
• Quadratic algorithms do not scale well as the input size grows.
Algorithm…
What are we Measuring in the Analysis?
• When we are measuring algorithms and data structures, we
consider:
• Operational Complexity
• Resource Complexity
• Operational Complexity
• The amount of time the operation takes to complete.
• Resource Complexity
• The amount of memory an algorithm uses.
• Algorithms can be optimized for either depending on the execution
environment.
• Example: Memory intensive algorithms might be perfectly acceptable in a
server environment with vast amounts of available memory. However, this
may not be appropriate with embedded systems where available memory is
limited.
Algorithm…
Properties of Algorithms
• Correct
• Always returns the desired output.
• Unambiguous
• Not open to more than one interpretation.
• Precise
• The steps are clearly outlined.
• Efficient
• Can be measured in terms of instances of the problem.
• Time
• Memory
Algorithm…
Representation of Algorithms
A single algorithm can be represented in many ways:
• Formulas
Example: Area(A) = Length(L) x Width(W)

• Words
Example: Multiply Length and Width to get the Area.
Algorithm…
Problem Solving
• Programming is a process of problem solving.

• Problem solving techniques


• Analyze the problem
• Outline the problem requirements
• Design steps (algorithm) to solve the problem
• Implement
• Test
• Maintain

• Algorithm is a step-by-step problem-solving process where the


solution achieved in finite amount of time.
Questions
1. Write an algorithm to determine a student’s final grade and
indicate whether it is passing or failing. The final grade is
calculated as the average of four marks.
2. Write an algorithm and draw a flowchart to convert the length in
centimeters to meters.
3. Write an algorithm that reads two values, determines the largest
value and displays the largest value while identifying the two
values.
Research
• Get an existing algorithm and represent it in the different ways.
• Highlight the properties of the algorithm.
• Efficiency of algorithms in terms of operational complexity and
resource complexity.
Review Questions
1. Define an algorithm.
2. List the properties of algorithms.
3. What is Asymptotic Analysis?
4. Distinguish between resource and operational complexities of
algorithms.
5. Discuss the different orders of algorithms as the number of
inputs increases.
6. Enumerate the number of ways we can represent an algorithm.
References
• Online
• E-resources

• Books
• E-books
• Library

You might also like