PIXELBANKv9.1.0
Menu

Beam Search over Partial Plans

Problem Statement

Expand a tree of candidate plan steps with beam search: at each depth keep only the top-beam_width partial plans by cumulative score, then return the best complete plan's total score.

Background

Start from the root (empty plan, score 0). At each level, every kept partial plan branches into children given by successors(node), each child adding its step score. After generating all children of a level, keep the beam_width highest-scoring ones (ties broken by the smaller node id for determinism). A node with no successors is terminal. After exploring depth levels (or when no expansions remain), return the maximum total score among all partial plans ever kept (including terminals). Nodes are integer ids; successors maps an id to a list of (child_id, step_score).

Your Task

def beam_search(successors, root, beam_width, depth):

Return the best cumulative score (float) found, rounded to 4 decimals.

Input Format

  • successors (dict: id -> list of (child_id, score)), root (int), beam_width (int), depth (int).

Output Format

  • A float rounded to 4 decimals.

Sample

succ = {0: [(1, 2.0), (2, 5.0)], 1: [(3, 10.0)], 2: [(4, 1.0)]}
print(beam_search(succ, 0, 1, 2))

Output:

6.0

Example:

Input:
succ = {0: [(1, 2.0), (2, 5.0)], 1: [(3, 10.0)], 2: [(4, 1.0)]}
print(beam_search(succ, 0, 1, 2))
Output:
6.0
Reasoning:
  • Initialization: Start with the root node 00 having a cumulative score of 0.00.0. The beam width is 11, meaning only the single best partial plan is kept at each step, and we explore up to a depth of 22.

  • Depth 1 Expansion: Expand the current beam {0}\{0\} using its successors. Node 00 branches to node 11 (score 0.0+2.0=2.00.0 + 2.0 = 2.0) and node 22 (score 0.0+5.0=5.00.0 + 5.0 = 5.0). Since the beam width is 11, we keep only the highest-scoring child, which is node 22 with a score of 5.05.0. The current best score is updated to 5.05.0.

  • Depth 2 Expansion: Expand the current beam {2}\{2\} using its successors. Node 22 branches to node 44 with a step score of 1.01.0. The new cumulative score is 5.0+1.0=6.05.0 + 1.0 = 6.0. This is the only child, so it becomes the new beam. The best score is updated to 6.06.0.

  • Termination: The maximum allowed depth of 22 has been reached. The algorithm stops and returns the highest cumulative score observed in any kept beam, which is 6.06.0.

  • The final output is 6.0

Constraints:

  • Beam keeps top beam_width by cumulative score each level; ties -> smaller node id.
  • A node absent from successors (or with empty list) is terminal.
  • Track the best cumulative score across all kept nodes; round to 4 decimals.
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.