Find combination of bank notes for particular sum - algorithm

How to issue money efficiently. For example if you have 1 bank note of 100$, 1 bank note of 50$ and 2 bank notes of 30$. How to determine that we need 50$ and two of 30$ to achieve 110$ summary.
In other words, we have fixed number of bank note types: 30$ (two bank notes), 50$ (one bank note), 100$ (one bank note). The problem is to determine which bank notes we should take to get a particular sum, for example, 110$? In this case we should take two 30$ bank notes and one 50$ bank note.
We can't use greedy algorithm here because if we take 100$ bank note first, then we can't achieve 110$ summary.
Which data structure do we need to use for storing bank notes? Simply quantity of each bank note type or may be an array to store each bank note: [100, 50, 30, 30]
And what is the algorithm to find which bank notes do we need to get a particular sum?

You can use dynamic programming approach. You keep an array, where index is reachable sum, and value - bill number, by which this sum is reached.
This approach is used in the Emercoin transaction optimizer.
See source code at:
https://github.com/Emercoin/emercoin/blob/master/src/wallet.cpp#L1112
Memory is O(Sum); Time is O(Sum * NumBills);
See short article about it:
http://cointelegraph.com/news/emercoin-implements-solution-to-reduce-blocksize-inflation

See my solution in this question Coin Change Problem
Also for such kind of problem, the hard part is always to determine if the coin / money system is canonical i.e. Can use greedy algorithm

Related

How to generate UK bank account number

Is there an algorithm for generating UK bank account numbers.
There is an algorithm for validation (called "Modulus checking" by Vocalink), but not for generating.
The bank will give me the sort code, so I just need to generate the account number.
If there is no generation algorithm, why is that ?
UK bank account numbers contains a six-digit sort code that identifies the branch of the bank where the account is held. Typically the first two digits identify the bank itself and the remaining four digits the specific branch in the bank.
These numbers are not algorithmic, they are simply just a list. Therefore there is no way of generating bank account numbers algorithmically especially without knowing where the account is held, i.e. the bank and the branch in the bank.
I think you can buy lists from banks containing information of different sort code identification but they are expensive. You can see some common sort codes in this article.

Optimizing allocation algorithm needed

Today I encountered an interesting problem when accounting for the travel costs of my last journey with my friends.
Let's assume we had the following expenditures:
# expenditures in US-$
Peter= 117
Joe= 38
Bill= 15
Chris= 0
Alan= 209
Tim= 201
Ahmet= 124
Pati= 57
Steven= 74
Now we have decided that everybody should pay the same amount of money. Given that the average expenditure was 92.77778 US-$, The Balances look like this:
# balances in US-$
Peter= 24.22
Joe= -54.78
Bill= -77.78
Chris= -92.78
Alan= 116.22
Tim= 108.22
Ahmet= 31.22
Pati= -35.78
Steven=-18.78
So now I would like to find an optimization method where we have a minimum number of total bank-transactions and a fairly distributed share of transactions over all participants (so two optimization aims)
I have looked up the Stable Marriage Problem but I think it doesn't apply in this case.
This doesn't seem like a difficult problem -- sort the participants by ascending expenditures, then, in that order, have each participant transfer their negated (current) balance to the next participant. n-1 transactions, and at most one transaction per participant. Each of which, of course, is the best you can do in general.
Or is there some extra constraint I'm missing?
This method only solves the part of "minimum number of total bank-transactions".
People are grouped into two categories, say Giver (whose balance is negative) and Receiver(whose balance is positive).
Key Observation In an optimum solution, a giver should always give away money, a receiver should always receive money. It's impossible for person A gives money to B and then B gives some money to C.
Now comes the solution.
While (there is some giver or receiver) {
Find the giver with the largest balance, say A.
Find the receiver with the largest balance, say B.
Let A gives as much money as B needs.
Delete Person if his/her balance becomes 0.
}

Finding most distinct elements in a set

Say we have a perfume shop that has 100 different perfumes.
Let's say 10,000 customers come in an rate each perfume one through five stars.
Let's say the question is: "how to best construct a pack of 5 perfumes so that 95% customers will give a 4+ star rating for at least one of them"
How to do this algorithmically?
NOTE: I can see that even the question isn't properly formed; there's no guarantee that such a construction even exists. There is a trade-off between 2 parameters.
NOTE: Also, (and this makes the perfume analogy becomes slightly artificial), it doesn't matter whether we get one good match or three good matches. So {4.3, 0, 0, 0, 0} would be equivalent to {4.3, 4.2, 4.2, 4.2, 4.2} -- in both cases the score is 4.3.
Let's say for the purpose of argument that perfumes 0-19 are sweet, perfumes 20-39 are sour, etc (sim. salt, bitter, unami)
So there would be very high crosscorrelation between 0-19.
If you modelled this with 100 points in space, then 0-19 would all attract each other very strongly, they would form a cluster.
Similarly you would get 4 other clusters for the other four tastes.
So from just one metric, we have separated out 5 distinct flavours.
But does this technique extend?
π
PS just giving the names of related techniques would be very helpful, as this would allow me to Google for further information. So any answer that just restates the question in industry accepted terminology would be useful!
This algorithm should find a solution to the problem:
Order the perfumes by the number of customers giving a 4+ rating
Choose the first perfume not concidered yet from the list
Delete the ratings from the customers now satisfied.
Repeat the process for perfumes 2 - 5 in the pack.
Backtrace when neccessary to obtain a selection satisfying the criterion.
The true problem is NP-hard, but you can make use of a greedy algorithm:
Let C be the whole of your customers.
Assign to each perfume a coverage given by the number of customers in C that gave 4+ to each perfume
Sort by descending coverage. If C is empty and all coverages are zero, choose a perfume at random (actually, if C is nonzero but < 5% of the original, your requisite is met)
Remove from C all customers (not ratings) satisfied by the perfume just chosen
Repeat from 2 unless you already have 5 perfumes.
This automatically takes care of taste clustering: a customer giving high marks to sweet perfumes will be satisfied by the most voted sweet perfume, and he will then be struck out from C, all his further ratings ignored, and the algorithm will proceed to satisfy other customers.
Also, you should notice that even if you can't satisfy the requisite (95%, 4+) with five perfumes, perfume similarity will ensure that this algorithm maximizes both the coverage and the marks - so you might end up with, say, (93%, 3.9).
Also, suppose that 10% of users do not give any marks above 3. There's no way that you can 4-satisfy 95% of customers, since 10% of total are at most 3-satisfiable. You might want to build C with customers that actually did give at least one 4+ rating.
Or you could change the algorithm and instead of the one in your question, decide on using a knapsack: you want to take home the highest cumulative rating. This also raises the likelihood of a customer being satisfied by the overall package (as is, he is almost guaranteed to very much like one perfume, but he might strongly dislike the other four).

Rating Algorithm

I'm trying to develop a rating system for an application I'm working on. Basically app allows you to rate an object from 1 to 5(represented by stars). But I of course know that keeping a rating count and adding the rating the number itself is not feasible.
So the first thing that came up in my mind was dividing the received rating by the total ratings given. Like if the object has received the rating 2 from a user and if the number of times that object has been rated is 100 maybe adding the 2/100. However I believe this method is not good enough since 1)A naive approach 2) In order for me to get the number of times that object has been rated I have to do a look up on db which might end up having time complexity O(n)
So I was wondering what alternative and possibly better ways to approach this problem?
You can keep in DB 2 additional values - number of times it was rated and total sum of all ratings. This way to update object's rating you need only to:
Add new rating to total sum.
Divide total sum by total times it was rated.
There are many approaches to this but before that check
If all feedback givers treated at equal or some have more weight than others (like panel review, etc)
If the objective is to provide only an average or any score band or such. Consider scenario like this website - showing total reputation score
And yes - if average is to be omputed, you need to have total and count of feedback and then have to compute it - that's plain maths. But if you need any other method, be prepared for more compute cycles. balance between database hits and compute cycle but that's next stage of design. First get your requirement and approach to solution in place.
I think you should keep separate counters for 1 stars, 2 stars, ... to calcuate the rating, you'd have to compute rating = (1*numOneStars+2*numTwoStars+3*numThreeStars+4*numFourStars+5*numFiveStars)/numOneStars+numTwoStars+numThreeStars+numFourStars+numFiveStars)
This way you can, like amazon also show how many ppl voted 1 stars and how many voted 5 stars...
Have you considered a vote up/down mechanism over numbers of stars? It doesn't directly solve your problem but it's worth noting that other sites such as YouTube, Facebook, StackOverflow etc all use +/- voting as it is often much more effective than star based ratings.

Finding a subset of numbers that equals a single number

The reason I place this post is that I am looking to reconcile customer accounts receivable accounts where "payments" are posted to accounts instead of matched with the open invoices and cleared. So here is my issue:
Have a single number (payment) that should equal a subset of a given set of numbers (invoice amounts). Simple example:
Payment $10,002
Invoices values:
5001
2932
876
98
21
9923
2069
123
432
765
I would want a way to pull out 5001, 2932 and 2069 from this set.
Being a non-programmer, an Excel spreadsheet application is easiest for me to create. Ideas?
You're talking about an NP-Complete problem called Subset-sum.
Basically, this means that in general it is very computationally hard to compute the subset of prices that sums to your grand total. It is, however, very easy to check your answer since you merely sum your answers together.
My guess is, that if you want to examine N prices, you're going to have to use about 2^N cells in Excel to calculate this. The wikiepdia article linked above give some heuristics for approximating this.
Bottom line is, if you need to do this on a large scale (N is, say, in the thousands hundreds) you should rethink why you need to do this.
If you can find out a way to do it very efficiently, there may be a prize involved.
I worked on a very similar Java application that mapped receipts to accounts receivable transactions. We did not try to progammatically link summed receipts to a single transactions or vice-versa for a number of reasons. However, we did allow users to manually do that mapping. We just mapped receipt figures to transactions figures that matched, if there were multiple reciepts and transactions with the same amount, we only matched when there were the same number of duplicate amounts.

Resources