Chapter 1
Chapter 1
Algorithms
COMPILED BY: GELANA. A
Contents to be covered
Data structures model the static part of the world. They are
unchanging while the world is changing.
In order to model the dynamic part of the world we need to work
with algorithms.
Algorithms are the dynamic part of a program’s world model
An algorithm transforms data structures from one state to another
state in two ways:
1. An algorithm may change the value held by a data structure or
2. An algorithm may change the data structure itself
Cont…
Algorithm vs program
Properties of an algorithm
1. FLOWCHART
▪ Flowchart is graphic representation of an algorithm.
▪ Easy to understand the logic of complicated and lengthy problems
Example: Algorithm to find the average of three numbers
2. PSEUDO-CODE METHOD:
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.
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:
1. Running Time
2. Memory Usage
3. Communication Bandwidth
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:
1. Empirical: Programming competing algorithms and trying them on different
instances.
2. Theoretical: Determining the quantity of resources required mathematically
(Execution time, memory space, etc.) needed by each algorithm.
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,
1. Specific processor speed
2. Current processor load
Cont…
int count(){
int k=0;
cout<< “Enter an integer”;
cin>>n;
for (i=0;i<n; i++)
k=k+1;
return 0;
}
Time Units to Compute
1 for the assignment statement: int k=0
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.
-------------------------------------------------------------------
T (n)= 1+1+1+(1+n+1+n)+2n+1 = 4n+6 = O(n)
Example 2
int total(int n)
{
int sum=0;
for (int i=1;i<=n;i++)
sum=sum+1;
return sum;
}
Time Units to Compute
1 for the assignment statement: int sum=0
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.
T (n)= 1+ (1+n+1+n)+2n+1 = 4n+4 = O(n)
Example 3
int sum (int n)
{
int partial_sum = 0;
for (int i = 1; i <= n; i++)
partial_sum = partial_sum +(i * i * i);
return partial_sum;
}
Time Units to Compute
1 for the assignment.
1 assignment, n+1 tests, and n increments.
n loops of 4 units for an assignment, an addition, and two
multiplications.
1 for the return statement.
-------------------------------------------------------------------
T (n)= 1+(1+n+1+n)+4n+1 = 6n+4 = O(n)
Formal Approach to Analysis
In the above examples we have seen that analysis is a bit complex.
However, it can be simplified by using some formal approach in
which case we can ignore initializations, loop control, and book
keeping.
for Loops: Formally
In general, a for loop translates to a summation. The index and
bounds of the summation are the same as the index and bounds of
the for loop.
The definition states that the function f(n) is at most c times the
function g(n) except when n is smaller than n0.
In other words, f(n) grows slower than or same rate as” g(n).
Examples
–f(n) = 3n+2
3n + 2 <= 4n, for all n >= 2, 3n + 2 = O(n)
–f(n) = 10n2+4n+2
10n2 +4n+2 <= 11n2, for all n >= 5,
10n2 +4n+2 = O(n2)
Omega(Ω)notation:
Examples
–f(n) = 3n+2
3n + 2 >= 3n, for all n >= 1, 3n + 2 = Ω (n)
–f(n) = 10n2+4n+2
10n2 +4n+2 >= n2 , for all n >= 1, 10n2+4n+2 = Ω (n2)
Theta(Θ)notation:
The definition states that the function f(n) lies between c1 times the
function g(n) and c2 times the function g(n) except when n is
smaller than n0.
In other words, f(n)grows same rate as g(n).
Example
f(n) = 3n+2
3n <= 3n + 2 <= 4n, for all n >= 2, 3n + 2 = Θ(n)
f(n) = 10n2+4n+2
n2<= 10n2+4n+2 <= 11n2, for all n >= 5, 10n2+4n+2 = Θ(n2)
Heap Sort
Heap sort operates by first converting the list in to a heap tree. Heap
tree is a binary tree in which each node has a value greater than both
its children (if any). It uses a process called "adjust to accomplish its
task (building a heap tree) whenever a value is larger than its parent.
The time complexity of heap sort is O(nlogn).
Hashing
A hash table is a data structure that stores data and allows insertions,
lookups, and deletions to be performed in constant, O(1), time.
Hash tables provide a means for rapid searching for a record with a
given key value and adapts well to the insertion and deletion of
records.
It is a technique used for performing insertions, deletions and finds
in constant time.
The aim of hashing is to map an extremely large key space onto a
reasonable small range of integers such that it is unlikely that two
keys are mapped onto the same integer.
.
END OF CHAPTER 1