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. .
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...
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...
This is an intro to java question. Please answer with pseudocode and code. Problem 2: RSA...
This is an intro to java question. Please answer with pseudocode and code. Problem 2: RSA Public Key (10 points) (Cyber Security) RSA is an asymmetric encryption scheme, where a public key is used to encrypt data and a different, private key decrypts that data. RSA public/private keys are generated from two prime numbers, typically very large ones. The idea behind RSA is based on the fact that its difficult to factorize very large integers. RSA public key generation is...
please write the java code so it can run on jGRASP Thanks! 1 /** 2 *...
please write the java code so it can run on jGRASP Thanks! 1 /** 2 * PassArray 3 * @Sherri Vaseashta 4 * @Version 1 5 * @see 6 */ 7 import java.util.Scanner; 8 9 /** 10 This program demonstrates passing an array 11 as an argument to a method 12 */13 14 public class PassArray 15 { 16 public static void main(String[] args) 17 { 18 19 final int ARRAY_SIZE = 4; //Size of the array 20 // Create...
I need Q1,2 and 3 ASAP please, thanks 1. Identify three applications of integration. 2. Give...
I need Q1,2 and 3 ASAP please, thanks 1. Identify three applications of integration. 2. Give two specific examples of how we use integration to solve real-world problems. 3. Write a paragraph about how integration is applied in non-STEM disciplines. You must start a thread before you can read and reply to other threads
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT