
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
Dynamic Programming
Dynamic Programming Classics
Dynamic Programming Problem Examples
Adhoc
Tiling problems
Geometry
More geometry algorithms
Graph theory
Tree algorithms
Network flow
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)
- :movie_camera: 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)
- :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(ElogE/V(V))
- :movie_camera: Eulerian Path (directed edges) - O(E+V)
- :movie_camera: Floyd Warshall algorithm (adjacency matrix, negative cycle check) - O(V3)
- :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))
- 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))
- :movie_camera: 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)
- :movie_camera: Tarjan's strongly connected components algorithm (adjacency list) - O(V+E)
- :movie_camera: Topological sort (acyclic graph, adjacency list) - O(V+E)
- :movie_camera: Traveling Salesman Problem (dynamic programming, iterative) - O(n22n)
- Traveling Salesman Problem (dynamic programming, recursive) - O(n22n)
Linear algebra
Mathematics
Other
Search algorithms
Sorting algorithms
String algorithms
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.
Consider sponsoring to support my creation of educational content:
