A Java program that demonstrates recursion finding the smallest value in an array, framed as a real-world problem: a DARPA-style autonomous drone swarm needs to dispatch the drone closest to a target. Pure recursion solves the geometry; xAI's Grok then analyzes the full swarm and returns a structured tactical decision (action / primary / backup / risk / ETA) which the program parses and executes — color-coded orders, ASCII tactical map, and a timestamped audit log.
Built as a CSC263 (Data Structures / Recursion) assignment, then expanded into a portfolio piece.
- Two recursive algorithms for finding the minimum of an array:
- Linear head-vs-rest recursion —
O(n)call depth - Divide-and-conquer recursion —
O(log n)call depth
- Linear head-vs-rest recursion —
- Iterative baseline that cross-checks both recursive answers.
- Call tracing that prints every recursive descent and combine step so the stack-unwind is visible in the terminal.
- ASCII tactical map of the battlefield (drones as letters, target as
X, primary drone in red, AI-chosen backup in magenta). - Agentic AI layer: Grok receives the full swarm state and replies in a
strict structured format (
ACTION / PRIMARY / BACKUP / ETA_MIN / RISK / REASONING). The program parses the reply and acts on it — color-coded orders, re-rendered map, and an append-onlydispatch_log.txtaudit trail. - Side-by-side timing comparison: local Java recursion (microseconds) vs Grok API round-trip (seconds, mostly network).
- Asymptotic-complexity benchmark across array sizes from 100 to 500,000. Linear recursion runs on a dedicated 64 MB-stack thread so it completes cleanly at every size — a textbook example of properly sizing thread stacks in production Java.
git clone https://github.com/timastras9/java-recursion-darpa-demo.git
cd java-recursion-darpa-demo
cp .env.example .env # paste your xAI key inside
./run.shgit clone https://github.com/timastras9/java-recursion-darpa-demo.git
cd java-recursion-darpa-demo
copy .env.example .env
notepad .env :: paste your xAI key, save
run.batjavac RecursiveMin/*.java
java -cp . RecursiveMin.DroneDispatchWithout a Grok key the program still runs end-to-end; the AI decision step
prints (Grok offline) and falls back to the recursive geometric pick.
- Java 11 or newer (developed and tested on Java 25; only uses the JDK standard library — no Maven, no Gradle, no external dependencies).
- An xAI API key (free tier works). Get one at https://console.x.ai/
RecursiveMin/
├── DroneDispatch.java # recursion algorithms + tactical map + benchmark + decision pipeline
└── GrokClient.java # tiny xAI Grok REST wrapper (no SDK, just java.net.http)
Two files. No build system. The whole thing is ~900 lines of commented Java that you can read top to bottom in one sitting.
| Method | Strategy | Stack depth |
|---|---|---|
findMinLinear |
Head-vs-rest | O(n) |
findMinDivideConquer |
Split in half, recurse, combine | O(log n) |
findMinIterative |
Simple loop (correctness cross-check) | O(1) |
findMinLinearOnBigStack |
Wraps linear in a 64 MB-stack thread | safe past 500k |
Each recursive method documents its base case and recursive case explicitly in the source.
- Recursion computes the geometrically nearest drone.
- The full swarm state + target + recursive pick are serialized into a prompt for Grok.
- Grok replies in a strict line-based schema (
ACTION:,PRIMARY:,BACKUP:,ETA_MIN:,RISK:,REASONING:). - The program parses the reply, validates callsigns against the swarm,
and executes: prints color-coded orders, redraws the map with the
backup highlighted, and appends a timestamped entry to
dispatch_log.txt.
- Java 11+ (JDK standard library only — no external deps)
- xAI Grok API (
grok-4.3) viajava.net.http.HttpClient - ANSI escape codes for color
.envfile loader built from scratch (no dotenv library needed)
The assignment was a basic "recursive minimum of an array." I wanted to ground it in a real problem — picking the nearest of N drones to a target is exactly that, plus it makes the answer visceral. The AI layer turns the geometry answer into a tactical decision a human commander would actually make.
MIT — do whatever you want with it.
Timothy Astras · github.com/timastras9
All original ideas for this program — the topic, the choice of two recursive strategies, the ASCII tactical map, the structured-decision AI integration, the asymptotic benchmark — are my own. Claude Code (Anthropic) was used as a coding assistant to help implement those ideas in Java. I directed every step, reviewed every line, ran the program, and verified the output.