Professional Documents
Culture Documents
Data Structures Algorithms - Lecture 3 4 - Algorithmic Toolkit - 1
Data Structures Algorithms - Lecture 3 4 - Algorithmic Toolkit - 1
Data Structures Algorithms - Lecture 3 4 - Algorithmic Toolkit - 1
AHMED
Lecture # 3 & 4
ALGORITHMIC Toolkit - 1
INSTRUCTOR: Saeed
Ahmed
Algorithm
■ An algorithm is a sequence of instructions that one must perform in
order to solve a well-formulated problem.
if(numbers[i]*numbers[j]>result) { cin>>numbers[i];
}
result=numbers[i]*numbers[j]; int result=MaxProd(numbers);
}
cout<<result;
}
return 0;
}
}
return result;
}
Testing Results
■ This program works for infinite input instances.
■ But, it does not work for many (may be infinite) of the instances, too.
int main() {
cout << "Size of char : " << sizeof(char) << endl;
cout << "Size of int : " << sizeof(int) << endl;
cout << "Size of short int : " << sizeof(short) << endl;
cout << "Size of long int : " << sizeof(long) << endl;
cout << "Size of long long : " << sizeof(long long) << endl;
cout << "Size of float : " << sizeof(float) << endl;
cout << "Size of double : " << sizeof(double) << endl;
cout << "Size of wchar_t : " << sizeof(wchar_t) << endl;
return 0;
}
Output
Range of Datatypes in C++
#include<iostream>
#include<limits.h>
#include<float.h>
using namespace std;
int main() {
cout << "char ranges from : " << CHAR_MIN << " to " << CHAR_MAX;
cout << "\n\nshort char ranges from : " << SCHAR_MIN << " to " << SCHAR_MAX;
cout << "\n\nunsigned char ranges from : " << 0 << " to " << UCHAR_MAX;
cout << "\n\n\nshort int ranges from : " << SHRT_MIN << " to " << SHRT_MAX;
cout << "\n\nunsigned short int ranges from : " << 0 << " to " << USHRT_MAX;
cout << "\n\nint ranges from : " << INT_MIN << " to " << INT_MAX;
cout << "\n\nunsigned int ranges from : " << 0 << " to " << UINT_MAX;
cout << "\n\nlong int ranges from : " << LONG_MIN << " to " << LONG_MAX;
cout << "\n\nunsigned long int ranges from : " << 0 << " to " << ULONG_MAX;
cout << "\n\nlong long int ranges from : " << LLONG_MIN << " to " << LLONG_MAX;
cout << "\n\nunsigned long long int ranges from : " << 0 << " to " << ULLONG_MAX;
cout << "\n\n\nfloat ranges from : " << FLT_MIN << " to " << FLT_MAX;
cout << "\n\nnegative float ranges from : " << -FLT_MIN << " to " << -FLT_MAX;
cout << "\n\ndouble ranges from : " << DBL_MIN << " to " << DBL_MAX;
cout << "\n\nnegative double ranges from : " << -DBL_MIN << " to " << +DBL_MAX;
return 0;
}
Output
Changing the Data Structure (Data Type)
long long MaxProd(vector<int>& numbers) { int main() {
long long result=0; int n;
int n=numbers.size(); cin>>n;
for(int i=0;i<n;++i) { vector<int> numbers(n);
for(int j=i+1;j<n;++j) {
for(int i=0;i<n;++i) {
if(((long long)numbers[i])*numbers[j]>
result) { cin>>numbers[i];
result=((long long)numbers[i])*numbers[j]; }
} long long result=MaxProd(numb
ers);
}
} cout<<result;
3
■ We do not multiply a number with itself, but
we multiply each and every number with every
4
other number in the list and make comparison.
…
n
Analysis of Algorithm #1
1
multiplications.
…
n
Analysis of Algorithm #1
1
3
■ So, there are n-2 comparisons and n-2
multiplications.
4
…
n
Analysis of Algorithm #1
1
…
■ Do we actually need this number of operations,
or we can optimize that? n
Algorithm #2
Problem: MaxPairwiseProduct(n, numbers[n])
Input: n non-negative integers as numbers
Output: result as maximum pairwise product
Start
num1=FirstMaximum(numbers) //simple for loop with n-1 comparisons
num2=SecondMaximum(numbers) //simple for loop with n-2 comparisons
result=num1 * num2
End
Analysis of Algorithm #2
1
3
■ This can be done by simply scanning the
values of the list.
4
…
■ At the end, we just need 1 multiplication.
n
Analysis of Algorithm #2
1
…
n
Comparison of Costs
size of n cost of algorithm #1 cost of algorithm #2
10 90 18
9000000000
8000000000
7000000000
6000000000
5000000000
4000000000
3000000000
2000000000
1000000000
0
0 10000 20000 30000 40000 50000 60000 70000 80000 90000 100000
// if(index1==0)
// index2=1;
Altering the Functions Again
■ Removing the comment from the second
line.
// if(index1==0)
index2=1;
Stress Testing
■ We have already run stress test on our
algorithms for a small number of n.
THANK YOU