1. Write a program to implement Strassen's Matrix Multiplication of 2*2 Matrixes.
import java.util.Scanner;
public class StrassenAlgorithm {
public static int[][] multiply(int[][] A, int[][] B) {
int n = A.length;
// Base case: If the matrix size is 1x1, perform a simple multiplication
if (n == 1) {
int[][] C = new int[1][1];
C[0][0] = A[0][0] * B[0][0];
return C;
// Divide matrices A and B into four submatrices
int[][] A11 = new int[n / 2][n / 2];
int[][] A12 = new int[n / 2][n / 2];
int[][] A21 = new int[n / 2][n / 2];
int[][] A22 = new int[n / 2][n / 2];
int[][] B11 = new int[n / 2][n / 2];
int[][] B12 = new int[n / 2][n / 2];
int[][] B21 = new int[n / 2][n / 2];
int[][] B22 = new int[n / 2][n / 2];
// Splitting input matrices into submatrices
splitMatrix(A, A11, 0, 0);
splitMatrix(A, A12, 0, n / 2);
splitMatrix(A, A21, n / 2, 0);
splitMatrix(A, A22, n / 2, n / 2);
splitMatrix(B, B11, 0, 0);
splitMatrix(B, B12, 0, n / 2);
splitMatrix(B, B21, n / 2, 0);
splitMatrix(B, B22, n / 2, n / 2);
// Compute intermediate matrices using Strassen's algorithm
int[][] P1 = multiply(add(A11, A22), add(B11, B22));
int[][] P2 = multiply(add(A21, A22), B11);
int[][] P3 = multiply(A11, subtract(B12, B22));
int[][] P4 = multiply(A22, subtract(B21, B11));
int[][] P5 = multiply(add(A11, A12), B22);
int[][] P6 = multiply(subtract(A21, A11), add(B11, B12));
int[][] P7 = multiply(subtract(A12, A22), add(B21, B22));
// Compute result submatrices
int[][] C11 = subtract(add(P1, P4), add(P5, P7));
int[][] C12 = add(P3, P5);
int[][] C21 = add(P2, P4);
int[][] C22 = subtract(add(P1, P3), add(P2, P6));
// Combine result submatrices into the final result matrix
int[][] C = new int[n][n];
joinMatrix(C11, C, 0, 0);
joinMatrix(C12, C, 0, n / 2);
joinMatrix(C21, C, n / 2, 0);
joinMatrix(C22, C, n / 2, n / 2);
return C;
public static int[][] add(int[][] A, int[][] B) {
int n = A.length;
int[][] C = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
C[i][j] = A[i][j] + B[i][j];
return C;
public static int[][] subtract(int[][] A, int[][] B) {
int n = A.length;
int[][] C = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
C[i][j] = A[i][j] - B[i][j];
return C;
public static void splitMatrix(int[][] P, int[][] C, int iB, int jB) {
for (int i1 = 0, i2 = iB; i1 < C.length; i1++, i2++) {
for (int j1 = 0, j2 = jB; j1 < C.length; j1++, j2++) {
C[i1][j1] = P[i2][j2];
public static void joinMatrix(int[][] C, int[][] P, int iB, int jB) {
for (int i1 = 0, i2 = iB; i1 < C.length; i1++, i2++) {
for (int j1 = 0, j2 = jB; j1 < C.length; j1++, j2++) {
P[i2][j2] = C[i1][j1];
public static void printMatrix(int[][] matrix) {
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
System.out.print(matrix[i][j] + " ");
System.out.println();
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the size of the matrices (power of 2): ");
int n = scanner.nextInt();
// Input matrices
int[][] A = new int[n][n];
int[][] B = new int[n][n];
System.out.println("Enter the elements of matrix A:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
A[i][j] = scanner.nextInt();
}
System.out.println("Enter the elements of matrix B:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
B[i][j] = scanner.nextInt();
// Multiply matrices using Strassen's algorithm
int[][] result = multiply(A, B);
// Print the result
System.out.println("Resultant matrix:");
printMatrix(result);
scanner.close();
Part B
5. Write program to implement greedy algorithm for job sequencing with deadlines
import java.util.*;
public class jobgreedy
public static void main(String args[])
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of Jobs");
int n=sc.nextInt();
String job[]=new String[n];
int profit[]=new int[n];
int deadline[]=new int[n];
for(int i=0;i<n;i++)
System.out.println("Enter the Jobs");
job[i]=sc.next();
System.out.println("Enter the Profit");
profit[i]=sc.nextInt();
System.out.println("Enter the DeadLine");
deadline[i]=sc.nextInt();
System.out.println("--Arranged Order--");
System.out.print("Jobs: ");
for(int i=0;i<n;i++)
System.out.print(job[i]+" ");
System.out.println();
System.out.print("Profit: ");
for(int i=0;i<n;i++)
System.out.print(profit[i]+" ");
System.out.println();
System.out.print("DeadLine:");
for(int i=0;i<n;i++)
System.out.print(deadline[i]+" ");
for(int i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
if(profit[i]<profit[j])
int temp=profit[i];
profit[i]=profit[j];
profit[j]=temp;
temp=deadline[i];
deadline[i]=deadline[j];
deadline[j]=temp;
String temp1=job[i];
job[i]=job[j];
job[j]=temp1;
System.out.println();
System.out.println("--Sorted Order--");
System.out.print("Jobs: ");
for(int i=0;i<n;i++)
System.out.print(job[i]+" ");
System.out.println();
System.out.print("Profit: ");
for(int i=0;i<n;i++)
System.out.print(profit[i]+" ");
}
System.out.println();
System.out.print("DeadLine:");
for(int i=0;i<n;i++)
System.out.print(deadline[i]+" ");
System.out.println();
int max=deadline[0];
for(int i=0;i<n;i++)
if(deadline[i]>max)
max=deadline[i];
String slot[]=new String[max];
int totalprofit=0;
for(int i=0;i<n;i++)
int val=deadline[i];
val=val-1;
if(slot[val]==null )
slot[val]=job[i];
totalprofit+=profit[i];
else
while(val!=-1)
{
if(slot[val]==null)
slot[val]=job[i];
totalprofit+=profit[i];
break;
val=val-1;
for(int i=0;i<max;i++)
System.out.print("-->"+slot[i]);
System.out.println();
System.out.print("Profit Earned: "+totalprofit);