PathForge — Bring Your Graph
Any graph works through one interface, IGraphSource. Implement it and pass it to
solver.BuildFrom(...) — exactly like the built-in adapters.
public interface IGraphSource
{
uint NumVertices { get; }
uint NumEdges { get; }
void FillEdges(uint[] sources, uint[] targets, uint[] weights);
bool IsBlocked(uint vertex);
}
Rules
NumVertices: the vertex count; IDs are0 .. NumVertices-1.NumEdges: the exact number of directed edges. A two-way connection is two edges (a → b and b → a); the two may have different costs, and one-way edges are fine.FillEdges: write every edge into the three arrays (each of lengthNumEdges).- Every cost is
1 .. 536 870 912(DnskSolver.MaxEdgeWeight); make the cheapest move cost 1. If one edge breaks the rule,BuildFromthrowsArgumentExceptionand adds nothing. Clamp costs you compute (Math.Max(1u, cost)); check costs your players enter. IsBlocked:truefor vertices that must not be entered (used by the helpers).
Example: adjacency list
public class AdjacencyListGraph : IGraphSource
{
private readonly Dictionary<uint, List<(uint target, uint weight)>> _adj;
private readonly HashSet<uint> _blocked;
private readonly uint _vertexCount;
public AdjacencyListGraph(uint vertexCount,
Dictionary<uint, List<(uint target, uint weight)>> adj, HashSet<uint> blocked)
{
_vertexCount = vertexCount; _adj = adj; _blocked = blocked;
}
public uint NumVertices => _vertexCount;
public uint NumEdges
{
get
{
uint count = 0;
foreach (var kvp in _adj) count += (uint)kvp.Value.Count;
return count;
}
}
public void FillEdges(uint[] sources, uint[] targets, uint[] weights)
{
int i = 0;
foreach (var kvp in _adj)
{
foreach (var (t, w) in kvp.Value)
{
sources[i] = kvp.Key;
targets[i] = t;
weights[i] = w;
i++;
}
}
}
public bool IsBlocked(uint vertex) => _blocked.Contains(vertex);
}
// Use it like any adapter:
var graph = new AdjacencyListGraph(vertexCount, adjacency, blocked);
var solver = new DnskSolver(graph.NumVertices);
solver.BuildFrom(graph);
Example: compressed rows (offsets + targets)
public class CsrGraph : IGraphSource
{
private readonly uint[] _offsets; // length vertexCount + 1
private readonly uint[] _targets;
private readonly uint[] _weights;
private readonly uint _vertexCount;
public CsrGraph(uint vertexCount, uint[] offsets, uint[] targets, uint[] weights)
{
_vertexCount = vertexCount; _offsets = offsets; _targets = targets; _weights = weights;
}
public uint NumVertices => _vertexCount;
public uint NumEdges => (uint)_targets.Length;
public void FillEdges(uint[] sources, uint[] targets, uint[] weights)
{
int i = 0;
for (uint v = 0; v < _vertexCount; v++)
{
for (uint j = _offsets[v]; j < _offsets[v + 1]; j++)
{
sources[i] = v;
targets[i] = _targets[j];
weights[i] = _weights[j];
i++;
}
}
}
public bool IsBlocked(uint vertex) => false;
}