NeFut Logo NeFut
Admin Login

Exploring Pull and Push Mechanisms in Dynamic Programming

Published at: 2026-05-27 09:17 Last updated: 2026-06-06 13:04
#algorithm #optimization #Dynamic Programming

Core Logic and Mathematical Principles

After determining the state space and DAG topological order, there are two fundamentally opposite dynamic programming driving mechanisms for implementing state transitions: Pull (Everyone for Me) and Push (I for Everyone). The two mechanisms represent different traversal perspectives of the edge set of the DAG in a mathematical sense.

  1. Pull (Everyone for Me):

    • Core Mathematical Form: $f[i] = \text{opt}_{j \in prev(i)} \{ f[j] + w(j, i) \}$
    • Logical Perspective: The current state $i$ acts as the receiver. To compute $f[i]$, it actively retrieves and pulls (Pull) all known information from predecessor states $j$ that can reach $i$.
    • Applicable Scenarios: Problems where the boundary of predecessor states is extremely clear and the rules are fixed.
  2. Push (I for Everyone):

    • Core Mathematical Form: For the current fixed state $j$, update all reachable successor states $i$: $f[i] = \text{opt}(f[i], f[j] + w(j, i))$
    • Logical Perspective: The current state $j$ acts as the contributor. Once the value of $f[j]$ is determined, it actively pushes (Push) its value to all successor states $i$ that originate from $j$.
    • Applicable Scenarios: Situations where the “subsequent impact” of the current state can be easily enumerated, but it is extremely difficult or complex to find “who can transition to the current state” (such as in table flushing methods, sparse transitions, and game theory state transformations).

State Design and Algorithm Derivation

Using the classic "Number Triangle / Variable Length Jump Path Maximum Profit" model to break down the two derivation logics. Assume that from position $i$ in the sequence, one can jump to $i+1$ or $i+2$, with a cost of $w(i, \text{target})$.

1. Pull (Everyone for Me) Design

$$f[i] = \max(f[i-1] + w(i-1, i), f[i-2] + w(i-2, i))$$

2. Push (I for Everyone) Design

$$f[i+1] = \max(f[i+1], f[i] + w(i, i+1))$$

$$f[i+2] = \max(f[i+2], f[i] + w(i, i+2))$$


C++ Standard Source Code (NOIP Style)

The following source code provides the complete implementation of "Pull" and "Push" in a single topological graph (one-dimensional multi-step non-fixed jump model), contrasting their underlying logic.

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

const int MAXN = 10005;
const int INF = 0x3f3f3f3f;

int w[MAXN]; // Node's own value
int f_pull[MAXN]; // Pull array
int f_push[MAXN]; // Push array

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n;
    if (!(cin >> n)) return 0;
    for (int i = 1; i <= n; ++i) {
        cin >> w[i];
    }

    // ==================== 1. Pull Implementation (Everyone for Me) ====================
    // Must strictly control predecessor legality
    f_pull[1] = w[1];
    f_pull[2] = w[1] + w[2]; 
    for (int i = 3; i <= n; ++i) {
        // Actively pull the state information from predecessors i-1 and i-2
        f_pull[i] = max(f_pull[i - 1], f_pull[i - 2]) + w[i];
    }

    // ==================== 2. Push Implementation (I for Everyone / Table Flushing Method) ====================
    // Must initialize unreachable states to extreme values to prevent illegal contributions
    for (int i = 1; i <= n; ++i) f_push[i] = -INF;

f_push[1] = w[1]; // The only legal starting point
    for (int i = 1; i < n; ++i) {
        if (f_push[i] == -INF) continue; // Critical pitfall: skip unreachable phantom states

        // Use the currently determined state to refresh the values of successor states
        if (i + 1 <= n) f_push[i + 1] = max(f_push[i + 1], f_push[i] + w[i + 1]);
        if (i + 2 <= n) f_push[i + 2] = max(f_push[i + 2], f_push[i] + w[i + 2]);
    }

    cout << f_pull[n] << " " << f_push[n] << "\n";
    return 0;
}

NOIP Practical Pitfall Guide

1. Push (I for Everyone) Not Removing "Illegal/Unreachable" Predecessor States

2. Pull (Everyone for Me) Boundary State Out-of-Bounds Forced Read


Classic NOIP/Luogu Problems

1. Luogu P1004 [NOIP2000 Advanced Group] Grid Collection

2. Luogu P1063 [NOIP2006 Advanced Group] Energy Necklace

Original Source: local://13.4

[h] Back to Home