diff --git a/README.md b/README.md index fe580f1d..b806ae53 100644 --- a/README.md +++ b/README.md @@ -57,16 +57,17 @@ a set of rules that precisely define a sequence of operations. * `B` [Primality Test](src/algorithms/math/primality-test) (trial division method) * `B` [Euclidean Algorithm](src/algorithms/math/euclidean-algorithm) - calculate the Greatest Common Divisor (GCD) * `B` [Least Common Multiple](src/algorithms/math/least-common-multiple) (LCM) - * `A` [Integer Partition](src/algorithms/math/integer-partition) * `B` [Sieve of Eratosthenes](src/algorithms/math/sieve-of-eratosthenes) - finding all prime numbers up to any given limit * `B` [Is Power of Two](src/algorithms/math/is-power-of-two) - check if the number is power of two (naive and bitwise algorithms) + * `B` [Pascal's Triangle](src/algorithms/math/pascal-triangle) + * `A` [Integer Partition](src/algorithms/math/integer-partition) * `A` [Liu Hui π Algorithm](src/algorithms/math/liu-hui) - approximate π calculations based on N-gons * **Sets** * `B` [Cartesian Product](src/algorithms/sets/cartesian-product) - product of multiple sets + * `B` [Fisher–Yates Shuffle](src/algorithms/sets/fisher-yates) - random permutation of a finite sequence * `A` [Power Set](src/algorithms/sets/power-set) - all subsets of a set * `A` [Permutations](src/algorithms/sets/permutations) (with and without repetitions) * `A` [Combinations](src/algorithms/sets/combinations) (with and without repetitions) - * `B` [Fisher–Yates Shuffle](src/algorithms/sets/fisher-yates) - random permutation of a finite sequence * `A` [Longest Common Subsequence](src/algorithms/sets/longest-common-subsequence) (LCS) * `A` [Longest Increasing Subsequence](src/algorithms/sets/longest-increasing-subsequence) * `A` [Shortest Common Supersequence](src/algorithms/sets/shortest-common-supersequence) (SCS) @@ -74,8 +75,8 @@ a set of rules that precisely define a sequence of operations. * `A` [Maximum Subarray](src/algorithms/sets/maximum-subarray) - "Brute Force" and "Dynamic Programming" (Kadane's) versions * `A` [Combination Sum](src/algorithms/sets/combination-sum) - find all combinations that form specific sum * **Strings** - * `A` [Levenshtein Distance](src/algorithms/string/levenshtein-distance) - minimum edit distance between two sequences * `B` [Hamming Distance](src/algorithms/string/hamming-distance) - number of positions at which the symbols are different + * `A` [Levenshtein Distance](src/algorithms/string/levenshtein-distance) - minimum edit distance between two sequences * `A` [Knuth–Morris–Pratt Algorithm](src/algorithms/string/knuth-morris-pratt) (KMP Algorithm) - substring search (pattern matching) * `A` [Z Algorithm](src/algorithms/string/z-algorithm) - substring search (pattern matching) * `A` [Rabin Karp Algorithm](src/algorithms/string/rabin-karp) - substring search @@ -100,11 +101,11 @@ a set of rules that precisely define a sequence of operations. * **Graphs** * `B` [Depth-First Search](src/algorithms/graph/depth-first-search) (DFS) * `B` [Breadth-First Search](src/algorithms/graph/breadth-first-search) (BFS) + * `B` [Kruskal’s Algorithm](src/algorithms/graph/kruskal) - finding Minimum Spanning Tree (MST) for weighted undirected graph * `A` [Dijkstra Algorithm](src/algorithms/graph/dijkstra) - finding shortest path to all graph vertices * `A` [Bellman-Ford Algorithm](src/algorithms/graph/bellman-ford) - finding shortest path to all graph vertices * `A` [Detect Cycle](src/algorithms/graph/detect-cycle) - for both directed and undirected graphs (DFS and Disjoint Set based versions) * `A` [Prim’s Algorithm](src/algorithms/graph/prim) - finding Minimum Spanning Tree (MST) for weighted undirected graph - * `B` [Kruskal’s Algorithm](src/algorithms/graph/kruskal) - finding Minimum Spanning Tree (MST) for weighted undirected graph * `A` [Topological Sorting](src/algorithms/graph/topological-sorting) - DFS method * `A` [Articulation Points](src/algorithms/graph/articulation-points) - Tarjan's algorithm (DFS based) * `A` [Bridges](src/algorithms/graph/bridges) - DFS based algorithm @@ -114,9 +115,9 @@ a set of rules that precisely define a sequence of operations. * `A` [Travelling Salesman Problem](src/algorithms/graph/travelling-salesman) - shortest possible route that visits each city and returns to the origin city * **Uncategorized** * `B` [Tower of Hanoi](src/algorithms/uncategorized/hanoi-tower) + * `B` [Square Matrix Rotation](src/algorithms/uncategorized/square-matrix-rotation) - in-place algorithm * `A` [N-Queens Problem](src/algorithms/uncategorized/n-queens) * `A` [Knight's Tour](src/algorithms/uncategorized/knight-tour) - * `B` [Square Matrix Rotation](src/algorithms/uncategorized/square-matrix-rotation) - in-place algorithm ### Algorithms by Paradigm @@ -135,13 +136,14 @@ algorithm is an abstraction higher than a computer program. * **Divide and Conquer** - divide the problem into smaller parts and then solve those parts * `B` [Binary Search](src/algorithms/search/binary-search) * `B` [Tower of Hanoi](src/algorithms/uncategorized/hanoi-tower) + * `B` [Pascal's Triangle](src/algorithms/math/pascal-triangle) * `B` [Euclidean Algorithm](src/algorithms/math/euclidean-algorithm) - calculate the Greatest Common Divisor (GCD) - * `A` [Permutations](src/algorithms/sets/permutations) (with and without repetitions) - * `A` [Combinations](src/algorithms/sets/combinations) (with and without repetitions) * `B` [Merge Sort](src/algorithms/sorting/merge-sort) * `B` [Quicksort](src/algorithms/sorting/quick-sort) * `B` [Tree Depth-First Search](src/algorithms/tree/depth-first-search) (DFS) * `B` [Graph Depth-First Search](src/algorithms/graph/depth-first-search) (DFS) + * `A` [Permutations](src/algorithms/sets/permutations) (with and without repetitions) + * `A` [Combinations](src/algorithms/sets/combinations) (with and without repetitions) * **Dynamic Programming** - build up a solution using previously found sub-solutions * `B` [Fibonacci Number](src/algorithms/math/fibonacci) * `A` [Levenshtein Distance](src/algorithms/string/levenshtein-distance) - minimum edit distance between two sequences diff --git a/src/algorithms/math/pascal-triangle/README.md b/src/algorithms/math/pascal-triangle/README.md new file mode 100644 index 00000000..523a9792 --- /dev/null +++ b/src/algorithms/math/pascal-triangle/README.md @@ -0,0 +1,39 @@ +# Pascal's Triangle + +In mathematics, **Pascal's triangle** is a triangular array of +the binomial coefficients. + +The rows of Pascal's triangle are conventionally enumerated +starting with row `n = 0` at the top (the `0th` row). The +entries in each row are numbered from the left beginning +with `k = 0` and are usually staggered relative to the +numbers in the adjacent rows. The triangle may be constructed +in the following manner: In row `0` (the topmost row), there +is a unique nonzero entry `1`. Each entry of each subsequent +row is constructed by adding the number above and to the +left with the number above and to the right, treating blank +entries as `0`. For example, the initial number in the +first (or any other) row is `1` (the sum of `0` and `1`), +whereas the numbers `1` and `3` in the third row are added +to produce the number `4` in the fourth row. + +![Pascal's Triangle](https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif) + +## Formula + +The entry in the `nth` row and `kth` column of Pascal's +triangle is denoted ![Formula](https://wikimedia.org/api/rest_v1/media/math/render/svg/206415d3742167e319b2e52c2ca7563b799abad7). +For example, the unique nonzero entry in the topmost +row is ![Formula example](https://wikimedia.org/api/rest_v1/media/math/render/svg/b7e35f86368d5978b46c07fd6dddca86bd6e635c). + +With this notation, the construction of the previous +paragraph may be written as follows: + +![Formula](https://wikimedia.org/api/rest_v1/media/math/render/svg/203b128a098e18cbb8cf36d004bd7282b28461bf) + +for any non-negative integer `n` and any +integer `k` between `0` and `n`, inclusive. + +## References + +- [Wikipedia](https://en.wikipedia.org/wiki/Pascal%27s_triangle) diff --git a/src/algorithms/math/pascal-triangle/__test__/pascalTriangleRecursive.test.js b/src/algorithms/math/pascal-triangle/__test__/pascalTriangleRecursive.test.js new file mode 100644 index 00000000..2dfdad6b --- /dev/null +++ b/src/algorithms/math/pascal-triangle/__test__/pascalTriangleRecursive.test.js @@ -0,0 +1,14 @@ +import pascalTriangleRecursive from '../pascalTriangleRecursive'; + +describe('pascalTriangleRecursive', () => { + it('should calculate Pascal Triangle coefficients for specific line number', () => { + expect(pascalTriangleRecursive(0)).toEqual([1]); + expect(pascalTriangleRecursive(1)).toEqual([1, 1]); + expect(pascalTriangleRecursive(2)).toEqual([1, 2, 1]); + expect(pascalTriangleRecursive(3)).toEqual([1, 3, 3, 1]); + expect(pascalTriangleRecursive(4)).toEqual([1, 4, 6, 4, 1]); + expect(pascalTriangleRecursive(5)).toEqual([1, 5, 10, 10, 5, 1]); + expect(pascalTriangleRecursive(6)).toEqual([1, 6, 15, 20, 15, 6, 1]); + expect(pascalTriangleRecursive(7)).toEqual([1, 7, 21, 35, 35, 21, 7, 1]); + }); +}); diff --git a/src/algorithms/math/pascal-triangle/pascalTriangleRecursive.js b/src/algorithms/math/pascal-triangle/pascalTriangleRecursive.js new file mode 100644 index 00000000..4e9d4dc8 --- /dev/null +++ b/src/algorithms/math/pascal-triangle/pascalTriangleRecursive.js @@ -0,0 +1,30 @@ +/** + * @param {number} lineNumber + * @return {number[]} + */ +export default function pascalTriangleRecursive(lineNumber) { + if (lineNumber === 0) { + return [1]; + } + + const currentLineSize = lineNumber + 1; + const previousLineSize = currentLineSize - 1; + + // Create container for current line values. + const currentLine = []; + + // We'll calculate current line based on previous one. + const previousLine = pascalTriangleRecursive(lineNumber - 1); + + // Let's go through all elements of current line except the first and + // last one (since they were and will be filled with 1's) and calculate + // current coefficient based on previous line. + for (let numIndex = 0; numIndex < currentLineSize; numIndex += 1) { + const leftCoefficient = (numIndex - 1) >= 0 ? previousLine[numIndex - 1] : 0; + const rightCoefficient = numIndex < previousLineSize ? previousLine[numIndex] : 0; + + currentLine[numIndex] = leftCoefficient + rightCoefficient; + } + + return currentLine; +}