3

Example: You have 4 baskets named P,Q,R,S. You have 4 items in those baskets named A,B,C,D.

The composition of baskets are as follows PIC

--A B C D

P 6 4 0 7

Q 6 4 1 1

R 4 6 3 6

S 4 6 2 3

Basket P has 6A, 4B, No C's and 7D.

Suppose you get following requests: You have to give out 10A, 10B, 3C and 8D.

The minimum number of basket required to process the request is 2 (P,R).

How can I reach this algorithmically. What algo should I use, what should be the strategy?

  • If you only have 4 baskets, you can test all combinations. Begin by all groups of 1 basket, if there is no solution all groups of 2, and so on. – Vince Dec 06 '18 at 10:00
  • @Vince This is just for example, I wanted to know for very high numbers of Baskets and items. – Jatin Chaudhary Dec 06 '18 at 10:07

2 Answers2

2

Make directed graph (network) like this:

enter image description here

Source has edges with cost=1 and capacity=bigvalue to P,Q,R,S nodes

P has edges with cost=0 and capacity 6,4,7 to A,B,D, same for other baskets.

A,B,C,D have edges with cost=0 and capacity=10,10,3,8 to sink

Now solve Minimum-cost flow problem for 10+10+3+8 flow.

MBo
  • 77,366
  • 5
  • 53
  • 86
  • this is nice, but you should limit A,B,C,D => sink capacity to the request as 1 extra A cannot replace 1 missing B. Over that a simple graphic would be nice :) . – Vince Dec 06 '18 at 10:31
  • Yes. I missed needed values. – MBo Dec 06 '18 at 10:37
0

There was an algorithm about putting queens on rights places in chess board and the rule is that they must not threat each other. Your problem looks like that one for me. You can create a recursive structure like below:

Find first rows that meets the requirements: In your example P and Q (because 6+6 > 10) So you handled first column, then go to second one and check if capacity of the basket P and Q can meet the requirement: They don't in your case (Because 4+4 < 10)

In here go back to first step (call the same recursive function for first column by increasing the pointer which was showing B before) and find the second rows that meet the requirements. P and R for your example. (6+4 = 10) Then do the second step for P and R.

So the idea is for every column find the baskets that meets the requirement and then go to the second column. If you can find the rows that meets the requirements then go for 3. If you can not find the ones in 3rd step then go back to 2nd step and again if no combinations of the rows that you choosed 2nd step meets the requirements than go to first and iterate it again.

I could not gave you a pseudocode properly but I think main idea is clear and not that hard to implement.

JollyRoger
  • 737
  • 1
  • 12
  • 38