Data Structure Algorithm CH 1
Data Structure Algorithm CH 1
Some mysterious
processing
Input Output
-Computer program development process required
us to represent data in effective way.
4
Data structure is structural representation of the logical
relationship existing between individual elements of data.
Primitive DS Non-Primitive DS
Linked list
queue
tree stack
PDSs are basic structures and directly operated upon
by the machine instructions.
In general, there are different representation on
different computers.
Integer, Floating-point number, Character constants,
string constants, pointers etc, fall in this category.
NPDSs are more sophisticated data structures.
These are derived from the primitive data structures.
The non-primitive data structures emphasize on
22
No single data structure works well for all
purposes, and so it is important to know the
strengths and limitations of several of them
Collection with access only to the last element inserted
Last in first out
insert/push
remove/pop Data4 Top
top Data3
make empty
Data2
Data1
Collection with access only to the item that has been
present the longest
Last in last out or first in first out
enqueue, dequeue, front
priority queues and dequeue
Front Back
26
A Flexible structure, because can grow and shrink on
demand.
Elements can be:
Inserted
Accessed
Deleted
At any position
last
first
A Tree is a collection of elements called nodes.
One of the node is distinguished as a root, along with
ALGORITHM AND
ALGORITHM ANALYSIS
CENG 213 Data Structures 30
Algorithm
An algorithm is a clearly specified set of instructions to
be followed to solve a problem.
There can be more than one solution (more than one
algorithm) to solve a given problem.
It can be implemented using different programming
languages on different platforms.
An algorithm must be correct. It should correctly solve
the problem.
e.g. For sorting, this means even if (1) the input is
already sorted, or (2) it contains repeated elements.
Once we have a correct algorithm for a problem, we
have to determine the efficiency of that algorithm.
Prepared by Alemu Ginjo
CENG 213 Data Structures 31
Problems
Problem: is a task to be performed.
• Best thought of as inputs and matching outputs.
• Problem definition should include constraints on the
resources that may be consumed by any acceptable
solution.
Algorithm Properties
An algorithm possesses the following properties:
It must be correct.
It must be composed of a series of concrete steps.
There can be no ambiguity as to which step will be
performed next.
It must be composed of a finite number of steps.
It must terminate.
A computer program is an instance, or concrete
representation, for an algorithm in some programming
language.
32 Prepared by Alemu Ginjo
Prepared
CENG 213by Alemu
Data Ginjo
Structures 33
Algorithmic Performance
There are two aspects of algorithmic performance:
Time
• Instructions take time.
• How fast does the algorithm perform?
• What affects its runtime?
=> computer, compiler, algorithm used & input to the algorithm
Space
• Data structures take space
• What kind of data structures can be used?
• How does choice of data structure affect the runtime?
Analysis of Algorithms
Algorithm analysis refers to the process of determining
the amount of computing time and storage space required
by different algorithms.
In other words, it’s a process of predicting the resource
requirement of algorithms in a given environment.
In order to solve a problem, there are many possible
algorithms.
One has to be able to choose the best algorithm for the
problem at hand using some scientific method
CENG 213by
Prepared Data Structures
Alemu Ginjo 35
Analysis of Algorithms(cont.)
- To classify some data structures and algorithms as
good, we need precise ways of analyzing them in terms
of resource requirement.
- The main resources are:
Running Time
Memory Usage
Communication Bandwidth
Prepared
CENG 213by Alemu
Data Ginjo
Structures 36
Analysis of Algorithms(cont.)
Running time is usually treated as the most important
since computational time is the most precious resource
in most problem domains.
There are two approaches to measure the efficiency of
algorithms:
• Empirical: Programming competing algorithms
and trying them on different instances.
• Theoretical: Determining the quantity of resources
required mathematically (Execution time, memory
space, etc.) needed by each algorithm.
Prepared
CENG 213by Alemu
Data Ginjo
Structures 37
Analysis of Algorithms(cont.)
However, it is difficult to use actual clock-time as a
consistent measure of an algorithm’s efficiency, because
clock-time can vary based on many things. For example,
• Specific processor speed
• Current processor load
• Specific data for a particular run of the program
Input size
Input properties
• Operating Environment
Prepared
CENG 213by Alemu
Data Ginjo
Structures 38
Analysis of Algorithms(cont.)
Accordingly, we can analyze an algorithm according to
the number of operations required, rather than
according to an absolute amount of time involved.
This can show how an algorithm’s efficiently changes
according to the size of the input.
CENG 213by
Prepared Data Structures
Alemu Ginjo 39
Complexity Analysis
• Complexity Analysis is the systematic study of the cost of
computation, measured either in time units or in operations
performed, or in the amount of storage space required.
• The goal is to have a meaningful measure that permits
comparison of algorithms independent of operating
platform.
I.Time Complexity: Determine the approximate number of
operations required to solve a problem of size n.
II.Space Complexity: Determine the approximate memory
required to solve a problem of size n.
Prepared
CENG 213by Alemu
Data Ginjo
Structures 40
Complexity Analysis
Complexity analysis involves two distinct phases:
1.Algorithm Analysis: Analysis of the algorithm or data
structure to produce a function T (n) that describes the
algorithm in terms of the operations performed in order to
measure the complexity of the algorithm.
2.Order of Magnitude Analysis: Analysis of the function T
(n) to determine the general complexity category to which it
belongs.
There is no generally accepted set of rules for algorithm
analysis.
However, an exact count of operations is commonly used.
Prepared
CENG 213by Alemu
Data Ginjo
Structures 41
Analysis Rules
Analysis Rules:
1. We assume an arbitrary time unit.
2. Execution of one of the following operations takes time 1:
• Assignment operation, Single Input/output operation.
• Single Boolean Operations. ex: AND, OR, NOR, NAND,
XOR, etc.
• Single Arithmetic Operations. ex: +, *, /, etc. and Function
Return.
3. Running time of a selection statement (if, switch) is the
time for the condition evaluation + the maximum of the
running times for the individual clauses in the selection.
Prepared
CENG 213by Alemu
Data Ginjo
Structures 42
Analysis Rules(cont.)
4. Loops: Running time for a loop is equal to the running
time for the statements inside the loop * number of iteration.
The total running time of a statement inside a group of
nested loops is the running time of the statements
multiplied by 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.
5. Running time of a function call is 1 for setup + the time
for any parameter calculations + the time required for the
execution of the function body.
CENG 213by
Prepared Data Structures
Alemu Ginjo 43
Analysis Rules(cont.)
Generally:-
1 for the assignment statement.
1 for the output statement.
1 for the input statement.
In the for loop:
1 assignment, n+1 tests, and n increments.
n loops of 2 units for an assignment, and an addition.
1 for the return statement.
CENG 213by
Prepared Data Structures
Alemu Ginjo 44
Examples: 1
int count( ){
int k=0; 1
cout<<”Enter an integer”; 1
cin>>n; 1
for ( i=0; i<n; i++) 1+n+1+n
k=k+1; 2n
return 0; 1
}
T(n) = 1+1+1+(1+n+1+n)+2n+1 = 4n+6 = O(n)
CENG 213by
Prepared Data Structures
Alemu Ginjo 45
2.
int total(int n )
{
int sum=0;
for (int i=1; i<=n; i++)
sum=sum+1;
return sum;
}
Analysis of Algorithms(cont.)
• Analysis of Algorithms is the area of computer science
that provides tools to analyze the efficiency of different
methods of solutions.
• It predicts resources that an algorithm requires.
• How do we compare the time efficiency of two
algorithms that solve the same problem?
Naïve Approach: implement these algorithms in a
programming language (C++), and run them to compare
their time requirements.
Prepared
CENG 213by Alemu
Data Ginjo
Structures 49
Analysis of Algorithms(cont.)
Analysis of Algorithms(cont.)
• When we analyze algorithms, we should employ
mathematical techniques that analyze algorithms
independently of specific implementations, computers,
or data.
• To analyze algorithms:
First, we start to count the number of significant
operations in a particular solution to assess its
efficiency.
Then, we will express the efficiency of algorithms
using growth functions.
Prepared
CENG 213by Alemu
Data Ginjo
Structures 51
A sequence of operations:
Total Cost = c1 + c2
Prepared
CENG 213by Alemu
Data Ginjo
Structures 52
f
or(
i
nti
= 1
;i<=N;
i++
){
s
um= s
um+
i;
1N
}
i1
Suppose we count the number of additions that are done. There
is 1 addition per iteration of the loop, hence N additions in total.
CENG 213by
Prepared Data Structures
Alemu Ginjo 57
4. Conditionals: Formally
If (test) s1 else s2: Compute the maximum of the running time
for s1 and s2.
CENG 213by
Prepared Data Structures
Alemu Ginjo 60
What to Analyze
An algorithm can require different times to solve
different problems of the same size.
E.g. Searching an item in a list of n elements using
sequential search. Cost: 1,2,...,n
Worst-Case Analysis –The maximum amount of time
that an algorithm require to solve a problem of size n.
This gives an upper bound for the time complexity of
an algorithm.
Normally, we try to find worst-case behavior of an
algorithm.
Prepared
CENG 213by Alemu
Data Ginjo
Structures 62
Measures of Times
• In order to determine the running time of an algorithm it is
possible to define three functions Tbest(n), Tavg(n) and
Tworst(n), the average and the worst case running time of the
algorithm respectively.
1.Average Case (Tavg): The amount of time the algorithm
takes on an “average” set of inputs.
Worst Case (Tworst): The amount of time the algorithm takes
on the worst possible set of inputs.
That is the longest running time for any input of size n.
2.Best Case (Tbest): The amount of time the algorithm takes
on the smallest possible set of inputs.
Prepared
CENG 213by Alemu
Data Ginjo
Structures 64
Asymptotic Analysis
Asymptotic analysis is concerned with how the running
time of an algorithm increases with the size of the input in
the limit, as the size of the input increases without bound.
There are five notations used to describe a running time
function. These are:
Big-Oh Notation (O)
Big-Omega Notation ()
Theta Notation ()
Little-o Notation (o)
Little-Omega Notation ()
CENG 213by
Prepared Data Structures
Alemu Ginjo 65
Typical Orders
Here is a table of some typical cases. This uses logarithms to base 2, but these are simply
proportional to logarithms in other base.
End of chapter 1