acm today this week's problems… new and improved :-) acm table slacking! not slacking…...

Post on 04-Jan-2016

213 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

ACM today

This week's problems…

New and improved :-) ACM table

slacking!

not slacking…

missing entries? let me know…

Mock mock contest ?

Lots of teams solved problemsAll problems were solved by some team

honey

marbles birthday

Mock mock contest ?

Lots of teams solved problemsAll problems were solved by some team

honey

marbles birthday

Keep track of the number of paths of length K that end in each cell

go to K+1 …

From 2d to 1d:

compute the best horizontal and vertical displacements separately

Create a "containment" graph of nesting boxes, and then find the LONGEST path in it.

Graph algorithms

Always at least one contest problem…• all-pairs shortest

paths• maximum flow from a "source" to a "sink"

The source of all graph problems…

student projects: summer 2007

QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.

QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.

Kaylin Spitz

Emma Liu

measuring centrality in very large connection networks (of proteins)

4,519 nodes

The tollgate centrality of a node, V, anchored on nodes A and B, all in graph G, is the betweenness centrality of V in the A,B shortest-path subgraph of G.

All-pairs shortest paths

A

B

ED

C

8

13

1

6

12

9

7 0

11

0 8 13 - 1- 0 - 6 12

- 9 0 - -

7 - 0 0 -

- - - 11 0

AB

C

D

E

A B C D E

from

to

“Floyd-Warshall algorithm”

D0 = (dij )

0

dij = shortest distance from i to j through nodes {1, …, k}

k

dij = shortest distance from i to j through no nodes

0

Adjacency Matrix

All-pairs shortest paths...

D0 = (dij )

0

0 8 13 - 1- 0 - 6 12

- 9 0 - -

7 - 0 0 -

- - - 11 0

dij = shortest distance from i to j through {1, …, k}

k

“Floyd-Warshall algorithm”

BeforeAB

C

D

E

A B C D E

D1 = (dij )

1

0 8 13 - 1- 0 - 6 12

- 9 0 - -

7 15 0 0 8

- - - 11 0

AfterAB

C

D

E

A B C D E

All-pairs shortest paths...

D0 = (dij )

0

0 8 13 - 1- 0 - 6 12

- 9 0 - -

7 - 0 0 -

- - - 11 0

AB

C

D

E

D1 = (dij )

1

dij = shortest distance from i to j through {1, …, k}

k

0 8 13 - 1- 0 - 6 12

- 9 0 - -

7 15 0 0 8

- - - 11 0

“Floyd-Warshall algorithm”

Before AfterAB

C

D

E

A B C D E

AB

C

D

E

A B C D E

+ )

dij

=

k dij

k-1, dik

k-1 dkj

k-1min(

All-pairs shortest paths...

0 8 13 14 1- 0 - 6 12

- 9 0 15 21

7 15 0 0 8

- - - 11 0

AB

C

D

E

D2 = (dij )

2

0 8 13 14 1- 0 - 6 12

- 9 0 15 21

7 9 0 0 8

- - - 11 0

AB

C

D

E

D3 = (dij )

3

0 8 13 14 113 0 6 6 12

22 9 0 15 21

7 9 0 0 8

18 20 11 11 0

AB

C

D

E

D4 = (dij )

4

AB

C

D

E

D5 = (dij )

5

to store the path, another matrix can track the last intermediate vertex

0 8 12 12 113 0 6 6 12

22 9 0 15 21

7 9 0 0 8

18 20 11 11 0

Thanks to Wikipedia…Floyd Warshall…

How would you change this to find longest paths? What has to be true about the

graph?

Problem Set #2: Graph algorithms

Problem Set #2: Graph algorithms

20 0 2 20 0 0 10 1 0 20 2 1 21 2 2 2-131 1 1 1 2 31 1 2 1 1 31 1 3 1 2 31 1 1 1 1 01 1 0 1 0 01 0 0 0 0 0-10

number of dimensions

Input

starting point and destination point

edges in the graph

next number of dimensions

0 0

2 2

0 1

0 2 1 2

OutputMaze #1 can be traveled

Maze #2 cannot be traveled

Problem Set #2: Graph algorithms

1 25 43 13 23 43 50

1 22 33 44 55 10

Input

undirected edges in the graph

end of graph signal

"single point of failure"

first graph

second graph

Problem Set #2: Graph algorithms

1 25 43 13 23 43 50

1 22 33 44 55 10

Input

undirected edges in the graph

end of graph signal

OutputNetwork #1

SPF node 3 leaves 2 subnets

Network #2 No SPF nodes

first graph

second graph

Problem Set #2: Graph algorithms

10 2 30 31 32 35 2 40 10 31 22 30 0 0

Input

undirected edges in the graph

end of input signal

To generate: 10 2-bit numbers in a graph with 3 edges

To generate: 5 2-bit numbers in a graph with 4 edges

Graphs always have 2bits nodes.

1

2

3

0procedure:

Start at a random node, generate its node #

Choose a random edge and follow it

Generate the destination node's #

Problem Set #2: Graph algorithms

10 2 30 31 32 35 2 40 10 31 22 30 0 0

Input

undirected edges in the graph

end of input signal

To generate: 10 2-bit numbers in a graph with 3 edges

To generate: 5 2-bit numbers in a graph with 4 edges

Graphs always have 2bits nodes.

1

2

3

0procedure:

Start at a random node, generate its node #

Choose a random edge and follow it

Generate the destination node's #

step 1

step 2

step 3

00111011

start

Problem Set #2: Graph algorithms

10 2 30 31 32 35 2 40 10 31 22 30 0 0

Input

undirected edges in the graph

To generate: 10 2-bit numbers in a graph with 3 edges

Graphs always have 2bits nodes.

procedure:

Start at a random node, generate its node #

Choose a random edge and follow it

Generate the destination node's #

Output

NoYes "reasonably random" -

all bits are between 25% and 75% likely to be the value 0 or 1

some bits are biased more than 75%

1

2

3

0

Jotto!

Sophomores Juniors Seniors Me

fjord 3 fjord 0 fjord 1 fjord 2

tempt 1

marks 1

There are

2121 words scoring 0 vs. fjord2682 words scoring 1 vs. fjord1060 words scoring 2 vs. fjord132 words scoring 3 vs. fjord4 fjord: fords, frond, fordy, fardo, foder, dorje,

tempt 1

marks 3

tempt 2

marks 0

tempt 0

marks 1

Next time -

A second mock mock contest

Max Flow

Max network flow…

A

B

E

D

C13

F

16

10 4 9

12

14

7

20

4source

capacity

sink

The problem how much traffic can get from the source to the sink ?

Max FlowThe problem how much traffic can get from the source to the sink ?

A

B

E

D

C13

AB

C

D

E

FROM

A B C D E

F

- 16 13 - -- - 10 12 -

- 4 - - 14

- - 9 - -

- - - 7 -

--

-

20

4

- - - - - -F

F

16

10 4 9

12

14

7

20

4

TO

“Capacity Graph”

source

sink

capacity C

Find a path in CThe problem how much traffic can get from the source to the sink ?

A

B

E

D

C13

FROM

F

- 16 13 - -- - 10 12 -

- 4 - - 14

- - 9 - -

- - - 7 -

--

-

20

4

- - - - - -

16

10 4 9

12

14

7

20

4

TO

“Capacity Graph”

source

sink

capacity CAB

C

D

E

A B C D E

F

F

Create FCreate a FLOW GRAPH with the minimum weight from that

path

A

B

E

D

C13

AB

C

D

E

FROM

A B C D E

F

- 12 0 - --12

- 0 12 -

- 0 - - 0

- -12

0 - -

- - - 0 -

--

-

12

0

- - - -12

- -F

F

16

10 4 9

12

14

7

20

4

TO

“Flow Graph”

source

sink

capacity F

12

12

12

flow in one direction is negative flow in the other direction

R = C - FCreate a RESIDUAL GRAPH with the rest of the capacity

after that flow

A

B

E

D

C13

AB

C

D

E

FROM

A B C D E

F

F

F

4

10 4 9

0

14

7

8

4

TO

“Residual Graph”

source

sink

capacity R

12 12

reverse edges allow the "undoing" of previous flow!

12

- 4 13 - -12 - 10 0 -

- 4 - - 14

- 12 9 - -

- - - 7 -

--

-

8

4

- - - 12 - -

Keep going!Continue running this on the residual capacity until BFS

fails…

A

B

E

D

C12/13

F

11/16

0/10

1/4

0/9

12/12

11/14

7/7

19/20

4/4

source

sink

There is no longer a path from A to

F !

The max flow algorithm

A

B

E

D

C13

AB

C

D

E

FROM

A B C D E

F

- 12 11 - -4 - 9 9 -

2 5 - 2 5

- 3 7 - 1

- - 5 6 -

--

-

18

0

- - - 2 4 -F

F

16

10 4 9

12

14

7

20

4

TO

“Residual Graph”

source

sink

capacity

4

3

2

1

5

2

2

10

4

flowR

A

B

C

D

EF

218

4

3 residual

9

5

9

72592

11

124

61

Floyd-Fulkerson

1. Set F to all 02. Compute R.3. BFS in R from source to sink.4a. If no path, you’re done. 4b. If a path, add the available capacity to F; goto (2).

Get into the flow!def edmonds_karp(C, source, sink):

n = len(C) # C is the capacity matrix F = [[0] * n for i in range(n)] # F is the flow matrix # residual capacity from u to v is C[u][v] - F[u][v]

while True: path = BFS(C, F, source, sink) if not path: break flow = float(1e309) # Infinity ! # traverse path to find smallest capacity for (u,v) in path: flow = min(flow, C[u][v] - F[u][v]) # traverse path to update flow for u,v in path: F[u][v] += flow F[v][u] -= flow return sum([F[source][i] for i in xrange(n)])

def BFS(C, F, source, sink): queue = [source] paths = {source: []} while queue: u = queue.pop(0) for v in range(len(C)): if C[u][v] - F[u][v] > 0 and v not in paths: paths[v] = paths[u] + [(u,v)] if v == sink: return paths[v] queue.append(v) return None

Pseudo-code (Python)

A little bit of name contention…

edmonds_karp

is really Floyd-Fulkerson, but

with other people getting the credit…

Problem Set #2: Graph algorithms

Problem Set #2: Graph algorithms

20 0 2 20 0 0 10 1 0 20 2 1 21 2 2 2-131 1 1 1 2 31 1 2 1 1 31 1 3 1 2 31 1 1 1 1 01 1 0 1 0 01 0 0 0 0 0-10

number of dimensions

Input

starting point and destination point

edges in the graph

next number of dimensions

0 0

2 2

0 1

0 2 1 2

OutputMaze #1 can be traveled

Maze #2 cannot be traveled

Problem Set #2: Graph algorithms

1 25 43 13 23 43 50

1 22 33 44 55 10

Input

undirected edges in the graph

end of graph signal

"single point of failure"

first graph

second graph

Problem Set #2: Graph algorithms

1 25 43 13 23 43 50

1 22 33 44 55 10

Input

undirected edges in the graph

end of graph signal

OutputNetwork #1

SPF node 3 leaves 2 subnets

Network #2 No SPF nodes

first graph

second graph

Problem Set #2: Graph algorithms

10 2 30 31 32 35 2 40 10 31 22 30 0 0

Input

undirected edges in the graph

end of input signal

To generate: 10 2-bit numbers in a graph with 3 edges

To generate: 5 2-bit numbers in a graph with 4 edges

Graphs always have 2bits nodes.

1

2

3

0procedure:

Start at a random node, generate its node #

Choose a random edge and follow it

Generate the destination node's #

Problem Set #2: Graph algorithms

10 2 30 31 32 35 2 40 10 31 22 30 0 0

Input

undirected edges in the graph

end of input signal

To generate: 10 2-bit numbers in a graph with 3 edges

To generate: 5 2-bit numbers in a graph with 4 edges

Graphs always have 2bits nodes.

1

2

3

0procedure:

Start at a random node, generate its node #

Choose a random edge and follow it

Generate the destination node's #

step 1

step 2

step 3

00111011

start

Problem Set #2: Graph algorithms

10 2 30 31 32 35 2 40 10 31 22 30 0 0

Input

undirected edges in the graph

To generate: 10 2-bit numbers in a graph with 3 edges

Graphs always have 2bits nodes.

procedure:

Start at a random node, generate its node #

Choose a random edge and follow it

Generate the destination node's #

Output

NoYes "reasonably random" -

all bits are between 25% and 75% likely to be the value 0 or 1

some bits are biased more than 75%

1

2

3

0

Jotto!

Sophomores Juniors Seniors Me

fjord 3 fjord 0 fjord 1 fjord 2

????? ?

????? ?

There are

2121 words scoring 0 vs. fjord2682 words scoring 1 vs. fjord1060 words scoring 2 vs. fjord132 words scoring 3 vs. fjord4 words scoring 4 vs. fjord

????? ?

????? ?

????? ?

????? ?

????? ?

????? ?

Next time -

A second mock mock contest

ACM today

This week's problems…

New and improved :-) ACM table

slacking!

not slacking…

missing entries? let me know…

All-pairs shortest paths

A

B

ED

C

8

13

1

6

12

9

7 0

11

0 8 13 - 1- 0 - 6 12

- 9 0 - -

7 - 0 0 -

- - - 11 0

AB

C

D

E

A B C D E

from

to

“Floyd-Warshall algorithm”

D0 = (dij )

0

dij = shortest distance from i to j through nodes {1, …, k}

k

dij = shortest distance from i to j through no nodes

0

Adjacency Matrix

All-pairs shortest paths...

D0 = (dij )

0

0 8 13 - 1- 0 - 6 12

- 9 0 - -

7 - 0 0 -

- - - 11 0

dij = shortest distance from i to j through {1, …, k}

k

“Floyd-Warshall algorithm”

BeforeAB

C

D

E

A B C D E

D1 = (dij )

1

0 8 13 - 1- 0 - 6 12

- 9 0 - -

7 15 0 0 8

- - - 11 0

AfterAB

C

D

E

A B C D E

All-pairs shortest paths...

0 8 13 14 1- 0 - 6 12

- 9 0 15 21

7 15 0 0 8

- - - 11 0

AB

C

D

E

D2 = (dij )

2

0 8 13 14 1- 0 - 6 12

- 9 0 15 21

7 9 0 0 8

- - - 11 0

AB

C

D

E

D3 = (dij )

3

0 8 13 14 113 0 6 6 12

22 9 0 15 21

7 9 0 0 8

18 20 11 11 0

AB

C

D

E

D4 = (dij )

4

AB

C

D

E

D5 = (dij )

5

to store the path, another matrix can track the last intermediate vertex

0 8 12 12 113 0 6 6 12

22 9 0 15 21

7 9 0 0 8

18 20 11 11 0

Thanks to Wikipedia…Floyd Warshall…

How would you change this to find longest paths? What has to be true about the

graph?

Max FlowThe problem how much traffic can get from the source to the sink ?

A

B

E

D

C13

AB

C

D

E

FROM

A B C D E

F

- 16 13 - -- - 10 12 -

- 4 - - 14

- - 9 - -

- - - 7 -

--

-

20

4

- - - - - -F

F

16

10 4 9

12

14

7

20

4

TO

“Capacity Graph”

source

sink

capacity C

Create FCreate a FLOW GRAPH with the minimum weight from that

path

A

B

E

D

C13

AB

C

D

E

FROM

A B C D E

F

- 12 0 - --12

- 0 12 -

- 0 - - 0

- -12

0 - -

- - - 0 -

--

-

12

0

- - - -12

- -F

F

16

10 4 9

12

14

7

20

4

TO

“Flow Graph”

source

sink

capacity F

12

12

12

flow in one direction is negative flow in the other direction

R = C - FCreate a RESIDUAL GRAPH with the rest of the capacity

after that flow

A

B

E

D

C13

AB

C

D

E

FROM

A B C D E

F

F

F

4

10 4 9

0

14

7

8

4

TO

“Residual Graph”

source

sink

capacity R

12 12

reverse edges allow the "undoing" of previous flow!

12

- 4 13 - -12 - 10 0 -

- 4 - - 14

- 12 9 - -

- - - 7 -

--

-

8

4

- - - 12 - -

Get into the flow!def edmonds_karp(C, source, sink):

n = len(C) # C is the capacity matrix F = [[0] * n for i in range(n)] # F is the flow matrix # residual capacity from u to v is C[u][v] - F[u][v]

while True: path = BFS(C, F, source, sink) if not path: break flow = float(1e309) # Infinity ! # traverse path to find smallest capacity for (u,v) in path: flow = min(flow, C[u][v] - F[u][v]) # traverse path to update flow for u,v in path: F[u][v] += flow F[v][u] -= flow return sum([F[source][i] for i in xrange(n)])

def BFS(C, F, source, sink): queue = [source] paths = {source: []} while queue: u = queue.pop(0) for v in range(len(C)): if C[u][v] - F[u][v] > 0 and v not in paths: paths[v] = paths[u] + [(u,v)] if v == sink: return paths[v] queue.append(v) return None

Pseudo-code (Python)

A little bit of name contention…

edmonds_karp

is really Floyd-Fulkerson, but

with other people getting the credit…

Problem Set #2: Graph algorithms

1 25 43 13 23 43 50

1 22 33 44 55 10

Input

undirected edges in the graph

end of graph signal

OutputNetwork #1

SPF node 3 leaves 2 subnets

Network #2 No SPF nodes

first graph

second graph

Problem Set #2: Graph algorithms

10 2 30 31 32 35 2 40 10 31 22 30 0 0

Input

undirected edges in the graph

To generate: 10 2-bit numbers in a graph with 3 edges

Graphs always have 2bits nodes.

procedure:

Start at a random node, generate its node #

Choose a random edge and follow it

Generate the destination node's #

Output

NoYes "reasonably random" -

all bits are between 25% and 75% likely to be the value 0 or 1

some bits are biased more than 75%

1

2

3

0

Jotto!

Sophomores Juniors Seniors Me

A word-guessing game similar to mastermind…

fjord 3 fjord 0 fjord 1 fjord 2

????? ?

????? ?

????? ?

????? ?

????? ?

????? ?

????? ?

????? ?

A

B

C

D

E

A B C D E

from

to

Problem Set #1: multiple.java

263190

integers (N) with 2 < N < 10000

0 marks the end of the input

Input

change!

What is this course about?

• A chance to “improve” your programming skillsAlgorithm analysis and

insightProgram design and implementation

optimizing coding time

ACM programming contest

What

Why

Research/prototype programming

Hands-on practice with algorithms and techniquesFamiliarity with C++’s STL or Java’s API (more options in the spring…)

What is this course about?

• A chance to “improve” your programming skillsAlgorithm analysis and

insightProgram design and implementation

optimizing coding time

ACM programming contest

What

Why

Research/prototype programming

Hands-on practice with algorithms and techniques

Unofficial course name: CS -70

Familiarity with C++’s STL or Java’s API (more options in the spring…)

2006 ACM Results

http://socalcontest.acm.org/

http://icpc.baylor.edu/icpc/ Site for the world finals

Site for the regionals

Class Organization

Feedback from prior semesters…

• more contest-like practice sessions

• lab sessions not considered best use of time

• better to have the course graded individually

• there should be opportunities to start coding “cold”

• continue to play jotto

• have better snacks <-- need more feedback here

Course Organization

Sep 4 Welcome! (topic: dynamic programming)Sep 11 Practice session to work on problems (here - laptops?)Sep 18 Discussion session (topic: graph algorithms)

Sep 25 Practice session (here…)Oct 2 Discussion session (topic: search problems)

Oct 9 Practice session (here…)Oct 16 Discussion session (topic: geometry/parsing)

Oct 23 No class - Fall breakOct 30 Mock ACM contest, 9pm – 1am, teams of 3, in CS labs

Nov 6 Brief meeting for the ACM contest participants

Nov 10 Regional ACM contest in Riverside

Nov 13 Wrap-up session for everyone

Problems and grades

alternating format

discussion sessions

lab sessions

• problem and program analysis

• strategy, coding tips

• 3 problems, count individually

• 3 problems can be done by team or individually -- you decide

• meet here in this room (?) - bring a laptop - I'll have some too.

• contest conditions:• new problems• one computer per team• credit for the entire team

Problems can be done any time during the term.

Problems solved during the lab sessions count as2 problems before 6pm

Course webpage

reference links

administrative info

problem statements and example I/O

people

results!!

Grading

CS 189 is graded individually... (it’s possible to take it P/F, too)

Coding Guidelines• problems can be done any time

during the semester

• discussion of algorithms always OK

• coding should be within teams

• during lab "contests", you may only use C++ or Java API references

• anyother time, you may use any reference at all except an existing solution or partial solution…

• use /cs/ACM/acmSubmit <file> to submit

• try things out !

the reason for ACM!

Java !

• extensive library of data structures and algorithms available

Not required (C++ is the alternative), but consider...

• I/O made simpler with 1.5’s Scanner and printf

the C in CS!

H.ni() anyone?

Jotto!

Sophomores Juniors Seniors Me

A word-guessing game similar to mastermind…

vvvvv vvvvv vvvvv vvvvv

Problem Set #1: Dynamic Programming

Problem Set #1: multiple.java

263190

integers (N) with 2 < N < 10000

0 marks the end of the input

Input

10111011111001

the smallest (decimal) multiple of N with only

the digits 0 and 1 !

Output

change!

change!

Problem Set #1: multiple.java

263190

integers (N) with 2 < N < 10000

0 marks the end of the input

Input

10111011111001

the smallest (decimal) multiple of N with only

the digits 0 and 1 !

Output

change!

change!

Ideas?

Most naïve solution…

…how can we save time?

Dynamic programming

Storing intermediate results in a table for fast look-up: multiple.ja

vainput N = 6

possible remainders upon dividing by N (6)

# of digits in answer

0 1 2 3 4 5

2

1

3

4

Dynamic programming

Storing intermediate results in a table for fast look-up:input N = 6

possible remainders upon dividing by N (6)

# of digits in answer

0 1 2 3 4 5

2

1

3

4

1

Dynamic programming

Storing intermediate results in a table for fast look-up:input N = 6

possible remainders upon dividing by N (6)

# of digits in answer

0 1 2 3 4 5

2

1

3

4

1

10 111

Dynamic programming

Storing intermediate results in a table for fast look-up:input N = 6

possible remainders upon dividing by N (6)

# of digits in answer

0 1 2 3 4 5

2

1

3

4

1

111110 10 111

10 111

Dynamic programming

Storing intermediate results in a table for fast look-up:input N = 6

possible remainders upon dividing by N (6)

# of digits in answer

0 1 2 3 4 5

2

1

3

4

1

111110 10 111

10 111

1110 111110 10 111

Problem Set #1: multiple.java

263190

integers (N) with 2 < N < 10000

0 marks the end of the input

Input

10111011111001

the smallest (decimal) multiple of N with only

the digits 0 and 1 !

Output

change!

change!

Java Classes

BigInteger

LinkedList<String>int[]

Coding tips: multiple.java

import java.util.Scanner;import java.util.LinkedList;import java.math.BigInteger;

class multiple{ public static void pl(String s) { System.out.println(s); } public static void pld(String s) { /* pl(s); */ ; }

Coding tips: multiple.java

import java.util.Scanner;import java.util.LinkedList;import java.math.BigInteger;

class multiple{ public static void pl(String s) { System.out.println(s); } public static void pld(String s) { /* pl(s); */ ; }

public static void main(String[] argv){ Scanner s = new Scanner(System.in); // for input int[] remainders; // the DP table LinkedList<String> LL; // a queue

Coding tips: multiple.java

while (true) // while there is input{ LL = new LinkedList<String>(); // new, empty queue LL.addLast("1"); // starting value

while (LL.size() > 0) // more data left? { String next = LL.removeFirst(); // dequeue from front

the LinkedList class is a ready-made Deque

Coding tips: multiple.java

BigInteger r0_big = (new BigInteger(next0)).remainder(input); int r0 = r0_big.intValue(); // convert to an intif (r0 == 0) { // we're done! pl(next0); // print the answer break; // go to next input}

the BigInteger constructor takes a String

Coding tips: multiple.java

BigInteger r0_big = (new BigInteger(next0)).remainder(input); int r0 = r0_big.intValue(); // convert to an intif (r0 == 0) { // we're done! pl(next0); // print the answer break; // go to next input}

the BigInteger constructor takes a String

if (remainders[r0] == 0) { // have we seen this one? remainders[r0] = 42; // 42 marks it as seen pld(" " + next0 + "," + r0); // for debugging LL.addLast( next0 ); // put next0 on the queue}

our table

Problem Set #1: bookcase.java

14220 29195 20200 9180 30

number of data sets

Input

number of books in this data set

height and width of each book

Problem Set #1: bookcase.java

14220 29195 20200 9180 30

number of data sets

Input

number of books in this data set

height and width of each book

Fit the books tightly onto a rectangular, 3-shelf bookcase:

220

200

180

30

29

20 + 9

30

Problem Set #1: bookcase.java

14220 29195 20200 9180 30

number of data sets

Input

number of books in this data set

height and width of each book

Fit the books tightly onto a rectangular, 3-shelf bookcase:

220

200

180

30

29

20 + 9

30Output

What is the smallest total area for such a bookshelf?

Problem Set #1: bookcase.java

14220 29195 20200 9180 30

number of data sets

Thoughts?

number of books in this data set

height and width of each book

Fit the books tightly onto a rectangular, 3-shelf bookcase:

220

200

180

30

29

20 + 9

30Output

What is the smallest total area for such a bookshelf?

Input

Tracking width, height, and books…

Consider the books in order of height (decreasing)

width of Shelf #1

# of books in top two shelves

0 1 2 3 4 5

2

1

3

4

1

111110 10 111

10 111

1110 111110 10 111

Key decisions will be when to start using the 2nd + 3rd shelves

Coaches’ Room

top related