• Loading...

The Smart Movers (Algorithm 1)

Points : 30

Sam is algorithms consultant of XYZMovers, which provides packing and moving of goods in New York city.

XYZMovers works as follows.It has its agents at a variety of fixed locations of the city. Every "Pack and Move" is headed by an agent with help of some workers. Each time a user needs a service, he needs to register his request at the XYZMover's website with details of the pickup time and location and also drop off time and location.

XYZMovers need to make sure that there is enough number of agents at each location at the start of the day so that at least, one agent is always available for every pickup. However, as it is expensive to hire more agents, so minimizing the total number of agents is an important goal. So what is required is an algorithm to process user requests so as to minimize the total number of agents that must be placed initially at various locations.

In a nutshell,the problem is to process a list of user requests which are tuples of form (l1,t1,l2,t2) where l1 is pickup location and l2 is drop off location t1 and t2 being pickup and drop off times respectively.

Obviously, when a client makes a pickup from location A, the number of agents at A is decreased by 1 and on a drop off to location B, the number of agents at B increases by 1. Assume that there are m locations l1 to lm.

Help Sam in designing an efficient algorithm to accomplish this task. You also need to explain time and space complexities of your algorithm.

Points Distribution

  • 25 points for the algorithm
  • 5 points for providing space and time complexities and its explanation

Solution

We note the following property (call it Property 1):
if a location starts with c+1 agents, then at all times it has exactly one more agent than if it had started with only c agents. This motivates the following algorithm:

1.) Take two arrays:
    arr_initial[num_locations] // contains initial number of agents at each location
    arr_current[num_locations] // contains current number of agents at each location
for each location, set its initial number of agents to zero.
    arr_initial[num_locations] = {0} // Initialize to zero
    arr_current[num_locations] = {0} // as initially, there are no agents at any location.
 
2.) Now sort the pick-ups and drop-offs by time; in case of ties, put drop-offs first.

Each request (l1,l2,t1,t2) consists of two events:
A pickup: from location l1 at time t1 say (l1,t1,p)
A drop-off: at location l2 at time t2 say (l2,t2,d)

Sort these by time.

Simulate the day by iterating over the sorted list of events.
    At a drop-off event,
    increase the number of agents currently at the specified location by one.
    i.e at drop-off at location loc, arr_current[loc] += 1;

    similarly, at a pick-up event at location loc, do the following:
    if ( arr_current[loc] > 0 )
     arr_current[loc]-- ; //simply decrease that number by one;
    otherwise arr_current[loc] == 0,
    So increase both the initial number of agents and
    the number of agents currently at that location by one,
    i.e arr_initial[loc]++;
        arr_current[loc]++;
    then decrease the number of agents currently at the location by one as usual.
        arr_current[loc]--;
     
3.) When all the events have been iterated over,
return the array containing the initial numbers of agents at each
location.

Proof of correctness

The algorithm is correct because of the greedy-choice property: whenever we decide to increment the initial number of agents for a particular location loc from c to c+1,(property 1) suggests that no feasible solution puts fewer than c+1 agents at location loc. So incrementing is safe. Also, as we passed the entire day with no deficit of agents at any location, the solution is feasible.

Complexity

  • Time: O(nlogn) //for sorting the events.
  • Space:O(n) // linear in number of locations and events

Post your queries regarding the solution at Algorithm 1 Forum Thread.

Submit your solution

You need to be logged in to submit a solution.

discussions


MultifariousSolution format
Q:So do you want us to explain the algorithm in words? In psuedocode? Or in actual C/C++? If so, should it compile?
A:In words or pseudocode. No codes please!
solverinformation about order
Q:Dad sam get the all requests one day before or at any time in a day?
A:Sam has the information of all the requests from one day before.
atulaggarwalcodemasters
Q:what are the initial conditions should we know all the requests in advance or there is no previous information? what happens if there are two pick up or drop up at the same location at same time?
A:Initially, you have the list of requests to be processed that day in form of tuples as described in the problem statement. Regarding overlapping of events, you can safely assume there be no such conditions (The request list is preprocessed to handle such conditions).
abhisnehMaximun order
Q:What is maximum number of order which can be placed in one day
A:You are required to process orders registered on the XYZMovers website and output an initial allocation of agents for the same. You can however safely assume the number of orders to be a finite number.
abacus2007Should agent do only one transaction at a time?
Q:Should the Agent remain idle from pickup time to drop off time or he can process more than 1 requests and make sure he is available at the pickup and dropoff times at the location
A:Agent can serve only one request at a time.So while serving a request for Pick and Drop he can not serve other requests.
xolveAgents without work
Q:Suppose if an agent is without any work to do after making his deliveries. Can he move to the other locations to pick up further deliveries. Do we consider time of transit?
A:An agent which drops off at location X stays there unless he has to process pickup requests from location X.
harish610Time of events
Q:Is the time measured as 1-24 or is it unbounded meaning 1- infinity ??
A:The allotment of agents at various locations is desired for 1 day, so you can choose either 12 hour or 24 hour format for representing times.
quimeyOverlapping of events
Q:What happens if there are a drop off and a pick up in the same location at the same time?
A:You can safely assume that there will be no such situation.

post a question

Loading...

Round 2 Submissions
Puzzle 1
25 pt
You are not logged in
29
submissions

Puzzle 2
20 pt
You are not logged in
23
submissions

Algorithm 1
30 pt
You are not logged in
26
submissions

Algorithm 2
30 pt
You are not logged in
26
submissions

Debugging 1
30 pt
You are not logged in
50
submissions

Debugging 2
20 pt
You are not logged in
34
submissions

Programming
20 pt
You are not logged in
77
submissions

Puzzle 1
17 pt
You are not logged in
32
submissions

Puzzle 2
8 pt
You are not logged in
47
submissions

Algorithm 1
17 pt
You are not logged in
19
submissions

Algorithm 2
13 pt
You are not logged in
15
submissions

Debugging 1
7 pt
You are not logged in
98
submissions

Debugging 2
8 pt
You are not logged in
57
submissions

Programming
30 pt
You are not logged in
28
submissions