From 514e5db415e65776a34933a9f87f0745fc80336b Mon Sep 17 00:00:00 2001 From: 19UCS165 Amritesh Anand Date: Wed, 6 Oct 2021 14:43:58 +0000 Subject: [PATCH 1/3] Implemented program to find nth Catalan number --- DynamicProgramming/CatalanNumber.java | 59 +++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 DynamicProgramming/CatalanNumber.java diff --git a/DynamicProgramming/CatalanNumber.java b/DynamicProgramming/CatalanNumber.java new file mode 100644 index 000000000000..7e81fb358b27 --- /dev/null +++ b/DynamicProgramming/CatalanNumber.java @@ -0,0 +1,59 @@ +package DynamicProgramming; + +/** + * This file contains an implementation of finding the nth CATALAN NUMBER using dynamic programming + * Wikipedia: https://en.wikipedia.org/wiki/Catalan_number + * + * Time Complexity: O(n^2) + * Space Complexity: O(n) + * + * @author AMRITESH ANAND (https://github.com/amritesh19) + */ + +import java.util.Scanner; + +public class CatalanNumber { + + /** + * This method finds the nth Catalan number + * + * @param n input n which determines the nth Catalan number + * n should be less than equal to 50 as 50th Catalan number is 6,533,841,209,031,609,592 + * for n > 50, BigInteger class should be used instead long + * + * @return catalan[n] + */ + static long catalanDP(int n){ + + // Table to store the results of subproblems + long catalan[] = new long[n + 1]; + + // Initialising C₀ = 1 and C₁ = 1 + catalan[0] = 1; + catalan[1] = 1; + + /** + * The Catalan numbers satisfy the recurrence relation + * C₀=1 and Cn = Σ Ci*Cn-1-i, i = 0 to n-1 , n > 0 + */ + for(int i = 2; i <= n; i++){ + catalan[i] = 0; + for(int j = 0; j < i; j++){ + catalan[i] += catalan[j] * catalan[i - j - 1]; + } + } + + return catalan[n]; + } + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + + System.out.println("Enter the number n to find nth Catalan number (n <= 50)"); + int n = sc.nextInt(); + System.out.println(n + "th Catalan number is " + catalanDP(n)); + + sc.close(); + } +} + From dc3a835342a94df8d5d16beb620bd7618dc80f50 Mon Sep 17 00:00:00 2001 From: 19UCS165 Amritesh Anand Date: Wed, 6 Oct 2021 15:11:31 +0000 Subject: [PATCH 2/3] some fixes according to convention --- DynamicProgramming/CatalanNumber.java | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/DynamicProgramming/CatalanNumber.java b/DynamicProgramming/CatalanNumber.java index 7e81fb358b27..156654cecad3 100644 --- a/DynamicProgramming/CatalanNumber.java +++ b/DynamicProgramming/CatalanNumber.java @@ -21,37 +21,38 @@ public class CatalanNumber { * n should be less than equal to 50 as 50th Catalan number is 6,533,841,209,031,609,592 * for n > 50, BigInteger class should be used instead long * - * @return catalan[n] + * @return catalanArray[n] the nth Catalan number */ - static long catalanDP(int n){ + static long findNthCatalan(int n){ - // Table to store the results of subproblems - long catalan[] = new long[n + 1]; + // Array to store the results of subproblems i.e Catalan numbers from [1...n-1] + long catalanArray[] = new long[n + 1]; // Initialising C₀ = 1 and C₁ = 1 - catalan[0] = 1; - catalan[1] = 1; + catalanArray[0] = 1; + catalanArray[1] = 1; /** * The Catalan numbers satisfy the recurrence relation - * C₀=1 and Cn = Σ Ci*Cn-1-i, i = 0 to n-1 , n > 0 - */ + * C₀=1 and Cn = Σ (Ci * Cn-1-i), i = 0 to n-1 , n > 0 + */ for(int i = 2; i <= n; i++){ - catalan[i] = 0; + catalanArray[i] = 0; for(int j = 0; j < i; j++){ - catalan[i] += catalan[j] * catalan[i - j - 1]; + catalanArray[i] += catalanArray[j] * catalanArray[i - j - 1]; } } - return catalan[n]; + return catalanArray[n]; } + // Main method public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter the number n to find nth Catalan number (n <= 50)"); int n = sc.nextInt(); - System.out.println(n + "th Catalan number is " + catalanDP(n)); + System.out.println(n + "th Catalan number is " + findNthCatalan(n)); sc.close(); } From 6626af017d3f24f8ba220057632188c34ddcf801 Mon Sep 17 00:00:00 2001 From: 19UCS165 Amritesh Anand Date: Sun, 31 Oct 2021 18:15:23 +0000 Subject: [PATCH 3/3] Implemented Banker's Algorithm --- Others/BankersAlgorithm.java | 186 +++++++++++++++++++++++++++++++++++ 1 file changed, 186 insertions(+) create mode 100644 Others/BankersAlgorithm.java diff --git a/Others/BankersAlgorithm.java b/Others/BankersAlgorithm.java new file mode 100644 index 000000000000..40d7267baacf --- /dev/null +++ b/Others/BankersAlgorithm.java @@ -0,0 +1,186 @@ +package Others; + +/** + * This file contains an implementation of BANKER'S ALGORITM + * Wikipedia: https://en.wikipedia.org/wiki/Banker%27s_algorithm + * + * The algorithm for finding out whether or not a system is in a safe state can be described as follows: + * 1. Let Work and Finish be vectors of length ‘m’ and ‘n’ respectively. + * Initialize: Work= Available + * Finish [i]=false; for i=1,2,……,n + * 2. Find an i such that both + * a) Finish [i]=false + * b) Need_i<=work + * + * if no such i exists goto step (4) + * 3. Work=Work + Allocation_i + * Finish[i]= true + * goto step(2) + * 4. If Finish[i]=true for all i, + * then the system is in safe state. + * + * Time Complexity: O(n*n*m) + * Space Complexity: O(n*m) + * where n = number of processes and m = number of resources. + * + * @author AMRITESH ANAND (https://github.com/amritesh19) + */ + +import java.util.Scanner; + +public class BankersAlgorithm { + + /** + * This method finds the need of each process + */ + static void calculateNeed(int needArray[][], int maxArray[][], int allocationArray[][], int totalProcess, int totalResources) + { + for (int i = 0 ; i < totalProcess ; i++){ + for (int j = 0 ; j < totalResources ; j++){ + needArray[i][j] = maxArray[i][j] - allocationArray[i][j]; + } + } + } + + /** + * This method find the system is in safe state or not + * @param processes[] int array of processes (0...n-1), size = n + * @param availableArray[] int array of number of instances of each resource, size = m + * @param maxArray[][] int matrix(2-D array) of maximum demand of each process in a system, size = n*m + * @param allocationArray[][] int matrix(2-D array) of the number of resources of each type currently allocated to each process, size = n*m + * @param totalProcess number of total processes, n + * @param totalResources number of total resources, m + * + * @return boolean if the system is in safe state or not + */ + static boolean checkSafeSystem(int processes[], int availableArray[], int maxArray[][], int allocationArray[][], int totalProcess, int totalResources) + { + int [][]needArray = new int[totalProcess][totalResources]; + + calculateNeed(needArray, maxArray, allocationArray, totalProcess, totalResources); + + boolean []finishProcesses = new boolean[totalProcess]; + + int []safeSequenceArray = new int[totalProcess]; + + int []workArray = new int[totalResources]; + + for (int i = 0; i < totalResources ; i++) + workArray[i] = availableArray[i]; + + int count = 0; + + // While all processes are not finished or system is not in safe state. + while (count < totalProcess) + { + boolean foundSafeSystem = false; + for (int m = 0; m < totalProcess; m++) + { + if (finishProcesses[m] == false) + { + int j; + + for (j = 0; j < totalResources; j++) + if (needArray[m][j] > workArray[j]) + break; + + if (j == totalResources) + { + for (int k = 0 ; k < totalResources ; k++) + workArray[k] += allocationArray[m][k]; + + safeSequenceArray[count++] = m; + + finishProcesses[m] = true; + + foundSafeSystem = true; + } + } + } + + // If we could not find a next process in safe sequence. + if (foundSafeSystem == false) + { + System.out.print("The system is not in the safe state because lack of resources"); + return false; + } + } + + System.out.print("The system is in safe sequence and the sequence is as follows: "); + for (int i = 0; i < totalProcess ; i++) + System.out.print("P"+safeSequenceArray[i] + " "); + + return true; + } + + /** + * This is main method of Banker's Algorithm + */ + public static void main(String[] args){ + int numberOfProcesses, numberOfResources; + + Scanner sc = new Scanner(System.in); + + System.out.println("Enter total number of processes"); + numberOfProcesses = sc.nextInt(); + + System.out.println("Enter total number of resources"); + numberOfResources = sc.nextInt(); + + int processes[] = new int[numberOfProcesses]; + for(int i = 0; i < numberOfProcesses; i++){ + processes[i] = i; + } + + System.out.println("--Enter the availability of--"); + + int availableArray[] = new int[numberOfResources]; + for( int i = 0; i < numberOfResources; i++){ + System.out.println("resource "+ i +": "); + availableArray[i] = sc.nextInt(); + } + + System.out.println("--Enter the maximum matrix--"); + + int maxArray[][] = new int[numberOfProcesses][numberOfResources]; + for( int i = 0; i < numberOfProcesses; i++){ + System.out.println("For process "+ i + ": "); + for( int j = 0; j < numberOfResources; j++){ + System.out.println("Enter the maximum instances of resource "+ j); + maxArray[i][j] = sc.nextInt(); + } + } + + System.out.println("--Enter the allocation matrix--"); + + int allocationArray[][] = new int[numberOfProcesses][numberOfResources]; + for( int i = 0; i < numberOfProcesses; i++){ + System.out.println("For process "+ i + ": "); + for( int j = 0; j < numberOfResources; j++){ + System.out.println("Allocated instances of resource "+ j ); + allocationArray[i][j] = sc.nextInt(); + } + } + + checkSafeSystem(processes, availableArray, maxArray, allocationArray, numberOfProcesses, numberOfResources); + + sc.close(); + } +} + +/* + Example: + n = 5 + m = 3 + + Process Allocation Max Available + 0 1 2 0 1 2 0 1 2 + + 0 0 1 0 7 5 3 3 3 2 + 1 2 0 0 3 2 2 + 2 3 0 2 9 0 2 + 3 2 1 1 2 2 2 + 4 0 0 2 4 3 3 + + Result: The system is in safe sequence and the sequence is as follows: P1, P3, P4, P0, P2 + */