Creating a non-recursive pathfinding graph algorithm in MiniZinc involves modeling the problem without recursion, typically using constraints and integer variables to represent paths, distances, or visited nodes.
Let’s walk through an example: finding the shortest path from a source node to a destination node in a directed graph, modeled as a constraint satisfaction problem (CSP) in MiniZinc.
🧩 Problem Overview
We are given:
- A directed graph with
n nodes.
- An adjacency matrix or list of edges with associated weights (costs/distances).
- A source node and a target node.
Goal:
- Find the shortest path from source to target using MiniZinc, avoiding recursive definitions.
✅ Step-by-Step Non-Recursive Pathfinding Model in MiniZinc
% Parameters
int: n; % Number of nodes
set of int: NODES = 1..n;
% Adjacency matrix: cost[i,j] is the cost from i to j, or 0 if no edge
array[NODES, NODES] of int: cost;
int: source;
int: target;
% Decision Variables
% We'll use distance[i] to represent the shortest known distance from source to node i
array[NODES] of var 0..total_max_cost: dist;
% Total maximum possible cost (sum of all edge costs)
int: total_max_cost = sum(i in NODES, j in NODES)(cost[i,j]);
% Constraints
constraint dist[source] = 0;
constraint forall(i in NODES where i != source)(
exists(j in NODES where cost[j,i] > 0)(
dist[i] = dist[j] + cost[j,i]
)
);
% Ensure that we reach the target
constraint dist[target] <= total_max_cost;
% Objective: minimize the distance to the target node
solve minimize dist[target];
% Output
output ["Shortest path cost to node ", show(target), ": ", show(dist[target])];
📌 Notes on This Model
- This model mimics Dijkstra's algorithm behavior but in a declarative way using constraints.
- It uses
dist[i] to track the minimal distance to each node.
- The key idea is: for every node
i, there must be some predecessor j such that dist[i] = dist[j] + cost[j,i].
- This avoids recursion by expressing the dependency across the entire graph via constraints.
- No explicit path construction here — just the cost of the shortest path. You can extend this to recover the actual path if needed.
🔁 Optional: Reconstructing the Path
If you want to get the actual path, add these components:
% Add variable to store previous node on shortest path
array[NODES] of var 0..n: prev;
% For each node i ≠ source, there exists a j such that:
constraint forall(i in NODES where i != source)(
exists(j in NODES where cost[j,i] > 0)(
dist[i] = dist[j] + cost[j,i] /\
prev[i] = j
)
);
% Now output the path
function array[int] of var int: get_path(var int: end_node) =
if end_node == source then
[source]
else
array_concat([end_node], get_path(prev[end_node]))
endif;
output [
"Path: " ++ show(reverse(get_path(target))) ++
", Cost: " ++ show(dist[target])
];
However, note that get_path uses recursion, so if you want strictly non-recursive, you need to avoid it. Here's how to reconstruct the path without recursion:
🚫 Non-Recursive Path Reconstruction (Alternative)
Use a fixed-size array and loop-like constraints:
int: max_path_length = n;
% Path will have at most n nodes
array[1..max_path_length] of var 0..n: path;
% First node is source
constraint path[1] = source;
% Enforce valid steps along the path
constraint forall(i in 2..max_path_length)(
(path[i] = 0 /\ path[i-1] = 0) \/
(path[i-1] != 0 /\ cost[path[i-1], path[i]] > 0)
);
% The last node is target
constraint exists(i in 1..max_path_length)(path[i] = target);
% All nodes before target should not be zero
constraint forall(i in 1..max_path_length where path[i] == target)(
if i > 1 then path[i-1] != 0 endif
);
% Ensure the total cost matches dist[target]
constraint dist[target] = sum(i in 2..max_path_length where path[i] != 0)(
cost[path[i-1], path[i]]
);
output [
"Path: " ++ show([path[i] | i in 1..max_path_length where path[i] != 0]) ++
", Cost: " ++ show(dist[target])
];
This version avoids recursion entirely and builds the path step-by-step using constraints.
📦 Example Data File (data.dzn)
n = 5;
cost = array2d(1..5, 1..5, [
0, 3, 2, 0, 0,
0, 0, 0, 4, 0,
0, 0, 0, 5, 0,
0, 0, 0, 0, 1,
0, 0, 0, 0, 0
]);
source = 1;
target = 5;
✅ Summary
| Feature | Implemented |
|---|
| Non-recursive | ✅ |
| Shortest path cost | ✅ |
| Path reconstruction | ✅ (optional, non-recursive version provided) |
| CSP-based | ✅ |
Would you like a full working .mzn file and data example? Or would you prefer a different kind of pathfinding logic (e.g., BFS or DFS)?