I mean to ask whether there is any dp question that can be solved with top down but not with bottom up or vice versa. If so, I would like to see some examples.

# | User | Rating |
---|---|---|

1 | tourist | 3979 |

2 | Benq | 3623 |

3 | MiracleFaFa | 3604 |

4 | Radewoosh | 3545 |

5 | maroonrk | 3534 |

6 | slime | 3511 |

7 | greenheadstrange | 3430 |

8 | ecnerwala | 3342 |

9 | sunset | 3338 |

10 | xtqqwq | 3331 |

# | User | Contrib. |
---|---|---|

1 | YouKn0wWho | 207 |

2 | Monogon | 200 |

3 | Um_nik | 193 |

4 | awoo | 190 |

5 | -is-this-fft- | 185 |

6 | sus | 177 |

7 | Errichto | 175 |

8 | antontrygubO_o | 173 |

9 | maroonrk | 167 |

9 | SecondThread | 167 |

I mean to ask whether there is any dp question that can be solved with top down but not with bottom up or vice versa. If so, I would like to see some examples.

↑

↓

Codeforces (c) Copyright 2010-2022 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Jan/27/2022 14:10:33 (h1).

Desktop version, switch to mobile version.

Supported by

User lists

Name |
---|

I don't have the examples right now, but the following are actually there on the internet, so you can just search them up.

Fo the top-down approach, in theory, are better because it only visits necessary states. In practice, if you often need to visit all of the states, then there will be no noticable differences. But for some concrete problem, the top-down approach end up visits very few states, and you can calculate the total number of the necessary states, the top-down should be prefered.

On the other hand, the bottom-up dp has 1 optimization, not for time, but space. Consider problem that your states having form

`dp[i][j]`

, and`dp[i][j]`

only depends on`dp[i-1][k]`

for some`k`

. Then for the current`i`

, you don't need to store every values of`dp[x][y]`

for all`x < i-1`

. That way, the memory is reduced by a factor of`n`

, which is actually necessary for a lot of problem. This trick is calledrotation. To apply it, you can use 2 vectors, one for the previous state, one for the current. After the calculation for the current state, just swap 2 vectors. The other way is to declare an array of size`dp[2][m]`

instead of`dp[n][m]`

, and for each`i`

, you can overwrite the current do values onto`dp[i%2][j]`

. Rotation can also be applied to more than 2layers.Thank you

Actually, if you have to visit nearly all states top-down is often 4-8x slower.

I guess one question was that you can add or subtract 1 , also divide by 2,3,5 if divisible.minimum steps to make n to 1

https://atcoder.jp/contests/agc044/tasks/agc044_a

Another one is the cses problem where given n you can substract any single digit from n which is present in n, n <= 10^18

https://cses.fi/problemset/task/2174

This tasks have sparse states but are dependent on n, in this type of questions only top down can be applied , please correct me if I am wrong.

i am pretty sure that there exist many problems that gives TLE with top down approach but AC with bottom up approach.

I guess he is concerned over class of problem , rather than fitting in time limit.

Often it's easier to make forward transitions in DP (what state can I go to from this one) than backward transitions (from which states can I reach this one). Top down DP is incompatible with forward transitions.

To add another layer of nuance to this discussion: A big majority of DP that arise in practice can be formulated in two ways: One forward and one backward. For example, to count the paths from $$$u \to v$$$ in a DAG, you can consider paths $$$u \to x$$$ for some $$$x$$$ (the forward DP) or you can consider paths $$$x \to v$$$ for some $$$x$$$ (the backward DP). The choice of which to calculate is distinct from (and only sometimes related to) that of whether you compute the DP in a bottom-up or a top-down (lazy) fashion.

I think the example navneet.h gave of AGC044A suffices to show some of the subtlety here. The most obvious forward DP (computing the minimum cost to go from $$$0$$$ to $$$x$$$ for various $$$x$$$) is easy to lazily compute, but tricky or unreasonable to eagerly compute bottom-up without either wasting a lot of work or explicitly pre-calculating which values $$$x$$$ will be relevant (which isn't so different from lazily computing). The corresponding reverse DP (computing the minimum cost to go from $$$x$$$ to $$$N$$$ for various $$$x$$$) is easy to calculate bottom-up (i.e. from $$$N$$$) for the values of $$$x$$$ that could be relevant, but tricky to calculate top-down (i.e. from $$$0$$$) because it's not obvious how to compute the forward transitions. (That last difficulty is, incidentally, (the reverse of) the same issue that ivan100sic touched on in his comment.)