In this post, I have the demo controller that makes two reference points $ k $Shortest route algorithms. The entire GitHub project is here. Next, my code continues:

`net.coderodde.graph.kshortest.Demo`

```
net.coderodde.graph.kshortest package;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import net.coderodde.graph.DirectedGraphNode;
import net.coderodde.graph.DirectedGraphWeightFunction;
import net.coderodde.graph.kshortest.impl.DefaultKShortestPathFinder;
import net.coderodde.graph.kshortest.impl.FasterDefaultKShortestPathFinder;
public class demo {
int static private end WARMUP_ITERATIONS = 5;
int private final private BENCHMARK_ITERATIONS = 20;
int private final private GRAPH_NODES = 5_000;
static int private end GRAPH_ARCS = 50_000;
final static private double MAX_WEIGHT = 1000.0;
static final private int K = 25;
empty main public static (String[] args) {
demo1 ();
long seed = System.currentTimeMillis ();
Random random = new random (seed);
List graph = constructGraph (GRAPH_NODES,
GRAPH_ARCS,
random);
DirectedGraphWeightFunction weightFunction =
new DirectedGraphWeightFunction ();
addArcWeights (graphic, weightFunction, MAX_WEIGHT, random);
System.out.println ("=== Graphic built! Heating ... ===");
heating (random, graphic, weight function, K);
System.out.println ("=== Full Warm Up! Benchmarking ... ===");
reference point (random, graphic, weightFunction, K);
}
Final static private list
constructGraph (int graphNodes,
int graphArcs,
Random Random) {
List graph = new ArrayList <> (graphNodes);
for (int i = 0; i <graphNodes; i ++) {
graph.add (new DirectedGraphNode (i));
}
for (int i = 0; i <graphArcs; i ++) {
DirectedGraphNode tail = choose (graphic, random);
DirectedGraphNode head = choose (graphic, random);
tail.addChild (head);
}
return chart;
}
static final private vacuum
addArcWeights (List graphic,
DirectedGraphWeightFunction weightFunction,
double maxWeight,
Random Random) {
for (DirectedGraphNode tail: graph) {
for (DirectedGraphNode head: tail.children ()) {
weightFunction.put (tail, head, random.nextDouble () * maxWeight);
}
}
}
static empty private
heating (random chance,
List graphic,
DirectedGraphWeightFunction weightFunction,
int k) {
run fake
WARMUP_ITERATIONS,
random,
graphic,
Function weight
k);
}
static private reference index (random randomized,
List graphic,
DirectedGraphWeightFunction weightFunction,
int k) {
run
BENCHMARK_ITERATIONS,
random,
graphic,
Function weight
k);
}
Private static list
Random nodes (List graphic,
Randomly,
int size) {
List nodes = new ArrayList <> (size);
for (int i = 0; i <size; i ++) {
nodes.add (choose (graphic, random));
}
return nodes;
}
static static vacuum execution (Boolean printing,
int iterations,
Randomly,
List graphic,
DirectedGraphWeightFunction weightFunction,
int k) {
AbstractKShortestPathFinder finder1 = new DefaultKShortestPathFinder ();
AbstractKShortestPathFinder finder2 =
new FasterDefaultKShortestPathFinder ();
List sourceNodes = randomNodes (graphic,
random,
iterations);
List targetNodes = randomNodes (graphic,
random,
iterations);
List <List> results1 = new ArrayList <> (iterations);
List <List> results2 = new ArrayList <> (iterations);
long nanoTotalDuration1 = 0L;
long nanoTotalDuration2 = 0L;
for (int iter = 0; iter <iterations; iter ++) {
DirectedGraphNode sourceNode = sourceNodes.get (iter);
DirectedGraphNode targetNode = targetNodes.get (iter);
long startTime = System.nanoTime ();
List result1 =
finder1.findShortestPaths (
sourceNode,
targetNode,
weight function, k);
long endTime = System.nanoTime ();
long nanoDuration = endTime - startTime;
nanoTotalDuration1 + = nanoDuration;
if (print) {
System.out.println (finder1.getClass (). GetSimpleName () + "in" +
(long) (1e-6 * nanoDuration) +
"milliseconds");
}
nanoTotalDuration1 + = nanoDuration;
results1.add (result1);
}
for (int iter = 0; iter <iterations; iter ++) {
DirectedGraphNode sourceNode = sourceNodes.get (iter);
DirectedGraphNode targetNode = targetNodes.get (iter);
long startTime = System.nanoTime ();
List result2 =
finder2.findShortestPaths (
sourceNode,
targetNode,
weight function, k);
long endTime = System.nanoTime ();
long nanoDuration = endTime - startTime;
nanoTotalDuration2 + = nanoDuration;
if (print) {
System.out.println (finder2.getClass (). GetSimpleName () + "in" +
(long) (1e-6 * nanoDuration) +
"milliseconds");
}
results2.add (result2);
}
if (print) {
System.out.println (
"Algorithms of agreement:" + results1.equals (results2));
System.out.println (
finder1.getClass (). getSimpleName () + "in n" +
(long) (1e-6 * nanoTotalDuration1) + "milliseconds.");
System.out.println (finder2.getClass (). GetSimpleName () + "in n" +
(long) (1e-6 * nanoTotalDuration2) + "milieconds.");
}
}
private static T choose (List list, random random) {
return list.get (random.nextInt (list.size ()));
}
private static demo demo1 () {
// 1 4
// / /
// 0 3 6
// / / /
// 2 5
DirectedGraphNode a = new DirectedGraphNode (0);
DirectedGraphNode b = new DirectedGraphNode (1);
DirectedGraphNode c = new DirectedGraphNode (2);
DirectedGraphNode d = new DirectedGraphNode (3);
DirectedGraphNode e = new DirectedGraphNode (4);
DirectedGraphNode f = new DirectedGraphNode (5);
DirectedGraphNode g = new DirectedGraphNode (6);
// The edges on line 0 - 6 have a weight of 1.0.
// The borders below line 0 - 6 have a weight of 2.0
DirectedGraphWeightFunction weightFunction =
new DirectedGraphWeightFunction ();
a.addChild (b); weightFunction.put (a, b, 1);
a.addChild (c); weightFunction.put (a, c, 2);
b.addChild (d); weightFunction.put (b, d, 1);
c.addChild (d); weightFunction.put (c, d, 2);
d.addChild (e); weightFunction.put (d, e, 1);
d.addChild (f); weightFunction.put (d, f, 2);
e.addChild (g); weightFunction.put (e, g, 1);
f.addChild (g); weightFunction.put (f, g, 2);
List routes = new FasterDefaultKShortestPathFinder ()
.findShortestPaths (a, g, weightFunction, 5);
for (Path: routes) {
System.out.println (Arrays.toString (path.getNodeList (). ToArray ()));
}
}
}
```

**Sample output**

The typical output follows:

```
```
Algorithms of agreement: truth
DefaultKShortestPathFinder in
31517 milliseconds
FasterDefaultKShortestPathFinder in
1304 millieconds

**Critical request**

Please, tell me anything you can think of.