CoppersmithFreivaldのアルゴリズムを実装するためのC++プログラム
Freivaldsのアルゴリズムは、O(kn ^ 2)で2 ^ -k未満の失敗の確率で、選択されたk値に対して行列が等しいかどうかを判断します。
行列の乗算を検証するために使用されます。
アルゴリズム
Begin Take matrix1(n*n), matrix2(n*n), matrix3(n*n) as input. // According to the algorithm we have to verify: // matrix1 × matrix2 = matrix3. 1) Choose vector a[n][1] randomly and uniformly in which component will be 0 or 1. 2) Compute matrix2 * a, matrix3 * a and then matrix1 * (matrix2 * a) for evaluating the expression, matrix1 * (matrix2 * a) - matrix3 * a. 3) Check if matrix1 * (matrix2 * a) - matrix3 * a = 0 or not. 4) If it is zero, then matrix multiplication is correct otherwise not. End.
例
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main(int argc, char **argv) {
cout << "Enter the dimension of the matrices: ";
int n;
cin >> n;
cout << "Enter the 1st matrix: ";
double matrix1[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> matrix1[i][j];
}
}
cout << "Enter the 2nd matrix: ";
double matrix2[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> matrix2[i][j];
}
}
cout << "Enter the result matrix: ";
double matrix3[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> matrix3[i][j];
}
}
double a[n][1];
for (int i = 0; i < n; i++) {
a[i][1] = rand() % 2;
}
double matrix2a[n][1];
for (int i = 0; i < n; i++) {
for (int j = 0; j < 1; j++) {
for (int k = 0; k < n; k++) {
matrix2a[i][j] = matrix2a[i][j] + matrix2[i][k] * a[k][j];
}
}
}
double matrix3a[n][1];
for (int i = 0; i < n; i++) {
for (int j = 0; j < 1; j++) {
for (int k = 0; k < n; k++) {
matrix3a[i][j] = matrix3a[i][j] + matrix3[i][k] * a[k][j];
}
}
}
double matrix12a[n][1];
for (int i = 0; i < n; i++) {
for (int j = 0; j < 1; j++) {
for (int k = 0; k < n; k++) {
matrix12a[i][j] = matrix12a[i][j] + matrix1[i][k] *matrix2a[k][j];
}
}
}
for (int i = 0; i < n; i++) {
matrix12a[i][0] -= matrix3a[i][0];
}
bool flag = true;
for (int i = 0; i < n; i++) {
if (matrix12a[i][0] == 0)
continue;
else
flag = false;
}
if (flag == true)
cout << "This is the resultant matrix";
else
cout << "This is not the resultant matrix";
} 出力-1
Enter the dimension of the matrices: 2 Enter the 1st matrix: 1 2 3 4 Enter the 2nd matrix: 2 0 1 2 Enter the result matrix: 4 4 10 8 This is the resultant matrix
出力-2
Enter the dimension of the matrices: 2 Enter the 1st matrix: 1 2 3 4 Enter the 2nd matrix: 2 0 1 2 Enter the result matrix: 4 5 5 5 This is not the resultant matrix
-
バブルソートを実装するC++プログラム
バブルソートは、比較ベースのソートアルゴリズムです。このアルゴリズムでは、隣接する要素が比較および交換されて、正しいシーケンスが作成されます。このアルゴリズムは他のアルゴリズムよりも単純ですが、いくつかの欠点もあります。このアルゴリズムは、多数のデータセットには適していません。並べ替えタスクの解決には時間がかかります。 バブルソート手法の複雑さ 時間計算量:最良の場合はO(n)、O(n 2 )平均および最悪の場合 スペースの複雑さ:O(1) Input − A list of unsorted data: 56 98 78 12 30 51 Output &mi
-
基数ソートを実装するC++プログラム
基数ソートは、非比較ソートアルゴリズムです。この並べ替えアルゴリズムは、同じ位置と値を共有する数字をグループ化することにより、整数キーで機能します。基数は、記数法のベースです。 10進法では、基数または基数は10であることがわかっているので、いくつかの10進数を並べ替えるには、数値を格納するために10個の位取りボックスが必要です。 基数ソート手法の複雑さ 時間計算量:O(nk) スペースの複雑さ:O(n + k) Input − The unsorted list: 802 630 20 745 52 300 612 932 78 187 Output &minus