How will greedy work in this coin change case [closed] - algorithm

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
The problem is here
Say I have just 25-cent, 10-cent and 4-cent coins and my total amount is 41. Using greedy, I'll pick 25-cent and then 10-cent, and then the remaining 6 cents can not be made.
So my question is, does greedy in this case will tell me that there is no solution?

It looks like your problem was answered right in the the Greedy algorithm wiki: http://en.wikipedia.org/wiki/Greedy_algorithm#Cases_of_failure
Imagine the coin example with only 25-cent, 10-cent, and 4-cent coins. The greedy algorithm would not be able to make change for 41 cents, since after committing to use one 25-cent coin and one 10-cent coin it would be impossible to use 4-cent coins for the balance of 6 cents, whereas a person or a more sophisticated algorithm could make change for 41 cents with one 25-cent coin and four 4-cent coins.

The greedy algorithm mentioned in your link assumes the existence of a unit coin. Otherwise there are some integer amounts it can't handle at all.
Regarding optimality - as stated there, it depends on the available coins. For {10,5,1} for example the greedy algorithm is always optimal (i.e. returns the minimum number of coins to use). For {1,3,4} the greedy algorithm is not guaranteed to be optimal (it returns 6=4+1+1 instead of 6=3+3).

It seems that greedy algorithm is not always the best and this case is used as example to illustrate when it doesn't work
See example in Wikipedia

Related

What is the best algorithm for optimizing a combination of variables? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 20 hours ago.
Improve this question
I want to program a software that calculates the best combination of materials to use base on parameters such as its tensile strength, elastic modulus, stiffness, and results from doing certain tests from those materials. Those each factor are going to be weighted differently in a WDM. Is there an algorithm that would allow me to find the best combination without actually going through all the combinations and doing each individual calculations? I will be working with a lot of data, so efficiency is important
I tried researching algorithms like kruskal's and other things, but I'm not very fammiliar with them
First step is to write down an equation to calculate a number that you want to optimize.
If you can do that and the equation has no squares or other exponential terms then this is the classical linear programming problem https://en.wikipedia.org/wiki/Linear_programming
Your equation needs to look something like this:
max O = n1 * p1 + n2 * p2 - n3 * p3 ...
If so, then your best bet is to choose a linear programming package ( ask google ) with a good introductory tutorial and plug your problem into that. After a day or so on a steep learning curve, your problem will become almost trivial.
If you cannot do that, then you will need to use some sort of hill climbing algorithm - probably best to hire an expert to help with that.

n balls and n cups algorithm design [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 days ago.
Improve this question
You are given n balls and n cups. Each cup holds a particular weight, and once a ball is placed in it tells you whether the ball is too heavy or light or just right. You can’t compare the weight of the balls directly. A perfect pairing between balls and cups exist. Design an expected nlogn algorithm to find the pairing. Hint: modify quicksort.
I’ve thought about this problem for a long time with no leads.
Is there a efficient way to compare the weight of two balls, or am I thinking about this wrong? Can someone please give a hint?
If you compare all balls with a single randomly picked cup, you will find the matching ball, and the other balls will be partitioned into those higher and those lower. You can use the matching ball to also partition the cups in a similar way. Then you have essentially randomized quicksort.

Minimum number of Trips [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have latitude and longitude points of N societies, order count of these societies, I also have latitude and longitude points of a warehouse from where the trucks will deploy and will be sent to these various societies(like Amazon deliveries). A truck can deliver maximum 350 orders (order count < 350). So no need to consider items with order count above 350 (We generally would send two trucks there or a bigger truck). Now I need to determine a pattern in which the trucks should be deployed in such a way that a minimum number of trips occur.
Considering that we determine the distance between two societies or warehouses is 'X' from this script is accurate, How do we solve this? I first thought that we could solve it using sum of subset problem, maybe? Seems like dp on graphs to me, traveling salesman problem with infinite number of salemans.
There are no restrictions on the number of trucks.
This is a typical Travelling salesman problem (TSP) which is known as NP-complete. It means that if you are looking for the optimal solution you have to test most of combinatorics. And as you know, !350 is tremendeous.
Nevertheless, as Henry suggests, you can look for a good solution which is not necessarily the best. A lot of algorithm called "heuristic" let you find one good solution in a very efficient way. Just have a look here for some examples https://en.wikipedia.org/wiki/Travelling_salesman_problem.
The most simple heuristic algorithm may be a greedy solution like always take the closest unvisited point as next society.

Subset product & quantum computers, is an instance solvable [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
Suppose you have a quantum computer that can run Shor's algorithm for factorization of integers.
Is it then possible to produce an oracle that determines if no solution exists for an instance of the Subset Product problem, with 100% confidence, in sub-exponential time?
So, the oracle is given a sequence x1, ... xn, as the description of a subset product problem.
It responds either Yes, a solution to this instance does not exist, or No, a solution to this instance may or may not exist.
If we take he prime factors of all elements in the sequence and then check to see if all of them are present in the target product's factors, this should tell us if a solution is not at all possible. A solution exist may exist if and only if all the prime factors are accounted for. On quantum computers, prime factorization is sub-exponential.
Would like some feedback on if this is correct logic- if it works- and if the complexity is indeed different between classical and quantum systems for this oracle/algorithm. Would also appreciate an explanation on reductions - can Subset Product be reduced to 3SAT without consequence?
Your algorithm, if I understood it correctly, will fail for the elements [6, 15] and the target 10. It will determine that 6*15 = 2*3*3*5, which has all of the factors used in 10=2*5, and incorrectly assert that this means you can make 10 by multiplying 6 and 15.
There are two reasons that it's unlikely you'll be able to fix this algorithm:
Subset Product is NP-Complete. Finding a polynomial time quantum algorithm for it, or showing that no such algorithm exists, is probably as hard as determining if P=NP. That is to say, very very hard.
You don't want the prime factors, you want the "no-need-to-reduce" factors. For example, if every time a number in the problem has a prime factor of 13 it's accompanied by a factor of 17 then there's no need to break 221 into 13*17. You can apply Euclid's gcd algorithm to various combinations of elements to find these no-need-to-reduce factors, no quantum-ness required.

Optimal Seat Allocation Algorithm [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am looking for an optimal seat allocation algorithm, such that for example, if i have a cinema with capacity 100, and n groups of people, i want to choose the right groups that will fill in as maximum seats as possible.
The only thing that will work is brute force, but I'm sure there must be cleverer ways to do that. Any ideas?
This is a special case of the Knapsack problem known as the Subset Sum problem. There is a lot of work already done on this so the wiki article is a good jumping off point discussing many possible algorithms. The correct choice in algorithm will depend on the sort of data you’re operating on.

Resources