Professional Documents
Culture Documents
KIoT
College of Informatics
Department of IS
Wollo University,
KIoT
College of Informatics
Department of IS
DATA STRUCTURE AND ALGORITHM
CHAPTER 1 AND 2
ALGORITHM ANALYSIS CONCEPTS
Revision contents…
self study: The details of the revision contents
Function: A function is defined as a group of instruction used to achieve a
specific task.
There are two types of functions. These are:
Library/built-in functions and
User defined functions.
Defining a Function
The general form of a C++ function definition is as follows: -
return_type function_name (parameter list) {
body of the function
}
Function must be called in order to be executed.
Parameter and arguments
Function call : by value, by reference and by pointer
Revision Cont…
Arrays: These collection of data which have the same data type.
data structures consisting of related data items of the same type.
consecutive group of memory locations that all have the same type.
Instead of declaring too many variables using array we can use array.
Declaring Array: syntax
type arrayName [ arraySize];
the above is for single dimensional array, for multi dimensional array we use the following:
type arrayName [size 1][size 2]. . .[size N];
An array element can be accessed by indexing the array name.
arrayName[0] refers to the lower bound element( or the first element and arrayName[N-1] refers to
the last element where N is the size of the array.
String : we can manipulate strings using array …. Read more about the string manipulation built in
functions
Revision Cont.…
Pointers: is a variable which stores the address of another variable.
The only difference between pointer variable and regular variable is the data they hold
There are two pointer operators in C++:
&: the address of operator
* : the dereference(value pointed by) operator
Declaration: data_type *pointer_name; // Add "= initial_value " if applicable
dt var= val;
dt *ptr=&var;
Array of pointer, Pointer Arthemetic, pointer and string, pointer to pointer operations self reading
Structure
A STRUCTURE is a C++ data structure that can be used to store together elements of different data
types.
In C++, a structure is a user-defined data type. The structure creates a data type for grouping items of
different data types under a single data type.
struct struct_name {
// struct members
}
Question:
struct Person { Write a C++ program that uses structure as a parameter for
char name[30]; function. Thus, your function accepts an information of the
Member person (name, citizen and age), then display the inputted
int citizenship;
variables information.
int age;
}
Person p; //Creating Struct Instances
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
Data structures model the static part of the world and are unchanged.
And, Algorithm is a tool to work with the dynamic part of the world. (Modeling)
Cont…
An algorithm transforms data structures from one state to another state in two ways:
An algorithm may change the value held by a data structure
An algorithm may change the data structure itself
correct data structures lead to simple and efficient algorithms and correct algorithms lead to accurate and
efficient data structures.
Properties of an algorithm
• Finiteness: Algorithm must complete after a finite number of steps.
• Definiteness: Each step must be clearly defined and tell exactly what happens next.
• Sequence: Each step must have a unique defined preceding and succeeding step.
• Feasibility: It must be possible to perform each instruction. •
Correctness: It must compute correct answer for all possible legal inputs • Language
Independence: It must not depend on any one programming language.
• Completeness: It must solve the problem completely. •
Effectiveness: perform each step exactly and in a finite amount of time. • Efficiency:
must solve with the least amount of computational resources
• Generality: Algorithm should be valid on all possible inputs.
• Input/Output: There must be a specified number of input values, and one or more output
Algorithm Analysis Concepts
Algorithm analysis refers to the process of determining the amount of computing time and storage
space required by different algorithms
Is process of predicting resource requirement
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 choose the best algorithm it is necessary to analyze the algorithm in terms of resources.
The Main Resources are
Running Time Memory Usage Communication Bandwidth
There are two approaches to measure the efficiency of algorithms:
Emprical: Programming competing algorithms and trying them on different instances (total running time of
the program).
Theoretical: Determining the quantity of resources required mathematically (Execution time , memory
space, etc.) needed by each algorithm.
Algorithm Analysis Concepts Cont…
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 : Processor speed and load, Data size and properties, the operating env’t
So, we can analyze algorithm based on the number of operations required than the time involved in the operation.
From this we can understand that an algorithm’s efficiency changes according to the size of input.
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.
There are things to be considered while performing the analysis.
Time complexity: Number of operations to solve problem of size N.
Space Complexity: Approximate size of memory required to solve problem of size N.
Complexity Analysis Have two phases:
Algorithm analysis: analysis of Algorithm or data structure to produce a function T(n) that describe
algorithm in terms of operation to performed to measure complexity.
Order of magnitude analysis: Analysis of the function T (n) to determine the general complexity category to
which it belongs.
…cont’d
So, the space requirement S(P) of any algorithm P may be written as
S(P) = c + SP
Where SP is the instance characteristics and c is a constant.
Space complexity must be taken seriously for multiuser systems and in
situations where limited memory is available.
05/22/2022 11
12
Cont…
we have to consider the following while working on the space complexity
05/22/2022
Calculating the Space Complexity
For calculating the space complexity, we need to know the value of
memory used by different type of datatype variables.
// n is the length of array a[]
int sum(int a[], int n) {
int x = 0;
for(int i = 0; i < n; i++)
{
x = x + a[i];
}
return(x);
}
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.
Time Complexity:
Example1:
int count(){
1 for the assignment statement: int k=0
int k=0;
1 for the output statement.
cout<< “Enter an integer”;
cin>>n; 1 for the input statement.
}
sum = sum+i; 1 N
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.
Nested Loops: Formally
Nested for loops translate into multiple summations, one for each for loop.
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++) { N M N
}
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.
Cont…
Consecutive Statements: Formally
Add the running times of the separate blocks of your code
for (int i = 1; i <= N; i++) {
sum = sum+i;
} N N N
2
for (int i = 1; i <= N; i++) { 1 2 N 2 N
for (int j = 1; j <= N; j++) { i 1 i 1 j 1
sum = sum+i+j;
}
}
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
1, 2
max
sum = sum+i;
}} 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 2 N 2
sum = sum+i+j;
}}
Cont…
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? Running time =2*1016/106 = 2*1010
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.
Generally, the complexity is investigated in three cases of measure of time.
Best Case − Minimum time required for program execution.
Average Case − Average time required for program execution.
Worst Case − Maximum time required for program execution.
Asymptotic Notations
There are five notations used to describe a running time function. These are:
Big-Oh Notation (O)
These three notations are the commonly used
Big-Omega Notation (W)
asymptotic notations to calculate the running
Theta Notation (Q) complexity of an algorithm
Little-o Notation (o)
Little-Omega Notation (w)
These asymptotic notations are used to determine the order of magnitude analysis.