You are on page 1of 21

Wollo University,

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

p.age = 27; //Accessing Struct Members


Chapter 2 : Data Structures and Algorithms Analysis
 A program is written in order to solve a problem. A solution to a problem actually consists of two
things:
  A way to organize the data : Data Structure
 Sequence of steps to solve the problem: Algorithm
 Given a problem, 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.

Problem Abstraction Model

 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 multi­user 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);
}

total memory requirement will be (4n + 12)


05/22/2022 13
Analysis Rules:
 We assume an arbitrary time unit.
 Execution of one of the following operations takes time 1:
 Assignment Operation
 Single Input / Output Operation
 Single Boolean Operations
 Single Arithmetic Operations
 Function Return
 Running time of a selection statement (if, switch) is the time for the condition evaluation(1) + the maximum of the running times for
the individual clauses in the selection.
Example: int x=10; // 1 for the assignement
x=x+10; // 1 for the assignement and 1 for the arthimetic operation
cout<< x; // 1 for output statement
 Loops: Running time for a loop is equal to the running time for the statements inside the loop * number of iterations.
for(i=1; i<=4; i++)
cout<<i;

 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.

for (i=0;i<n;i++) In the for loop:


1 assignment, n+1 tests, and n increments.
k=k+1;
n loops of 2 units for an assignment, and an addition.
return 0; 1 for the return statement.
} T (n)= 1+1+1+(1+n+1+n)+2n+1 = 4n+6 = O(n)
Time complexity cont…
Example2: while (i<n){
void func() x++;
{ i++;
int x=0; }
int i=0; while (j<n)
int j=1; {
cout<< “Enter an Integer
value”;
j++;
cin>>n; }
}

T (n)= 1+1+1+1+1+n+1+2n+n+n-1 = 5n+5


Formal Approach to Analysis
 In the above examples we have seen that analysis so 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.
N
for (int i = 1; i <= N; i++) {

}
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.

You might also like