Question

In: Computer Science

Please give Java Code ASAP Matrix Multiplication Due Friday 30th October 2020 by 23:55. (2 marks)...

Please give Java Code ASAP

Matrix Multiplication Due Friday 30th October 2020 by 23:55.

For this exercise, you are to find the optimal order for multiplying a sequence of matrices. Note: you do not actually have to perform any matrix multiplications. As usual, your program will prompt for the name of an input file and the read and process the data contained in this file.

The file contains the following data. N, the number of matrices to be multiplied together N pairs of integers which are the row and column dimensions of each matrix. E.g.

The following input

3 3 4 4 2 2 5

Defines a problem in which we are to multiply three matrices, say M[0], M[1] and M[2], where:

M[0] has 3 rows and 4 columns; M[1] has 4 rows and 2 columns; M[2] has 2 rows and 5 columns.

Output for the program is the value of best(0,N), the minimum number of multiplications required to compute the matrix R = M[0]xM[1]x…xM[N‐1].

You may leave your solution as a memoized, recursive formulation if you have problems formulating the looped iterative scheme.

As usual, do not use classes or STL. Submit ex10.ext via moodle as usual where ext is one of c, cpp, java or py.

Solutions

Expert Solution

import java.io.*;
import java.util.*;

public class Main
{
    // function to calculate minimum multiplication count
    public static int minMultiplication(int arr[], int start, int end)
    {
        int res=Integer.MAX_VALUE;// stores min value of multiplication count required
        if(start!=end)
        {
            for(int i=start;i<end;i++) 
            {
                int temp=minMultiplication(arr,start,i)+minMultiplication(arr,i+1,end)+arr[start-1]*arr[i]*arr[end];
                if(temp<res)
                    res=temp;
            }
        }
        else    
            res=0;
        return res;
    }
    // driver code
        public static void main(String[] args) throws Exception{
            Scanner s=new Scanner(System.in);
            System.out.print("Enter input file name: ");
            String file=s.nextLine();
                BufferedReader br=new BufferedReader(new FileReader(file));
                String ll=br.readLine();
                int n=Integer.parseInt(ll.trim());// total number of matrices
                
                int m[]=new int[n+1];
                int k=0,count=1;
                while((ll=br.readLine())!=null){// loop till line in file is present
                    String mm[]=ll.split("\\s+");// split string on space
                    if(count==1){// for first matrix 
                        m[k++]=Integer.parseInt(mm[0]);
                        m[k++]=Integer.parseInt(mm[1]);
                    }
                    else{
                        m[k++]=Integer.parseInt(mm[1]);
                    }
                    count++;
                }
                System.out.println("Minimum number of multiplication required: "+minMultiplication(m,1,m.length-1));
        }
}


Related Solutions

Please give C++ code ASAP Matrix Multiplication Due Friday 30th October 2020 by 23:55. (2 marks)...
Please give C++ code ASAP Matrix Multiplication Due Friday 30th October 2020 by 23:55. For this exercise, you are to find the optimal order for multiplying a sequence of matrices. Note: you do not actually have to perform any matrix multiplications. As usual, your program will prompt for the name of an input file and the read and process the data contained in this file. The file contains the following data. N, the number of matrices to be multiplied together...
Due Friday Oct. 30th, 11:59pm (10 marks total): 1. Explain the purpose of a sequence diagram....
Due Friday Oct. 30th, 11:59pm (10 marks total): 1. Explain the purpose of a sequence diagram. . 2. Draw a sequence diagram for the online ticketing system in Assignment #3. Identify the objects, lifelines, messages, and focuses in your diagram. .
1. Write an algorithm to calculate the Matrix multiplication (or write with pseudo code) 2. Write...
1. Write an algorithm to calculate the Matrix multiplication (or write with pseudo code) 2. Write an algorithm to calculate the recursive Matrix multiplication (or write with pseudo code) 3. Find the time complexity of your pseudo code and analyze the differences
Please provide the missng code(parts) for the given code in java. ASAP. import java.util.Stack; class StackQueue<T>...
Please provide the missng code(parts) for the given code in java. ASAP. import java.util.Stack; class StackQueue<T> implements Queue<T>{ private Stack<T> inStack; private Stack<T> outStack; private int size; public StackQueue(){ inStack = new Stack<T> (); outStack = PART(a); size = 0; } public int size(){ return size; } public boolean isEmpty(){ return size==0; } public T first(){ if (size == 0) return null;; if (outStack.empty()) while (!inStack.empty()) outStack.push(inStack.pop()); return PART(b); //return the element at the top of the outStack without removing...
[12:18, 10/2/2020] Mohan Reddy: You are to implement a program for matrix multiplication in C++ without...
[12:18, 10/2/2020] Mohan Reddy: You are to implement a program for matrix multiplication in C++ without thread AND with thread. [12:18, 10/2/2020] Mohan Reddy: ou are to implement (M by N matrix) times (N by 1 vector) operation and see how multiple threads can speed up the computation. The resulting vector will be (M by 1 vector). See the following steps/requirements. 1. Accept M and N as keyboard input. 2. Generate a random (M by N matrix) and a random...
Please write code in java ASAP and add comments too, will be really appreciated. Thanks CSCI203/CSCI803...
Please write code in java ASAP and add comments too, will be really appreciated. Thanks CSCI203/CSCI803 This assignment involves extension to the single source-single destination shortest path problem. The Program Your program should: 1. Read the name of a text file from the console. (Not the command line) 2. Read an undirected graph from the file. 3. Find the shortest path between the start and goal vertices specified in the file. 4. Print out the vertices on the path, in...
This is the question about the java problem, please give the detail comment and code of...
This is the question about the java problem, please give the detail comment and code of each class. Please write tests for all the code of all the classes Thank you Create a class Mammal with the following UML diagrams: +---------------------------------+ | Mammal | +---------------------------------+ | - name: String | +---------------------------------+ | + Mammal(String name) | | + getName(): String | | + isCookable(): boolean | | + testMammal(): void | (this method is static ) +---------------------------------+ The isCookable method...
Due Date: 30 October 2020 Question 1 (100 marks) Note: you must use your own words...
Due Date: 30 October 2020 Question 1 Note: you must use your own words in answering the questions; those parts copied from any source without your inputs/explanation/examples or elaborations will not be given any marks. I . The Conceptual Framework for Financial Reporting sets and discusses the objective and fundamentals that serve as the basis for developing financial accounting and reporting standards in different countries. The fundamentals are the underlying concepts of financial accounting that guide the selection of transactions,...
Can someone please solve parts c, d, e, and h ASAP? This is due tomorrow 2....
Can someone please solve parts c, d, e, and h ASAP? This is due tomorrow 2. (30 pts) Consider a production function given by: Q = 12(KL)2 – L 4 . a. (5 pts) Does this production function exhibit increasing, constant, or decreasing returns to scale? b. (5 pts) Derive the equation for the average product of labor and the marginal product of labor? c. (4 pts) Let K = 2. Find the level of L at which the marginal...
Please give me an example of this java code. Vehicle - number: int - color: String...
Please give me an example of this java code. Vehicle - number: int - color: String - price: double - horsepower: double + Vehicle ( ) + Vehicle (int number) + Vehicle (int number, String color) + Vehicle (int number, String color, double price) + Vehicle (int number, String color, double price, double horsepower) +getnumber( ):int +setnumber(int number):void +getcolor( ):String +setcolor(String color):void +getprice( ):double +setprice(double price):void +gethorsePower( ):double +sethorsePower(double horsePower):void toString( ):String public String toString( ) { return "\n\n number...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT