-
Notifications
You must be signed in to change notification settings - Fork 3
[Model] DynamicStorageAllocation #811
Description
Motivation
DYNAMIC STORAGE ALLOCATION (P150) from Garey & Johnson, A4 SR2. A classical NP-complete problem modeling the packing of variable-sized items into a fixed-size memory, where each item has a known arrival time and departure time. Items occupying overlapping time intervals must not overlap in memory space. This is equivalent to a strip packing problem with fixed item widths and time-constrained placement.
Associated rules:
- [Rule] 3-PARTITION to DYNAMIC STORAGE ALLOCATION #828/[Rule] 3-PARTITION to DYNAMIC STORAGE ALLOCATION #397: 3-PARTITION → DynamicStorageAllocation (NP-completeness in the strong sense, Stockmeyer 1976)
The problem is NP-complete in the strong sense, even when item sizes are restricted to {1, 2}. It becomes polynomial when all item sizes are equal (reducible to interval graph coloring).
Definition
Name: DynamicStorageAllocation
Reference: Garey & Johnson, Computers and Intractability, A4 SR2
Mathematical definition:
INSTANCE: Set A of items to be stored, each a ∈ A having a size s(a) ∈ Z+, an arrival time r(a) ∈ Z0+, and a departure time d(a) ∈ Z+, and a positive integer storage size D.
QUESTION: Is there a feasible allocation of storage for A, i.e., a function σ: A → {1,2,...,D} such that for every a ∈ A the allocated storage interval I(a) = [σ(a),σ(a)+s(a)−1] is contained in [1,D] and such that, for all a,a' ∈ A, if I(a) ∩ I(a') is nonempty then either d(a) ≤ r(a') or d(a') ≤ r(a)?
Variables
- Count: |A| (one variable per item, representing its starting address in memory)
- Per-variable domain: {1, 2, ..., D - s(a) + 1} — the starting address for item a, constrained so the item fits within [1, D]
- Meaning: σ(a) ∈ {1, ..., D - s(a) + 1} assigns each item a starting memory address. The item occupies addresses [σ(a), σ(a) + s(a) - 1]. Two items whose time intervals overlap (neither departs before the other arrives) must have non-overlapping memory intervals.
Schema (data type)
Type name: DynamicStorageAllocation
Variants: none (general item set with time intervals and sizes)
| Field | Type | Description |
|---|---|---|
items |
Vec<(usize, usize, usize)> |
Items: (arrival_time, departure_time, size) for each a ∈ A |
memory_size |
usize |
Storage size D |
Notes:
- This is a feasibility (decision) problem:
Value = Or. - Key getter methods:
num_items()(= |A|),memory_size()(= D).
Complexity
- Decision complexity: NP-complete in the strong sense (Stockmeyer, 1976; transformation from 3-Partition). NP-complete even with item sizes restricted to {1, 2}. Polynomial when all item sizes are equal (interval graph coloring, Gavril 1972).
- Best known exact algorithm: Brute-force enumeration of starting addresses gives O(D^|A|) in the worst case. No pseudo-polynomial algorithm exists unless P = NP (strong NP-completeness).
- Concrete complexity expression:
"(memory_size + 1)^num_items"(fordeclare_variants!) - Approximation: Factor 3 approximation by Gergov (1999). Improved to (2+ε) by Mömke & Wiese (2015).
- References:
- L.J. Stockmeyer (1976). Cited in Garey & Johnson as the source of the NP-completeness proof.
- J. Gergov (1999). "Algorithms for Compile-Time Memory Optimization." SODA 1999, pp. 907-908.
- F. Gavril (1972). "Algorithms for Minimum Coloring, Maximum Clique, Minimum Covering by Cliques, and Maximum Independent Set of a Chordal Graph." SIAM J. Comput. 1(2), pp. 180-187.
Extra Remark
Full book text:
INSTANCE: Set A of items to be stored, each a ∈ A having a size s(a) ∈ Z+, an arrival time r(a) ∈ Z0+, and a departure time d(a) ∈ Z+, and a positive integer storage size D.
QUESTION: Is there a feasible allocation of storage for A, i.e., a function σ: A → {1,2,...,D} such that for every a ∈ A the allocated storage interval I(a) = [σ(a),σ(a)+s(a)−1] is contained in [1,D] and such that, for all a,a' ∈ A, if I(a) ∩ I(a') is nonempty then either d(a) ≤ r(a') or d(a') ≤ r(a)?
Reference: [Stockmeyer, 1976b]. Transformation from 3-PARTITION.
Comment: NP-complete in the strong sense, even if s(a) ∈ {1,2} for all a ∈ A. Solvable in polynomial time if all item sizes are the same, by interval graph coloring algorithms (e.g., see [Gavril, 1972]).
How to solve
- It can be solved by (existing) bruteforce. (Enumerate all possible starting addresses σ(a) for each item; verify no two time-overlapping items have overlapping memory intervals.)
- It can be solved by reducing to integer programming. (ILP with integer variables σ_a for starting address of each item; constraints: 1 ≤ σ_a ≤ D - s(a) + 1; for each pair (a, a') with overlapping time intervals, add non-overlap constraints on memory intervals using big-M or indicator variables. Companion rule issue to be filed separately.)
- Other: First-Fit Decreasing and other heuristics for practical instances.
Example Instance
Input:
A = {a_1, a_2, a_3, a_4, a_5} (5 items), memory size D = 6.
| Item | Arrival r(a) | Departure d(a) | Size s(a) |
|---|---|---|---|
| a_1 | 0 | 3 | 2 |
| a_2 | 0 | 2 | 3 |
| a_3 | 1 | 4 | 1 |
| a_4 | 2 | 5 | 3 |
| a_5 | 3 | 5 | 2 |
Peak load analysis (maximum total size at any time):
- Time [1,2): a_1 + a_2 + a_3 = 2+3+1 = 6 = D (tight)
- Time [2,3): a_1 + a_3 + a_4 = 2+1+3 = 6 = D (tight)
- Time [3,4): a_3 + a_4 + a_5 = 1+3+2 = 6 = D (tight)
Solution: σ(a_1)=1, σ(a_2)=3, σ(a_3)=6, σ(a_4)=3, σ(a_5)=1.
- a_1 occupies [1,2] during [0,3)
- a_2 occupies [3,5] during [0,2)
- a_3 occupies [6,6] during [1,4)
- a_4 occupies [3,5] during [2,5) — a_2 departs at time 2, a_4 arrives at time 2, no time overlap ✓
- a_5 occupies [1,2] during [3,5) — a_1 departs at time 3, a_5 arrives at time 3, no time overlap ✓
All 7 time-overlapping pairs verified: no memory conflicts. All items fit within [1,6]. ✓
Answer: YES — a feasible storage allocation exists.
Non-trivial structure: The memory is completely full at three distinct time intervals (peak load = D = 6). Items a_4 and a_5 must reuse memory addresses freed by a_2 and a_1 respectively — a greedy left-to-right allocation would fail.
Negative modification: Increase s(a_3) from 1 to 2. Now peak load at time [1,2) is 2+3+2=7 > 6, so no allocation can fit all items. Answer: NO.
Python validation script
items = [(0,3,2), (0,2,3), (1,4,1), (2,5,3), (3,5,2)] # (arrival, departure, size)
D = 6
sigma = [1, 3, 6, 3, 1] # starting addresses
# Check all items fit within [1, D]
for i, (r, d, s) in enumerate(items):
assert 1 <= sigma[i] and sigma[i] + s - 1 <= D
# Check no time-overlapping pairs have memory overlap
for i in range(5):
for j in range(i+1, 5):
ri, di, si = items[i]; rj, dj, sj = items[j]
time_overlap = not (di <= rj or dj <= ri)
if time_overlap:
li, hi = sigma[i], sigma[i]+si-1
lj, hj = sigma[j], sigma[j]+sj-1
mem_overlap = not (hi < lj or hj < li)
assert not mem_overlap, f"Items {i},{j} conflict"
print("All verified: feasible allocation")
# Negative: increase s(a3) to 2, peak load exceeds D
items_neg = [(0,3,2), (0,2,3), (1,4,2), (2,5,3), (3,5,2)]
# At time [1,2): items 0,1,2 present, sizes 2+3+2=7 > 6
peak = max(sum(s for r,d,s in items_neg if r <= t < d) for t in range(5))
assert peak > D
print(f"Negative: peak load {peak} > {D} (infeasible)")Metadata
Metadata
Assignees
Labels
Type
Projects
Status