Lecture 20: Dynamic Programming II: Text Justification, Blackjack

MIT OpenCourseWare · 559K views · 2013 · 52:12

Open on YouTube

Algorithms & data structuresUniversity lectures & courseslecture

Why Stack Overflow recommends it

Stack Overflow users link it as the standard worked explanation of text justification with dynamic programming and as the source of the Blackjack DP problem.

Recommended by 2 people across 2 different months, 2013 to 2020.

What it is

Erik Demaine's MIT 6.006 lecture on dynamic programming, covering the text justification (word wrap) problem with a cubic badness penalty and then a Blackjack optimal-play example. It is one lecture from the Fall 2011 Introduction to Algorithms course, published by MIT OpenCourseWare.

What people said

Strong rec Bas Swinckels · 2013-10 · Stack Overflow · on “Dividing a lumpy sequence into even parcels?”

I am not sure if it exactly solves your program, but in the MIT OCW course Introduction to Algorithms [LINK] , they use dynamic programming [LINK] for solving the optimal splitting of lines so that the text is nicely filled out along a page (' word-wrapping [LINK] '), similar to what Latex does. See 17 minutes into this video [this video] . This algorithm will give the guaranteed optimal splitting given some function that defines a penalty based on how ugly the line-splitting is. In the lecture, they define this uglyness function as (pagewidth - actual_linewidth)^3 , but you could define your own function. The algorithm than more or less tries all the different splitting possibilities (in a smart

Recommends Orif Milod · 2020-10 · Stack Overflow · on “Algorithm to Convert text from Left Align to Justify”

I would suggest whoever wants to go into details and understand ins and outs of this problem, to watch MIT 6.006 course - Lecture No.20 Here is the link to it. [this video]

Cites Niklas B. · 2016-01 · Stack Overflow · on “Is it possible to convert all backtracking algorithms in to dynamic programming approach?”

@harold Just out of interest, I checked some more sources that I am taking a bit more serious than random stack overflow answers and codechef tutorials. CLRS [LINK] , for example, has to say "There are usually two equivalent ways to implement a dynamic-programming approach. We shall illustrate both of them with our rod-cutting example. The first approach is top-down with memoization." Erik Demaine from MIT in his introductory algorithms lecture [this video] uses the term "memoization": Really no "war" to be found.

Cites Suuuehgi · 2017-03 · Stack Overflow · on “Implementing Text Justification with Dynamic Programming”

For anyone else still interested in this: The key is to move backwards from the end of the text (as mentioned here [this video] ). If you do so, you just compare already memorized elements. Say, words is a list of strings to be wrapped according to textwidth . Then, in the notation of the lecture, the task reduces to three lines of code: import numpy as np textwidth = 80 DP = [0]*(len(words)+1) for i in range(len(words)-1,-1,-1): DP[i] = np.min([DP[j] + badness(words[i:j],textwidth) for j in range(i+1,len(words)+1)]) With: def badness(line,textwidth): # Number of gaps length_line = len(line) - 1 for word in line: length_line += len(word) if length_line > textwidth: return float('inf') return ( te

Cites Abhijit Sarkar · 2019-05 · Stack Overflow · on “How to solve Blackjack using Dynamic Programming?”

This question is from the book Daily Coding Problem [LINK] , and is a slightly modified version of the problem described in this [this video] MIT video. The question is as follows: Blackjack is a two player card game whose rules are as follows: The player and then the dealer are each given two cards. The player can then "hit", or ask for arbitrarily many additional cards, so as his or her total does not exceed 21. The dealer must then hit if his or her total is 16 or lower, otherwise pass. Finally, the two compare totals, and the one with the greatest sum not exceeding 21 is the winner. For this problem, we simplify the card values to be as follows: each card between 2 and 10 counts as their face