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 10 years ago.
Improve this question
Regarding the 3 jugs of water problem:
we have 3 water jugs that the capacity of first jug is 12 and capacity of second jug is 8 and capacity of third jug is 3.
the initial state is: (0,0,0)
the successor function is:
Add: completely fill a jag
Pour to another one: Pour the content of one jug two a second one (until the first is empty or second is completely full)
Empty is: empty a jar from all its content
The goal state is: (1,1,1)
i want to draw its state tree. i did it by myself but i'm not really sure that it is right or not?
(0,0,0)
/ | \
/ | \
/ | \
(12,0,0) (0,8,0) (0,0,3)
the child node for(12,0,0) is: (12,0,0),(12,8,0),(12,8,3),(0,8,3),(0,0,3),(0,0,0),(9,8,3),(12,8,0),(4,8,3),(12,0,3),(12,5,3),(12,5,3),(12,8,0)
which (12,0,0),(0,0,0)==>because it is in root,(12,8,0)==>
are fail node and we don't expand them.
i think if i expand (0,0,3), i will reach to my goal state:
the child for the node (0,0,3): (3,0,0),(0,3,0),(0,0,3),(1,1,1) (1,1,1) is the goal state am i right?
Question: Am I understanding it correctly? Are these the states and generated tree?
The graph is correct for the first step, however - you expand the siblings (12,0,0), (0,8,0) and (0,0,3) wrong.
You should do a single step, not multiple in each iteration, and not try to do many steps.
Thus:
successors((12,0,0)) = { (12,0,3), (12,8,0), (0,0,0), (9,0,3), (4,8,0) }
successors((0,8,0)) = { (12,8,0), (0,8,3), (8,0,0), (0,5,3), (0,0,0) }
successors((0,0,3)) = { (12,0,3), (0,8,3), (3,0,0), (0,3,0), (0,0,0) }
(From each state, you can only do 1 allowed operation, not more - to get the successors/following states).
By keep expanding these, you will get eventually all possibilities.
FYI, this problem is sometimes known as The Die Hard Problem, and is a classic example of problem solving using reductions to graph algorithms by building the states graph and running a pathfinding algorithm, such as A* or BFS.
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 2 years ago.
Improve this question
Suppose I have a image that looks like
x x x x
x x x x
x x x x
Dimensions might change, but just give a rough idea about this.
I am curious if there is an algorithm that will help me quickly check if the current point I am looking at is a corner / points on one of the 4 sides / within the square itself, as well as helping me check all points around the point I am currently looking at.
My current approach is like writing a few helper functions that separately check if the current coordinate is a corner / points on one of the 4 sides / within the square itself? And within each helper function, I use several loops to check all the neighbor points around the point I am currently looking at. But I feel like this approach is extremely ineffective, I believe there must exists a more advanced way to do this, can anyone help me if you have encountered this kind of question before?
Thanks.
Largely you are correct, but there should be no need to use loops. You can make your functions efficient by using some index calculations and using direct access to 1-dimensional array.
Imagine that your image is stored in a 1-dimensional array D. The image is of size (m,n). Hence the array will have a size of m x n. Each data point will have its ID as the index to the array D.
To access neighbors of ID = a, use the following offsets:
a-1, a+1 for left and right neighbors
a-m, a+m for bottom and top neighbors
a-m+1, a-m-1, a+m+1, a+m-1 for diagonal neighbors
After every offset you need to check for the following:
is the neighbor index out of bound for the array D?
does the neighbor index wrap around the x-bounds i.e. assert that
abs((neighbor_id % m)-(a%m)) <= 1 , else neighbor_id is not my neighbor.
Of course, the second test assumes that your image is large enough (perhaps m > 3).
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.
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 7 years ago.
Improve this question
Say I'm a runner, ran a fixed distance of 42KM, no more, no less.
There are a couple of cities (or shops if you think distances between cities are normally bigger than 42KM) can be traversed in this range.
You can start from ANY vertex(city), how to schedule a route so that I can reach the most number of cities? The runner visit each city exactly once and returns to the origin city. (When the runner returned to the original point, the KMs he ran could be smaller than 42KM)
In real world, this graph should be a cyclic able, non-directed, weighted graph.
However, you are welcome to give any opinion including changing the constraints.
Edit:
I had a brute force solution as follows, still trying to find out a better solution.
(1) setup a collection of possible result set (E.g. 4 cities, a, b, c, d; then I get a combination of 2 cities[ab,ac,ad,bc,bd,cd], 3 cities[abc,abd,acd,bcd], 4 cities[abcd]);
(2) run TSP on each of this result, the result is a distance; if this distance is smaller than 42KM, this result is a candidate.
(3) from the candidate list, choose the one with most cities. This is a brute force solution, however.
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
Here is an interview question, will somebody give me some hint? I am thinking about DFS or BFS, however, I cannot think out a clear solution from my head.
Three coke machines. Each one has two values min & max, which means if
you get coke from this machine it will load you a random volume in the
range [min, max]. Given a cup size n and minimum soda volume m, show
if it's possible to make it from these machines.
This is assuming you're not allowed to overflow the cup. If you can overflow it, you can always make it.
Let's mark the machines with (min1,max1),(min2,max2),(min3,max3). a1,a2 and a3 shows the amount of times you've used each machine.
We need to find a1, a2 and a3 in order to satisfy :
Condition 1 : a1*min1 + a2*min2 + a3*min3 >= m
Condition 2 : a1*max1 + a2*max2 + a3*max3 <= n
Apparently it's not required to find the most optimal way to fill the cup (minimizing a1+a2+a3) so you can simply use DFS.
You use Condition 2 as the depth limit (meaning if Condition 2 isn't fulfilled you stop going deeper in the graph) and if you ever reach Condition 1 you have yourself the answer (return yes). If you finish the search and find no answers, return no.
Seeing as this is an interview question though, I really doubt that DFS or BFS would be the way to solve it. This could easily time out with big m and n values.
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
Imagine a straight line through the origin. Since rotations and
reflections are easy, assume the slope is in the range 0 to 1.
We have a grid of the integer points in the cartesian plane.
I want to find the grid point greater than 0 and <= D the
line passes closest to.
The simple approach is for each x from 1 .. D, find the point above and
below the line and calculate the perpendicular distance to the line.
This will take 2 x D comparisons to find the minimum.
That's not bad but I am trying to come up with a log(D) approach.
Is there one?
An equivalent problem would be to find the closest rational
number n / d where d <= D.
This question seems to be equivalent to yours: Finding the closest integer fraction to a given random real
The accepted answer there uses a Farey Sequence.
Also links to this interesting blog post.
Not a full answer, but an optimization in some cases: If the slope of the line is a rational number, then there will be repetition, allowing you to look at fewer points if D is larger than the denominator.
Eg: if the slope is 12/17, then you don't need to look at more than 17 points out from the origin. After that it will repeat.
Of course, if D < 17 in that example, it's no benefit.
Also, if your slope is π, you're out of luck...