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.
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 / Structure | Package | Time Complexity | Space / Key Mechanism |
|---|---|---|---|
| IndexedMinHeap<K> | structures | O(log V) decreaseKey | HashMap position bijection |
| LruCache<K, V> | structures | O(1) get / put / remove | Sentinel doubly-linked nodes |
| Dijkstra Shortest Path | graph | O(E log V) | IndexedMinHeap edge relaxation |
| Kruskal MST | graph | O(E log E) | Iterative DisjointSetUnion |
| Bottom-Up MergeSort | sorting | O(N log N) worst-case | Single reusable auxiliary buffer |
| Hoare QuickSelect | sorting | O(N) average median | In-place partition |
| 0/1 Knapsack & Edit Dist. | dp | O(N · W) / O(N · M) | 1D rolling array compression |
Run checks: mvn test -B4. 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
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().
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.
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.