Add graph-algorithms module: Dijkstra (heap, array, lazy deletion), Bellman-Ford reference, Kahn topological sort, Union-Find strategies, Kruskal and Prim (15 tests, 13 transcripts)

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017ic4b3RckKrNbvvStS36UM
This commit is contained in:
2026-11-18 21:05:59 +05:30
co-authored by Claude Sonnet 5.5
parent eef175d669
commit 05f41a3ef7
30 changed files with 1490 additions and 0 deletions
@@ -0,0 +1,29 @@
Dijkstra trace, classic 6-vertex graph, source A
================================================
pop 0 @0 settled
relax 0 -> 1 dist[1] = 7
relax 0 -> 2 dist[2] = 9
relax 0 -> 5 dist[5] = 14
pop 1 @7 settled
relax 1 -> 3 dist[3] = 22
pop 2 @9 settled
relax 2 -> 3 dist[3] = 20
relax 2 -> 5 dist[5] = 11
pop 5 @11 settled
relax 5 -> 4 dist[4] = 20
pop 5 @14 stale (best known is 11) -> skipped
pop 4 @20 settled
pop 3 @20 settled
pop 3 @22 stale (best known is 20) -> skipped
dist[A] = 0
dist[B] = 7
dist[C] = 9
dist[D] = 20
dist[E] = 20
dist[F] = 11
cheapest path to E: A -> C -> F -> E (cost 20)
stats: Stats[pushes=8, pops=8, stalePops=2, edgeScans=18, relaxations=7]
@@ -0,0 +1,7 @@
Lazy deletion: with and without the stale-entry check (seeded graph, V=2000, E=10000)
=====================================================================================
with check : Stats[pushes=2831, pops=2831, stalePops=849, edgeScans=9915, relaxations=2830]
without check : Stats[pushes=2831, pops=2831, stalePops=849, edgeScans=14168, relaxations=2830]
same distances: true
@@ -0,0 +1,6 @@
Dijkstra vs Bellman-Ford on seeded random graphs
================================================
cases checked : 3000
cases with an unreachable vertex: 2323
disagreements : 0
@@ -0,0 +1,7 @@
One negative edge: A->B 1, A->C 2, C->B -5
==========================================
Dijkstra.run(g, A) throws : Dijkstra needs non-negative weights; use Bellman-Ford instead
visited-set version, dist[B] : 1
lazy version, check removed, dist[B]: -3 (pops 4)
Bellman-Ford, dist[B] : -3
@@ -0,0 +1,9 @@
Array scan O(V^2) vs binary heap: work counted, not timed
=========================================================
sparse V=2000 E=8000:
array version : min-scan steps 3916000
heap version : pushes 2541, pops 2541, edge scans 7784
dense V=600 E=150000:
array version : min-scan steps 360000
heap version : pushes 2292, pops 2292, edge scans 150000
@@ -0,0 +1,12 @@
Kahn's algorithm on a 7-course prerequisite graph
=================================================
kahn (queue order) : Intro, Maths, DBMS, DSA, OS, Networks, Compilers acyclic=true
kahn (smallest first): Intro, Maths, DSA, DBMS, OS, Networks, Compilers acyclic=true
semester 1 : Intro, Maths
semester 2 : DSA, DBMS
semester 3 : OS
semester 4 : Networks, Compilers
cycle 1->2->1 plus 2->3, kahn returns: [0] acyclic=false
(vertex 0 is the only one that could be ordered; the flag is the only signal of the cycle)
@@ -0,0 +1,7 @@
Kahn and DFS vs a transitive-closure reference on seeded random graphs
======================================================================
cases checked : 3000
cases with a cycle : 2016
cases acyclic (orders valid): 984
disagreements : 0
@@ -0,0 +1,5 @@
A 100,000-vertex chain on a thread with a 512 KB stack
======================================================
recursive DFS : StackOverflowError
Kahn : ordered 100000 vertices, acyclic=true
@@ -0,0 +1,13 @@
union(0,1), union(1,2), ... union(n-2,n-1) with n=20000, then find(0) twice
===========================================================================
NAIVE max depth 19999 | find(0) #1: 19999 hops | find(0) #2: 19999 hops
BY_SIZE max depth 1 | find(0) #1: 1 hops | find(0) #2: 1 hops
COMPRESSION max depth 19999 | find(0) #1: 19999 hops | find(0) #2: 1 hops
BY_SIZE_AND_COMPRESSION max depth 1 | find(0) #1: 1 hops | find(0) #2: 1 hops
Tournament pattern, n=16384: round r joins sets of size 2^r, so equal sizes meet every time
NAIVE max depth 14
BY_SIZE max depth 14
COMPRESSION max depth 13
BY_SIZE_AND_COMPRESSION max depth 13
@@ -0,0 +1,6 @@
Union-Find (all four strategies) vs BFS component count
=======================================================
cases checked : 2000
strategies : [NAIVE, BY_SIZE, COMPRESSION, BY_SIZE_AND_COMPRESSION]
disagreements : 0
@@ -0,0 +1,14 @@
Minimum spanning tree of the classic 6-vertex graph
===================================================
Kruskal total weight: 33
C - F (2)
D - E (6)
A - B (7)
A - C (9)
E - F (9)
Prim total weight : 33
random graphs checked : 2000
of which disconnected : 1330
Kruskal/Prim disagreements : 0
@@ -0,0 +1,6 @@
Bellman-Ford with int infinity: 2 -> 3 is unreachable from 0
============================================================
Integer.MAX_VALUE + 5 = -2147483644
int version, dist[3] : -2147483644
long + check, dist[3] : unreachable (INF)
@@ -0,0 +1,5 @@
union(0,1) then union(0,2), then ask connected(0,1)
===================================================
buggy connected(0,1): false
correct connected(0,1): true