Manuscript Offprint

arXiv:2609.31114

From Shortcut Learning to Discrete Neural Insertion Sort

Konstantinos Mylonas, Thrasyvoulos Spyropoulos

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

Abstract

Neural algorithmic reasoning aims to train neural networks to follow known algorithms and generalize beyond the input sizes seen during training. However, correct final outputs and intermediate supervision do not necessarily show that a model follows the intended execution. We study this problem using insertion sort. Our analysis of the CLRS30 baseline NAR shows that the hint objective is weakly optimized and that hint accuracy remains low. Moreover, many intermediate representations can already be decoded into sorted sequences before the reference insertion-sort execution terminates, suggesting that the model learns a shortcut to the final output. Motivated by these findings, we introduce Discrete Neural Insertion Sort. Our model represents the sequence as a chain, separates scalar exchanges from control-state transitions, and projects node representations back to discrete states after every processor step. When trained only on sequences of length 16, the model achieves 100% sorted-sequence accuracy on sequences of length 64 and 128. However, an ablation shows that discretization and graph structure alone are insufficient: without additional supervision of the global inner-loop state, the model fails even at the training length. Our results show that discrete execution can support strong length generalization, while also highlighting the problem-specific inductive bias required to learn a faithful algorithmic execution.

1. Introduction

Neural networks have achieved impressive results in areas such as computer vision, language generation, and reasoning. However, strong performance within the training distribution does not always translate to reliable out-of-distribution generalization (Geirhos et al., 2020). This limitation can appear even in simple algorithmic tasks. For example, neural networks trained to add real numbers may fit their training distribution accurately while failing to recover the exact addition rule or extrapolate outside that distribution (Klindt, 2023).

This behavior is particularly interesting because many of these tasks already have known algorithmic solutions. The same addition procedure can be applied to numbers of different magnitudes, and the same sorting algorithm can be applied to sequences of different lengths. In contrast, a neural network may learn statistical patterns that are specific to the examples observed during training. Its predictions can therefore break down when the input size or distribution changes.

Neural algorithmic reasoning (NAR) studies whether neural networks can instead be trained to follow the steps of a known algorithm (Veličković & Blundell, 2021). Rather than learning only a direct mapping from inputs to outputs, the model is encouraged to reproduce the intermediate execution of the reference algorithm. If it learns the underlying transition rule, the same neural processor may be applied repeatedly to inputs and trajectories that are larger than those seen during training.

Our goal is not simply to train a neural network that can sort a sequence: insertion sort already solves this problem exactly. Instead, we use insertion sort as a controlled setting for studying whether a neural model can learn a computation that is faithful to a reference algorithm, interpretable through its intermediate states, and able to generalize beyond its training distribution. More broadly, we ask what architectural constraints and supervision are needed for such a computation to emerge through gradient-based optimization.

The CLRS benchmark provides a common framework for studying this question (Veličković et al., 2022). In addition to the input and final output, it provides hints that describe the intermediate states of the reference algorithm. A neural processor is supervised to predict these hints at every execution step. The goal is to encourage the model to learn the transition rules of the algorithm rather than a direct mapping from the input to the final answer. If the learned transition is independent of the training size, the same processor can be applied for more steps and on larger inputs.

However, accurate final predictions and intermediate supervision do not necessarily show that the model follows the intended algorithm. This distinction is especially important for sorting. Different sorting procedures produce the same final ordering while following very different intermediate trajectories. A model may therefore learn a shortcut that supports the final prediction without reproducing the execution that generated its hints.

In this work, we study this issue using insertion sort. We train the official CLRS baseline on insertion sort and examine both its hint predictions and its intermediate node representations. We find that the output objective is optimized much more effectively than the hint objective, while the accuracy of a representative hint remains low throughout the trajectory. We then apply the final-output decoder to embeddings produced at intermediate processor steps. For many inputs, these embeddings already decode to a sorted sequence several steps before the reference insertion-sort execution terminates. We refer to this behavior as rush-to-output: the model learns representations that support the final answer without faithfully following the intended intermediate trajectory.

Motivated by this observation, we introduce Discrete Neural Insertion Sort, a neural executor that more closely follows the control flow of insertion sort. The input sequence is represented as a bidirectional chain graph, reflecting the local comparisons and exchanges performed by the algorithm. Each node carries a scalar value and a discrete state describing its current role, such as i, j, or k (see Fig.1 for an explanation of these roles). Scalar movement and discrete state transitions are handled separately. After every processor step, the updated node representations are projected back to discrete states, preventing arbitrary continuous node embeddings from being carried across the complete execution.

When trained only on sequences of length 16, the resulting model achieves 100% accuracy on sequences of length 64 and 128. These results show that strong length generalization is possible when the model learns the correct discrete transition rule. However, our ablation shows that discretization alone is not sufficient. Additional inductive bias is needed to guide the optimization process toward the correct solution. This raises a broader question for neural algorithmic reasoning: how much knowledge about an algorithm must be encoded in the architecture and training objective for reliable execution to emerge?

Our contributions are as follows:

• We analyze the CLRS30 insertion-sort baseline and show that its intermediate representations do not faithfully follow the supervised insertion-sort trajectory.

• We propose a discrete neural executor for insertion sort that separates scalar updates from control-state updates and restricts the recurrent computation through a discrete state bottleneck.

• We show that a model trained on sequences of length 16 can achieve perfect execution on sequences of length 64 and 128.

• We show that graph structure and discretization is not enough and additional inductive bias is required for this result.