Thoughts about Programming Languages, Science and Philosophy.
Capacity to ship packages within given days
Get link
Facebook
X
Pinterest
Email
Other Apps
Today, let’s focus on a very good application of the famous algorithm binary search. Most of you may be familiar with binary search since it is widely used to implement different algorithms. In fact, I have used it in a few articles before.
Minimum capacity to ship within given days - Problem statement
A conveyor belt has packages that must be shipped from one port to another within d days.
The ith package on the conveyor belt has a weight of w[i]. Each day, we load the ship with packages on the conveyor belt (in the order given by weights). We may not load more weight than the maximum weight capacity of the ship.
Return the least weight capacity of the ship that will result in all the packages on the conveyor belt being shipped within d days. Also note that we are not allowed to split the items apart and then ship.
Minimum capacity to ship within given days - Definition
We first need to define the window of possible capacities. Let w be the weight array of items, d be the number of days and n be the number of items to be shipped. Let W be the sum of the weights wi of all the items. Thus,
W=n∑i=1wi
Let wmax be the maximum weight element in our array w. Then, the minimum possible capacity can be defined by the equation Cmin−possible=max(wmax,⌈W/d⌉). Not surprisingly, we can define the maximum possible capacity as Cmax−possible=⌈n/d⌉⋅wmax.
Minimum capacity to ship within given days - Approach
With the above definitions under our belt, we can now move on and start solving the problem at hand. Let’s do a binary search on our window of possible capacities, choosing a capacity and check whether that capacity allows us to ship the packages within the given number of days. If so, we consider the first half of the window, otherwise, we consider the second half of the window in the next step. At each step, we halve the window size, until it reaches 0, at which point the algorithm exits.
Note that our window size is defined by, window−size=h−l+1=0 and h+1=l which implies h<l and at that point our while loop terminates. Here’s the pseudocode for the algorithm of finding the minimum capacity to ship the packages within d days.
**Input:** Package weight array w and number of days d
**Output:** The least weight capacity of the ship that will result in all the packages on the conveyor belt being shipped within d days.
SHIP−WITHIN−DAYS(w,d)
++n←w.length
++mw←0
++s←0
++ `for ` i=1 to n
++++s←s+w[i]
++++mw←max(mw,w[i])
++h←⌈nd⌉⋅mw
++l←max(mw,⌈sd⌉)
++c←0
++ `while` l≤h
++++m←⌊(l+h)2⌋
++++ `if` POSSIBLE−CAPACITY(w,m,d)
++++++h←m−1
++++++c←m
++++ `else`
++++++l←m+1
++ `return` c
**Input:** Package weight array w, capacity being considered c and the number of days d
**Output:** true, if items can be shipped within d days using the given capacity c, false otherwise.
POSSIBLE−CAPACITY(w,c,d)
++n←w.length
++s←0
++ `for` i=1 to n
++++ `if` s+w[i]>c
++++++d=d−1
++++++s←w[i]
++++ `else`
++++++s←s+w[i]
++d←d−1
++d≥0
Here’s the sample implementation of algorithms described above along with a driver program.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Let m be the size of our capacity window and let n be the length of the array w. We perform a binary search on our capacity window which incurs us log2m steps. At each step, we call the procedure POSSIBLE−CAPACITY which takes O(n) time. So, our approach of finding the minimum capacity to ship the packages within d days involves O(log2m⋅n) time complexity and O(1) space.
Introduction Today we are going to take a look at Reactive Programming using Java8 and RxJava, which is a library for composing asynchronous and event-based programs by using observable sequences. RxJava stands for Reactive Extensions for the JVM. It extends the observer pattern [Gamma95] to support sequences of data/events and adds operators that allow you to compose sequences together declaratively while abstracting away concerns about things like low-level threading, synchronization, thread-safety and concurrent data structures [1]. Pre-Requisites : Having a good knowledge about Java8, Functional Programming and spring boot microservices. What is Reactive programming? In essence, Reactive programming is an asynchronous programming paradigm concerned with data streams. The system time, a list of entries from the DB/API, user clicks, everything can be a stream of data. Data from different streams can easily be combined and transformed, and in the end processed/observed by the ...
INTRODUCTION What is RabbitMQ? RabbitMQ is an open source message broker which uses the AMQP (Advanced Message Queueing Protocol) protocol which is written in Erlang. What is AMQP? AMQP is a standard wire-level protocol and semantic framework for high performance enterprise messaging. AMQP is an Open Standard for Messaging Middleware. By complying to the AMQP standard, middleware products written for different platforms and in different languages can send messages to one another. AMQP addresses the problem of transporting value-bearing messages across and between organisations in a timely manner. AMQP enables complete interoperability for messaging middleware; both the networking protocol and the semantics of broker services are defined in AMQP [1]. JMS vs RabbitMQ Java Message Service is an API that is part of Java EE for sending messages between two or more clients. There are many JMS providers such as ActiveMQ and OpenMQ [2]. RabbitMQ is an o...
Suppose that we are designing a program to translate text from English to French. For each occurrence of each English word in the text, we need to look up its French equivalent. We could perform these lookup operations by building a binary search tree with n English words as keys and their French equivalents as satellite data. Because we will search the tree for each individual word in the text, we want the total time spent searching to be as low as possible. We could ensure an Θ(log2n) search time per occurrence by using a red-black tree or any other balanced binary search tree. Words appear with different frequencies, however, and a frequently used word such as the may appear far from the root while a rarely used word appears near the root. Such an organization would slow down the translation, since the number of nodes visited when searching for a key in a binary search tree equals one plus the depth of the node containing the key. We want words that occur frequently in the t...
Comments
Post a Comment