Principle:Spcl Graph of thoughts Best N Selection
| Knowledge Sources | |
|---|---|
| Domains | Graph_Reasoning, Thought_Operations |
| Last Updated | 2026-02-14 |
| Implemented By | Implementation:Spcl_Graph_of_thoughts_KeepBestN_Operation |
Overview
Selection pattern that retains only the N highest-scoring (or lowest-scoring) thoughts from predecessors, enabling quality-based pruning in reasoning graphs.
Description
The KeepBestN operation is a filtering mechanism in the Graph of Thoughts framework that selects the top N thoughts from all predecessor operations based on their scores. This operation is essential for pruning the reasoning graph, allowing only the highest-quality candidates to proceed to subsequent operations.
Key characteristics:
- Requires all predecessor thoughts to be scored before execution (asserts that every thought has the
scoredflag set) - Sorts all predecessor thoughts by score and retains only the top N
- The
higher_is_betterflag determines the sort direction: whenTrue(default), higher scores are preferred; whenFalse, lower scores are preferred - Creates cloned copies of the selected thoughts (via
Thought.from_thought) rather than passing references, ensuring immutability of predecessor state - The parameter
nmust be greater than zero (enforced by assertion) - Includes error handling for non-float scores, filtering out invalid scores and logging errors
Usage
Use the KeepBestN operation after a Score operation to prune the set of candidate thoughts down to the best N. This is a common pattern in GoT topologies:
- After generation: Generate multiple candidates, score them, then KeepBestN to select the best one(s) for further processing
- Before aggregation: When merging results from multiple branches, keep only the best candidate from each branch
- Iterative refinement: In generate-score-keep-improve cycles, KeepBestN controls the beam width
Typical usage patterns include:
Generate(5) -> Score -> KeepBestN(1)to select the single best candidate from 5 generated optionsGenerate -> Score -> KeepBestN(3) -> Improve -> Score -> KeepBestN(1)for iterative refinement with a narrowing beam
Theoretical Basis
The KeepBestN operation implements beam search pruning within the Graph of Thoughts framework. In the GoT paper, the authors describe how multiple candidate solutions can be generated and evaluated in parallel, with only the best being retained for further processing. This pruning is critical for controlling the computational cost of graph-based reasoning.
In graph-theoretic terms, KeepBestN acts as a bottleneck vertex that reduces the number of active thought paths. Given a set of k predecessor thoughts with scores s_1, s_2, ..., s_k, the KeepBestN(n) operation selects the subset of size min(n, k) with the highest (or lowest) scores:
KeepBestN(n, thoughts) = top_n(sort(thoughts, key=score, reverse=higher_is_better))
This is analogous to beam search in sequence generation, where the beam width determines how many candidates are kept at each step. In GoT, KeepBestN enables a more flexible version of this concept, as it can be applied at any point in the graph rather than only at sequential steps.
The scoring mechanism is decoupled from the selection: the Score operation assigns values, and KeepBestN filters based on those values. This separation of concerns allows different scoring strategies (LM-based, programmatic, combined) to be used interchangeably.
Code Reference
The Best N Selection principle is implemented in the KeepBestN class:
- Source file:
graph_of_thoughts/operations/operations.py, Lines 612-709 - Class:
KeepBestN(Operation) - Operation type:
OperationType.keep_best_n
Related Pages
- Implementation:Spcl_Graph_of_thoughts_KeepBestN_Operation - Concrete implementation of this principle
- Heuristic:Spcl_Graph_of_thoughts_Scoring_With_Error_Counting
- Principle:Spcl_Graph_of_thoughts_Validity_Filtering - KeepValid operation for boolean-based filtering (complementary to score-based filtering)
- Principle:Spcl_Graph_of_thoughts_Thought_Aggregation - Aggregate operation often follows KeepBestN in GoT topologies
- Workflow:Spcl_Graph_of_thoughts_GoT_Sorting_Pipeline - Sorting workflow demonstrating Generate-Score-KeepBestN pattern