Awesome
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.
Contributing
This repository is contribution friendly :smiley:. If you'd like to add or improve an algorithm, your contribution is welcome! Please be sure to check out the Wiki for instructions.
Other programming languages?
This repository provides algorithm implementations in Java, however, there are other forks that provide implementations in other languages, most notably:
Running an algorithm implementation
To compile and run any of the algorithms here, you need at least JDK version 8. Gradle can make things more convenient for you, but it is not required.
Running with Gradle (recommended)
This project supports the Gradle Wrapper. The Gradle wrapper automatically downloads Gradle the first time it runs, so expect a delay when running the first command below.
If you are on Windows, use gradlew.bat
instead of ./gradlew
below.
Run a single algorithm like this:
./gradlew run -Palgorithm=<algorithm-subpackage>.<algorithm-class>
Alternatively, you can run a single algorithm specifying the full class name
./gradlew run -Pmain=<algorithm-fully-qualified-class-name>
For instance:
./gradlew run -Palgorithm=search.BinarySearch
or
./gradlew run -Pmain=com.williamfiset.algorithms.search.BinarySearch
Compiling and running with only a 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
- :movie_camera: Balanced Trees
- :movie_camera: Binary Search Tree
- Splay Tree
- :movie_camera: Dynamic Array
- :movie_camera: Fenwick Tree
- Fibonacci Heap
- :movie_camera: Hashtable
- :movie_camera: Linked List
- :movie_camera: Priority Queue
- :movie_camera: Queue
- Segment Tree
- :movie_camera: Sparse Table
- :movie_camera: Stack
- :movie_camera: Suffix Array
- Trie
- :movie_camera: Union Find
Dynamic Programming
Dynamic Programming Classics
- Coin change problem - O(nW)
- Edit distance (iterative) - O(nm)
- Edit distance (recursive) - O(nm)
- :movie_camera: 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(n<sup>2</sup>)
- Longest Palindrome Subsequence (LPS) - O(n<sup>2</sup>)
- :movie_camera: Traveling Salesman Problem (dynamic programming, iterative) - O(n<sup>2</sup>2<sup>n</sup>)
- Traveling Salesman Problem (dynamic programming, recursive) - O(n<sup>2</sup>2<sup>n</sup>)
- Minimum Weight Perfect Matching (iterative, complete graph) - O(n<sup>2</sup>2<sup>n</sup>)
Dynamic Programming Problem Examples
Adhoc
Tiling problems
- :movie_camera: Tiling Dominoes
- :movie_camera: Tiling Dominoes and Trominoes
- :movie_camera: Mountain Scenes
Geometry
- Angle between 2D vectors - O(1)
- Angle between 3D 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 cut - O(n)
- Convex polygon contains points - O(log(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-circle intersection point(s) - O(1)
- Line segment to general form (ax + by = c) - O(1)
- Line segment-line segment intersection - O(1)
- Longitude-Latitude geographic distance - O(1)
- Point is inside triangle check - O(1)
- Point rotation about point - O(1)
- Triangle area algorithms - O(1)
- [UNTESTED] Circle-circle intersection area - O(1)
- [UNTESTED] Circular segment area - O(1)
Graph theory
Tree algorithms
- :movie_camera: Rooting an undirected tree - O(V+E)
- :movie_camera: Identifying isomorphic trees - O(?)
- :movie_camera: Tree center(s) - O(V+E)
- Tree diameter - O(V+E)
- :movie_camera: Lowest Common Ancestor (LCA, Euler tour) - O(1) queries, O(nlogn) preprocessing
Network flow
- Bipartite graph verification (adjacency list) - O(V+E)
- :movie_camera: Max flow & Min cut (Ford-Fulkerson with DFS, adjacency list) - O(fE)
- Max flow & Min cut (Ford-Fulkerson with DFS, adjacency matrix) - O(fV<sup>2</sup>)
- :movie_camera: Max flow & Min cut (Edmonds-Karp, adjacency list) - O(VE<sup>2</sup>)
- :movie_camera: Max flow & Min cut (Capacity scaling, adjacency list) - O(E<sup>2</sup>log<sub>2</sub>(U))
- :movie_camera: Max flow & Min cut (Dinic's, adjacency list) - O(EV<sup>2</sup>) 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(E<sup>2</sup>V<sup>2</sup>)
- Min Cost Max Flow (Johnson's algorithm, adjacency list) - O(E<sup>2</sup>Vlog(V))
Main graph theory algorithms
- Articulation points/cut vertices (adjacency list) - O(V+E)
- Bellman-Ford (edge list, negative cycles, fast & optimized) - O(VE)
- :movie_camera: Bellman-Ford (adjacency list, negative cycles) - O(VE)
- Bellman-Ford (adjacency matrix, negative cycles) - O(V<sup>3</sup>)
- :movie_camera: Breadth first search (adjacency list) - O(V+E)
- Breadth first search (adjacency list, fast queue) - O(V+E)
- Bridges/cut edges (adjacency list) - O(V+E)
- Find connected components (adjacency list, union find) - O(Elog(E))
- Find connected components (adjacency list, DFS) - O(V+E)
- Depth first search (adjacency list, iterative) - O(V+E)
- Depth first search (adjacency list, iterative, fast stack) - O(V+E)
- :movie_camera: Depth first search (adjacency list, recursive) - O(V+E)
- :movie_camera: Dijkstra's shortest path (adjacency list, lazy implementation) - O(Elog(V))
- :movie_camera: Dijkstra's shortest path (adjacency list, eager implementation + D-ary heap) - O(Elog<sub>E/V</sub>(V))
- :movie_camera: Eulerian Path (directed edges) - O(E+V)
- :movie_camera: Floyd Warshall algorithm (adjacency matrix, negative cycle check) - O(V<sup>3</sup>)
- Graph diameter (adjacency list) - O(VE)
- :movie_camera: Kahn's algorithm (topological sort, adjacency list) - O(E+V)
- Kruskal's min spanning tree algorithm (edge list, union find) - O(Elog(E))
- :movie_camera: Kruskal's min spanning tree algorithm (edge list, union find, lazy sorting) - O(Elog(E))
- Kosaraju's strongly connected components algorithm (adjacency list) - O(V+E)
- :movie_camera: Prim's min spanning tree algorithm (lazy version, adjacency list) - O(Elog(E))
- Prim's min spanning tree algorithm (lazy version, adjacency matrix) - O(V<sup>2</sup>)
- :movie_camera: Prim's min spanning tree algorithm (eager version, adjacency list) - O(Elog(V))
- Steiner tree (minimum spanning tree generalization) - O(V<sup>3</sup> + V<sup>2</sup> _ 2<sup>T</sup> + V _ 3<sup>T</sup>)
- :movie_camera: Tarjan's strongly connected components algorithm (adjacency list) - O(V+E)
- :movie_camera: Topological sort (acyclic graph, adjacency list) - O(V+E)
- Topological sort (acyclic graph, adjacency matrix) - O(V<sup>2</sup>)
- Traveling Salesman Problem (brute force) - O(n!)
- :movie_camera: Traveling Salesman Problem (dynamic programming, iterative) - O(n<sup>2</sup>2<sup>n</sup>)
- Traveling Salesman Problem (dynamic programming, recursive) - O(n<sup>2</sup>2<sup>n</sup>)
Linear algebra
- Freivald's algorithm (matrix multiplication verification) - O(kn<sup>2</sup>)
- Gaussian elimination (solve system of linear equations) - O(cr<sup>2</sup>)
- Gaussian elimination (modular version, prime finite field) - O(cr<sup>2</sup>)
- Linear recurrence solver (finds nth term in a recurrence relation) - O(m<sup>3</sup>log(n))
- Matrix determinant (Laplace/cofactor expansion) - O((n+2)!)
- Matrix inverse - O(n<sup>3</sup>)
- Matrix multiplication - O(n<sup>3</sup>)
- Matrix power - O(n<sup>3</sup>log(p))
- Square matrix rotation - O(n<sup>2</sup>)
Mathematics
- [UNTESTED] 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<sup>1/4</sup>)
- Totient function using sieve (phi function, relatively prime number count) - O(nlog(log(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)
- Primality check (Rabin-Miller) - O(k)
- Least Common Multiple (LCM) - ~O(log(a + b))
- Modular inverse - ~O(log(a + b))
- Prime factorization (pollard rho) - O(n<sup>1/4</sup>)
- Relatively prime check (coprimality check) - ~O(log(a + b))
Other
- Bit manipulations - O(1)
- List permutations - O(n!)
- :movie_camera: Power set (set of all subsets) - O(2<sup>n</sup>)
- 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)
- Lazy Range Adder - O(1) range updates, O(n) to finalize all updates
Search algorithms
- Binary search (real numbers) - O(log(n))
- Interpolation search (discrete discrete) - 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(n<sup>2</sup>)
- Bucket sort - Θ(n + k)
- Counting sort - O(n + k)
- Heapsort - O(nlog(n))
- Insertion sort - O(n<sup>2</sup>)
- :movie_camera: Mergesort - O(nlog(n))
- Quicksort (in-place, Hoare partitioning) - Θ(nlog(n))
- Quicksort3 (Dutch National Flag algorithm) - Θ(nlog(n))
- Selection sort - O(n<sup>2</sup>)
- 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)
- :movie_camera: Longest Common Substring (LCS) - O(nlog(n)) bounded by SA construction, otherwise O(n)
- :movie_camera: 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.
Donate
Consider donating to support my creation of educational content: