DS&A-Chapter One
DS&A-Chapter One
The model defines an abstract view to the problem. This implies that the model focuses only on
problem related stuff and that a programmer tries to define the properties of the problem.
An entity with the properties just described is called an abstract data type (ADT).
1
DS&A- Chapter One
A data structure is a language construct that the programmer has defined in order to implement an
abstract data type.
There are lots of formalized and standard Abstract data types such as Stacks, Queues, Trees, etc.
1.1.2. Abstraction
Abstraction is a process of classifying characteristics as relevant and irrelevant for the particular
purpose at hand and ignoring the irrelevant ones. Applying abstraction correctly is the essence of
successful programming.
How do data structures model the world or some part of the world?
The value held by a data structure represents some specific characteristic of the world
The characteristic being modeled restricts the possible values held by a data structure
The characteristic being modeled restricts the possible operations to be performed on the
data structure.
Note: Notice the relation between characteristic, value, and data structures
Where are algorithms, then?
1.2. Algorithms
An algorithm is a well-defined computational procedure that takes some value or a set of values
as input and produces some value or a set of values as output. 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:
The quality of a data structure is related to its ability to successfully model the characteristics of
the world. Similarly, the quality of an algorithm is related to its ability to successfully simulate the
changes in the world.
However, independent of any particular world model, the quality of data structure and algorithms
is determined by their ability to work together well. Generally speaking, correct data structures
2
DS&A- Chapter One
lead to simple and efficient algorithms and correct algorithms lead to accurate and efficient data
structures.
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.
Running Time
Memory Usage
Communication Bandwidth
Running time is usually treated as the most important since computational time is the most
precious resource in most problem domains.
3
DS&A- Chapter One
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
efficiency changes according to the size of the input.
The goal is to have a meaningful measure that permits comparison of algorithms independent of
operating platform.
There are two things to consider:
Time Complexity: Determine the approximate number of operations required to solve a
problem of size n.
Space Complexity: Determine the approximate memory required to solve a problem of
size n.
There is no generally accepted set of rules for algorithm analysis. However, an exact count of
operations is commonly used.
4
DS&A- Chapter One
Loops: Running time for a loop is equal to the running time for the statements inside the
loop * number of iterations.
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.
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.
Examples:
1. 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)
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)
3. void func()
5
DS&A- Chapter One
{
int x=0;
int i=0;
int j=1;
cout<< “Enter an Integer value”;
cin>>n;
while (i<n){
x++;
i++;
}
while (j<n)
{
j++;
}
}
Time Units to Compute
-------------------------------------------------
1 for the first assignment statement: x=0;
1 for the second assignment statement: i=0;
1 for the third assignment statement: j=1;
1 for the output statement.
1 for the input statement.
In the first while loop:
n+1 tests
n loops of 2 units for the two increment (addition) operations
In the second while loop:
n tests
n-1 increments
-------------------------------------------------------------------
T (n)= 1+1+1+1+1+n+1+2n+n+n-1 = 5n+5 = O(n)
4. 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)
6
DS&A- Chapter One
1 N
for (int i = 1; i <= N; i++) {
sum = sum+i;
}
i 1
Suppose we count the number of additions that are done. There is 1 addition per iteration of
the loop, hence N additions in total.
}
sum = sum+i+j; 2 2M
i 1 j 1 i 1
2 MN
}
Again, count the number of additions. The outer summation is for the outer for loop.
Conditionals: Formally
If (test) s1 else s2: Compute the maximum of the running time for s1 and s2.
if (test == 1) {
for (int i = 1; i <= N; i++) { N N N
sum = sum+i; max
1, 2
}} i 1 i 1 j 1
else for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) { max N , 2 N 2
2N 2
sum = sum+i+j;
}}
7
DS&A- Chapter One
Example:
Suppose we have hardware capable of executing 106 instructions per second. How long would it
take to execute an algorithm whose complexity function was:
T (n) = 2n2 on an input size of n=108?
The total number of operations to be performed would be T (108):
Exercises
Determine the run time equation and complexity of each of the following code segments.
1. for (i=0;i<n;i++)
for (j=0;j<n; j++)
sum=sum+i+j;
5. int x=0;
for(int i=1;i<n;i=i+5)
x++;
What is the value of x when n=25?
6. int x=0;
for(int k=n;k>=n/3;k=k-5)
x++;
What is the value of x when n=25?
7. int x=0;
for (int i=1; i<n;i=i+5)
8
DS&A- Chapter One
8. int x=0;
for(int i=1;i<n;i=i+5)
for(int j=0;j<i;j++)
for(int k=n;k>=n/2;k=k-3)
x++;
What is the correct big-Oh Notation for the above code segment?
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.
Best Case (Tbest): The amount of time the algorithm takes on the smallest possible set of inputs.
We are interested in the worst-case time, since it provides a bound for all input – this is called the
“Big-Oh” estimate.
Big-Oh notation is a way of comparing algorithms and is used for computing the
complexity of algorithms; i.e., the amount of time takes for computer program to run.
9
DS&A- Chapter One
It’s only concerned with what happens for very large value of n. Therefore only largest
term in the expression (function) is needed.
For ex, if the number of operations in an algorithm is n2 – n, n is insignificant compared
to n2 for large values of n. Hence the n term is ignored. Of course, for small values of n,
it may be important.
However, Big-Oh is mainly concerned with large values of n.
The notation Ο(n) is a formal way to express the upper bound of an algorithm's running
time.
It measures worst case time complexity or the longest amount of time an algorithm can
possibly take to complete. Ex...
10
DS&A- Chapter One
f(n)= Ω( g (n)) means that f(n) is greater than or equal to some constant multiple of g(n)
for all values of n greater than or equal to some k.
In simple terms, f(n)= Ω( g (n)) means that the growth rate of f(n) is greater than or equal to g(n).
The notation Ω(n) is the formal way to express the lower bound of an algorithm's running
time.
It measures the best case time complexity or the best amount of time an algorithm can
possibly take to complete.
The θ(n) is the formal way to express both the lower bound and upper bound of an algorithm's
running time. It is represented as following
A function f (n) belongs to the set of Θ (g(n)) if there exist positive constants c1 and c2 such that
it can be sandwiched between c1.g(n) & c2.g(n), for sufficiently large values of n.
11
DS&A- Chapter One
If f(n)= Θ (g(n)), then g(n) is an asymptotically tight bound for f(n). In simple terms, f(n)=
Θ (g(n)) means that f(n) and g(n) have the same rate of growth.
Big-Oh notation may or may not be asymptotically tight, for example: 2n2 = O(n2)=O(n3)
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.
12
DS&A- Chapter One
13