Skip to content

Routes & Core Algorithms

In plain English

Core problem-solving structures implemented from scratch in Java — including shortest-path routing, fast lookup caches, sorting, and dynamic programming.

A Java 21 library implementing core generic data structures alongside validated graph, sorting, selection, and dynamic programming algorithms.

1. Why I built it

Created to keep generic foundational data structures together in one modular Maven package with unit tests and an empirical timing harness.

Problem being solvedStandard library heaps lack `decreaseKey`, while naive recursive MergeSort and 2D DP tables allocate excessive stack frames and heap arrays.

2. How it works

Organised across `agency.shss7.algolab.structures`, `graph`, `sorting`, `dp`, and `benchmark` packages tested by `AlgorithmLaboratoryTest.java`.

Standard `java.util.PriorityQueue` lacks `O(log V)` in-place `decreaseKey`, forcing naive Dijkstra implementations to leave duplicate entries on the heap.

Java Graph Algorithms builds generic `IndexedMinHeap<K>` (maintaining a live index bijection in `positionByKey`), `LruCache<K, V>` (`HashMap` + doubly-linked sentinel list with `get`, `put`, `containsKey`, and `remove`), and iterative `DisjointSetUnion`. Built on top are Dijkstra shortest paths, Kahn topological sort, Kruskal MST, iterative bottom-up MergeSort, Hoare QuickSelect, and 1D rolling-array DP.

Defensive validation rejects null graphs, out-of-range vertex indices (`0..vertexCount - 1`), and negative DP bounds, with a JIT-warmed runner (`java-algorithms-lab-1.2.0.jar`) comparing MergeSort against QuickSelect.

3. Output & Source Specimen

Algorithm Time & Space Complexity Matrix

Algorithm / StructurePackageTime ComplexitySpace / Key Mechanism
IndexedMinHeap<K>structuresO(log V) decreaseKeyHashMap position bijection
LruCache<K, V>structuresO(1) get / put / removeSentinel doubly-linked nodes
Dijkstra Shortest PathgraphO(E log V)IndexedMinHeap edge relaxation
Kruskal MSTgraphO(E log E)Iterative DisjointSetUnion
Bottom-Up MergeSortsortingO(N log N) worst-caseSingle reusable auxiliary buffer
Hoare QuickSelectsortingO(N) average medianIn-place partition
0/1 Knapsack & Edit Dist.dpO(N · W) / O(N · M)1D rolling array compression
Asymptotic bounds implemented across agency.shss7.algolab packagesRun checks: mvn test -B

4. Design Decisions & Challenges

Key Design Decisions

  • 01.Maintained a synchronised `positionByKey` hash index inside `IndexedMinHeap` so swaps keep `insertOrDecreaseKey` `O(log V)`.
  • 02.Added `containsKey()` and `remove()` alongside explicit thread-safety notes in `LruCache`.
  • 03.Validated graph nullness, Kruskal vertex bounds, and negative capacities cleanly across `GraphAlgorithms` and `DynamicProgramming`.

Edge Cases Handled

  • Keeping `heap` and `positionByKey` bijective across every swap permutation during `siftUp` and `siftDown`.

5. Testing & Limits

Automated Checks

Verified with 11 JUnit 5 tests in `AlgorithmLaboratoryTest.java` covering LRU eviction/removal/order snapshot, heap decrease-key & `remove()`, Dijkstra paths, Bellman-Ford negative weights/cycles, topological sort, Kruskal bounds, sorting, QuickSelect, and DP.

Current Limits & Next Steps

  • `BenchmarkRunner` uses manual warmup loops + `System.nanoTime()` to avoid extra dependencies rather than full JMH fork isolation.

6. Key Takeaways

What building Routes & Core Algorithms reinforced

Implementing iterative bottom-up MergeSort and QuickSelect side by side clarifies why linear `O(N)` selection beats full `O(N log N)` sorting when only medians are needed.

  • — 11 JUnit 5 suites pass under `mvn test -B` in CI.
  • — Source hosted at github.com/16lv/java-graph-algorithms.

Recent Activity

Latest repository updates

All changes for 16lv/java-graph-algorithms

feat: add O(log V) IndexedMinHeap.remove() and LruCache order snapshot

Implemented key removal with index-bijection maintenance on IndexedMinHeap and read-only recency inspection via LruCache.keysMostRecentFirst().

2026-11-24837ac73

feat: add Bellman-Ford shortest paths with reachable negative-cycle detection

Added SignedEdge<V> and GraphAlgorithms.bellmanFord() supporting negative edge weights while throwing IllegalStateException on reachable negative-weight cycles.

2026-11-23de6eb24

Bound QuickSort stack depth to O(log N) by recursing on smaller partition

Updated SortingAndSelection.quickSortRange() to recurse strictly on the smaller subarray and iterate across the larger side cleanly.

2026-11-214394cae