How trekhleb/javascript-algorithms works
Purpose and Entry Points
This is an educational JavaScript repository of standalone data-structure and algorithm implementations, each paired with explanatory documentation and Jest verification. It is not a packaged application or service: consumers import individual ES-module files directly. The repository index is README.md; experimentation entry point is src/playground/playground.js, exercised by src/playground/__test__/playground.test.js.
Source Organization
Two top-level implementation domains define the codebase:
• Reusable data-structure implementations in src/data-structures, including graph, heaps, linked lists, trees, and lookup structures.
• Problem/technique implementations in src/algorithms, grouped by domain rather than exposed through a central registry.
Shared comparison behavior is centralized in src/utils/comparator/Comparator.js; algorithms may compose it or a relevant structure, but remain independently importable.
Foundational Data Structures
Core structures provide the principal object-model boundary for algorithms:
• Graph model: src/data-structures/graph/Graph.js, src/data-structures/graph/GraphVertex.js, src/data-structures/graph/GraphEdge.js represents directed/undirected weighted topology.
• Heap and queue primitives support ordered/frontier-driven work: src/data-structures/heap/Heap.js, src/data-structures/priority-queue/PriorityQueue.js, src/data-structures/queue/Queue.js.
• Set/connectivity and associative structures: src/data-structures/disjoint-set/DisjointSet.js, src/data-structures/hash-table/HashTable.js, src/data-structures/bloom-filter/BloomFilter.js.
• Linked and tree structures encapsulate nodes and mutation invariants: src/data-structures/linked-list/LinkedList.js, src/data-structures/tree/BinaryTreeNode.js, src/data-structures/tree/binary-search-tree/BinarySearchTree.js.
Algorithm Families
Algorithms are isolated modules, organized as educational alternatives and paradigms rather than a single execution pipeline:
• Graph traversal, shortest-path, connectivity, and MST algorithms consume graph abstractions: src/algorithms/graph/dijkstra/dijkstra.js, src/algorithms/graph/kruskal/kruskal.js, src/algorithms/graph/topological-sorting/topologicalSort.js.
• Sorting uses a common class-based base/testing model: src/algorithms/sorting/Sort.js, src/algorithms/sorting/quick-sort/QuickSort.js, src/algorithms/sorting/merge-sort/MergeSort.js.
• Math, set, and dynamic-programming modules operate chiefly on primitive arrays/numbers: src/algorithms/math/matrix/Matrix.js, src/algorithms/sets/knapsack-problem/Knapsack.js, src/algorithms/sets/longest-common-subsequence/longestCommonSubsequence.js.
• String and cryptography modules take strings and return derived values or matches: src/algorithms/string/knuth-morris-pratt/knuthMorrisPratt.js, src/algorithms/cryptography/polynomial-hash/PolynomialHash.js.
• Specialized domains retain local input boundaries, e.g. image-data processing in src/algorithms/image-processing/seam-carving/resizeImageWidth.js with src/algorithms/image-processing/utils/imageData.js, and ML examples in src/algorithms/ml/k-means/kMeans.js.
Control Flow and Boundaries
The normal flow is direct import → invoke algorithm/structure API → assert result. There is no API server, persistence layer, UI, global dependency container, or cross-module runtime orchestration. Algorithms frequently demonstrate multiple implementations of the same problem, such as brute-force, recursive, dynamic-programming, greedy, or bitwise variants; these are parallel alternatives, not chained stages. Each feature directory owns its implementation, local README, and colocated test boundary.
Runtime and Quality Infrastructure
The runtime is Node.js 16+ with npm dependency installation (package.json, .nvmrc). Babel configuration supports modern JavaScript syntax through .babelrc. Jest is the test runner configured by jest.config.js; CI is executed through GitHub Actions at .github/workflows/CI.yml. ESLint is repository-wide code-quality enforcement via .eslintrc. No production infrastructure or runtime external service integrations are present; README links to external educational and charitable resources only.
Main entry points: Graph.js Graph model · playground.js Playground · dijkstra.js Graph algorithms · CI.yml GitHub Actions CI
Components 20
Data structures
Reusable ES-module object models and primitives.
- Comparator · Centralizes comparison behavior.
src/utils/comparator/Comparator.js - Graph model · Directed or undirected weighted topology.
src/data-structures/graph/Graph.js - Graph vertex
src/data-structures/graph/GraphVertex.js - Graph edge
src/data-structures/graph/GraphEdge.js - Heap
src/data-structures/heap/Heap.js - Priority queue
src/data-structures/priority-queue/PriorityQueue.js - Disjoint set
src/data-structures/disjoint-set/DisjointSet.js - Linked and tree structures · Linked-list and binary-tree implementations preserve local invariants.
src/data-structures/linked-list/LinkedList.js
Algorithms
Independently importable educational implementations.
- Graph algorithms · Traversal, shortest-path, connectivity, and MST alternatives.
src/algorithms/graph/dijkstra/dijkstra.js - Sort base · Shared class-based sorting behavior and test model.
src/algorithms/sorting/Sort.js - Sorting algorithms · Alternative sorting implementations.
src/algorithms/sorting/quick-sort/QuickSort.js - Math, set and DP · Array and number-oriented problem implementations.
src/algorithms/math/matrix/Matrix.js - String and hashing · String matching and derived-value implementations.
src/algorithms/string/knuth-morris-pratt/knuthMorrisPratt.js - Image and ML examples · Local image-data and machine-learning input boundaries.
src/algorithms/image-processing/seam-carving/resizeImageWidth.js
Quality tooling
Verification and repository automation.
- Jest tests · Verifies colocated feature behavior.
jest.config.js - Playground test
src/playground/__test__/playground.test.js - GitHub Actions CI · Runs repository verification.
.github/workflows/CI.yml - Node and npm · Node.js 16+ runtime and dependency installation.
package.json
Other components
- Repository index · Documents direct-import usage and learning material.
README.md - Playground · Manual experimentation through direct imports.
src/playground/playground.js
Connections 16
- Repository index → Playground: points to
- Playground → Graph model: imports modules
- Graph model → Graph vertex: uses
- Graph model → Graph edge: uses
- Graph algorithms → Graph model: consumes
- Graph algorithms → Priority queue: uses frontier
- Graph algorithms → Disjoint set: uses connectivity
- Priority queue → Heap: builds on
- Heap → Comparator: compares with
- Sort base → Comparator: uses
- Sorting algorithms → Sort base: extends
- Playground test → Playground: exercises
- Jest tests → Playground test: runs
- GitHub Actions CI → Node and npm: sets up
- GitHub Actions CI → Jest tests: runs verification
- Node and npm → Playground: executes