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 3 years ago.
Improve this question
I am a little stuck on a problem in which we are supposed to fill in the values after running two iterations of the Bellman-Ford algorithm on a basic directed graph.
I believe I understand the first iteration and I understand the concept of "relaxing edge weights" as shorter paths are found. However, I don't see how the second iteration, in this particular problem, would yield any shorter paths than the ones located in the first iteration.
For example, I know that visiting node 'C' via the path of starting at node A, then going to node 'B' then going to node 'C' would have a total "cost" of 6+8 = 14. However, because the traversal order of this graph is: AB, AC, BC, BD, etc., the cost of reaching node C via node B (14) would never be saved because a shorter path to C directly from A would have already been found (yielding a cost of 7) I don't see how any additional iterations would give a shorter path length from A to C for example which seems to be the significance of the subsequent iterations.
upon closer inspection it appears that the data is indeed correct. It's just a poorly formatted question in the sense that the second iteration does not, in this particular case, yield any further "relaxation" of edges and is therefore misleading to those expecting to see a difference over the second iteration.
Related
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 6 years ago.
Improve this question
I have n (about 10^5) points on a hypersphere of dimension m (between 10^4 to 10^6).
I am going to make a bunch of queries of the form "given a point p, find the closest of the n points to p". I'll make about n of these queries.
(Not sure if the hypersphere fact helps at all.)
The simple naive algorithm to solve this is, for each query, to compare p to all other n points. Doing this n times ends up with a runtime of O(n^2 m), which is far too big for me to be able to compute.
Is there a more efficient algorithm I can use? If I could get it to O(nm) with some log factors that'd be great.
Probably not. Having many dimensions makes efficient indexing extremely hard. That is why people look for opportunities to reduce the number of dimensions to something manageable.
See https://en.wikipedia.org/wiki/Curse_of_dimensionality and https://en.wikipedia.org/wiki/Dimensionality_reduction for more.
Divide your space up into hypercubes -- call these cells -- with edge size chosen so that on average you'll have one point per cube. You'll want a map from hypercells to the set of points they contain.
Then, given a point, check its hypercell for other points. If it is empty, look at the adjacent hypercells (I'd recommend a literal hypercube of hypercells for simplicity rather than some approximation to a hypersphere built out of hypercells). Check that for other points. Keep repeating until you get a point. Assuming your points are randomly distributed, odds are high that you'll find a second point within 1-2 expansions.
Once you find a point, check all hypercells that could possibly contain a closer point. This is possible because the point you find may be in a corner, but there's some closer point outside of the hypercube containing all the hypercells you've inspected so far.
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 have been stuck on this uva problem from a long time now.
Abridged problem statement : Given a set of sticks of various lengths, is it possible to join them end-to-end to form a square? There are a maximum of 20 sticks and each stick has a length less than 10000.
There are different solutions possible for this problem. One of them is a backtracking solution explained here. But there exists other dynamic programming solutions explained here, here and here with a better running time. But I can't understand what approach they are using. Please help me understand the dp algorithm.
If you are not familiar with dynamic programming over subsets, I suggest you read about it first. This link can help, but there may be better tutorials out there.
Back to the given problem, since M is no more than 20, the following 2M×M approach will probably work.
For each of the 2M subsets of the given sticks, we know the total length of the sticks in that subset. We also know the total length of all the given sticks and thus the length of the square side. We construct the square by laying sticks on its sides. Let us fix the order in which we construct our square: we start at the upper left corner and move along the square border in clockwise direction, laying sticks on the way and leaving no gaps. So, first we fully construct the upper side (from left to right), then the right side (top-down), then the lower one (right-to-left) and finally the left one (bottom-up). Whenever the distance to the next square corner in our traversal is L, we can't lay a stick of length greater than L; at least not until we reach that corner using other sticks. Now, the question is: can we order the sticks in such a way that the square can be constructed by our procedure?
There are M! different orders in which we can try to lay the sticks. But, if we lay sticks one-by-one, when choosing the next stick, all that we are concerned with is the set of sticks already laid, not their particular order. This observation leads us to considering only 2M subsets which is way smaller than M! orders.
Next we define subproblems of the problem defined before. For each subset of sticks, the question is: can we order the sticks in such a way that all of them can be laid sequentially by the rules of the above procedure? In other words, can we construct a "valid prefix" of the square traversal, as it is defined above?
We will say a subset of sticks is good if the answer to the above question is "yes", and bad otherwise. Sure, the empty subset is good. In the end, we are interested in whether the whole given set of sticks is also good. To find that out, we can process the subsets in natural order (for M=3, that would be 000, 001, 010, 011, 100, 101, 110, 111 where 1s correspond to sticks in the subset). For each new non-empty subset S, it is good if and only if some of its "immediate subsets" T (S without exactly one element - say a stick of length X) is good, and T can be extended by that stick of length X according to the rules of our construction procedure (that is, laying a stick of length X, we won't have to bend it around some corner).
What's left is implementation details. For each subset, either store or calculate the total length of the sticks in it and find the distance to the next corner L. If this subset is good, it can be extended only by sticks having two properties: (1) length no more than L and (2) not already in the subset.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have a list whose members are sets of 5 numbers chosen from the
integers 1 to 600 [or 0 to 599 for storage purposes].
I need to choose a sublist of this list such that among the sets in this
sublist, each integer in the 1 to 600 range appears exactly once, so a
sublist of 120 elements. My list has either 4200 or 840 elements in
it--I'll find out by running whether the bigger number is necessary.
I need any one such sublist.
This sounds like a standard problem to me, but I have no idea how to
search. Can someone help with providing an algorithm, please?
From Set Cover Problem
The greedy algorithm for set covering chooses sets according to one rule: at each stage, choose the set that contains the largest number of uncovered elements
Wikipedia seems to say that this algorithm works the best under plausible complexity assumptions.
I would boil it down to these steps:
Pick an element from the list (the first one, probably)
Pick the next element you come across where all 5 numbers are not yet represented in the sub-list
If you reach the end, go back to the beginning of the list and lower the criteria of step #2 to 4 numbers
Repeat steps 2 & 3 until you have covered all integers
Depending on the programming language you're using, there are ways of making this pretty quick.
Edit: the poster has explained that each integer must be used exactly once
So, what you really need to do is just continue adding elements until the element contains an integer that is already present in your subset. The "exactly" criterion takes precedent over the "not yet in the subset" criterion. You'll break out of the loop when you hit 120 subsets.
You may also want to keep track of the order in which you add elements to your subset, and when you hit a dead end (e.g., each of the elements remaining in the superset contains an integer that is already present in your subset) you backtrack one element and continue.
In order to backtrack and remember what combinations do not work, you will need to keep a list of "banned collections", and each time you decide whether to add a new element you should first make sure it's not in this list of banned collections. The best way (that I've found) to do this in Ruby is to store the Hash of the collection rather than the collection itself. This provides an inexpensive way to evaluate whether the prospective collection has already been tried and has led to a dead-end.
Good luck!
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Given a Binary Tree, write a function to check whether the given Binary Tree is Complete Binary Tree or not.
A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible. source: wikipedia
My approach is do BFS using queue and count the no of nodes. Run a
loop till the queue is not null but break once you find one of the
below condition holds good:
left node is not present for a node
left node is present but right node is not present.
Now we can compare the count that we get from the above approach and
the original count of the nodes in the tree. If both equal then
complete binary tree else not.
Please tell me whether the approach is correct or not. Thanks.
This question is same as that of this. But i wan to verify my method here.
Edit:
The algorithm is verified by #Boris Strandjev below. I felt this is the easiest algorithm to implement out of some algorithms available in net. Sincere apologize if you do not agree with my assertion.
Your algorithm should solve the problem.
What you are doing with the BFS is entirely equivalent to drawing the tree and then tracing the nodes with your finger top-down and left-right. The first time you can not continue you stop tracing with your finger. If you have not counted all the nodes then the structure is not as expected obviously.
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I am trying to find a way to solve an Optimization problem as follows:
I have 22 different objects that can be selected more than once. I have a evaluation function f that takes the multiplicities and calculates the total value.
f is a product over fractions of linear (affine) terms and as such, differentiable and even smooth in the allowed region.
I want to optimize f with respect to the 22 variables, with the additional conditions that certain sums may not exceed certain values (for example, if a,...,v are my variables, a + e + i + m + q + s <= 9). By this, all of the variables are bounded.
If f were strictly monotonuous, this could be solved optimally by a (minimalistically modified) knapsack solution. However, the function isnt convex. That means it is even impossible to assume that if taking an object A is better than B on an empty knapsack, that this choice holds even when adding a third object C (as C could modify B's benefit to be better than A). This means that a greedy algorithm cannot be used;
Are there similar algorithms that solve such a problem in a optimal (or at least, nearly optimal) way?
EDIT: As requested, an example of what the problem is (I chose 5 variables a,b,c,d,e for simplicity)
for example,
f(a,b,c,d,e) = e*(a*0.45+b*1.2-1)/(c+d)
(Every variable only appears once, if this helps at all)
Also, for example, a+b+c=4, d+e=3
The problem is to optimize that with respect to a,b,c,d,e as integers. There is a bunch of optimization algorithms that hold for convex functions, but very few for non-convex...