Chapter - One
Chapter - One
Introduction to Data
Structure and Algorithm
Introduction to DS and Algorithm Analysis
A program
A set of instruction which is written in
order
to solve a problem.
Therefore,
a program is Data structures
plus Algorithm.
3
Introduction to Data Structures
Data structures are used to model the world or
part of the world. How?
1. The value held by a data structure represents some
specific characteristic of the world.
2. The characteristic being modeled restricts the possible
values held by a data structure and the operations to
be performed on the data structure
The first step to solve the problem is
obtaining ones own abstract view, or
model, of the problem.
This process of modeling is called
abstraction.
Introduction....(continued)
Abstraction
Type: Set of values that can be stored + set of operations that can be performed.
Life time: The time interval during execution of a program while the variable exists.
Algorithm
Isa brief specification of an operation for
solving a problem.
Inputs Algorithm
Outputs
13
Algorithm
An algorithm transforms data structures
from one state to another state.
What is the purpose of algorithms in programs?
Take values as input. Example: cin>>age;
Change the values held by data structures. Example:
age=age+1;
Change the organization of the data structure:
Example:
Sort students by name
Produce outputs:
15
Algorithm
16
Properties of Algorithms
Finiteness:
Each step must be clearly defined, having
one and only one interpretation.
18
Sequential:
Each step must have a uniquely defined
preceding and succeeding step.
19
Feasibility:
2) int x,y;
cin>>x;
y=x*x;
cout<<y;
23
E x a mp le:
24
Input/output:
25
Algorithm Analysis
• Memory Usage
• Communication Bandwidth
• Computer hardware
(TotalTime) = t2-t1;
It is difficult to determine efficiency of algorithms using
this approach,
Because clock-time can vary based on many factors.
Example:
a) Processor speed of the computer
b) Current processor load
c) Specific data for a particular run of the program
d) Operating System
e) Programming language
f) The programmer
g) Operating environment/platform (PC, sun, smartphone
etc) 30
32
It helps to determine the complexity of
Complexity Analysis
Complexity Analysis is the systematic study
of the cost of computation, measured either
in:
Time units
Operations performed, or
The amount of storage space required.
33
Complexity Analysis
Cont.….
Complexity.
Asymptotic Complexity of an algorithm determines the
size of problems that can be solved by the algorithm.
Factors affecting the running time of a program:
CPU type (80286, 80386, 80486, Pentium I---IV)
Memory used
Computer used
Programming Language
C (fastest), C++ (faster), Java (fast)
C is relatively faster than Java, because C is relatively nearer to Machine
language, so, Java takes relatively larger amount of time for
interpreting/translation to machine code.
- Algorithm used
- Input size 35
Note: Important factors for this course are Input size and Algorithm
Complexity Analysis Cont.…
Analysis Rules:
1. Assume an arbitrary time unit.
2. Execution of one of the following operations
takes time 1 unit:
Assignment Operation
Example: i=0;
Single Input/Output Operation
37
Example: cin>>a;
Single Boolean Operations
Example: i>=10
Example: a+b;
Function Return
=5
4. Loop statements:
The running time for the statements inside the
loop * number of iterations + time for setup(1) +
time for checking (number of iteration + 1) + time
for update (number of iteration)
The total running time of statements inside a
group of nested loops is the running time of the
statements * the product of the sizes of all the
loops.
For nested loops, analyze inside out.
Always assume that the loop executes the
maximum number of iterations possible. (Why?)
Because we are interested in the worst case
complexity.
40
5. Function call:
• 1 for setup + the time for any
parameter calculations + the time
required for the execution of the
function body.
Examples:
1)
int k=0,n;
cout<<“Enter an integer”;
cin>>n
for(int i=0;i<n; i++)
k++;
T(n)= 3+1+n+1+n+n=3n+5
41
2)
int k=0;
for(int i=1 ; i<=n; i++)
for( int j=1; j<=n; j++)
k++;
T(n)=1+1+(n+1)+n+n(1+(n+1)+n+n)
= 2n+3+n(3n+2)
= 2n+3+3n2+2n
= 3n2+4n+3
42
3) int i=0;
while(i<n)
{
cout<<i;
i++;
}
int j=1;
while(j<=10)
{
cout<<j;
j++;
}
T(n)=1+n+1+n+n+1+11+2(10)
= 3n+34
43
4). int sum=0;
for(i=1;i<=n;i++))
sum=sum+i;
T(n)=1+1+(n+1)+n+(1+1)n
=3+4n
T(n)=1+1+(n+1)+n+n*(1+(n+1)+n+n)
=3+2n+n2+2n+2n2
=3+2n+3n2+2n
=3n2+4n+3 45
Categories of Algorithm
Analysis
Algorithms may be examined under different
situations to correctly determine their
efficiency for accurate comparison.
algorithm.
Types of Asymptotic
Notations
1. Big-Oh Notation
Definition: We say f(n)=O(g(n)), if there are
positive constants no and c, such that to the
right of no, the value of f(n) always lies on or
below c.g(n).
As n increases f(n) grows no faster than g(n).
It’s only concerned with what happens for
very large values of n.
Describes the worst case analysis.
▶ O-Notations are used to represent
the amount of time an algorithm
takes on the worst possible set of
inputs, “Worst-Case”.
Question-
1f(n)=10n+5 and
g(n)=n.
Show that f(n) is O(g(n)) ?
T(n)=2*n=2n=O(n).
O(n2).
Reading Assignments
Formal Approach to Analysis