GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com โ€” GitHub - williamfiset/algorithms: A collection of algorithms and data structures

BranchesTags
Open more actions menu

Latest commit

 

History

1,777 Commits
1,777 Commits

Folders and files

NameName
Last commit message
Last commit date
.claude
.claude
 
 
.gemini
.gemini
 
 
.github
.github
 
 
references
references
 
 
slides
slides
 
 
src
src
 
 
.claude.md
.claude.md
 
 
.gemini.md
.gemini.md
 
 
.gitignore
.gitignore
 
 
BUILD.bazel
BUILD.bazel
 
 
LICENSE
LICENSE
 
 
MODULE.bazel
MODULE.bazel
 
 
README.md
README.md
 
 

Repository files navigation

License: MIT Bazel Tests README Checker Sponsor

Algorithms & data structures project

Algorithms and data structures are fundamental to efficient code and good software design. Creating and designing excellent algorithms is required for being an exemplary programmer. This repository's goal is to demonstrate how to correctly implement common data structures and algorithms in the simplest and most elegant ways.

๐ŸŽฌ Many of the algorithms and data structures in this repo have companion video explanations on the William Fiset YouTube channel โ€” so if the code alone doesn't click, grab some popcorn and watch the videos!

Running an algorithm implementation

To compile and run any of the algorithms here, you need at least JDK version 8 and Bazel

Running with Bazel (recommended)

This project uses Bazel as its build system. Install Bazel by following the official installation guide.

Run a single algorithm like this:

bazel run //src/main/java/com/williamfiset/algorithms/<subpackage>:<ClassName>

For instance:

bazel run //src/main/java/com/williamfiset/algorithms/search:BinarySearch

Run all tests:

bazel test //src/test/...

Run tests for a specific package:

bazel test //src/test/java/com/williamfiset/algorithms/sorting:all

Compiling and running with only a JDK

If you don't want to use Bazel, you can compile and run with just the JDK:

Create a classes folder

cd Algorithms
mkdir classes

Compile the algorithm

javac -sourcepath src/main/java -d classes src/main/java/<relative-path-to-java-source-file>

Run the algorithm

java -cp classes <class-fully-qualified-name>

Example

$ javac -d classes -sourcepath src/main/java src/main/java/com/williamfiset/algorithms/search/BinarySearch.java
$ java -cp classes com.williamfiset.algorithms.search.BinarySearch

Data Structures

  • ๐ŸŽฅ Balanced Trees
    • AVL Tree (recursive)
    • Red Black Tree (recursive)
  • ๐ŸŽฅ Binary Search Tree
  • Splay Tree
  • ๐ŸŽฅ Dynamic Array
    • Dynamic array (integer only, fast)
  • ๐ŸŽฅ Fenwick Tree
    • Fenwick Tree (range query, point updates)
    • Fenwick Tree (range update, point query)
  • Fibonacci Heap
  • ๐ŸŽฅ Hashtable
    • Hashtable (double hashing)
    • Hashtable (linear probing)
    • Hashtable (quadratic probing)
    • Hashtable (separate chaining)
  • ๐ŸŽฅ Linked List
  • ๐ŸŽฅ Priority Queue
    • Min Binary Heap
    • Min Indexed Binary Heap (sorted key-value pairs, similar to hash-table)
    • Min D-Heap
    • ๐ŸŽฅ Min Indexed D-Heap (sorted key-value pairs, similar to hash-table)
  • ๐ŸŽฅ Queue
    • Queue (integer only, fixed size, fast)
    • Queue (linked list, generic)
  • Segment Tree
    • Segment tree (array based, compact)
    • Segment tree (pointer implementation)
  • ๐ŸŽฅ Sparse Table
  • ๐ŸŽฅ Stack
    • Stack (integer only, fixed size, fast)
    • Stack (linked list, generic)
    • Stack (array, generic)
  • ๐ŸŽฅ Suffix Array
    • Suffix Array (O(nยฒlogn) construction)
    • Suffix Array (O(nlogยฒ(n)) construction)
    • Suffix Array (O(nlog(n)) construction)
  • Trie
  • ๐ŸŽฅ Union Find

Dynamic Programming

Dynamic Programming Classics

  • Coin change problem - O(nW)
  • Edit distance (iterative) - O(nm)
  • Edit distance (recursive) - O(nm)
  • ๐ŸŽฅ Knapsack 0/1 - O(nW)
  • Knapsack unbounded (0/โˆž) - O(nW)
  • Maximum contiguous subarray - O(n)
  • Longest Common Subsequence (LCS) - O(nm)
  • Longest Increasing Subsequence (LIS) - O(n2)
  • Longest Palindrome Subsequence (LPS) - O(n2)
  • ๐ŸŽฅ Traveling Salesman Problem (dynamic programming, iterative) - O(n22n)
  • Traveling Salesman Problem (dynamic programming, recursive) - O(n22n)
  • Minimum Weight Perfect Matching (iterative, complete graph) - O(n22n)

Dynamic Programming Problem Examples

Adhoc

  • ๐ŸŽฅ Magic Cows
  • ๐ŸŽฅ Narrow Art Gallery

Tiling problems

  • ๐ŸŽฅ Tiling Dominoes
  • ๐ŸŽฅ Tiling Dominoes and Trominoes
  • ๐ŸŽฅ Mountain Scenes

Geometry

  • Angle between 2D vectors - O(1)
  • Circle-circle intersection point(s) - O(1)
  • Circle-line intersection point(s) - O(1)
  • Circle-line segment intersection point(s) - O(1)
  • Circle-point tangent line(s) - O(1)
  • Closest pair of points (line sweeping algorithm) - O(nlog(n))
  • Collinear points test (are three 2D points on the same line) - O(1)
  • Convex hull (Graham Scan algorithm) - O(nlog(n))
  • Convex hull (Monotone chain algorithm) - O(nlog(n))
  • Convex polygon area - O(n)
  • Convex polygon contains points - O(log(n))
  • Triangle area algorithms - O(1)
  • Line segment-circle intersection point(s) - O(1)
  • Line segment-line segment intersection - O(1)
More geometry algorithms
  • Angle between 3D vectors - O(1)
  • Convex polygon cut - O(n)
  • Coplanar points test (are four 3D points on the same plane) - O(1)
  • Line class (handy infinite line class) - O(1)
  • Line-circle intersection point(s) - O(1)
  • Line segment to general form (ax + by = c) - O(1)
  • Longitude-Latitude geographic distance - O(1)
  • Point is inside triangle check - O(1)
  • Point rotation about point - O(1)
  • [UNTESTED] Circle-circle intersection area - O(1)
  • [UNTESTED] Circular segment area - O(1)

Graph theory

Tree algorithms

  • ๐ŸŽฅ Rooting an undirected tree - O(V+E)
  • ๐ŸŽฅ Identifying isomorphic trees - O(V*log(V))
  • ๐ŸŽฅ Tree center(s) - O(V+E)
  • Tree diameter - O(V+E)
  • ๐ŸŽฅ Lowest Common Ancestor (LCA, Euler tour) - O(1) queries, O(nlogn) preprocessing

Network flow

  • Bipartite graph verification (adjacency list) - O(V+E)
  • ๐ŸŽฅ Max flow & Min cut (Ford-Fulkerson with DFS, adjacency list) - O(fE)
  • Max flow & Min cut (Ford-Fulkerson with DFS, adjacency matrix) - O(fV2)
  • ๐ŸŽฅ Max flow & Min cut (Edmonds-Karp, adjacency list) - O(VE2)
  • ๐ŸŽฅ Max flow & Min cut (Capacity scaling, adjacency list) - O(E2log2(U))
  • ๐ŸŽฅ Max flow & Min cut (Dinic's, adjacency list) - O(EV2) or O(EโˆšV) for bipartite graphs
  • Maximum Cardinality Bipartite Matching (augmenting path algorithm, adjacency list) - O(VE)
  • Min Cost Max Flow (Bellman-Ford, adjacency list) - O(E2V2)
  • Min Cost Max Flow (Johnson's algorithm, adjacency list) - O(E2Vlog(V))

Main graph theory algorithms

  • Articulation points/cut vertices (adjacency list) - O(V+E)
  • Bellman-Ford (edge list, negative cycles, fast & optimized) - O(VE)
  • ๐ŸŽฅ Bellman-Ford (adjacency list, negative cycles) - O(VE)
  • ๐ŸŽฅ Breadth first search (adjacency list) - O(V+E)
  • Bridges/cut edges (adjacency list) - O(V+E)
  • Boruvkas (adjacency list, min spanning tree algorithm) - O(Elog(V))
  • Find connected components (adjacency list, union find) - O(V+E)
  • Find connected components (adjacency list, DFS) - O(V+E)
  • ๐ŸŽฅ Depth first search (adjacency list, recursive) - O(V+E)
  • ๐ŸŽฅ Dijkstra's shortest path (adjacency list, lazy implementation) - O(Elog(V))
  • ๐ŸŽฅ Dijkstra's shortest path (adjacency list, eager implementation + D-ary heap) - O(ElogE/V(V))
  • ๐ŸŽฅ Eulerian Path (directed edges) - O(E+V)
  • ๐ŸŽฅ Floyd Warshall algorithm (adjacency matrix, negative cycle check) - O(V3)
  • ๐ŸŽฅ Kahn's algorithm (topological sort, adjacency list) - O(E+V)
  • Kruskal's min spanning tree algorithm (edge list, union find) - O(Elog(E))
  • Kosaraju's strongly connected components algorithm (adjacency list) - O(V+E)
  • ๐ŸŽฅ Prim's min spanning tree algorithm (lazy version, adjacency list) - O(Elog(E))
  • ๐ŸŽฅ Prim's min spanning tree algorithm (eager version, adjacency list) - O(Elog(V))
  • Steiner tree (minimum spanning tree generalization) - O(V3 + V2 ยท 2T + V ยท 3T)
  • ๐ŸŽฅ Tarjan's strongly connected components algorithm (adjacency list) - O(V+E)
  • ๐ŸŽฅ Topological sort (acyclic graph, adjacency list) - O(V+E)
  • ๐ŸŽฅ Traveling Salesman Problem (dynamic programming, iterative) - O(n22n)
  • Traveling Salesman Problem (dynamic programming, recursive) - O(n22n)

Linear algebra

  • Freivald's algorithm (matrix multiplication verification) - O(kn2)
  • Gaussian elimination (solve system of linear equations) - O(cr2)
  • Gaussian elimination (modular version, prime finite field) - O(cr2)
  • Linear recurrence solver (finds nth term in a recurrence relation) - O(m3log(n))
  • Matrix determinant (Laplace/cofactor expansion) - O((n+2)!)
  • Matrix inverse - O(n3)
  • Matrix multiplication - O(n3)
  • Matrix power - O(n3log(p))
  • Square matrix rotation - O(n2)

Mathematics

  • Chinese remainder theorem
  • Prime number sieve (sieve of Eratosthenes) - O(nlog(log(n)))
  • Prime number sieve (sieve of Eratosthenes, compressed) - O(nlog(log(n)))
  • Totient function (phi function, relatively prime number count) - O(โˆšn)
  • Extended euclidean algorithm - ~O(log(a + b))
  • Greatest Common Divisor (GCD) - ~O(log(a + b))
  • Fast Fourier transform (quick polynomial multiplication) - O(nlog(n))
  • Fast Fourier transform (quick polynomial multiplication, complex numbers) - O(nlog(n))
  • Primality check - O(โˆšn)
  • Least Common Multiple (LCM) - ~O(log(a + b))
  • Modular inverse - ~O(log(a + b))
  • Modular exponentiation - O(log(n))
  • Prime factorization (pollard rho) - O(n1/4)
  • Relatively prime check (coprimality check) - ~O(log(a + b))

Other

  • Bit manipulations - O(1)
  • List permutations - O(n!)
  • ๐ŸŽฅ Power set (set of all subsets) - O(n ยท 2n)
  • Set combinations - O(n choose r)
  • Set combinations with repetition - O((n+r-1) choose r)
  • Sliding Window Minimum/Maximum - O(1)
  • Square Root Decomposition - O(1) point updates, O(โˆšn) range queries
  • Unique set combinations - O(n choose r)

Search algorithms

  • Binary search (real numbers) - O(log(n))
  • Interpolation search (discrete numbers) - O(n) or O(log(log(n))) with uniform input
  • Ternary search (real numbers) - O(log(n))
  • Ternary search (discrete numbers) - O(log(n))

Sorting algorithms

  • Bubble sort - O(n2)
  • Bucket sort - ฮ˜(n + k)
  • Counting sort - O(n + k)
  • Heapsort - O(nlog(n))
  • Insertion sort - O(n2)
  • ๐ŸŽฅ Mergesort - O(nlog(n))
  • Quicksort (in-place, Hoare partitioning) - ฮ˜(nlog(n))
  • Quicksort3 (Dutch National Flag algorithm) - ฮ˜(nlog(n))
  • Selection sort - O(n2)
  • Shell sort - O(n2) worst, ~O(n1.5) average
  • Tim sort - O(nlog(n))
  • Radix sort - O(n*w)

String algorithms

  • Booth's algorithm (finds lexicographically smallest string rotation) - O(n)
  • Knuth-Morris-Pratt algorithm (finds pattern matches in text) - O(n+m)
  • Longest Common Prefix (LCP) array - O(nlog(n)) bounded by SA construction, otherwise O(n)
  • ๐ŸŽฅ Longest Common Substring (LCS) - O(nlog(n)) bounded by SA construction, otherwise O(n)
  • ๐ŸŽฅ Longest Repeated Substring (LRS) - O(nlog(n))
  • Manacher's algorithm (finds all palindromes in text) - O(n)
  • Rabin-Karp algorithm (finds pattern match positions in text) - O(n+m)
  • Substring verification with suffix array - O(nlog(n)) SA construction and O(mlog(n)) per query

License

This repository is released under the MIT license. In short, this means you are free to use this software in any personal, open-source or commercial projects. Attribution is optional but appreciated.

Sponsor

Consider sponsoring to support my creation of educational content:

Sponsor

About

A collection of algorithms and data structures

Topics

adjacencyadjacency-matrixalgorithmalgorithmsdijkstradynamic-programmingedmonds-karp-algorithmgeometrygraph-theorylinear-algebramathematicsmatrix-multiplicationmaxflownlogsearch-algorithmsearch-algorithmssorting-algorithmsstringstraveling-salesmantree-algorithms

Resources

Readme
MIT license
Activity

Stars

18.9k stars

Watchers

398 watching

Forks

4.5k forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402503 โ€” GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800ร—600. No cookies required.