William Fiset Algorithms and Data Structures

repository·master·Indexed 12 days ago

https://github.com/williamfiset/algorithms

A comprehensive collection of fundamental algorithms and data structures implemented in Java and JavaScript. The library covers graph theory (Dijkstra's, Bellman-Ford, Dinic's), dynamic programming, linear algebra, geometry, string algorithms (KMP, Manacher's), and a wide array of data structures including Red-Black Trees, Fenwick Trees, and Fibonacci Heaps. Designed as an educational resource and reference for correct implementations.

Tokens
8.5K
Snippets
32
Records
46
Agent score
97%

What's inside William Fiset Algorithms

  1. Overview of Dynamic Programming implementations

    master

    The repository provides implementations for several Dynamic Programming (DP) categories:

    • Classics: Coin Change, Edit Distance (Iterative/Recursive), Knapsack (0/1 and Unbounded), Maximum Contiguous Subarray, Longest Common Subsequence (LCS), Longest Increasing Subsequence (LIS), Longest Palindrome Subsequence (LPS), Traveling Salesman Problem (TSP), and Minimum Weight Perfect Matching.
    • Problem Examples: Adhoc problems (Magic Cows, Narrow Art Gallery) and Tiling problems (Dominoes, Dominoes and Trominoes, Mountain Scenes).
  2. Overview of available Data Structures

    master

    The repository contains implementations of various data structures, including:

    • Trees: Balanced Trees (AVL, Red-Black), Binary Search Trees, Splay Trees, Segment Trees, and Fenwick Trees.
    • Heaps & Queues: Fibonacci Heap, Priority Queues (Min Binary Heap, Min Indexed Binary Heap, Min D-Heap, Min Indexed D-Heap), and various Queue implementations.
    • Hashing: Hashtables with different collision resolution strategies (Double Hashing, Linear Probing, Quadratic Probing, Separate Chaining).
    • Linear Structures: Dynamic Arrays, Linked Lists, Stacks (Array, List, and Integer-only versions), and Deques.
    • Advanced Structures: Suffix Arrays, Tries, and Union-Find.
  3. Explore Geometry algorithms

    master

    The repository contains a variety of geometry algorithms implemented in Java and JavaScript. These include:

    • 2D/3D Vector operations: Angle between vectors.
    • Circle & Line intersections: Circle-circle, circle-line, circle-line segment, and line segment-circle intersections.
    • Convex Hull algorithms: Graham Scan and Monotone Chain.
    • Polygon operations: Convex polygon area, checking if a convex polygon contains a point, and convex polygon cutting.
    • Point & Line properties: Collinear points test, triangle area, point rotation, and line segment to general form conversion.
    • Other: Closest pair of points (line sweeping), and geographic distance (Longitude-Latitude).
  4. Explore Main Graph Theory algorithms

    master

    A comprehensive collection of graph algorithms is provided, including:

    • Connectivity & Components: Articulation points, bridges, finding connected components (via Union-Find or DFS), and Strongly Connected Components (Kosaraju's or Tarjan's algorithms).
    • Shortest Paths: Bellman-Ford (edge list or adjacency list) and Dijkstra's (lazy implementation or eager implementation with D-ary heap).
    • Spanning Trees: Boruvkas, Kruskal's (edge list), and Prim's (lazy or eager versions).
    • Traversal: Breadth-First Search (BFS) and Depth-First Search (DFS).
    • Topological Sorting: Kahn's algorithm and general topological sort for acyclic graphs.
    • Other: Eulerian Path, Floyd Warshall (adjacency matrix), Steiner Tree, and Traveling Salesman Problem (TSP) using iterative or recursive dynamic programming.
  5. Explore Tree algorithms

    master

    The library provides several algorithms specifically for tree structures:

    • Rooting: Rooting an undirected tree.
    • Isomorphism: Identifying if two trees are isomorphic.
    • Structure: Finding tree centers and the tree diameter.
    • Ancestry: Lowest Common Ancestor (LCA) using the Euler tour method (O(1) queries after O(n log n) preprocessing).
  6. Explore Network Flow algorithms

    master

    For network flow problems, the following implementations are available:

    • Bipartite Graphs: Verification using adjacency lists and Maximum Cardinality Bipartite Matching (augmenting path algorithm).
    • Max Flow & Min Cut:
      • Ford-Fulkerson (DFS with adjacency list or adjacency matrix).
      • Edmonds-Karp (adjacency list).
      • Capacity Scaling (adjacency list).
      • Dinic's algorithm (adjacency list).
    • Min Cost Max Flow:
      • Using Bellman-Ford (adjacency list).
      • Using Johnson's algorithm (adjacency list).
  7. What is a Fenwick Tree

    master
    A Fenwick Tree (FT), also known as a Binary Indexed Tree (BIT), is an efficient data structure used for performing range and point queries and updates. While the current implementations in this repository focus on summation, Fenwick Trees can be modified to support any invertible function. More specific operations, such as min/max queries, are possible but require maintaining additional information.
  8. Run algorithms using Bazel (recommended)

    master

    The project uses Bazel as its build system. This is the recommended way to compile and run specific algorithm implementations or run tests.

    To run a single algorithm, use bazel run followed by the target path in the format //src/main/java/com/williamfiset/algorithms/<subpackage>:<ClassName>.

    To run all tests in the repository, use bazel test //src/test/....

    To run tests for a specific package, use bazel test //src/test/java/com/williamfiset/algorithms/<package>:all.

    # Run a single algorithm
    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
  9. Compile and run algorithms using only a JDK

    master

    If you do not want to use Bazel, you can use a standard JDK (version 8 or higher).

    1. Create a classes folder: Create a directory named classes to hold compiled files.
    2. Compile: Use javac with the -sourcepath pointing to src/main/java and the -d flag pointing to your classes folder.
    3. Run: Use java with the -cp (classpath) flag pointing to the classes folder and provide the fully qualified class name.
    # 1. Create classes folder
    mkdir classes
    
    # 2. Compile the algorithm
    javac -sourcepath src/main/java -d classes src/main/java/com/williamfiset/algorithms/search/BinarySearch.java
    
    # 3. Run the algorithm
    java -cp classes com.williamfiset.algorithms.search.BinarySearch
  10. Use the QuadTree for spatial indexing

    master

    The QuadTree class is a spatial data structure used to store and query points with integer coordinates within a defined rectangular region. It supports adding points, counting points within a specific area, and performing k-nearest neighbor (kNN) searches.

    Note: This implementation is currently under development.

    // Example initialization
    QuadTree.Rect region = new QuadTree.Rect(0, 0, 100, 100);
    QuadTree qt = new QuadTree(region);
    
    // Adding a point
    qt.add(10, 10);
    
    // Counting points in an area
    int count = qt.count(new QuadTree.Rect(5, 5, 15, 15));
    
    // Finding k-nearest neighbors
    List<QuadTree.Pt> neighbors = qt.kNearestNeighbors(3, 12, 12);
  11. Use the SkipList data structure

    master

    A SkipList is a probabilistic data structure designed for efficient operations on dynamic sorted data. It provides $O(\log n)$ average time complexity for insertion, removal, and search operations. This specific implementation is augmented to support index-based lookups in $O(\log n)$ average time.

    Complexity

    • Average Time Complexity: $O(\log n)$ for insert, remove, find, and getIndex.
    • Average Space Complexity: $O(n)$.
    • Worst Case Space Complexity: $O(n \log n)$.
    • Worst Case Time Complexity: $O(n)$ for all operations.

    Optimization Tip

    To achieve optimal performance, instantiate the SkipList with a height approximately equal to or slightly greater than $\log(n)$, where $n$ is the expected number of elements in the list.

    // Example initialization
    // height: log2(expected_elements)
    // minValue: lower bound for elements
    // maxValue: upper bound for elements
    SkipList skipList = new SkipList(10, Integer.MIN_VALUE, Integer.MAX_VALUE);