You are on page 1of 15

Data Structures

Lecture 2
Preliminaries

Algorithmic Notation
Recap
An algorithm is a finite step-by-step list of well-defined instructions
for solving a particular problem.

I.
II.

The efficiency of an algorithm depends on 2 factors


Time of execution
Space required
The complexity of an algorithm is the function which gives the
running time and /or space in terms the input size.

Algorithmic Notation
Example: An array DATA of numerical values is in memory.We want to
find the location LOC and the value MAX of the largest element of
DATA.

Algorithmic Notation
Conventions to be followed in algorithms in this course.

Identifying Number
Steps, Control, Exit
Comments
Variable names and Indexes
Input and Output
Procedures (sub-algorithm)

Control Structures
According to Bhm and Jacopini's research,
Algorithm and their equivalent programs could be written in
terms of mainly three control structures, namely;
I.
Sequence structure/logic, or sequential flow
II.
Selection structure/logic, or conditional flow
III.
Iteration structure/logic, or Repetitive flow
Sequence logic (Sequential flow)
Built into C++.
Unless directed otherwise, the computer executes C++ statements
one after the other in the order in which they are written i.e. in
sequence

Control Structures
Selection logic (Conditional flow)
Employs a number of conditions.
Flow of control transfers to one of the true condition.
C++ provides three types of selection statements;
if , if-else and switch.
Iteration logic (Repetitive flow)
Enable programs to perform statements repeatedly as long as a
condition remains true.
C++ provides three types of repetition statements
for, while and do-while loops.

Control Structures
Using the while loop instead of a Goto statement, the algorithm 2.1
can be rewritten as follow

Complexity of Algorithms

I.
II.
III.

Complexity of an algorithm gives us space required or execution


time.
Space required = multiple of data size n.
Usually complexity refers to running time.

Complexity of an algorithm f(n) may involves different cases i.e.


Worst case = maximum value
Average case = expected value
Best case = minimum value
Unless otherwise stated complexity of an algorithm shall mean the
function which gives the running time of the worst case in terms of
input size.

Analysis of Complexity

Different algorithms can be compared on the basis of their


complexity functions.

Most common tools for analyzing complexities of algorithms are


Big-O, Big-(Big-omega), Big-(Big-theta) and Little-o notations.

These notations are approximations of the original complexity


functions which hide some of the details for convenience.

Big-O notation

Big-O notation expresses computing time as the term in a function


that increases most rapidly relatively to the size of a problem.

Mathematically it can be defined as, given two positive valued


functions f and g, f(n) is O(g(n)) if there exists positive numbers M and
n0 such that f(n)Mg(n) for all nn 0
The definition states that g is almost always greater than or equal
to f if it is multiplied by constant M.

For example,

f(n) = n2+2n+1=O(n2)
where g(n)=n2
Here f(n) is O(g(n)) and is read as f is Big-O of g or Big-O of n2.

Big-O notation

Usually, in an algorithm, certain operations are dominant.


Various values of n, 2n and n 2.

Big-O notation

Suppose that a computer can execute 1 billion basic operations per


second.The table below shows the time that the computer takes to
execute f(n) basic operations.

Big-O notation

Growth rate of various functions

The idea in Big O notation isnt to give an actual figure for running
time, but to convey how running times are affected by the number
of items.

Big-O notation

Method for finding M and n 0


Let

First solve

f(n) = n2+2n+1=O(n2)
where g(n)=n2
n

2 + 2 + 1 2

9/4

2 + 2 + 1

16/9

25/16

36/25

49/36

Make table by giving values to n and finding M

Big-O notation

As

Method for finding M and n 0


f(n) = n2+2n+1=O(n2)

In the f(n) the only candidates for the largest term


are n2 and 2n. The value of n 0 can be found by
comparing these i.e.
2 > 2
>2
or
0 > 2

This value of n0 will satisfy

You might also like