In this course, we will learn about algorithms through examples.
An algorithm is a series of well-defined instructions for solving a specific problem in computer programming. It accepts a collection of inputs and outputs the desired result. As an example,
An algorithm for multiplying two numbers:
Consider two numerical inputs.
Using the + operator, add two numbers together.
Show the outcome
Input and output must be carefully defined.
Each stage of the algorithm should be distinct and unequivocal.
Algorithms should be the most effective solution among many possible solutions to a problem.
Computer code should not be included in an algorithm. Instead, the method should be constructed in a form that allows it to be utilized in a variety of programming languages.
Input- 0 or more
Output- At least 1 output
Definiteness- Should have definite results or solutions
Finite- It must be stop or there should be an end state
Effectiveness- No unnecessary statement or redundant statements in algorithm
Design oriented
Need domain knowledge
Any language can be used
No hardware and operating system specification
This is an analysis of any solution
Implementation oriented
Need programming knowledge
Only programming language can be used
Specified hardware and operating system
This is an testing oriented approach
Algorithm
Independent of language
Hardware independent
Time and space function
Program
Language dependent
Hardware dependent
Analysis of time and bytes
Algorithm Sawap(a, b){
temp:= a ;
a:= b ;
b:= temp ;
}
Time - how much time the algorithm is taken?
Space- how much space/memory it is consumed?
Network consumption- Reduction of data quality for transferring a data.
Power consumption- how much power the algorithm will consume?
CPU consumption- how many register it is consuming?
Example:
Algorithm Sawap(a, b){
temp:= a ; // require 1 unit of time
a:= b ; // require 1 unit of time
b:= temp ; // require 1 unit of time
}
Time analysis: f(n)= 1+1+1 = 3, that means this algorithm require order of 1 or, O(1)
Space analysis: how many variables are require to solve this algorithm?
Here, we require 3 variables to solve swap problem. temp, a, and b. So, S(n)=3 or order of 1 or, O(1)
Example 1:
Algorithm Sum(A, n){
s=0; \\require 1 unit of time
for(i=0; i<n; i++){ \\ require n+1 unit of time
s=s+A[i] \\ require n unit of time
}
return s; \\require 1 unit of time
}
Time analysis: f(n)= 1+n+1+n+1=2n+3, that means this algorithm require order of n or, O(n) [the highest power of n in the time function]
Space analysis:
Here we have one array of length n, 'A', variables n, s, and i.
array, A = requires n unit of space
variable, n= requires 1 unit of space
variable, s= requires 1 unit of space
variable, i= requires 1 unit of space
Total S(n) = n+1+1+1= n+3 that means this algorithm require order of n or, O(n) [the highest power of n in the space function]
Example 2:
Algorithm Add_Array(A, B, n){
for(i=0; i<n; i++){ \\ require n+1 unit of time
for(j=0; j<n; j++){ \\ require n(n+1) unit of time
C[i,j]= A[i,j]+B[i,j] ; \\ require n * n unit of time
}
}
return C; \\require 1 unit of time
}
Time analysis: f(n)= n+1+n(n+1)+n*n+1=2n^2+2n+1, that means this algorithm require order of n^2 or, O(n^2) [the highest power of n in the time function]
Space analysis:
Here we have one array of length n, 'A, B, C', variables n, j, and i.
array, A = requires n^2 unit of space [As we are using 2d array]
array, B = requires n^2 unit of space [As we are using 2d array]
array, C = requires n^2 unit of space [As we are using 2d array]
variable, n= requires 1 unit of space
variable, i= requires 1 unit of space
variable, j= requires 1 unit of space
Total S(n) = 3n^2+3 that means this algorithm require order of n^2 or, O(n^2) [the highest power of n in the space function]
Example 3: [Do it now!]
Algorithm Multiply_Array(A, B, n){
for(i=0; i<n; i++){
for(j=0; j<n; j++){
C[i,j]= 0 ;
for(k=0; k<n; k++){
C[i,j]= C[i,j]+ A[i,k]*B[k,j];
}
}
}
return C;
}