cs4102 l15 gerrymander greedysched

10
3/25/19 1 Warm up Given access to unlimited quantities of pennies, nickels dimes, and quarters, (worth value 1, 5, 10, 25 respectively), provide an algorithm which gives change for a given value ! using the fewest number of coins. CS4102 Algorithms Spring 2019 1 Change Making 43 cents 2 Change Making Algorithm Given: target value !, list of coins " = [% & ,…,% ) ] (in this case " = [1,5,10,25]) Repeatedly select the largest coin less than the remaining target value: 3 while(!>0) let % = max(% 4 ∈ {% & ,…,% ) }|% 4 ≤ !) print % !=!−%

Upload: others

Post on 15-Oct-2021

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: cs4102 L15 gerrymander greedysched

3/25/19

1

Warm upGiven access to unlimited quantities of pennies, nickels

dimes, and quarters, (worth value 1, 5, 10, 25 respectively), provide an algorithm which gives change for a given value ! using the fewest number of coins.

CS4102 AlgorithmsSpring 2019

1

Change Making43 cents

2

Change Making Algorithm• Given: target value !, list of coins " = [%&,… , %)]

(in this case " = [1,5,10,25])• Repeatedly select the largest coin less than the remaining

target value:

3

while(! > 0)let % = max(%4 ∈ {%& ,… , %)} | %4 ≤ !)print %! = ! − %

Page 2: cs4102 L15 gerrymander greedysched

3/25/19

2

Why does this always work?• If ! < 5, then pennies only– 5 pennies can be exchanged for a nickel

• If 5 ≤ ! < 10we must have a nickel– 2 nickels can be exchanged for a dime

• If 10 ≤ ! < 25we must have at least 1 dime– 3 dimes can be exchanged for a quarter and a

nickel• If ! ≥ 25we must have at least 1 quarter

4

Only case Greedy uses pennies!

Only case Greedy uses nickels!

Only case Greedy uses dimes!

Only case Greedy uses quarters!

Today’s Keywords• Dynamic Programming• Gerrymandering• Greedy Algorithms• Choice Function• Change Making

5

CLRS Readings• Chapter 15• Chapter 16

6

Page 3: cs4102 L15 gerrymander greedysched

3/25/19

3

Homeworks

• Homework 5 due Wednesday March 27 at 11pm– Seam Carving!– Dynamic Programming (implementation)– Java or Python

• Homework 6 out tonight, due Wednesday April 3 at 11pm– Dynamic Programming and Greedy Algorithms–Written (using Latex!)

7

Dynamic Programming• Requires Optimal Substructure– Solution to larger problem contains the solutions to smaller ones

• Idea:1. Identify recursive structure of the problem• What is the “last thing” done?

2. Select a good order for solving subproblems• “Top Down”: Solve each recursively• “Bottom Up”: Iteratively solve smallest to largest

3. Save solution to each subproblem in memory

8

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

9

Page 4: cs4102 L15 gerrymander greedysched

3/25/19

4

DP Algorithms so far

• 2×# domino tiling (Fibonacci)• Log cutting• Matrix Chaining• Longest Common Subsequence• Seam Carving

10

Domino Tiling

11

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

12

10987654321Length:

!"#(%): 0

0

Solve Smallest subproblem first

!"# 4 = max!"# 3 + .[1]!"# 2 + . 2!"# 1 + . 3!"# 0 + .[4]

4

Page 5: cs4102 L15 gerrymander greedysched

3/25/19

5

Matrix Chaining

13

30

35

×%& 35

15

×%(15

5

×%) 5

10

×%*

10

20

×%, 20

25

%-

./01 2, 4 = min9:&

;<&./01 2, = + ./01 = + 1, 4 + ?@?;A&B9

./01 2, 2 = 00 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

./01 1,6 = min

./01 1,1 + ./01 2, 6 + ?&?(B-

./01 1,2 + ./01 3, 6 + ?&?)B-

./01 1,3 + ./01 4, 6 + ?&?*B-

./01 1,4 + ./01 5, 6 + ?&?,B-

./01 1,5 + ./01 6, 6 + ?&?-B-

15125

4 == 2

Longest Common Subsequence

14

!"# $, & =0 if $ = 0 or& = 0!"# $ − 1, & − 1 + 1 if / $ = 0[&]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

/ = 8 9 " 99 : 81 2 3 74 5 600 =

01

3456

29

"898

:

To fill in cell ($, &)we need cells $ − 1, & − 1 , $ − 1, & , ($, & − 1)Fill from Top->Bottom, Left->Right (with any preference)

16

Page 6: cs4102 L15 gerrymander greedysched

3/25/19

6

Gerrymandering• Manipulating electoral district

boundaries to favor one political party over others

• Coined in an 1812 Political cartoon

• Governor Gerry signed a bill that redistricted Massachusetts to benefit his Democratic-Republican Party

17

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

18

VA 5th District

19

Page 7: cs4102 L15 gerrymander greedysched

3/25/19

7

Gerrymandering Today• Computers make it really effective

20

")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

21

Gerrymandering Today• Computers make it really effective

22

Page 8: cs4102 L15 gerrymander greedysched

3/25/19

8

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

23

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: !", !$ , … , !&– Each containing ' voters

• Output:– Districts (", ($ ⊂ {!" , !$ , … , !&}– Where (" = |($|– . (" , . ($ > 0&

1• .((3) gives number of “Regular Party” voters in (3• . (3 > 56

1 means (3 is majority “Regular Party”– “failure” if no such solution is possible

24

Dynamic Programming• Requires Optimal Substructure– Solution to larger problem contains the solutions to smaller ones

• Idea:1. Identify recursive structure of the problem• What is the “last thing” done?

2. Select a good order for solving subproblems• “Top Down”: Solve each recursively• “Bottom Up”: Iteratively solve smallest to largest

3. Save solution to each subproblem in memory

25

Page 9: cs4102 L15 gerrymander greedysched

3/25/19

9

Consider the last precinct

26

!"# precincts$ voters for R

!%& − # − 1 precincts) voters for R

*+

!"# + 1 precincts$ +-(*+) voters for R

!%& − # precincts) +-(*+) voters for R

If we assign *+to !"

If we assign *+to !%

After assigning the first & − 1 precincts

Valid gerrymandering if: # + 1 = +

%,$ + - *+ , ) > 3+

4

Valid gerrymandering if:n− # = +

%,$, ) + - *+ > 3+

4

Define Recursive Structure

! ", $, %, & =

27

True if from among the first ( precincts:) are assigned to *+exactly , vote for R in *+exactly - vote for R in *.

4D Dynamic Programming!!!

/ × / ×1/ ×1/

Two ways to satisfy ! ", $, %, & :

28

! ", $, %, & = True if:from among the first " precincts$ are assigned to ()exactly % vote for R in ()exactly & vote for R in (*

()$ precincts% voters for R

(*" − $ precincts& voters for R

()$ − 1 precincts% −-(/0) voters for R

(*" − $ precincts& voters for R

()$ precincts% voters for R

(*" − 1− $ precincts& −-(/0) voters for R

/0Then assign /0to ()

Then assign /0to (*

/0

OR

! ", $, %, & = ! " − 1, $ − 1, % − - /0 , & ∨ ! " − 1, $, %, & − - /0

Page 10: cs4102 L15 gerrymander greedysched

3/25/19

10

Final Algorithm

Initialize !(0,0,0,0) = Truefor ' = 1,…,*:

for + = 1,…,min(', /0):for 1 = 0,…, '2:

for 3 = 0,…, '2:! ', +, 1, 3 =

! ' −1,+ −1, 1 −5 67 , 3∨ ! ' −1,+, 1, 3 −5 67

Search for True entry at !(*, /0 ,>:/; ,>

:/; )

29

! ', +, 1, 3 = ! ' − 1, + − 1, 1 − 5 67 , 3 ∨ ! ' − 1, +, 1, 3 − 5 67

! ', +, 1, 3 = True if:from among the first ' precincts+ are assigned to <=exactly 1 vote for R in <=exactly 3 vote for R in <0

Run Time

Initialize !(0,0,0,0) = Truefor ' = 1,…,*:

for + = 1,…,min(', /0):for 1 = 0,…, '2:

for 3 = 0,…, '2:! ', +, 1, 3 =

! ' −1,+ −1, 1 −5 67 , 3∨ ! ' −1,+, 1, 3 −5 67

Search for True entry at !(*, /0 ,>:/; ,>

:/; )

30

! ', +, 1, 3 = ! ' − 1, + − 1, 1 − 5 67 , 3 ∨ ! ' − 1, +, 1, 3 − 5 67

**2 *2

*2

Θ(*;20)

Θ(#$%&)• Runtime depends on the value of %, not size of %• Run time is exponential in size of input• Note: Gerrymandering is NP-Complete

31