repo
stringlengths
5
106
file_url
stringlengths
78
301
file_path
stringlengths
4
211
content
stringlengths
0
32.8k
language
stringclasses
1 value
license
stringclasses
7 values
commit_sha
stringlengths
40
40
retrieved_at
stringdate
2026-01-04 14:56:49
2026-01-05 02:23:25
truncated
bool
2 classes
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/search/jump-search/__test__/jumpSearch.test.js
src/algorithms/search/jump-search/__test__/jumpSearch.test.js
import jumpSearch from '../jumpSearch'; describe('jumpSearch', () => { it('should search for an element in sorted array', () => { expect(jumpSearch([], 1)).toBe(-1); expect(jumpSearch([1], 2)).toBe(-1); expect(jumpSearch([1], 1)).toBe(0); expect(jumpSearch([1, 2], 1)).toBe(0); expect(jumpSearch([...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/search/linear-search/linearSearch.js
src/algorithms/search/linear-search/linearSearch.js
import Comparator from '../../../utils/comparator/Comparator'; /** * Linear search implementation. * * @param {*[]} array * @param {*} seekElement * @param {function(a, b)} [comparatorCallback] * @return {number[]} */ export default function linearSearch(array, seekElement, comparatorCallback) { const compara...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/search/linear-search/__test__/linearSearch.test.js
src/algorithms/search/linear-search/__test__/linearSearch.test.js
import linearSearch from '../linearSearch'; describe('linearSearch', () => { it('should search all numbers in array', () => { const array = [1, 2, 4, 6, 2]; expect(linearSearch(array, 10)).toEqual([]); expect(linearSearch(array, 1)).toEqual([0]); expect(linearSearch(array, 2)).toEqual([1, 4]); });...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/search/interpolation-search/interpolationSearch.js
src/algorithms/search/interpolation-search/interpolationSearch.js
/** * Interpolation search implementation. * * @param {*[]} sortedArray - sorted array with uniformly distributed values * @param {*} seekElement * @return {number} */ export default function interpolationSearch(sortedArray, seekElement) { let leftIndex = 0; let rightIndex = sortedArray.length - 1; while (...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/search/interpolation-search/__test__/interpolationSearch.test.js
src/algorithms/search/interpolation-search/__test__/interpolationSearch.test.js
import interpolationSearch from '../interpolationSearch'; describe('interpolationSearch', () => { it('should search elements in sorted array of numbers', () => { expect(interpolationSearch([], 1)).toBe(-1); expect(interpolationSearch([1], 1)).toBe(0); expect(interpolationSearch([1], 0)).toBe(-1); exp...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/statistics/weighted-random/weightedRandom.js
src/algorithms/statistics/weighted-random/weightedRandom.js
/** * Picks the random item based on its weight. * The items with higher weight will be picked more often (with a higher probability). * * For example: * - items = ['banana', 'orange', 'apple'] * - weights = [0, 0.2, 0.8] * - weightedRandom(items, weights) in 80% of cases will return 'apple', in 20% of cases wil...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/statistics/weighted-random/__test__/weightedRandom.test.js
src/algorithms/statistics/weighted-random/__test__/weightedRandom.test.js
import weightedRandom from '../weightedRandom'; describe('weightedRandom', () => { it('should throw an error when the number of weights does not match the number of items', () => { const getWeightedRandomWithInvalidInputs = () => { weightedRandom(['a', 'b', 'c'], [10, 0]); }; expect(getWeightedRand...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sorting/Sort.js
src/algorithms/sorting/Sort.js
import Comparator from '../../utils/comparator/Comparator'; /** * @typedef {Object} SorterCallbacks * @property {function(a: *, b: *)} compareCallback - If provided then all elements comparisons * will be done through this callback. * @property {function(a: *)} visitingCallback - If provided it will be called eac...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sorting/SortTester.js
src/algorithms/sorting/SortTester.js
export const sortedArr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]; export const reverseArr = [20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]; export const notSortedArr = [15, 8, 5, 12, 10, 1, 16, 9, 11, 7, 20, 3, 2, 6, 17, 18, 4, 13, 14, 19]; export const equalArr ...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sorting/insertion-sort/InsertionSort.js
src/algorithms/sorting/insertion-sort/InsertionSort.js
import Sort from '../Sort'; export default class InsertionSort extends Sort { sort(originalArray) { const array = [...originalArray]; // Go through all array elements... for (let i = 1; i < array.length; i += 1) { let currentIndex = i; // Call visiting callback. this.callbacks.visitin...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sorting/insertion-sort/__test__/InsertionSort.test.js
src/algorithms/sorting/insertion-sort/__test__/InsertionSort.test.js
import InsertionSort from '../InsertionSort'; import { equalArr, notSortedArr, reverseArr, sortedArr, SortTester, } from '../../SortTester'; // Complexity constants. const SORTED_ARRAY_VISITING_COUNT = 19; const NOT_SORTED_ARRAY_VISITING_COUNT = 100; const REVERSE_SORTED_ARRAY_VISITING_COUNT = 209; const EQU...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sorting/__test__/Sort.test.js
src/algorithms/sorting/__test__/Sort.test.js
import Sort from '../Sort'; describe('Sort', () => { it('should throw an error when trying to call Sort.sort() method directly', () => { function doForbiddenSort() { const sorter = new Sort(); sorter.sort(); } expect(doForbiddenSort).toThrow(); }); });
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sorting/radix-sort/RadixSort.js
src/algorithms/sorting/radix-sort/RadixSort.js
import Sort from '../Sort'; // Using charCode (a = 97, b = 98, etc), we can map characters to buckets from 0 - 25 const BASE_CHAR_CODE = 97; const NUMBER_OF_POSSIBLE_DIGITS = 10; const ENGLISH_ALPHABET_LENGTH = 26; export default class RadixSort extends Sort { /** * @param {*[]} originalArray * @return {*[]} ...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sorting/radix-sort/__test__/RadixSort.test.js
src/algorithms/sorting/radix-sort/__test__/RadixSort.test.js
import RadixSort from '../RadixSort'; import { SortTester } from '../../SortTester'; // Complexity constants. const ARRAY_OF_STRINGS_VISIT_COUNT = 24; const ARRAY_OF_INTEGERS_VISIT_COUNT = 77; describe('RadixSort', () => { it('should sort array', () => { SortTester.testSort(RadixSort); }); it('should visit ...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sorting/merge-sort/MergeSort.js
src/algorithms/sorting/merge-sort/MergeSort.js
import Sort from '../Sort'; export default class MergeSort extends Sort { sort(originalArray) { // Call visiting callback. this.callbacks.visitingCallback(null); // If array is empty or consists of one element then return this array since it is sorted. if (originalArray.length <= 1) { return o...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sorting/merge-sort/__test__/MergeSort.test.js
src/algorithms/sorting/merge-sort/__test__/MergeSort.test.js
import MergeSort from '../MergeSort'; import { equalArr, notSortedArr, reverseArr, sortedArr, SortTester, } from '../../SortTester'; // Complexity constants. const SORTED_ARRAY_VISITING_COUNT = 79; const NOT_SORTED_ARRAY_VISITING_COUNT = 102; const REVERSE_SORTED_ARRAY_VISITING_COUNT = 87; const EQUAL_ARRAY_...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sorting/shell-sort/ShellSort.js
src/algorithms/sorting/shell-sort/ShellSort.js
import Sort from '../Sort'; export default class ShellSort extends Sort { sort(originalArray) { // Prevent original array from mutations. const array = [...originalArray]; // Define a gap distance. let gap = Math.floor(array.length / 2); // Until gap is bigger then zero do elements comparisons ...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sorting/shell-sort/__test__/ShellSort.test.js
src/algorithms/sorting/shell-sort/__test__/ShellSort.test.js
import ShellSort from '../ShellSort'; import { equalArr, notSortedArr, reverseArr, sortedArr, SortTester, } from '../../SortTester'; // Complexity constants. const SORTED_ARRAY_VISITING_COUNT = 320; const NOT_SORTED_ARRAY_VISITING_COUNT = 320; const REVERSE_SORTED_ARRAY_VISITING_COUNT = 320; const EQUAL_ARRA...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sorting/counting-sort/CountingSort.js
src/algorithms/sorting/counting-sort/CountingSort.js
import Sort from '../Sort'; export default class CountingSort extends Sort { /** * @param {number[]} originalArray * @param {number} [smallestElement] * @param {number} [biggestElement] */ sort(originalArray, smallestElement = undefined, biggestElement = undefined) { // Init biggest and smallest el...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sorting/counting-sort/__test__/CountingSort.test.js
src/algorithms/sorting/counting-sort/__test__/CountingSort.test.js
import CountingSort from '../CountingSort'; import { equalArr, notSortedArr, reverseArr, sortedArr, SortTester, } from '../../SortTester'; // Complexity constants. const SORTED_ARRAY_VISITING_COUNT = 60; const NOT_SORTED_ARRAY_VISITING_COUNT = 60; const REVERSE_SORTED_ARRAY_VISITING_COUNT = 60; const EQUAL_A...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sorting/quick-sort/QuickSort.js
src/algorithms/sorting/quick-sort/QuickSort.js
import Sort from '../Sort'; export default class QuickSort extends Sort { /** * @param {*[]} originalArray * @return {*[]} */ sort(originalArray) { // Clone original array to prevent it from modification. const array = [...originalArray]; // If array has less than or equal to one elements the...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sorting/quick-sort/QuickSortInPlace.js
src/algorithms/sorting/quick-sort/QuickSortInPlace.js
import Sort from '../Sort'; export default class QuickSortInPlace extends Sort { /** Sorting in place avoids unnecessary use of additional memory, but modifies input array. * * This process is difficult to describe, but much clearer with a visualization: * @see: https://www.hackerearth.com/practice/algorith...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sorting/quick-sort/__test__/QuickSort.test.js
src/algorithms/sorting/quick-sort/__test__/QuickSort.test.js
import QuickSort from '../QuickSort'; import { equalArr, notSortedArr, reverseArr, sortedArr, SortTester, } from '../../SortTester'; // Complexity constants. const SORTED_ARRAY_VISITING_COUNT = 190; const NOT_SORTED_ARRAY_VISITING_COUNT = 62; const REVERSE_SORTED_ARRAY_VISITING_COUNT = 190; const EQUAL_ARRAY...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sorting/quick-sort/__test__/QuickSortInPlace.test.js
src/algorithms/sorting/quick-sort/__test__/QuickSortInPlace.test.js
import QuickSortInPlace from '../QuickSortInPlace'; import { equalArr, notSortedArr, reverseArr, sortedArr, SortTester, } from '../../SortTester'; // Complexity constants. const SORTED_ARRAY_VISITING_COUNT = 19; const NOT_SORTED_ARRAY_VISITING_COUNT = 12; const REVERSE_SORTED_ARRAY_VISITING_COUNT = 19; const...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sorting/bucket-sort/BucketSort.js
src/algorithms/sorting/bucket-sort/BucketSort.js
import RadixSort from '../radix-sort/RadixSort'; /** * Bucket Sort * * @param {number[]} arr * @param {number} bucketsNum * @return {number[]} */ export default function BucketSort(arr, bucketsNum = 1) { const buckets = new Array(bucketsNum).fill(null).map(() => []); const minValue = Math.min(...arr); con...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sorting/bucket-sort/__test__/BucketSort.test.js
src/algorithms/sorting/bucket-sort/__test__/BucketSort.test.js
import BucketSort from '../BucketSort'; import { equalArr, notSortedArr, reverseArr, sortedArr, } from '../../SortTester'; describe('BucketSort', () => { it('should sort the array of numbers with different buckets amounts', () => { expect(BucketSort(notSortedArr, 4)).toEqual(sortedArr); expect(Bucket...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sorting/selection-sort/SelectionSort.js
src/algorithms/sorting/selection-sort/SelectionSort.js
import Sort from '../Sort'; export default class SelectionSort extends Sort { sort(originalArray) { // Clone original array to prevent its modification. const array = [...originalArray]; for (let i = 0; i < array.length - 1; i += 1) { let minIndex = i; // Call visiting callback. this....
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sorting/selection-sort/__test__/SelectionSort.test.js
src/algorithms/sorting/selection-sort/__test__/SelectionSort.test.js
import SelectionSort from '../SelectionSort'; import { equalArr, notSortedArr, reverseArr, sortedArr, SortTester, } from '../../SortTester'; // Complexity constants. const SORTED_ARRAY_VISITING_COUNT = 209; const NOT_SORTED_ARRAY_VISITING_COUNT = 209; const REVERSE_SORTED_ARRAY_VISITING_COUNT = 209; const EQ...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sorting/bubble-sort/BubbleSort.js
src/algorithms/sorting/bubble-sort/BubbleSort.js
import Sort from '../Sort'; export default class BubbleSort extends Sort { sort(originalArray) { // Flag that holds info about whether the swap has occur or not. let swapped = false; // Clone original array to prevent its modification. const array = [...originalArray]; for (let i = 1; i < array....
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sorting/bubble-sort/__test__/BubbleSort.test.js
src/algorithms/sorting/bubble-sort/__test__/BubbleSort.test.js
import BubbleSort from '../BubbleSort'; import { equalArr, notSortedArr, reverseArr, sortedArr, SortTester, } from '../../SortTester'; // Complexity constants. const SORTED_ARRAY_VISITING_COUNT = 20; const NOT_SORTED_ARRAY_VISITING_COUNT = 189; const REVERSE_SORTED_ARRAY_VISITING_COUNT = 209; const EQUAL_ARR...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sorting/heap-sort/HeapSort.js
src/algorithms/sorting/heap-sort/HeapSort.js
import Sort from '../Sort'; import MinHeap from '../../../data-structures/heap/MinHeap'; export default class HeapSort extends Sort { sort(originalArray) { const sortedArray = []; const minHeap = new MinHeap(this.callbacks.compareCallback); // Insert all array elements to the heap. originalArray.for...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sorting/heap-sort/__test__/HeapSort.test.js
src/algorithms/sorting/heap-sort/__test__/HeapSort.test.js
import HeapSort from '../HeapSort'; import { equalArr, notSortedArr, reverseArr, sortedArr, SortTester, } from '../../SortTester'; // Complexity constants. // These numbers don't take into account up/dow heapifying of the heap. // Thus these numbers are higher in reality. const SORTED_ARRAY_VISITING_COUNT = ...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/tree/breadth-first-search/breadthFirstSearch.js
src/algorithms/tree/breadth-first-search/breadthFirstSearch.js
import Queue from '../../../data-structures/queue/Queue'; /** * @typedef {Object} Callbacks * @property {function(node: BinaryTreeNode, child: BinaryTreeNode): boolean} allowTraversal - * Determines whether BFS should traverse from the node to its child. * @property {function(node: BinaryTreeNode)} enterNode - C...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/tree/breadth-first-search/__test__/breadthFirstSearch.test.js
src/algorithms/tree/breadth-first-search/__test__/breadthFirstSearch.test.js
import BinaryTreeNode from '../../../../data-structures/tree/BinaryTreeNode'; import breadthFirstSearch from '../breadthFirstSearch'; describe('breadthFirstSearch', () => { it('should perform BFS operation on tree', () => { const nodeA = new BinaryTreeNode('A'); const nodeB = new BinaryTreeNode('B'); con...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/tree/depth-first-search/depthFirstSearch.js
src/algorithms/tree/depth-first-search/depthFirstSearch.js
/** * @typedef {Object} TraversalCallbacks * * @property {function(node: BinaryTreeNode, child: BinaryTreeNode): boolean} allowTraversal * - Determines whether DFS should traverse from the node to its child. * * @property {function(node: BinaryTreeNode)} enterNode - Called when DFS enters the node. * * @propert...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/tree/depth-first-search/__test__/depthFirstSearch.test.js
src/algorithms/tree/depth-first-search/__test__/depthFirstSearch.test.js
import BinaryTreeNode from '../../../../data-structures/tree/BinaryTreeNode'; import depthFirstSearch from '../depthFirstSearch'; describe('depthFirstSearch', () => { it('should perform DFS operation on tree', () => { const nodeA = new BinaryTreeNode('A'); const nodeB = new BinaryTreeNode('B'); const nod...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/uncategorized/n-queens/QueenPosition.js
src/algorithms/uncategorized/n-queens/QueenPosition.js
/** * Class that represents queen position on the chessboard. */ export default class QueenPosition { /** * @param {number} rowIndex * @param {number} columnIndex */ constructor(rowIndex, columnIndex) { this.rowIndex = rowIndex; this.columnIndex = columnIndex; } /** * @return {number} ...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/uncategorized/n-queens/nQueensBitwise.js
src/algorithms/uncategorized/n-queens/nQueensBitwise.js
/** * Checks all possible board configurations. * * @param {number} boardSize - Size of the squared chess board. * @param {number} leftDiagonal - Sequence of N bits that show whether the corresponding location * on the current row is "available" (no other queens are threatening from left diagonal). * @param {numb...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/uncategorized/n-queens/nQueens.js
src/algorithms/uncategorized/n-queens/nQueens.js
import QueenPosition from './QueenPosition'; /** * @param {QueenPosition[]} queensPositions * @param {number} rowIndex * @param {number} columnIndex * @return {boolean} */ function isSafe(queensPositions, rowIndex, columnIndex) { // New position to which the Queen is going to be placed. const newQueenPosition...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/uncategorized/n-queens/__test__/nQueens.test.js
src/algorithms/uncategorized/n-queens/__test__/nQueens.test.js
import nQueens from '../nQueens'; describe('nQueens', () => { it('should not hae solution for 3 queens', () => { const solutions = nQueens(3); expect(solutions.length).toBe(0); }); it('should solve n-queens problem for 4 queens', () => { const solutions = nQueens(4); expect(solutions.length).toB...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/uncategorized/n-queens/__test__/QueensPosition.test.js
src/algorithms/uncategorized/n-queens/__test__/QueensPosition.test.js
import QueenPosition from '../QueenPosition'; describe('QueenPosition', () => { it('should store queen position on chessboard', () => { const position1 = new QueenPosition(0, 0); const position2 = new QueenPosition(2, 1); expect(position2.columnIndex).toBe(1); expect(position2.rowIndex).toBe(2); ...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/uncategorized/n-queens/__test__/nQueensBitwise.test.js
src/algorithms/uncategorized/n-queens/__test__/nQueensBitwise.test.js
import nQueensBitwise from '../nQueensBitwise'; describe('nQueensBitwise', () => { it('should have solutions for 4 to N queens', () => { expect(nQueensBitwise(4)).toBe(2); expect(nQueensBitwise(5)).toBe(10); expect(nQueensBitwise(6)).toBe(4); expect(nQueensBitwise(7)).toBe(40); expect(nQueensBitw...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/uncategorized/rain-terraces/dpRainTerraces.js
src/algorithms/uncategorized/rain-terraces/dpRainTerraces.js
/** * DYNAMIC PROGRAMMING approach of solving Trapping Rain Water problem. * * @param {number[]} terraces * @return {number} */ export default function dpRainTerraces(terraces) { let waterAmount = 0; // Init arrays that will keep the list of left and right maximum levels for specific positions. const leftMa...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/uncategorized/rain-terraces/bfRainTerraces.js
src/algorithms/uncategorized/rain-terraces/bfRainTerraces.js
/** * BRUTE FORCE approach of solving Trapping Rain Water problem. * * @param {number[]} terraces * @return {number} */ export default function bfRainTerraces(terraces) { let waterAmount = 0; for (let terraceIndex = 0; terraceIndex < terraces.length; terraceIndex += 1) { // Get left most high terrace. ...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/uncategorized/rain-terraces/__test__/bfRainTerraces.test.js
src/algorithms/uncategorized/rain-terraces/__test__/bfRainTerraces.test.js
import bfRainTerraces from '../bfRainTerraces'; describe('bfRainTerraces', () => { it('should find the amount of water collected after raining', () => { expect(bfRainTerraces([1])).toBe(0); expect(bfRainTerraces([1, 0])).toBe(0); expect(bfRainTerraces([0, 1])).toBe(0); expect(bfRainTerraces([0, 1, 0]...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/uncategorized/rain-terraces/__test__/dpRainTerraces.test.js
src/algorithms/uncategorized/rain-terraces/__test__/dpRainTerraces.test.js
import dpRainTerraces from '../dpRainTerraces'; describe('dpRainTerraces', () => { it('should find the amount of water collected after raining', () => { expect(dpRainTerraces([1])).toBe(0); expect(dpRainTerraces([1, 0])).toBe(0); expect(dpRainTerraces([0, 1])).toBe(0); expect(dpRainTerraces([0, 1, 0]...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/uncategorized/square-matrix-rotation/squareMatrixRotation.js
src/algorithms/uncategorized/square-matrix-rotation/squareMatrixRotation.js
/** * @param {*[][]} originalMatrix * @return {*[][]} */ export default function squareMatrixRotation(originalMatrix) { const matrix = originalMatrix.slice(); // Do top-right/bottom-left diagonal reflection of the matrix. for (let rowIndex = 0; rowIndex < matrix.length; rowIndex += 1) { for (let columnInd...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/uncategorized/square-matrix-rotation/__test__/squareMatrixRotation.test.js
src/algorithms/uncategorized/square-matrix-rotation/__test__/squareMatrixRotation.test.js
import squareMatrixRotation from '../squareMatrixRotation'; describe('squareMatrixRotation', () => { it('should rotate matrix #0 in-place', () => { const matrix = [[1]]; const rotatedMatrix = [[1]]; expect(squareMatrixRotation(matrix)).toEqual(rotatedMatrix); }); it('should rotate matrix #1 in-pla...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/uncategorized/hanoi-tower/hanoiTower.js
src/algorithms/uncategorized/hanoi-tower/hanoiTower.js
import Stack from '../../../data-structures/stack/Stack'; /** * @param {number} numberOfDiscs * @param {Stack} fromPole * @param {Stack} withPole * @param {Stack} toPole * @param {function(disc: number, fromPole: number[], toPole: number[])} moveCallback */ function hanoiTowerRecursive({ numberOfDiscs, fromP...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/uncategorized/hanoi-tower/__test__/hanoiTower.test.js
src/algorithms/uncategorized/hanoi-tower/__test__/hanoiTower.test.js
import hanoiTower from '../hanoiTower'; import Stack from '../../../../data-structures/stack/Stack'; describe('hanoiTower', () => { it('should solve tower of hanoi puzzle with 2 discs', () => { const moveCallback = jest.fn(); const numberOfDiscs = 2; const fromPole = new Stack(); const withPole = ne...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/uncategorized/knight-tour/knightTour.js
src/algorithms/uncategorized/knight-tour/knightTour.js
/** * @param {number[][]} chessboard * @param {number[]} position * @return {number[][]} */ function getPossibleMoves(chessboard, position) { // Generate all knight moves (even those that go beyond the board). const possibleMoves = [ [position[0] - 1, position[1] - 2], [position[0] - 2, position[1] - 1]...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/uncategorized/knight-tour/__test__/knightTour.test.js
src/algorithms/uncategorized/knight-tour/__test__/knightTour.test.js
import knightTour from '../knightTour'; describe('knightTour', () => { it('should not find solution on 3x3 board', () => { const moves = knightTour(3); expect(moves.length).toBe(0); }); it('should find one solution to do knight tour on 5x5 board', () => { const moves = knightTour(5); expect(mo...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/uncategorized/best-time-to-buy-sell-stocks/dpBestTimeToBuySellStocks.js
src/algorithms/uncategorized/best-time-to-buy-sell-stocks/dpBestTimeToBuySellStocks.js
/** * Finds the maximum profit from selling and buying the stocks. * DYNAMIC PROGRAMMING APPROACH. * * @param {number[]} prices - Array of stock prices, i.e. [7, 6, 4, 3, 1] * @param {function(): void} visit - Visiting callback to calculate the number of iterations. * @return {number} - The maximum profit */ con...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/uncategorized/best-time-to-buy-sell-stocks/accumulatorBestTimeToBuySellStocks.js
src/algorithms/uncategorized/best-time-to-buy-sell-stocks/accumulatorBestTimeToBuySellStocks.js
/** * Finds the maximum profit from selling and buying the stocks. * ACCUMULATOR APPROACH. * * @param {number[]} prices - Array of stock prices, i.e. [7, 6, 4, 3, 1] * @param {function(): void} visit - Visiting callback to calculate the number of iterations. * @return {number} - The maximum profit */ const accum...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/uncategorized/best-time-to-buy-sell-stocks/dqBestTimeToBuySellStocks.js
src/algorithms/uncategorized/best-time-to-buy-sell-stocks/dqBestTimeToBuySellStocks.js
/** * Finds the maximum profit from selling and buying the stocks. * DIVIDE & CONQUER APPROACH. * * @param {number[]} prices - Array of stock prices, i.e. [7, 6, 4, 3, 1] * @param {function(): void} visit - Visiting callback to calculate the number of iterations. * @return {number} - The maximum profit */ const ...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/uncategorized/best-time-to-buy-sell-stocks/peakvalleyBestTimeToBuySellStocks.js
src/algorithms/uncategorized/best-time-to-buy-sell-stocks/peakvalleyBestTimeToBuySellStocks.js
/** * Finds the maximum profit from selling and buying the stocks. * PEAK VALLEY APPROACH. * * @param {number[]} prices - Array of stock prices, i.e. [7, 6, 4, 3, 1] * @param {function(): void} visit - Visiting callback to calculate the number of iterations. * @return {number} - The maximum profit */ const peakv...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/uncategorized/best-time-to-buy-sell-stocks/__tests__/accumulatorBestTimeToBuySellStocks.test.js
src/algorithms/uncategorized/best-time-to-buy-sell-stocks/__tests__/accumulatorBestTimeToBuySellStocks.test.js
import accumulatorBestTimeToBuySellStocks from '../accumulatorBestTimeToBuySellStocks'; describe('accumulatorBestTimeToBuySellStocks', () => { it('should find the best time to buy and sell stocks', () => { let visit; expect(accumulatorBestTimeToBuySellStocks([1, 5])).toEqual(4); visit = jest.fn(); ...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/uncategorized/best-time-to-buy-sell-stocks/__tests__/dqBestTimeToBuySellStocks.test.js
src/algorithms/uncategorized/best-time-to-buy-sell-stocks/__tests__/dqBestTimeToBuySellStocks.test.js
import dqBestTimeToBuySellStocks from '../dqBestTimeToBuySellStocks'; describe('dqBestTimeToBuySellStocks', () => { it('should find the best time to buy and sell stocks', () => { let visit; expect(dqBestTimeToBuySellStocks([1, 5])).toEqual(4); visit = jest.fn(); expect(dqBestTimeToBuySellStocks([1]...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/uncategorized/best-time-to-buy-sell-stocks/__tests__/dpBestTimeToBuySellStocks.test.js
src/algorithms/uncategorized/best-time-to-buy-sell-stocks/__tests__/dpBestTimeToBuySellStocks.test.js
import dpBestTimeToBuySellStocks from '../dpBestTimeToBuySellStocks'; describe('dpBestTimeToBuySellStocks', () => { it('should find the best time to buy and sell stocks', () => { let visit; expect(dpBestTimeToBuySellStocks([1, 5])).toEqual(4); visit = jest.fn(); expect(dpBestTimeToBuySellStocks([1]...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/uncategorized/best-time-to-buy-sell-stocks/__tests__/peakvalleyBestTimeToBuySellStocks.test.js
src/algorithms/uncategorized/best-time-to-buy-sell-stocks/__tests__/peakvalleyBestTimeToBuySellStocks.test.js
import peakvalleyBestTimeToBuySellStocks from '../peakvalleyBestTimeToBuySellStocks'; describe('peakvalleyBestTimeToBuySellStocks', () => { it('should find the best time to buy and sell stocks', () => { let visit; expect(peakvalleyBestTimeToBuySellStocks([1, 5])).toEqual(4); visit = jest.fn(); expe...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/uncategorized/recursive-staircase/recursiveStaircaseDP.js
src/algorithms/uncategorized/recursive-staircase/recursiveStaircaseDP.js
/** * Recursive Staircase Problem (Dynamic Programming Solution). * * @param {number} stairsNum - Number of stairs to climb on. * @return {number} - Number of ways to climb a staircase. */ export default function recursiveStaircaseDP(stairsNum) { if (stairsNum < 0) { // There is no way to go down - you climb...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/uncategorized/recursive-staircase/recursiveStaircaseMEM.js
src/algorithms/uncategorized/recursive-staircase/recursiveStaircaseMEM.js
/** * Recursive Staircase Problem (Recursive Solution With Memoization). * * @param {number} totalStairs - Number of stairs to climb on. * @return {number} - Number of ways to climb a staircase. */ export default function recursiveStaircaseMEM(totalStairs) { // Memo table that will hold all recursively calculate...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/uncategorized/recursive-staircase/recursiveStaircaseIT.js
src/algorithms/uncategorized/recursive-staircase/recursiveStaircaseIT.js
/** * Recursive Staircase Problem (Iterative Solution). * * @param {number} stairsNum - Number of stairs to climb on. * @return {number} - Number of ways to climb a staircase. */ export default function recursiveStaircaseIT(stairsNum) { if (stairsNum <= 0) { // There is no way to go down - you climb the stai...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/uncategorized/recursive-staircase/recursiveStaircaseBF.js
src/algorithms/uncategorized/recursive-staircase/recursiveStaircaseBF.js
/** * Recursive Staircase Problem (Brute Force Solution). * * @param {number} stairsNum - Number of stairs to climb on. * @return {number} - Number of ways to climb a staircase. */ export default function recursiveStaircaseBF(stairsNum) { if (stairsNum <= 0) { // There is no way to go down - you climb the st...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/uncategorized/recursive-staircase/__test__/recursiveStaircaseBF.test.js
src/algorithms/uncategorized/recursive-staircase/__test__/recursiveStaircaseBF.test.js
import recursiveStaircaseBF from '../recursiveStaircaseBF'; describe('recursiveStaircaseBF', () => { it('should calculate number of variants using Brute Force solution', () => { expect(recursiveStaircaseBF(-1)).toBe(0); expect(recursiveStaircaseBF(0)).toBe(0); expect(recursiveStaircaseBF(1)).toBe(1); ...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/uncategorized/recursive-staircase/__test__/recursiveStaircaseMEM.test.js
src/algorithms/uncategorized/recursive-staircase/__test__/recursiveStaircaseMEM.test.js
import recursiveStaircaseMEM from '../recursiveStaircaseMEM'; describe('recursiveStaircaseMEM', () => { it('should calculate number of variants using Brute Force with Memoization', () => { expect(recursiveStaircaseMEM(-1)).toBe(0); expect(recursiveStaircaseMEM(0)).toBe(0); expect(recursiveStaircaseMEM(1)...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/uncategorized/recursive-staircase/__test__/recursiveStaircaseIT.test.js
src/algorithms/uncategorized/recursive-staircase/__test__/recursiveStaircaseIT.test.js
import recursiveStaircaseIT from '../recursiveStaircaseIT'; describe('recursiveStaircaseIT', () => { it('should calculate number of variants using Iterative solution', () => { expect(recursiveStaircaseIT(-1)).toBe(0); expect(recursiveStaircaseIT(0)).toBe(0); expect(recursiveStaircaseIT(1)).toBe(1); e...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/uncategorized/recursive-staircase/__test__/recursiveStaircaseDP.test.js
src/algorithms/uncategorized/recursive-staircase/__test__/recursiveStaircaseDP.test.js
import recursiveStaircaseDP from '../recursiveStaircaseDP'; describe('recursiveStaircaseDP', () => { it('should calculate number of variants using Dynamic Programming solution', () => { expect(recursiveStaircaseDP(-1)).toBe(0); expect(recursiveStaircaseDP(0)).toBe(0); expect(recursiveStaircaseDP(1)).toBe...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/uncategorized/jump-game/dpTopDownJumpGame.js
src/algorithms/uncategorized/jump-game/dpTopDownJumpGame.js
/** * DYNAMIC PROGRAMMING TOP-DOWN approach of solving Jump Game. * * This comes out as an optimisation of BACKTRACKING approach. * * It relies on the observation that once we determine that a certain * index is good / bad, this result will never change. This means that * we can store the result and not need to ...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/uncategorized/jump-game/greedyJumpGame.js
src/algorithms/uncategorized/jump-game/greedyJumpGame.js
/** * GREEDY approach of solving Jump Game. * * This comes out as an optimisation of DYNAMIC PROGRAMMING BOTTOM_UP approach. * * Once we have our code in the bottom-up state, we can make one final, * important observation. From a given position, when we try to see if * we can jump to a GOOD position, we only eve...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/uncategorized/jump-game/dpBottomUpJumpGame.js
src/algorithms/uncategorized/jump-game/dpBottomUpJumpGame.js
/** * DYNAMIC PROGRAMMING BOTTOM-UP approach of solving Jump Game. * * This comes out as an optimisation of DYNAMIC PROGRAMMING TOP-DOWN approach. * * The observation to make here is that we only ever jump to the right. * This means that if we start from the right of the array, every time we * will query a posit...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/uncategorized/jump-game/backtrackingJumpGame.js
src/algorithms/uncategorized/jump-game/backtrackingJumpGame.js
/** * BACKTRACKING approach of solving Jump Game. * * This is the inefficient solution where we try every single jump * pattern that takes us from the first position to the last. * We start from the first position and jump to every index that * is reachable. We repeat the process until last index is reached. * W...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/uncategorized/jump-game/__test__/dpTopDownJumpGame.test.js
src/algorithms/uncategorized/jump-game/__test__/dpTopDownJumpGame.test.js
import dpTopDownJumpGame from '../dpTopDownJumpGame'; describe('dpTopDownJumpGame', () => { it('should solve Jump Game problem in top-down dynamic programming manner', () => { expect(dpTopDownJumpGame([1, 0])).toBe(true); expect(dpTopDownJumpGame([100, 0])).toBe(true); expect(dpTopDownJumpGame([2, 3, 1, ...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/uncategorized/jump-game/__test__/dpBottomUpJumpGame.test.js
src/algorithms/uncategorized/jump-game/__test__/dpBottomUpJumpGame.test.js
import dpBottomUpJumpGame from '../dpBottomUpJumpGame'; describe('dpBottomUpJumpGame', () => { it('should solve Jump Game problem in bottom-up dynamic programming manner', () => { expect(dpBottomUpJumpGame([1, 0])).toBe(true); expect(dpBottomUpJumpGame([100, 0])).toBe(true); expect(dpBottomUpJumpGame([2,...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/uncategorized/jump-game/__test__/backtrackingJumpGame.test.js
src/algorithms/uncategorized/jump-game/__test__/backtrackingJumpGame.test.js
import backtrackingJumpGame from '../backtrackingJumpGame'; describe('backtrackingJumpGame', () => { it('should solve Jump Game problem in backtracking manner', () => { expect(backtrackingJumpGame([1, 0])).toBe(true); expect(backtrackingJumpGame([100, 0])).toBe(true); expect(backtrackingJumpGame([2, 3, 1...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/uncategorized/jump-game/__test__/greedyJumpGame.test.js
src/algorithms/uncategorized/jump-game/__test__/greedyJumpGame.test.js
import greedyJumpGame from '../greedyJumpGame'; describe('greedyJumpGame', () => { it('should solve Jump Game problem in greedy manner', () => { expect(greedyJumpGame([1, 0])).toBe(true); expect(greedyJumpGame([100, 0])).toBe(true); expect(greedyJumpGame([2, 3, 1, 1, 4])).toBe(true); expect(greedyJum...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/uncategorized/unique-paths/uniquePaths.js
src/algorithms/uncategorized/unique-paths/uniquePaths.js
import pascalTriangle from '../../math/pascal-triangle/pascalTriangle'; /** * @param {number} width * @param {number} height * @return {number} */ export default function uniquePaths(width, height) { const pascalLine = width + height - 2; const pascalLinePosition = Math.min(width, height) - 1; return pascal...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/uncategorized/unique-paths/btUniquePaths.js
src/algorithms/uncategorized/unique-paths/btUniquePaths.js
/** * BACKTRACKING approach of solving Unique Paths problem. * * @param {number} width - Width of the board. * @param {number} height - Height of the board. * @param {number[][]} steps - The steps that have been already made. * @param {number} uniqueSteps - Total number of unique steps. * @return {number} - Numb...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/uncategorized/unique-paths/dpUniquePaths.js
src/algorithms/uncategorized/unique-paths/dpUniquePaths.js
/** * DYNAMIC PROGRAMMING approach of solving Unique Paths problem. * * @param {number} width - Width of the board. * @param {number} height - Height of the board. * @return {number} - Number of unique paths. */ export default function dpUniquePaths(width, height) { // Init board. const board = Array(height)....
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/uncategorized/unique-paths/__test__/dpUniquePaths.test.js
src/algorithms/uncategorized/unique-paths/__test__/dpUniquePaths.test.js
import dpUniquePaths from '../dpUniquePaths'; describe('dpUniquePaths', () => { it('should find the number of unique paths on board', () => { expect(dpUniquePaths(3, 2)).toBe(3); expect(dpUniquePaths(7, 3)).toBe(28); expect(dpUniquePaths(3, 7)).toBe(28); expect(dpUniquePaths(10, 10)).toBe(48620); ...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/uncategorized/unique-paths/__test__/uniquePaths.test.js
src/algorithms/uncategorized/unique-paths/__test__/uniquePaths.test.js
import uniquePaths from '../uniquePaths'; describe('uniquePaths', () => { it('should find the number of unique paths on board', () => { expect(uniquePaths(3, 2)).toBe(3); expect(uniquePaths(7, 3)).toBe(28); expect(uniquePaths(3, 7)).toBe(28); expect(uniquePaths(10, 10)).toBe(48620); expect(unique...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/uncategorized/unique-paths/__test__/btUniquePaths.test.js
src/algorithms/uncategorized/unique-paths/__test__/btUniquePaths.test.js
import btUniquePaths from '../btUniquePaths'; describe('btUniquePaths', () => { it('should find the number of unique paths on board', () => { expect(btUniquePaths(3, 2)).toBe(3); expect(btUniquePaths(7, 3)).toBe(28); expect(btUniquePaths(3, 7)).toBe(28); expect(btUniquePaths(10, 10)).toBe(48620); ...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/data-structures/heap/MinHeap.js
src/data-structures/heap/MinHeap.js
import Heap from './Heap'; export default class MinHeap extends Heap { /** * Checks if pair of heap elements is in correct order. * For MinHeap the first element must be always smaller or equal. * For MaxHeap the first element must be always bigger or equal. * * @param {*} firstElement * @param {*}...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/data-structures/heap/MaxHeapAdhoc.js
src/data-structures/heap/MaxHeapAdhoc.js
/** * The minimalistic (ad hoc) version of a MaxHeap data structure that doesn't have * external dependencies and that is easy to copy-paste and use during the * coding interview if allowed by the interviewer (since many data * structures in JS are missing). */ class MaxHeapAdhoc { constructor(heap = []) { t...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/data-structures/heap/MinHeapAdhoc.js
src/data-structures/heap/MinHeapAdhoc.js
/** * The minimalistic (ad hoc) version of a MinHeap data structure that doesn't have * external dependencies and that is easy to copy-paste and use during the * coding interview if allowed by the interviewer (since many data * structures in JS are missing). */ class MinHeapAdhoc { constructor(heap = []) { t...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/data-structures/heap/MaxHeap.js
src/data-structures/heap/MaxHeap.js
import Heap from './Heap'; export default class MaxHeap extends Heap { /** * Checks if pair of heap elements is in correct order. * For MinHeap the first element must be always smaller or equal. * For MaxHeap the first element must be always bigger or equal. * * @param {*} firstElement * @param {*}...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/data-structures/heap/Heap.js
src/data-structures/heap/Heap.js
import Comparator from '../../utils/comparator/Comparator'; /** * Parent class for Min and Max Heaps. */ export default class Heap { /** * @constructs Heap * @param {Function} [comparatorFunction] */ constructor(comparatorFunction) { if (new.target === Heap) { throw new TypeError('Cannot const...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/data-structures/heap/__test__/MinHeap.test.js
src/data-structures/heap/__test__/MinHeap.test.js
import MinHeap from '../MinHeap'; import Comparator from '../../../utils/comparator/Comparator'; describe('MinHeap', () => { it('should create an empty min heap', () => { const minHeap = new MinHeap(); expect(minHeap).toBeDefined(); expect(minHeap.peek()).toBeNull(); expect(minHeap.isEmpty()).toBe(t...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/data-structures/heap/__test__/Heap.test.js
src/data-structures/heap/__test__/Heap.test.js
import Heap from '../Heap'; describe('Heap', () => { it('should not allow to create instance of the Heap directly', () => { const instantiateHeap = () => { const heap = new Heap(); heap.add(5); }; expect(instantiateHeap).toThrow(); }); });
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/data-structures/heap/__test__/MaxHeap.test.js
src/data-structures/heap/__test__/MaxHeap.test.js
import MaxHeap from '../MaxHeap'; import Comparator from '../../../utils/comparator/Comparator'; describe('MaxHeap', () => { it('should create an empty max heap', () => { const maxHeap = new MaxHeap(); expect(maxHeap).toBeDefined(); expect(maxHeap.peek()).toBeNull(); expect(maxHeap.isEmpty()).toBe(t...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/data-structures/heap/__test__/MinHeapAdhoc.test.js
src/data-structures/heap/__test__/MinHeapAdhoc.test.js
import MinHeapAdhoc from '../MinHeapAdhoc'; describe('MinHeapAdhoc', () => { it('should create an empty min heap', () => { const minHeap = new MinHeapAdhoc(); expect(minHeap).toBeDefined(); expect(minHeap.peek()).toBe(undefined); expect(minHeap.isEmpty()).toBe(true); }); it('should add items to...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/data-structures/heap/__test__/MaxHeapAdhoc.test.js
src/data-structures/heap/__test__/MaxHeapAdhoc.test.js
import MaxHeap from '../MaxHeapAdhoc'; describe('MaxHeapAdhoc', () => { it('should create an empty max heap', () => { const maxHeap = new MaxHeap(); expect(maxHeap).toBeDefined(); expect(maxHeap.peek()).toBe(undefined); expect(maxHeap.isEmpty()).toBe(true); }); it('should add items to the heap ...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/data-structures/trie/Trie.js
src/data-structures/trie/Trie.js
import TrieNode from './TrieNode'; // Character that we will use for trie tree root. const HEAD_CHARACTER = '*'; export default class Trie { constructor() { this.head = new TrieNode(HEAD_CHARACTER); } /** * @param {string} word * @return {Trie} */ addWord(word) { const characters = Array.fro...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/data-structures/trie/TrieNode.js
src/data-structures/trie/TrieNode.js
import HashTable from '../hash-table/HashTable'; export default class TrieNode { /** * @param {string} character * @param {boolean} isCompleteWord */ constructor(character, isCompleteWord = false) { this.character = character; this.isCompleteWord = isCompleteWord; this.children = new HashTable...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/data-structures/trie/__test__/TrieNode.test.js
src/data-structures/trie/__test__/TrieNode.test.js
import TrieNode from '../TrieNode'; describe('TrieNode', () => { it('should create trie node', () => { const trieNode = new TrieNode('c', true); expect(trieNode.character).toBe('c'); expect(trieNode.isCompleteWord).toBe(true); expect(trieNode.toString()).toBe('c*'); }); it('should add child nod...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/data-structures/trie/__test__/Trie.test.js
src/data-structures/trie/__test__/Trie.test.js
import Trie from '../Trie'; describe('Trie', () => { it('should create trie', () => { const trie = new Trie(); expect(trie).toBeDefined(); expect(trie.head.toString()).toBe('*'); }); it('should add words to trie', () => { const trie = new Trie(); trie.addWord('cat'); expect(trie.head....
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/data-structures/graph/GraphEdge.js
src/data-structures/graph/GraphEdge.js
export default class GraphEdge { /** * @param {GraphVertex} startVertex * @param {GraphVertex} endVertex * @param {number} [weight=0] * @param key */ constructor(startVertex, endVertex, weight = 0, key = null) { this.startVertex = startVertex; this.endVertex = endVertex; this.weight = wei...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/data-structures/graph/GraphVertex.js
src/data-structures/graph/GraphVertex.js
import LinkedList from '../linked-list/LinkedList'; export default class GraphVertex { /** * @param {*} value */ constructor(value) { if (value === undefined) { throw new Error('Graph vertex must have a value'); } /** * @param {GraphEdge} edgeA * @param {GraphEdge} edgeB */ ...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/data-structures/graph/Graph.js
src/data-structures/graph/Graph.js
export default class Graph { /** * @param {boolean} isDirected */ constructor(isDirected = false) { this.vertices = {}; this.edges = {}; this.isDirected = isDirected; } /** * @param {GraphVertex} newVertex * @returns {Graph} */ addVertex(newVertex) { const key = newVertex.getKe...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/data-structures/graph/__test__/GraphEdge.test.js
src/data-structures/graph/__test__/GraphEdge.test.js
import GraphEdge from '../GraphEdge'; import GraphVertex from '../GraphVertex'; describe('GraphEdge', () => { it('should create graph edge with default weight', () => { const startVertex = new GraphVertex('A'); const endVertex = new GraphVertex('B'); const edge = new GraphEdge(startVertex, endVertex); ...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false