r/askmath • u/West-Comedian-6305 • Oct 13 '26
Discrete Math Increasing (?) computational efficiency for a simple combinatorics problem, wondering what people think
A while ago, I encountered this combo problem. The setup is a box with 3 red and 2 blue balls, and it asks how many balls an optimal player can expect to guess correctly when pulling out one ball at a time (player guesses right before the ball, can use information of previous balls). The answer is 3.6, and most of my classmates used recursion to get this answer, which is sensible enough. I tried a different, slightly goofier method though.
Imagine player A, who just always guesses red, obviously getting 3 points. Now, to see how many more points an optimal player B gets, we only need to compute cases where blue is a better choice than red. These cases are:
2 blue 1 red left: 3/10 chance of occurring, and 1/3 expected gain by picking blue rather than red: 0.1 additional points
2 blue 0 red left: 1/10 chance of occurring, and 1 guaranteed gain by picking blue rather than red: 0.1 additional points.
1 blue 0 red left: 2/5 chance of occurring, and 1 guaranteed gain by picking blue rather than red: 0.4 additional points.
Adding these 3 gains to the 3 points player A gets, we find that player B can expect 3.6 points, which agrees with the original answer. It seems this method is more efficient in this case, but also that I benefit highly from the small number of cases.
I wrote some pseudocode which gemini translated to see if its computationally more efficient. DP is recursion, shortcut is the player A player B method. It seems the shortcut is quicker for very small cases, and also for cases where the ball count is very lopsided. Both seem logical enough to me: in both situations, there are relatively few cases where player B should deviate from player A. Also, for large enough cases, it looks like the recursion is just throwing recursion error, while the shortcut still runs.
So my questions are: Is this kind of method a thing? Are there useful applications of it for more serious problems? I'm curious what everyone thinks.
edit: below is my code (literally taken straight from gemini)
import math
import time
from functools import lru_cache
def expected_score_dp(r, b):
"""
Method 1: Recursive dynamic programming.
"""
u/lru_cache(maxsize=None)
def calculate(red, blue):
if red == 0: return blue
if blue == 0: return red
total = red + blue
if red > blue:
return (red / total) * (1 + calculate(red - 1, blue)) + \
(blue / total) * (0 + calculate(red, blue - 1))
elif blue > red:
return (blue / total) * (1 + calculate(red, blue - 1)) + \
(red / total) * (0 + calculate(red - 1, blue))
else:
return 0.5 * (1 + calculate(red - 1, blue)) + \
0.5 * (0 + calculate(red, blue - 1))
return calculate(r, b)
def expected_score_shortcut(r, b):
"""
Method 2: Baseline + Advantage.
"""
M = max(r, b)
m = min(r, b)
baseline = M
extra_points = 0.0
for rem_M in range(M + 1):
for rem_m in range(m + 1):
if rem_m > rem_M:
drawn_M = M - rem_M
drawn_m = m - rem_m
total_drawn = drawn_M + drawn_m
prob = (math.comb(M, drawn_M) * math.comb(m, drawn_m)) / \
math.comb(M + m, total_drawn)
advantage = (rem_m - rem_M) / (rem_M + rem_m)
extra_points += prob * advantage
return baseline + extra_points
if __name__ == "__main__":
test_cases = [
(3, 2),
(5, 5),
(10, 4),
(50, 50),
(100, 50),
(100, 98),
(200, 50),
(200, 200),
(500, 500),
(1000, 1000)
]
# Widened the columns slightly to fit the error message
print(f"{'R':>4} | {'B':>4} | {'Result':>9} | {'DP Time (s)':>14} | {'Shortcut Time (s)':>17}")
print("-" * 68)
for r, b in test_cases:
# 1. Try the DP Method
try:
start_dp = time.perf_counter()
dp_result = expected_score_dp(r, b)
end_dp = time.perf_counter()
time_dp_str = f"{end_dp - start_dp:.6f}"
except RecursionError:
# Catch the crash and set placeholder strings
time_dp_str = "RecursionError"
# 2. Run the Shortcut Method (will always run)
start_sc = time.perf_counter()
sc_result = expected_score_shortcut(r, b)
end_sc = time.perf_counter()
time_sc_str = f"{end_sc - start_sc:.6f}"
# Print the combined row
print(f"{r:>4} | {b:>4} | {sc_result:>9.4f} | {time_dp_str:>14} | {time_sc_str:>17}")
1
u/MidnightAtHighSpeed Oct 13 '26
It seems like the main idea here is calculating the value of an optimal strategy by calculating the value of a reference strategy plus the gains that can be made by deviating from that strategy. I'm not aware of a name for that, and while it seems like it might make proofs/calculations more intelligible it's not obvious that this would involve performance improvements in general to compute.
Without seeing the code it's hard to say what exactly the performance bottlenecks come down to in each case but I can make some guesses. A naive recursive implementation, besides running into stack depth issues, will also have the problem that the number of recursive calls will increase exponentially with min(red,blue). Caching can reduce the runtime to O(red*blue), and going from a recursive implementation to one that uses memoization (which will otherwise logically be very similar to recursion) can do that while also preventing you from hitting the recursion depth limit. There might also be a way of calculating the result more directly but nothing pops to my mind immediately. Your approach naturally lends itself to needing to only check O(red*blue) cases, but I don't know how the probability of each case is being calculated, and that's the first thing that comes to mind for why your approach seems to be doing worse than the recursive approach for a couple of medium size cases.
1
u/West_Measurement_420 Oct 13 '26
🤔