# trekhleb/javascript-algorithms architecture

> Architecture diagram of the GitHub repository trekhleb/javascript-algorithms, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-09-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/trekhleb/javascript-algorithms
- Repository: https://github.com/trekhleb/javascript-algorithms

## Overview

#### 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: [`src/data-structures/graph/Graph.js`](https://github.com/trekhleb/javascript-algorithms/blob/master/src/data-structures/graph/Graph.js) (Graph model), [`src/playground/playground.js`](https://github.com/trekhleb/javascript-algorithms/blob/master/src/playground/playground.js) (Playground), [`src/algorithms/graph/dijkstra/dijkstra.js`](https://github.com/trekhleb/javascript-algorithms/blob/master/src/algorithms/graph/dijkstra/dijkstra.js) (Graph algorithms), [`.github/workflows/CI.yml`](https://github.com/trekhleb/javascript-algorithms/blob/master/.github/workflows/CI.yml) (GitHub Actions CI)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_structures["Data structures"]
  node_comparator["Comparator<br/>shared utility<br/>[Comparator.js]"]
  node_graph["Graph model<br/>graph structure<br/>[Graph.js]"]
  node_graph_vertex["Graph vertex<br/>graph model<br/>[GraphVertex.js]"]
  node_graph_edge["Graph edge<br/>graph model<br/>[GraphEdge.js]"]
  node_heap["Heap<br/>ordered primitive<br/>[Heap.js]"]
  node_priority_queue["Priority queue<br/>frontier primitive<br/>[PriorityQueue.js]"]
  node_disjoint_set["Disjoint set<br/>connectivity primitive<br/>[DisjointSet.js]"]
  node_linked_tree["Linked and tree structures<br/>mutable structures<br/>[LinkedList.js]"]
end

subgraph group_algorithms["Algorithms"]
  node_graph_algorithms["Graph algorithms<br/>algorithm family<br/>[dijkstra.js]"]
  node_sorting_base["Sort base<br/>algorithm base<br/>[Sort.js]"]
  node_sorting_algorithms["Sorting algorithms<br/>algorithm family<br/>[QuickSort.js]"]
  node_numeric_algorithms["Math, set and DP<br/>algorithm family<br/>[Matrix.js]"]
  node_string_algorithms["String and hashing<br/>algorithm family"]
  node_image_ml["Image and ML examples<br/>specialized algorithms"]
end

subgraph group_quality["Quality tooling"]
  node_jest["Jest tests<br/>test runner<br/>[jest.config.js]"]
  node_playground_test["Playground test<br/>test boundary<br/>[playground.test.js]"]
  node_ci["GitHub Actions CI<br/>continuous integration<br/>[CI.yml]"]
  node_node_npm["Node and npm<br/>runtime<br/>[package.json]"]
end

node_readme["Repository index<br/>documentation<br/>[README.md]"]
node_playground["Playground<br/>experiment entry<br/>[playground.js]"]

node_readme -.->|"points to"| node_playground
node_playground -->|"imports modules"| node_graph
node_graph -->|"uses"| node_graph_vertex
node_graph -->|"uses"| node_graph_edge
node_graph_algorithms -->|"consumes"| node_graph
node_graph_algorithms -->|"uses frontier"| node_priority_queue
node_graph_algorithms -->|"uses connectivity"| node_disjoint_set
node_priority_queue -->|"builds on"| node_heap
node_heap -->|"compares with"| node_comparator
node_sorting_base -->|"uses"| node_comparator
node_sorting_algorithms -->|"extends"| node_sorting_base
node_playground_test -->|"exercises"| node_playground
node_jest -->|"runs"| node_playground_test
node_ci -->|"sets up"| node_node_npm
node_ci -->|"runs verification"| node_jest
node_node_npm -.->|"executes"| node_playground

click node_readme "https://github.com/trekhleb/javascript-algorithms/blob/master/README.md"
click node_playground "https://github.com/trekhleb/javascript-algorithms/blob/master/src/playground/playground.js"
click node_comparator "https://github.com/trekhleb/javascript-algorithms/blob/master/src/utils/comparator/Comparator.js"
click node_graph "https://github.com/trekhleb/javascript-algorithms/blob/master/src/data-structures/graph/Graph.js"
click node_graph_vertex "https://github.com/trekhleb/javascript-algorithms/blob/master/src/data-structures/graph/GraphVertex.js"
click node_graph_edge "https://github.com/trekhleb/javascript-algorithms/blob/master/src/data-structures/graph/GraphEdge.js"
click node_heap "https://github.com/trekhleb/javascript-algorithms/blob/master/src/data-structures/heap/Heap.js"
click node_priority_queue "https://github.com/trekhleb/javascript-algorithms/blob/master/src/data-structures/priority-queue/PriorityQueue.js"
click node_disjoint_set "https://github.com/trekhleb/javascript-algorithms/blob/master/src/data-structures/disjoint-set/DisjointSet.js"
click node_linked_tree "https://github.com/trekhleb/javascript-algorithms/blob/master/src/data-structures/linked-list/LinkedList.js"
click node_graph_algorithms "https://github.com/trekhleb/javascript-algorithms/blob/master/src/algorithms/graph/dijkstra/dijkstra.js"
click node_sorting_base "https://github.com/trekhleb/javascript-algorithms/blob/master/src/algorithms/sorting/Sort.js"
click node_sorting_algorithms "https://github.com/trekhleb/javascript-algorithms/blob/master/src/algorithms/sorting/quick-sort/QuickSort.js"
click node_numeric_algorithms "https://github.com/trekhleb/javascript-algorithms/blob/master/src/algorithms/math/matrix/Matrix.js"
click node_string_algorithms "https://github.com/trekhleb/javascript-algorithms/blob/master/src/algorithms/string/knuth-morris-pratt/knuthMorrisPratt.js"
click node_image_ml "https://github.com/trekhleb/javascript-algorithms/blob/master/src/algorithms/image-processing/seam-carving/resizeImageWidth.js"
click node_jest "https://github.com/trekhleb/javascript-algorithms/blob/master/jest.config.js"
click node_playground_test "https://github.com/trekhleb/javascript-algorithms/blob/master/src/playground/__test__/playground.test.js"
click node_ci "https://github.com/trekhleb/javascript-algorithms/blob/master/.github/workflows/CI.yml"
click node_node_npm "https://github.com/trekhleb/javascript-algorithms/blob/master/package.json"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_comparator,node_graph,node_graph_vertex,node_graph_edge,node_heap,node_priority_queue,node_disjoint_set,node_linked_tree toneBlue
class node_graph_algorithms,node_sorting_base,node_sorting_algorithms,node_numeric_algorithms,node_string_algorithms,node_image_ml toneAmber
class node_jest,node_playground_test,node_ci,node_node_npm toneMint
class node_readme,node_playground toneNeutral
```

## Components

### Data structures

Reusable ES-module object models and primitives.

- [Comparator](https://github.com/trekhleb/javascript-algorithms/blob/master/src/utils/comparator/Comparator.js): shared utility · Centralizes comparison behavior. · `src/utils/comparator/Comparator.js`
- [Graph model](https://github.com/trekhleb/javascript-algorithms/blob/master/src/data-structures/graph/Graph.js): graph structure · Directed or undirected weighted topology. · `src/data-structures/graph/Graph.js`
- [Graph vertex](https://github.com/trekhleb/javascript-algorithms/blob/master/src/data-structures/graph/GraphVertex.js): graph model · `src/data-structures/graph/GraphVertex.js`
- [Graph edge](https://github.com/trekhleb/javascript-algorithms/blob/master/src/data-structures/graph/GraphEdge.js): graph model · `src/data-structures/graph/GraphEdge.js`
- [Heap](https://github.com/trekhleb/javascript-algorithms/blob/master/src/data-structures/heap/Heap.js): ordered primitive · `src/data-structures/heap/Heap.js`
- [Priority queue](https://github.com/trekhleb/javascript-algorithms/blob/master/src/data-structures/priority-queue/PriorityQueue.js): frontier primitive · `src/data-structures/priority-queue/PriorityQueue.js`
- [Disjoint set](https://github.com/trekhleb/javascript-algorithms/blob/master/src/data-structures/disjoint-set/DisjointSet.js): connectivity primitive · `src/data-structures/disjoint-set/DisjointSet.js`
- [Linked and tree structures](https://github.com/trekhleb/javascript-algorithms/blob/master/src/data-structures/linked-list/LinkedList.js): mutable structures · Linked-list and binary-tree implementations preserve local invariants. · `src/data-structures/linked-list/LinkedList.js`

### Algorithms

Independently importable educational implementations.

- [Graph algorithms](https://github.com/trekhleb/javascript-algorithms/blob/master/src/algorithms/graph/dijkstra/dijkstra.js): algorithm family · Traversal, shortest-path, connectivity, and MST alternatives. · `src/algorithms/graph/dijkstra/dijkstra.js`
- [Sort base](https://github.com/trekhleb/javascript-algorithms/blob/master/src/algorithms/sorting/Sort.js): algorithm base · Shared class-based sorting behavior and test model. · `src/algorithms/sorting/Sort.js`
- [Sorting algorithms](https://github.com/trekhleb/javascript-algorithms/blob/master/src/algorithms/sorting/quick-sort/QuickSort.js): algorithm family · Alternative sorting implementations. · `src/algorithms/sorting/quick-sort/QuickSort.js`
- [Math, set and DP](https://github.com/trekhleb/javascript-algorithms/blob/master/src/algorithms/math/matrix/Matrix.js): algorithm family · Array and number-oriented problem implementations. · `src/algorithms/math/matrix/Matrix.js`
- [String and hashing](https://github.com/trekhleb/javascript-algorithms/blob/master/src/algorithms/string/knuth-morris-pratt/knuthMorrisPratt.js): algorithm family · String matching and derived-value implementations. · `src/algorithms/string/knuth-morris-pratt/knuthMorrisPratt.js`
- [Image and ML examples](https://github.com/trekhleb/javascript-algorithms/blob/master/src/algorithms/image-processing/seam-carving/resizeImageWidth.js): specialized algorithms · Local image-data and machine-learning input boundaries. · `src/algorithms/image-processing/seam-carving/resizeImageWidth.js`

### Quality tooling

Verification and repository automation.

- [Jest tests](https://github.com/trekhleb/javascript-algorithms/blob/master/jest.config.js): test runner · Verifies colocated feature behavior. · `jest.config.js`
- [Playground test](https://github.com/trekhleb/javascript-algorithms/blob/master/src/playground/__test__/playground.test.js): test boundary · `src/playground/__test__/playground.test.js`
- [GitHub Actions CI](https://github.com/trekhleb/javascript-algorithms/blob/master/.github/workflows/CI.yml): continuous integration · Runs repository verification. · `.github/workflows/CI.yml`
- [Node and npm](https://github.com/trekhleb/javascript-algorithms/blob/master/package.json): runtime · Node.js 16+ runtime and dependency installation. · `package.json`

### Other components

- [Repository index](https://github.com/trekhleb/javascript-algorithms/blob/master/README.md): documentation · Documents direct-import usage and learning material. · `README.md`
- [Playground](https://github.com/trekhleb/javascript-algorithms/blob/master/src/playground/playground.js): experiment entry · Manual experimentation through direct imports. · `src/playground/playground.js`

## Connections

- 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
