Introduction to Algorithms
Introduction to Algorithms
3rd Edition
ISBN: 9780262033848
Author: Thomas H. Cormen, Ronald L. Rivest, Charles E. Leiserson, Clifford Stein
Publisher: MIT Press
Question
Book Icon
Chapter 35, Problem 1P

a

Program Plan Intro

To prove the problem of determining the minimum number of bins required is NP-HARD.

a

Expert Solution
Check Mark

Explanation of Solution

Suppose S={x1,x2,.......,xk} and x=1ikxi . It can be assumed that tx12 because the answer to and are the same (indeed if the answer is yes for. Then there exists C' such that xi=t and then there is xi(S/S')xi=Xt and so the answer is yes for. On the other hand if the answer is yes for , so if t<x/12 , it can be considered t;<xtx/2 . Then compute x=1ikxi and compare it with t .

Note that

  1inSi=1ikxi/t=1t1inxi+(2tx)/t=x/t+(2tx)/2=2

So, the answer of derived bin-packing problem can-not be less than 2 . First assume that the answer to the given instance of sub set -sum problem is “yes”.

Now, there is S'={xi1,xi2,.......,xil} so that ijlxij=t . Thus, the result is ijlSij=1<j<lxij/t=t/t=1 .

Now {Si1,Si2,.......,Sil} can be packed in one bin and the remaining objects in the other bin (the sum of all object sizes is 2. So, the sum of remaining object sizes are1). Since it is known that the answer to this bin-packing problem can’t be less than 2, so the answer to it will be 2. Now assume that the answer to the derived instance of bin-packing problem is 2. Assume {Si1,Si2,.......,Sil} be the sizes of the objects in one of two bins that does not contain the object with the size Sk+1=(2tx)/t we have ij<lSij=1

  1<j<lxij/t=11<j<lxij=t . So a subset S'={xi1,xi2,.......,xil} of S is found so that 1<j<lxij=t . Thus the answer to the given instance of the subset-sum problem is “yes”.

b

Program Plan Intro

To argue that the optimal number of bins required is at least S .

b

Expert Solution
Check Mark

Explanation of Solution

Consider the packing ofn objects with sizes S1,S2,........,SN intob bins. Let S=i=1NSi

Let e1,e2,........,eb be the unused space in each bin and let e=i=1bei

Therefore,

  b=totalspaceinbinssizeofeachbin=spaceusedinbins+spaceunusedinbins1=S+eS (sincee0).

Since bis an integer number, b[S] . This is true in particular for the optimal solution b*[S] .

c

Program Plan Intro

To argue that the first-fit heuristic leaves at most one bin less than half full.

c

Expert Solution
Check Mark

Explanation of Solution

The first- fit heuristic places each object in the first available bin. Suppose by contradiction that two bins bi and bj are half full and supposei < j. All object in would fit into since both and are half full. Therefore, the first-fit heuristic would have placed these objects in bi . Therefore, it is not possible that two bins may be half empty under the first-fit heuristic.

d

Program Plan Intro

To prove that the number of bins used by the first-fit heuristic is never more than 2S

d

Expert Solution
Check Mark

Explanation of Solution

Consider a bin- packing solution provided by the first-fit HEURISTIC.

Using similar notation as in (b) (let S=i=1NSi and e=i=1bei )

  b=S+e=S+i=1bei .......(1)

It can be assumed without loss of generality that b2 for the case whereb = 1 is easily satisfied. If all the bins are at least half full, then b=S+eS+b2 which implies b2S[2S]

e

Program Plan Intro

To prove an approximation ratio of 2 for the first-fit heuristic.

e

Expert Solution
Check Mark

Explanation of Solution

Letb be the number of the bins used the first-fit HEURISTIC and let b* be the optimal number of bins. By part (d) [S]b* . Combining both inequalities, the result is b2S[2S]2b* , therefore bb*2 that is, the approximation ratio is 2 .

e

Program Plan Intro

To give an efficient implementation of the first-fit heuristic, and analyze its running time.

e

Expert Solution
Check Mark

Explanation of Solution

Algorithm First-fit (n,S1,S2,........,SN) :

1. b0

2. initialize data structures

3. fori = 1 to ndo

4. letjbe the first bin that can fit objecti with size Si(*)

5. ifjexists then

6. inserti at the list (list L[i] of objects in bin)

7.update data structures

8.else

9.

  bb+1

10. insertiat the list L[b]

11.update data structures II

12.endif

13. endfor

Each of the above steps can be done in O(logb) basic steps. Therefore, the total running time for this algorithm is O(nlogb)O(nlogh) .

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
Add a timer in the following code. public class GameGUI extends JPanel {    private final Labyrinth labyrinth;    private final Player player;    private final Dragon dragon;    private Timer timer;    private long elapsedTime;  public GameGUI(Labyrinth labyrinth, Player player, Dragon dragon) {        this.labyrinth = labyrinth;        this.player = player;        this.dragon = dragon;        String playerName = JOptionPane.showInputDialog("Enter your name:");        player.setName(playerName);        elapsedTime = 0;        timer = new Timer(1000, e -> {            elapsedTime++;            repaint();        });        timer.start(); }    @Override    protected void paintComponent(Graphics g) {        super.paintComponent(g);        int cellSize = Math.min(getWidth() / labyrinth.getSize(), getHeight() / labyrinth.getSize());}
Change the following code so that when player wins the game, the game continues by creating new GameGUI with the same player. However the player's starting position is same, everything else should be reseted. public static void main(String[] args) {        Labyrinth labyrinth = new Labyrinth(10);        Player player = new Player(9, 0);        Random rand = new Random();        Dragon dragon = new Dragon(rand.nextInt(10), 9);        JFrame frame = new JFrame("Labyrinth Game");        GameGUI gui = new GameGUI(labyrinth, player, dragon);         frame.setLayout(new BorderLayout());        frame.setSize(600, 600);        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        frame.add(gui, BorderLayout.CENTER);        frame.pack();        frame.setResizable(false);        frame.setVisible(true);    } public class GameGUI extends JPanel {    private final Labyrinth labyrinth;    private final Player player;    private final Dragon dragon;    private Timer timer;    private long…
Create a menu item which restarts the game. Also add a timer, which counts the elapsed time since the start of the game level. When the restart is pressed, the restarted game should ask the player' name  (in the GameGUI constructor) and set the score of player to 0 (player.setScore(0)), and the timer should restart again. And create a logic so that if the player loses his life (checkGame if the condition is false), then save this number together with his name into two variables. And display two buttons where one quits the game altogether (System.exit(0)) and the other restarts the game. public class GameGUI extends JPanel {    private final Labyrinth labyrinth;    private final Player player;    private final Dragon dragon;     private final ImageIcon playerIcon = new ImageIcon("data/images/player.png");    private final ImageIcon dragonIcon = new ImageIcon("data/images/dragon.png");    private final ImageIcon wallIcon = new ImageIcon("data/images/wall.png");    private final ImageIcon…
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education