can you fill a !×! board with the corners missing using dominoes?jh2jf/courses/fall2019/cs... ·...
TRANSCRIPT
Fall 2019
1
Can you fill a 𝟖×𝟖 board with the corners missing using dominoes?
Can you tile this?
With these?
Fall 2019
2
Can you fill a 𝟖×𝟖 board with the corners missing using dominoes?
Can you tile this?
With these?
Today’s Keywords
• Dynamic Programming• Gerrymandering
3
CLRS Readings
• Chapter 15• Chapter 16
4
Administrativa
• Midterm take-home due tonight at 11pm– Individual work!
• Homework 5 out tonight!– Due Thursday, October 24 at 11pm– Seam Carving!– Dynamic Programming (implementation)– Java or Python
5
Dynamic Programming
• Requires Optimal Substructure– Solution to larger problem contains the solutions to smaller ones
• Idea:1. Identify the recursive structure of the problem• What is the “last thing” done?
2. Save the solution to each subproblem in memory3. Select a good order for solving subproblems• “Top Down”: Solve each recursively• “Bottom Up”: Iteratively solve smallest to largest
6
Generic Top-Down Dynamic Programming Solnmem = {}def myDPalgo(problem):
if mem[problem] not blank:return mem[problem]
if baseCase(problem):solution = solve(problem)mem[problem] = solutionreturn solution
for subproblem of problem:subsolutions.append(myDPalgo(subproblem))
solution = OptimalSubstructure(subsolutions)mem[problem] = solutionreturn solution
7
DP Algorithms so far
• 2×𝑛 domino tiling (Fibonacci) • Log cutting• Matrix Chaining• Longest Common Subsequence• 4×𝑛 domino tiling (Homework 4 Problem)• Seam Carving (Homework 5 Problem)
8
Domino Tiling
9
Tile(n):Initialize Memory MM[0] = 0M[1] = 0for i = 0 to n:
M[i] = M[i-1] + M[i-2]return M[n]
M
0
1
2
3
4
5
6
Log Cutting
10
10987654321Length:
𝐶𝑢𝑡(𝑖): 0
0
Solve Smallest subproblem first
𝐶𝑢𝑡 4 = max
𝐶𝑢𝑡 3 + 𝑃[1]𝐶𝑢𝑡 2 + 𝑃 2𝐶𝑢𝑡 1 + 𝑃 3𝐶𝑢𝑡 0 + 𝑃[4]
4
Matrix Chaining
11
30
35
×𝑀9 35
15
×𝑀:15
5
×𝑀; 5
10
×𝑀<
10
20
×𝑀= 20
25
𝑀>
𝐵𝑒𝑠𝑡 𝑖, 𝑗 = minFG9
HI9𝐵𝑒𝑠𝑡 𝑖, 𝑘 + 𝐵𝑒𝑠𝑡 𝑘 + 1, 𝑗 + 𝑟L𝑟HM9𝑐F
𝐵𝑒𝑠𝑡 𝑖, 𝑖 = 0 0 15750 7875 9375 11875
0 2625 4375 7125 10500
0 750 2500 5375
35000 1000
50000
0
1 2 3 4 5 61
2
3
4
5
6
𝐵𝑒𝑠𝑡 1,6 = min
𝐵𝑒𝑠𝑡 1,1 + 𝐵𝑒𝑠𝑡 2, 6 + 𝑟9𝑟:𝑐>𝐵𝑒𝑠𝑡 1,2 + 𝐵𝑒𝑠𝑡 3, 6 + 𝑟9𝑟;𝑐>𝐵𝑒𝑠𝑡 1,3 + 𝐵𝑒𝑠𝑡 4, 6 + 𝑟9𝑟<𝑐>𝐵𝑒𝑠𝑡 1,4 + 𝐵𝑒𝑠𝑡 5, 6 + 𝑟9𝑟=𝑐>𝐵𝑒𝑠𝑡 1,5 + 𝐵𝑒𝑠𝑡 6, 6 + 𝑟9𝑟>𝑐>
15125
𝑗 == 𝑖
Generic Top-Down Dynamic Programming Solnmem = {}def myDPalgo(problem):
if mem[problem] not blank:return mem[problem]
if baseCase(problem):solution = solve(problem)mem[problem] = solutionreturn solution
for subproblem of problem:subsolutions.append(myDPalgo(subproblem))
solution = OptimalSubstructure(subsolutions)mem[problem] = solutionreturn solution
12
Generic Top-Down Dynamic Programming Soln
mem = [][]Matrix = [] #1-D array of all 2-D matricesdef matrixChain(i,j):
if mem[i][j] not blank:return mem[i][j]
if (j-i) == 0:mem[i][j] = 0return 0
if (j-i) == 1:solution = size(Matrix[i]) * size(Matrix[j]) * size(Matrix[j][0]) #cost to multiplymem[i][j] = solutionreturn solution
for k in i+1 .. j-1:subsolutions.append(matrixChain(i,k) + matrixChain(k+1,j) + cost(i,k,j))
solution = min(subsolutions) # optimal substructuremem[i][j] = solutionreturn solution 13
Longest Common Subsequence
14
𝐿𝐶𝑆 𝑖, 𝑗 =0 if 𝑖 = 0 or𝑗 = 0𝐿𝐶𝑆 𝑖 − 1, 𝑗 − 1 + 1 if 𝑋 𝑖 = 𝑌[𝑗]max(𝐿𝐶𝑆 𝑖, 𝑗 − 1 , 𝐿𝐶𝑆 𝑖 − 1, 𝑗 ) otherwise
0 0 0 0 0 0 0 0
0 0 1 1 1 1 1 1
0 0 1 1 1 2 2 2
0 0 1 2 2 2 2 2
0 1 1 2 2 2 3 3
0 1 2 2 3 3 3 4
0 1 2 2 3 3 4 4
𝑋 = 𝐴 𝑇 𝐶 𝑇𝑇 𝐺 𝐴1 2 3 74 5 60𝑌 =
0
1
3456
2𝑇
𝐶𝐴𝑇𝐴
𝐺
To fill in cell (𝑖, 𝑗) we need cells 𝑖 − 1, 𝑗 − 1 , 𝑖 − 1, 𝑗 , (𝑖, 𝑗 − 1)Fill from Top->Bottom, Left->Right (with any preference)
Seam Carving
15
S(n-1,k-1)
𝑝],H
S(n-1,k) S(n-1,k+1)
S(n,k)
𝑆 𝑛, 𝑘 = 𝑚𝑖𝑛 𝑆 𝑛 − 1, 𝑘 − 1 + 𝑒(𝑝],H)
𝑆 𝑛 − 1, 𝑘 + 𝑒(𝑝],H)
𝑆 𝑛 − 1, 𝑘 + 1 + 𝑒(𝑝],H)
16
17
Gerrymandering
• Manipulating electoral district boundaries to favor one political party over others
• Coined in an 1812 Political cartoon• Governor Elbridge Gerry signed a
bill that redistricted Massachusetts to benefit his Democratic-Republican Party
18
The Gerrymander
According to the Supreme Court
• Gerrymandering cannot be used to:– Disadvantage racial/ethnic/religious groups
• It can be used to:– Disadvantage political parties
19
VA 5th District
20
VA 5th District
21
2018 Election
Gerrymandering Today
• Computers make it really effective
22
")4
")1
")3
")7
")2
Ivor
Carsley
Courthouse
Berlin
Sebrell
Precinct 3-1
Newville
Yale
Pons
Surry
Eastern
Orbit
Brandon
Chesapeake
Salem
Zuni
Little Zion
Piankatank
Templeton
Westville
Old Mill
Harris Grove
4-B
Bryan
Achilles
Harrison
Stony Creek
Roanes
Henry
Rushmere
Wakefield
Raynor
Precinct 3-1
Harcum
Enon
Bacon's Castle
New Market
Precinct 1-1
Bethel
Eltham
Cumberland
Courthouse
Saluda
Dendron
Claremont
Asbury
Sweet Hall
Precinct 2-1
Reams
Lee Hall
Wilton
Waller Mill
Whitlocks
Magruder
Bartlett
Watkins
Blackwater
Shackleford's
Courthouse
Blackwater
Courthouse
Rives
Chuckatuck
Providence Forge
Windsor
Stonehouse A
White Marsh
Bland
Stonehouse C
Powhatan A
Elko
Old Church
Tunstall
Carrollton
Little Mill
New Hope
Wall's Bridge
Courthouse
Nash
Roberts B
Driver
Seaford
Botetourt
Matoaca
Town Hall
Mars Hill
Rohoic
Dorey
Southern Branch
Church View
Edgehill
Timberneck
Walters
Airport
Thirty Nine
Battlefield
Black Creek
Dare
Cherry Hill
Eanes
Winfrees Store
Burbank
Ebenezer
Jamestown A
Union Branch
King's Fork
Yeates
Dunbar
609
Iron Bridge
Harmony Village
Nansemond River
Ettrick
Kentwood
HayesCourts Bldg
Sullivans
Antioch
Smithfield
4-A
Ecoff
806
Armstrong
Mehfoud
Clay
Spring Grove
Zion Grace
Stryker
Sandston
Bellwood
West Wakefield
Jefferson Park
Bird
Hilton
Yorktown
Gates
Elizabeth Scott
Precinct 5-1
Richard Bland
Deep Creek
Stonehouse B
Berkeley C
Bennetts Creek
413
Warwick
Chickahominy
Machen
Tabb
106
Cold Harbor
Quinton
Rural Point Precinct 1-1
811
Dinwiddie
Rolfe
West Point
812
Harrowgate
Chickahominy River
Drewryville
Manquin
Ocean View School
Greenwood
Downtown
Bland
Kiln Creek
Glen Lea
Ward 2
Phillips
Chesdin
Thirty-Six
903
Reed
410
Davis
River
Urbanna
Berkeley B Part 1
Wilder
Five Forks
Riverside
Bethel
Ten
Western
Jolliff One
Central
Village
Beulah
Donahoe
503
402
101
Wells
910
Ocean View Center
810
Drewry's Bluff
Fifth
Five
Laburnum
Southside
Dutch Gap
Sedley
404
Fourth
Totopotomoy
706
Kiln Creek
203
Bayview School
Palmer
Nelson
Kraft
Nine
703
Harwoods Mill
Atlee
Epes
Ward 4
Roberts C Part 1
Easton
Powhatan C
607
Titustown Center
Deer Park
Cooper
Carver
North Chester
602
Avalon
Lindsay
Lake Cohoon
Waverly
Edgehill
309
South Chester
505
610
Barron Black
412
Third
Queens Lake
Bethel
Smith
AzaleaBelmont
204
Sunray Ii
Second
Ratcliffe
814
Coventry
Willard
Riverview
Hermitage
Fairlawn
Thomas
Powhatan D
707
Watkins
Phenix
Cedar Fork
Aberdeen
Yates
Wythe
Thirty
Denbigh
Falling Creek
Masonic
510
Berkley
Newman
603
Stonewall Jackson
508
Camelot
Salem Church
909
501
Courthouse
301
Fellowship
Powhatan B
Montrose
306
Briarfield
GranbyWesley
One
802
104
Richneck
St. Julians
509
307
Mallory
Tarrallton
Crestview
111
Adams
Lakeside
Booker
Bassette
Old Dominion
Sanford
308
902
Glenside
Campostella
Hunterdale
102
105
Rosemont
Longan
Twenty
Beach
River Birch
Tuckahoe112
Shady Grove
302
Staples Mill
Jamestown B
Berkeley
Langley
Harbour View
Wilson
Sarah's Creek
911
First Ward First Precinct
Seventh Ward First Precinct
Phoebus
Berkeley A Part 1
Greendale
East Hampton
Roberts A Part 2
First
Reservoir
Pleasants
Nansemond
Tallwood
Charles
McIntosh
Jenkins
Ward 1
Manchester
Georgetown
Skipwith
Sandy Bottom
Poplar Halls
Second Ward First Precinct
Fourteen
Meadowbrook
604
Precinct 4-1
Churchland
305
Ward 3
Ward 7
Saunders
Seven
Hungary
Taylor Road
Moody
504
304
United Way
Syms
Forrest
Berkeley B Part 2
Johnson
South Norfolk Recreation
Beaufont 908
Fairfield
Maplewood606
Carver
Indian River
Oaklette
Third Ward First Precinct
East Ocean View
City Hall
Crestwood
LongdaleYellow Tavern
Thirty-Four Ingleside
Ward 5Ward 6
Silverwood
Chippenham
213
Bailey Creek
Thirty-Five
Reon
701
Stuart
Tucker Capps
Thirty-Two
Bayside
Hidenwood
Providence
Seventeen
114
Tanglewood
Chamberlayne
206
Boulevard
Nineteen
MechanicsvilleHighland Gardens
211
Thirty-One
Belmont
Sherry Park
Sixteen
Carver School
Bland
Laurel Meadow
Wellesley
Providence
Thirty Seven
Westwood
Sunray I
Brambleton
Thirty Eight
E. W. Chittum School
Northside
Twenty-Five
Dumbarton
Joliff Middle School
Sedgefield
Larrymore
Holllybrook
Chesterfield
705
Roberts A Part 1
Central Gardens
Pebble Creek
Crossroads
702
Taylor Elementary SchoolMaury
Jones
Newmarket
Twenty-Eight
Fourth Ward First Precinct
113
Beaverdam Creek
Park PlaceBallentine
Twenty-Two
Hanover Grove
Thirteen
Johnson Park
Little Creek
303
Three Chopt
Twenty-Seven
Geneva Park
Windsor
Chrysler Museum
Kecoughtan
Bowling Park
Suburban Park
Maude Trevvett
Highland Springs
Eleven
Sixth Ward First Precinct
Tyler
Young Park
Jefferson
Thirty-Three
207
Rollingwood
Sherwood Rec CenterLafayette
Twenty-Three
Summit CourtHilliard
208
Greenwood
Larchmont Library
Fifth Ward First Precinct
Twenty-One
Third Presbyterian
Azalea Gardens
Twenty-Four
Shell
Twenty-Six
Twenty-Nine
South Morrison
Brookland
212Nine Mile
Arrowhead
Marshall
College Park
South Norfolk
Union Chapel
Oakview
Lafayette-Winona
Hampton Library
Oceanair
Tucker
Norview Methodist
Tanner's Creek
Roberts C Part 2
Lambert's Point
Norfolk Highlands
Newsome Park
Norview Middle School
Magruder
Hunton Y
Jacobs
Larchmont Recreation Center
Monument Hills
Oyster Point
Centerville
Oscar Smith School
Coleman Place SchoolGhent Square
Courtland
Canterbury
Stratford Hall
Tucker House
ReamsHuguenot
Bon Air
4-C Elephants Fork/Westhaven
StudleyStudley
Sussex
Surry
York
Gloucester
Isle of Wight
Henrico
Mathews
New Kent
Prince George
Charles City
James City
Suffolk
Dinwiddie
Chesterfield
Middlesex
Southampton
Hampton
Norfolk
Hanover
Newport News
King William
Lancaster
King and Queen
Poquoson
Portsmouth
Greensville
Richmond city
Chesapeake
Petersburg
Hopewell
WilliamsburgColonial Heights
HB 251 (2012)Congressional District 3
Gerrymandering Today
• Computers make it really effective
23
Gerrymandering Today
24
How does it work?
• States are broken into precincts• All precincts have the same size• We know voting preferences of each precinct• Group precincts into districts to maximize the number of districts
won by my party
25
R:65D:35
R:45D:55
R:47D:53
R:60D:40
Overall: R:217 D:183
vs
How does it work?
• States are broken into precincts• All precincts have the same size• We know voting preferences of each precinct• Group precincts into districts to maximize the number of districts
won by my party
26
R:65D:35
R:45D:55
R:47D:53
R:60D:40
Overall: R:217 D:183
R:65D:35
R:45D:55
R:47D:53
R:60D:40
R:125 R:92
R:65D:35
R:45D:55
R:47D:53
R:60D:40
R:112 R:105
Gerrymandering Problem Statement
• Given:– A list of precincts: 𝑝9, 𝑝:, … , 𝑝]– Each containing 𝑚 voters
• Output:– Districts 𝐷9, 𝐷: ⊂ {𝑝9, 𝑝:, … , 𝑝]}– Where 𝐷9 = |𝐷:|– 𝑅 𝐷9 > g]
<and 𝑅 𝐷: > g]
<• 𝑅(𝐷L) gives number of “Regular Party” voters in 𝐷L• 𝑅 𝐷L > ij
<means 𝐷L is majority “Regular Party”
– “failure” if no such solution is possible
27
𝑚 ⋅𝑛2 ⋅12
Valid Gerrymandering!
Dynamic Programming
• Requires Optimal Substructure– Solution to larger problem contains the solutions to smaller ones
• Idea:1. Identify the recursive structure of the problem• What is the “last thing” done?
2. Save the solution to each subproblem in memory3. Select a good order for solving subproblems• “Top Down”: Solve each recursively• “Bottom Up”: Iteratively solve smallest to largest
28
Dynamic Programming
• Requires Optimal Substructure– Solution to larger problem contains the solutions to smaller ones
• Idea:1. Identify the recursive structure of the problem• What is the “last thing” done?
2. Save the solution to each subproblem in memory3. Select a good order for solving subproblems• “Top Down”: Solve each recursively• “Bottom Up”: Iteratively solve smallest to largest
29
World Two
World One
Consider the last precinct
30
𝐷9𝑘 precincts𝑥 voters for R
𝐷:𝑛 − 𝑘 − 1 precincts𝑦 voters for R
𝑝]
𝐷9𝑘 + 1 precincts𝑥 + 𝑅(𝑝]) voters for R
𝐷:𝑛 − 𝑘 precincts𝑦 + 𝑅(𝑝]) voters for R
If we assign 𝑝]to 𝐷9
If we assign 𝑝]to 𝐷:
After assigning the first 𝑛 − 1 precincts
𝑝9, 𝑝:, … , 𝑝]G9
Valid gerrymandering if: 𝑘 + 1 = ]
:,
𝑥 + 𝑅 𝑝] , 𝑦 >g]<
Valid gerrymandering if:n − 𝑘 = ]
:,
𝑥, 𝑦 + 𝑅 𝑝] > g]<
𝐷9𝑘 + 1 precincts𝑥 + 𝑅(𝑝]) voters for R
𝐷9𝑘 precincts𝑥 voters for R
𝐷:𝑛 − 𝑘 − 1 precincts𝑦 voters for R
𝐷:𝑛 − 𝑘 precincts𝑦 + 𝑅(𝑝]) voters for R
Define Recursive Structure
𝑆 𝑗, 𝑘, 𝑥, 𝑦 =
31
True if from among the first 𝒋 precincts:𝒌 are assigned to 𝐷9exactly 𝒙 vote for R in 𝐷9exactly 𝒚 vote for R in 𝐷:
4D Dynamic Programming!!!
𝑛 × 𝑛 ×𝑚𝑛 ×𝑚𝑛
32
Two ways to satisfy 𝑆 𝑗, 𝑘, 𝑥, 𝑦 :𝑆 𝑗, 𝑘, 𝑥, 𝑦 = True if:
from among the first 𝑗 precincts𝑘 are assigned to 𝐷9exactly 𝑥 vote for R in 𝐷9exactly 𝑦 vote for R in 𝐷:
𝐷9𝑘 precincts𝑥 voters for R
𝐷:𝑗 − 𝑘 precincts𝑦 voters for R
𝐷9𝑘 − 1 precincts𝑥 − 𝑅(𝑝F) voters for R
𝐷:𝑗 − 𝑘 precincts𝑦 voters for R
𝐷9𝑘 precincts𝑥 voters for R
𝐷:𝑗 − 1 − 𝑘 precincts𝑦 − 𝑅(𝑝F) voters for R
𝑝F
Then assign 𝑝Fto 𝐷9
Then assign 𝑝Fto 𝐷:
𝑝F
OR
𝑆 𝑗, 𝑘, 𝑥, 𝑦 = 𝑆 𝑗 − 1, 𝑘 − 1, 𝑥 − 𝑅 𝑝F , 𝑦 ∨ 𝑆 𝑗 − 1, 𝑘, 𝑥, 𝑦 − 𝑅 𝑝F
Final Algorithm
Initialize 𝑆(0,0,0,0) = Truefor 𝑗 = 1,… , 𝑛:
for 𝑘 = 1,… ,min(𝑗, ]:):
for 𝑥 = 0,… , 𝑗𝑚:for 𝑦 = 0,… , 𝑗𝑚:𝑆 𝑗, 𝑘, 𝑥, 𝑦 =
𝑆 𝑗 − 1, 𝑘 − 1, 𝑥 − 𝑅 𝑝F , 𝑦 ∨ 𝑆 𝑗 − 1, 𝑘, 𝑥, 𝑦 − 𝑅 𝑝FSearch for True entry at 𝑆(𝑛, ]
:, > g]
<, > g]
<)
33
𝑆 𝑗, 𝑘, 𝑥, 𝑦 = 𝑆 𝑗 − 1, 𝑘 − 1, 𝑥 − 𝑅 𝑝F , 𝑦 ∨ 𝑆 𝑗 − 1, 𝑘, 𝑥, 𝑦 − 𝑅 𝑝F
𝑆 𝑗, 𝑘, 𝑥, 𝑦 = True if:from among the first 𝑗 precincts𝑘 are assigned to 𝐷9exactly 𝑥 vote for R in 𝐷9exactly 𝑦 vote for R in 𝐷:
Initialize 𝑆(0,0,0,0) = Truefor 𝑗 = 1,… , 𝑛:
for 𝑘 = 1,… ,min(𝑗, ]:):
for 𝑥 = 0,… , 𝑗𝑚:for 𝑦 = 0,… , 𝑗𝑚:𝑆 𝑗, 𝑘, 𝑥, 𝑦 =
𝑆 𝑗 − 1, 𝑘 − 1, 𝑥 − 𝑅 𝑝F , 𝑦 ∨ 𝑆 𝑗 − 1, 𝑘, 𝑥, 𝑦 − 𝑅 𝑝FSearch for True entry at 𝑆(𝑛, ]
:, > g]
<, > g]
<)
Run Time
34
𝑛𝑛2𝑛𝑚𝑛𝑚
Θ(𝑛<𝑚:)
𝑆 𝑗, 𝑘, 𝑥, 𝑦 = 𝑆 𝑗 − 1, 𝑘 − 1, 𝑥 − 𝑅 𝑝F , 𝑦 ∨ 𝑆 𝑗 − 1, 𝑘, 𝑥, 𝑦 − 𝑅 𝑝F
Θ(𝑛<𝑚:)
• Input: list of precincts (size 𝑛), number of voters (integer 𝑚)• Runtime depends on the value of 𝑚, not size of 𝑚– Run time is exponential in size of input– Input size is 𝑛 + 𝑚 = 𝑛 + log𝑚
• Note: Gerrymandering is NP-Complete
35