You are on page 1of 10

Chapter Two

Analysis of Algorithms

Analysis of Algorithms
Quick Mathematical Review
Running Time
Pseudo-Code
Complexity of Algorithm
Asymptotic Notations
Asymptotic Analysis

1|Page
Analysis of Algorithms
* What is Algorithm?

A clearly specified set of simple instructions to be followed to solve a problem

Takes a set of values, as input and

produces a value, or set of values, as output

* Algorithm May be specified by:

In English

As a computer program

As a pseudo-code

* Data structures

* Data Structures are a Methods of organizing data so a Program is = algorithms + data structures

* Why need algorithm analysis?

Writing a working program directly is not good enough So by that The program may be inefficient!
If the program is run on a large data set, then the running time becomes an issue.

* Example Algorithm: Selection Problem

Algorithm : To find the greatest number among three numbers:


Step 1- Start.
Step 2- Creating Objects.(A,B,C)of A,B and C. ( Getting Inputs)
Step 4- check the first number A wether if its the greatest.
Check if A>B and A>C then if True Display A otherwise go to next step.
Step 5- check the Second number B wether if its the greatest.
Check if B>A and B>C then if True Display B otherwise go to next step.
Step 6- check the Third number C wether if its the greatest.
Check if C>A and C>B then if True Display c otherwise go to next step.
Step 7- Otherwise All numbers are equal.

2|Page
In Algorithm Analysis:

We only analyze correct algorithms

An algorithm is correct only: If, for every input instance, it halts with the correct output

* Incorrect algorithms:

Might not halt at all on some input instances.


Might halt with other than the desired answer.

In analyzing an algorithm we also should:

Predict the resources that the algorithm requires


Resources include
Memory
Communication bandwidth
Computational time (usually most important)

Factors affecting the running time

computer type
compiler type
Algorithm used at that time.
input to the algorithm

The content of the input affects the running time


typically, the input size (number of items in the input) is the main consideration
For Example:
l E.g. sorting problem the number of items to be sorted
l E.g. multiply two matrices together the total number of elements in the two matrices

3|Page
A Quick Math Review
We need to have a knowledge to be able to create algorithms for various problems and situations
that requires mathematical skills in order to implement such programs.

Example:

Find out the capacity of auditorium hall of specific given size or even the opposite.

Logarithms and Exponents

- properties of logarithms & Examples :


logb(xy) = logbx + logby
logb(x/y) = logbx logby

4|Page
5|Page
- properties of exponentials:
a(b+c) = abac
abc = (ab)c
ab/ac = a(b-c)
b=a
bc = a
logba=
logab
c*logab

6|Page
Summations
- general definition:
- where f is a function, s is the start index, and t is
the end index
Geometric progression: f(i) ai
- given an integer n 0 and a real number 0 <a
- geometric progressions exhibit exponential growth
f ii = s
t= f s+ f s + 1+ f s + 2++ f t

7|Page
Note:

Primitive Operations: Low-level computations that are largely independent from the
programming language and can be identified in pseudo code,
e.g:
- calling a method and returning from a method
- performing an arithmetic operation (e.g. addition)
- comparing two numbers .etc.

Best Case vs. Average Case vs. Worst Case

Running Time of an Algorithm:

An algorithm may run faster on certain data sets than on others,


Finding the average case can be very difficult, so typically algorithms are measured by
the worst-case time complexity.

Also, in certain application domains (e.g., air traffic control, surgery) knowing the
worst-case time complexity is of crucial importance.

8|Page
Measuring the Running Time?

How should we measure the running time of an algorithm?

Usually by Experimental Study by using available data inputs used at the domain area.

- Write a program that implements the algorithm.

- Run the program with data sets of varying size and composition.
- Use a method like System.currentTimeMillis() to get an accurate measure of the
actual running time.

What is Pseudo-Code?

Pseudo-code is a description of an algorithm that is more structured than usual prose


but less formal than a programming language.

Example: finding the maximum element of an array.

Algorithm arrayMax(A, n):


Input: An array A storing n integers.
Output: The maximum element in A.
currentMax A[0]
for i 1 to n 1 do
if currentMax < A[i] then
currentMax A[i]
return currentMax

Note:
Pseudo-code is our preferred notation for describing algorithms.
However, pseudo-code hides program
design issues.

9|Page
What is Pseudo-Code?
A mixture of natural language and high-level programming concepts that describes the
main ideas behind a generic implementation of a data structure or algorithm.

- Expressions: use standard mathematical symbols to describe numeric and boolean


expressions

- use for assignment (= in Java)


- use = for the equality relationship (== in Java)

- Method Declarations:

- Algorithm name(param1, param2)

- Programming Constructs:

- decision structures: if ... then ... [else ... ]


- while-loops: while ... do
- repeat-loops: repeat ... until ...
- for-loop: for ... do
- array indexing: A[i]

- Methods:
- calls: object method(args)
- returns: return value

By inspecting the pseudo-code, we can count the number of primitive operations executed by an
Algorithm.
Example:
Algorithm arrayMax(A, n):
Input: An array A storing n integers.
Output: The maximum element in A.
currentMax A[0]
for i 1 to n 1 do
if currentMax < A[i] then
currentMax A[i]
return currentMax

10 | P a g e