A Strasen-like method for multiplying a 2x2 matrix
Write the algorithm for (brute force, divide and conquer or
karatsuba).
Calculate the complexity. See all possible situations.
Hi
Algorithm:
begin If n = threshold then compute C = a * b is a conventional matrix. Else Partition a into four sub matrices a11, a12, a21, a22. Partition b into four sub matrices b11, b12, b21, b22. Strassen ( n/2, a11 + a22, b11 + b22, d1) Strassen ( n/2, a21 + a22, b11, d2) Strassen ( n/2, a11, b12 – b22, d3) Strassen ( n/2, a22, b21 – b11, d4) Strassen ( n/2, a11 + a12, b22, d5) Strassen (n/2, a21 – a11, b11 + b22, d6) Strassen (n/2, a12 – a22, b21 + b22, d7) C = d1+d4-d5+d7 d3+d5 d2+d4 d1+d3-d2-d6 end if return (C) end.
Addition and Subtraction of two matrices takes O(N2) time. So time complexity can be written as
T(N) = 7T(N/2) + O(N2) From Master's Theorem, time complexity of above method is O(NLog7) which is approximately O(N2.8074)
Program:
#include <stdio.h>
int main()
{
int a[2][2],b[2][2],c[2][2],i,j;
int m1,m2,m3,m4,m5,m6,m7;
printf("Enter the 4 elements of first matrix:
");
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",&a[i][j]);
printf("Enter the 4 elements of second matrix:
");
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",&b[i][j]);
printf("\nThe first matrix is\n");
for(i=0;i<2;i++)
{
printf("\n");
for(j=0;j<2;j++)
printf("%d\t",a[i][j]);
}
printf("\nThe second matrix is\n");
for(i=0;i<2;i++)
{
printf("\n");
for(j=0;j<2;j++)
printf("%d\t",b[i][j]);
}
m1= (a[0][0] + a[1][1])*(b[0][0]+b[1][1]);
m2= (a[1][0]+a[1][1])*b[0][0];
m3= a[0][0]*(b[0][1]-b[1][1]);
m4= a[1][1]*(b[1][0]-b[0][0]);
m5= (a[0][0]+a[0][1])*b[1][1];
m6= (a[1][0]-a[0][0])*(b[0][0]+b[0][1]);
m7= (a[0][1]-a[1][1])*(b[1][0]+b[1][1]);
c[0][0]=m1+m4-m5+m7;
c[0][1]=m3+m5;
c[1][0]=m2+m4;
c[1][1]=m1-m2+m3+m6;
printf("\nAfter multiplication using \n");
for(i=0;i<2;i++)
{
printf("\n");
for(j=0;j<2;j++)
printf("%d\t",c[i][j]);
}
printf("\n");
return 0;
}
Screenshot:
A Strasen-like method for multiplying a 2x2 matrix Write the algorithm for (brute force, divide and...
Using sorting as an algorithm solving specific problem and compare their method of solving it and performances’ tradeoffs in terms of its time complexity. compare its performance using different approaches (three approaches) such as (divide and conquer, dynamic programming, brute force, greedy approach ). show which approach solve the problem best. Use sorting as an example and compare .
please answer all of the question
Brute Force Search A Write all possible circuits for the graph in the form <V,, V2V»,V4,V1> Example: <A,B,C,D,A> 1. There will be 24 possible circuits to list 24 18 27 2 For each circuit, calculate the total distance traveled The to tal distance is the sum of each distance on the route 17 Identify the Optimal Circuit 3. 15 The optimal path is the one with the shortest distance 23 Reflection 4. What about...
Java program: Convex Hull using Divide and Conquer Algorithm A convex hull is the smallest convex polygon containing all the given points. Input is an array of points specified by their x and y coordinates. The output is the convex hull of this set of points. Examples: Input : points[] = {(0, 0), (0, 4), (-4, 0), (5, 0), (0, -6), (1, 0)}; Output : (-4, 0), (5, 0), (0, -6), (0, 4) use Divide and Conquer Algorithm please explain...
a. Use pseudocode to specify a brute-force algorithm that takes as input a list of n positive integers and determines whether there are two distinct elements of the list that have as their sum a third element of the list. That is, whether there exists i, j.k such that iヂj, i关k,j关k and ai + aj = ak. The algorithm should loop through all triples of elements of the list checking whether the sum of the first two is the third...
7. Explain Dynamic Program ming algorithm in contrast to Divide and Conquer algorithm Discuss the advantages of Dynamic Programming over the other iophs method. 5pts) Then find the LCS of the following two strings X ABCBDAB) and Y- (BDCABA) (Explain the algorit g two strings. (He pts) thm as well 8. a) Explain the difference between recursive and iterative algorithms.(2 pts) b) The recursive Euclid algorithm is given as below: int GCD(int a, int b) f (b0) return a else...
Question #4 (15 points) In class, we discussed a divide-and-conquer algorithm for matrix multiplication that involved solving eight subproblems, each half the size of the original, and performing a constant number of e(n) addition operations. Strassen's Algo- rithm, which we did not cover, reduces the number of (half-sized) subproblems to seven, with a constant number of e(n) addition and subtraction operations. Provide clear, concise answers to each of the following related questions. • (7 points). Express the runtime of Strassen's...
10. Consider the Traveling Salesperson problem (a) Write the brute-force algorithm for this proble that considers (b) Implement the algorithm and use it to solve instances of size 6, 7, (c) Compare the performance of this algorithm to that of Algorithm all possible tours 8, 9, 10, 15, and 20 6.3 using the instances developed in (b) Algorithm 6.3 The Best-First Search with Branch-and-Bound Pruning Algorithm for the Traveling Salesperson problem Problem: Determine an optimal tour in a weighted, directed...
3. (50%) Use a programming language you familiar with to implement the brute force method and the branch and bound algorithm, both of which were already introduced in the class, for solving the traveling salesperson problem (35%). Compare their running time when the input size n is from 5 to 15 in steps of 1 (15%). Note that for each n, you should generate three problem instances and average the running time of solving these three instances. To verify the...
Topic Ch. 10 : Write a class definition for a 2x2 matrix, with methods for get(i,j), set(i,j,v) for getting and setting (i,j)-the entry where i is the row index 0 or 1, and j is the column index 0 or 1, and set() sets the entry in row i and column j to be v, a method for adding a matrix y to given x (e.g. x.add(y) will set x=x+y where the corresponding matrix entries are added), for multiplying( e.g....
The question of validity is to answer the question of whether for a Boolean expression consisting of n variables . There is a combination of values of variables that make the result of the expression true or not. In this phrase each The variable can be simple or contradictory. Force brute method of all compounds . Creates and evaluates the possible and the first time the result is true (if it is) the answer to the problem Come and stop...