Lab 5 – Using Dynamic Programming in the gallery

Github Classroom

Below are three DP-solvable subproblems that map cleanly onto the art exhibition layout project. You will be assigned a team to worm on this with.


Lab 1 — Room Wall-Space Selection (0/1 Knapsack)

Problem (exhibition framing)

You have one room with a fixed amount of usable wall length (after doors/windows). You must choose which wall-hung artworks to include. Each artwork consumes wall length (including required clearance) and has a “curatorial value” score.

Goal: maximize total value without exceeding wall length.

This is exactly 0/1 knapsack:

  • capacity = wall length
  • weight = artwork width (with clearance)
  • value = curatorial value

DP definition

Let items be 1..n. Capacity is 0..W.

  • dp[i][w] = best value using first i artworks with wall capacity w
  • Recurrence:
    • If artwork i has width wi and value vi:
    • dp[i][w] = dp[i-1][w] (skip)
    • If wi <= w: dp[i][w] = max(dp[i][w], dp[i-1][w-wi] + vi) (take)

Small dataset (do in class, show full table)

Wall capacity W = 10

ArtworkWidthValue
A23
B34
C45
D58

DP table (values), rows = i (0..4), cols = w (0..10)

i \ w012345678910
000000000000
1 (A)00333333333
2 (B)00344777777
3 (C)0034578991212
4 (D)003458811121315

Optimal value = 15 at capacity 10.

Example “equation under the table” you can require on each step:

  • For item D (w=5, v=8) at capacity 10:
    dp[4][10] = max(dp[3][10], dp[3][10-5] + 8) = max(12, 7 + 8) = 15

Larger dataset (for grading / testing)

Wall capacity W = 40

ArtworkWidthValue
P1614
P2918
P3713
P4510
P51225
P61020
P748
P8815
P91119
P1036
P111326
P1224

Deliverables:

  1. dp table for the small dataset (full grid)
  2. code that solves the large dataset + prints chosen artworks (backtracking)

Lab 2 — Panel / Wall-Segment Packing (“Word Wrap” DP)

Problem (exhibition framing)

You have a corridor (or a room) with a sequence of wall panels/segments each of length L. You must place artworks in a fixed order (curatorial narrative order, or physical walking order). You decide where to break between panels.

You want to avoid ugly gaps. Penalize unused space on each panel.

Goal: minimize total penalty.

This is classic text justification / word wrap DP.

DP definition

Given widths w1..wn, panel length L.

Define the cost of placing artworks i..j on one panel:

  • if sum(wi..wj) > L: cost = ∞ (doesn’t fit)
  • else cost = (L - sum(wi..wj))^2
    (often make the last panel cost = 0)

Then:

  • dp[j] = min penalty to place artworks 1..j
  • recurrence:
    • dp[j] = min over i=1..j ( dp[i-1] + cost(i, j) )

Small dataset (compute full cost matrix + dp)

Panel length L = 10
Artworks must stay in this order:

#Width
12
25
33
44
52

Cost table sample (cost(i,j)) (∞ means “doesn’t fit”):

i\j12345
1(10-2)²=64(10-7)²=9(10-10)²=0
2(10-5)²=25(10-8)²=4
3(10-3)²=49(10-7)²=9(10-9)²=1
4(10-4)²=36(10-6)²=16
5(10-2)²=64

Now compute dp (with last panel cost forced to 0):

  • dp[0]=0
  • dp[1]=64
  • dp[2]=9 (1..2 fits with penalty 9)
  • dp[3]=0 (1..3 fits perfectly: penalty 0)
  • dp[4]=18 (best is [1..2] penalty 9 + [3..4] penalty 9)
  • dp[5]=0 (best is [1..3] penalty 0 + [4..5] last panel cost 0)

So the optimal breaks are:

  • Panel 1: artworks 1–3 (2+5+3=10)
  • Panel 2: artworks 4–5 (4+2=6) (last panel → cost 0)

Deliverables:

  1. Fill the full cost(i,j) table for the small dataset
  2. Fill dp[0..n] and reconstruct the breaks
  3. Implement for large dataset

Larger dataset (for grading / testing)

Panel length L = 24
Widths (fixed order):
[6, 7, 5, 8, 4, 9, 3, 7, 6, 5, 4, 8, 6, 3]

Ask them to print:

  • minimum total penalty
  • the panel breaks (which artworks on each panel)

Lab 3 — Two-Room Allocation (2-Knapsack DP)

Problem (exhibition framing)

You have two rooms, each with a wall-length budget. Each artwork can be hung in Room A, Room B, or not displayed. (Still 0/1 per artwork.)

Goal: maximize total curatorial value subject to both room capacities.

This is a clean DP that feels like “layout across rooms” without getting into full NP-hard multi-room assignment.

DP definition

Capacities: CA and CB.

Let:

  • dp[a][b] = best value achievable with Room A capacity a and Room B capacity b after processing some prefix of artworks.

For each artwork (width w, value v), update:

  • skip: dp[a][b] stays
  • place in A if w<=a: candidate dp[a-w][b] + v
  • place in B if w<=b: candidate dp[a][b-w] + v

(Implementation usually loops items outermost and updates into a new table, or updates in reverse.)

Small dataset (show full dp grid)

Capacities: CA=6, CB=7

ArtworkWidthValue
P126
P235
P348
P459
P513

Final dp[a][b] table (rows a=0..6, cols b=0..7):

a\b01234567
003699111417
1339911141718
26991114171819
399111417181922
4911141717192223
51114171819222326
61417181922232626

Optimal value is 26.

Example “equation under the table” for one update (placing P4, w=5, v=9):

  • At state (a=6,b=7):
    dp'[6][7] = max(dp[6][7], dp[6-5][7] + 9, dp[6][7-5] + 9)

Deliverables:

  1. Produce the dp grid for the small dataset (exactly)
  2. Backtrack (or store choices) to output which room each chosen artwork went to
  3. Implement for large dataset

Larger dataset (for grading / testing)

Capacities: CA=20, CB=18

ArtworkWidthValue
A1614
A2716
A3511
A4919
A549
A6817
A737
A81022
A925
A10613
A11715
A12512

Require:

  • maximum total value
  • assignment list: A / B / not shown

If you want, I can also format each lab into a one-page student handout with: background story, explicit inputs/outputs, starter pseudocode, and a rubric aligned with “full credit for any reasonable solution + correct DP table + correct reconstruction.”

Scroll to Top