From b3e265ec43c5563c6a9c602e60f14a5520d0ef7b Mon Sep 17 00:00:00 2001 From: Anirudh Buvanesh Date: Thu, 14 Oct 2021 18:14:30 +0530 Subject: [PATCH 1/5] Added matrix exponentiation based implementation of Fibonacci numbers --- MatrixExponentiation/Fibonacci.java | 71 +++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 MatrixExponentiation/Fibonacci.java diff --git a/MatrixExponentiation/Fibonacci.java b/MatrixExponentiation/Fibonacci.java new file mode 100644 index 000000000000..2979c88d73a7 --- /dev/null +++ b/MatrixExponentiation/Fibonacci.java @@ -0,0 +1,71 @@ +package MatrixExponentiation; + +import java.util.Scanner; + +/** @author Anirudh Buvanesh (https://github.com/anirudhb11) */ +public class Fibonacci { + // Exponentiation matrix for Fibonacci sequence + private static final int [][] fibMatrix = {{1,1}, {1,0}}; + private static final int [][] identityMatrix = {{1,0}, {0,1}}; + //First 2 fibonacci numbers + private static final int [][] baseFibNumbers = {{1}, {0}}; + + /** + * Performs multiplication of 2 matrices + * @param matrix1 + * @param matrix2 + * @return The product of matrix1 and matrix2 + */ + + private static int[][] matrixMultiplication(int[][] matrix1, int[][] matrix2){ + //Check if matrices passed can be multiplied + int rowsInMatrix1 = matrix1.length; + int columnsInMatrix1 = matrix1[0].length; + + int rowsInMatrix2 = matrix2.length; + int columnsInMatrix2 = matrix2[0].length; + + assert columnsInMatrix1 == rowsInMatrix2; + int [][] product = new int[rowsInMatrix1][columnsInMatrix2]; + for (int rowIndex = 0; rowIndex < rowsInMatrix1; rowIndex ++){ + for(int colIndex = 0; colIndex < columnsInMatrix2; colIndex++){ + int matrixEntry = 0; + for(int intermediateIndex = 0; intermediateIndex < columnsInMatrix1; intermediateIndex++){ + matrixEntry += matrix1[rowIndex][intermediateIndex] * matrix2[intermediateIndex][colIndex]; + } + product[rowIndex][colIndex] = matrixEntry; + } + } + return product; + } + + /** + * Calculates the fibonacci number using matrix exponentiaition technique + * @param n The input n for which we have to determine the fibonacci number Outputs the nth + * * fibonacci number + * @return a 2 X 1 array as { {F_n+1}, {F_n} } + */ + public static int[][] fib(int n){ + if(n == 0){ + return Fibonacci.identityMatrix; + } + else{ + int [][] matrixExpResult = matrixMultiplication(fib(n/2), fib(n/2)); + if(n%2 == 0){ + return matrixExpResult; + } + else{ + return matrixMultiplication(Fibonacci.fibMatrix, matrixExpResult); + } + } + } + + public static void main(String[] args) { + // Returns [0, 1, 1, 2, 3, 5 ..] for n = [0, 1, 2, 3, 4, 5.. ] + Scanner sc = new Scanner(System.in); + int n = sc.nextInt(); + int [][] result = matrixMultiplication(fib(n), baseFibNumbers); + System.out.println("Fib(" + n + ") = "+ result[1][0] ); + sc.close(); + } +} From bfd9fc4ba31cf3eaf6bfde859fde194e45c81cfc Mon Sep 17 00:00:00 2001 From: Anirudh Buvanesh Date: Thu, 14 Oct 2021 19:04:41 +0530 Subject: [PATCH 2/5] Added references --- MatrixExponentiation/Fibonacci.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/MatrixExponentiation/Fibonacci.java b/MatrixExponentiation/Fibonacci.java index 2979c88d73a7..3398ebe56df0 100644 --- a/MatrixExponentiation/Fibonacci.java +++ b/MatrixExponentiation/Fibonacci.java @@ -2,7 +2,9 @@ import java.util.Scanner; -/** @author Anirudh Buvanesh (https://github.com/anirudhb11) */ +/** @author Anirudh Buvanesh (https://github.com/anirudhb11) + * For more information see https://www.geeksforgeeks.org/matrix-exponentiation/ + * */ public class Fibonacci { // Exponentiation matrix for Fibonacci sequence private static final int [][] fibMatrix = {{1,1}, {1,0}}; From 8295b6325ffcb0e15c9883f2c30a0807857b2d74 Mon Sep 17 00:00:00 2001 From: Anirudh Buvanesh Date: Sat, 16 Oct 2021 15:35:47 +0530 Subject: [PATCH 3/5] Added Fenwick Tree Implementation --- DataStructures/Trees/FenwickTree.java | 71 +++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 DataStructures/Trees/FenwickTree.java diff --git a/DataStructures/Trees/FenwickTree.java b/DataStructures/Trees/FenwickTree.java new file mode 100644 index 000000000000..8cfb1cc5878f --- /dev/null +++ b/DataStructures/Trees/FenwickTree.java @@ -0,0 +1,71 @@ +package DataStructures.Trees; + +/** @author Anirudh Buvanesh (https://github.com/anirudhb11) + * For more information see https://www.geeksforgeeks.org/binary-indexed-tree-or-fenwick-tree-2/ + * */ +public class FenwickTree { + + static final int MAX = 100; + static int[] fenwickTree = new int [MAX]; + + /** + * @param index + * @return Prefix sum of the array from 0 to index + */ + public int getSum(int index){ + int sum = 0; + index = index + 1; + while(index > 0){ + sum += fenwickTree[index]; + index -= index & (-index); + } + return sum; + } + + /** + * + * @param n Number of elements in the array + * @param index The index of the fenwickTree[] that needs to be modified + * @param delta The value that should be added to index of fenwickTree[] + */ + public void updateElement(int n, int index, int delta){ + index = index + 1; + while(index <= n){ + fenwickTree[index] += delta; + index += index & (-index); + } + } + + /** + * + * @param arr Array for which Fenwick Tree is built + * @param n Number of elements in the array + */ + public void constructFenwickTree(int[] arr, int n){ + for(int i=0;i<=n;i++){ + fenwickTree[i] = 0; + } + for(int i=0;i Date: Sat, 16 Oct 2021 15:40:49 +0530 Subject: [PATCH 4/5] Revert "Added Fenwick Tree Implementation" This reverts commit 8295b6325ffcb0e15c9883f2c30a0807857b2d74. --- DataStructures/Trees/FenwickTree.java | 71 --------------------------- 1 file changed, 71 deletions(-) delete mode 100644 DataStructures/Trees/FenwickTree.java diff --git a/DataStructures/Trees/FenwickTree.java b/DataStructures/Trees/FenwickTree.java deleted file mode 100644 index 8cfb1cc5878f..000000000000 --- a/DataStructures/Trees/FenwickTree.java +++ /dev/null @@ -1,71 +0,0 @@ -package DataStructures.Trees; - -/** @author Anirudh Buvanesh (https://github.com/anirudhb11) - * For more information see https://www.geeksforgeeks.org/binary-indexed-tree-or-fenwick-tree-2/ - * */ -public class FenwickTree { - - static final int MAX = 100; - static int[] fenwickTree = new int [MAX]; - - /** - * @param index - * @return Prefix sum of the array from 0 to index - */ - public int getSum(int index){ - int sum = 0; - index = index + 1; - while(index > 0){ - sum += fenwickTree[index]; - index -= index & (-index); - } - return sum; - } - - /** - * - * @param n Number of elements in the array - * @param index The index of the fenwickTree[] that needs to be modified - * @param delta The value that should be added to index of fenwickTree[] - */ - public void updateElement(int n, int index, int delta){ - index = index + 1; - while(index <= n){ - fenwickTree[index] += delta; - index += index & (-index); - } - } - - /** - * - * @param arr Array for which Fenwick Tree is built - * @param n Number of elements in the array - */ - public void constructFenwickTree(int[] arr, int n){ - for(int i=0;i<=n;i++){ - fenwickTree[i] = 0; - } - for(int i=0;i Date: Wed, 20 Oct 2021 21:13:18 +0530 Subject: [PATCH 5/5] Memoized intermediate result --- MatrixExponentiation/Fibonacci.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/MatrixExponentiation/Fibonacci.java b/MatrixExponentiation/Fibonacci.java index 3398ebe56df0..bdcc06816db8 100644 --- a/MatrixExponentiation/Fibonacci.java +++ b/MatrixExponentiation/Fibonacci.java @@ -52,7 +52,8 @@ public static int[][] fib(int n){ return Fibonacci.identityMatrix; } else{ - int [][] matrixExpResult = matrixMultiplication(fib(n/2), fib(n/2)); + int [][] cachedResult = fib(n/2); + int [][] matrixExpResult = matrixMultiplication(cachedResult, cachedResult); if(n%2 == 0){ return matrixExpResult; }