At the outset the problem is a divide and conquer problem. The solution is
- Make the first cut at a point that is near the half way mark. The cost of making this cut is the length of the wood (L)
- Then we will have two halves with lengths L1 and L2. Apply the step 1 recursively for each of these pieces till we have made the cut at all the marked points.
- A hash table mapping the length L and list of points (a1, a2, ... an) to cut on the wood, as the key, and the cost of making such cuts as the value.
For example, consider a wood of length 10 metres to be cut at points a1 = 1 metre, a2 = 4 metre, a3 = 6 metre and a4 = 9 metre. Here are the steps.
- Make the first cut at a point near the half way (5 metre mark). In the above example points a2 and a3 are equidistant from centre. So lets choose a2. Now we have two woods of length 4 metre and 6 metre. After this cut the hash table will have an entry.
- Now apply the above rule for the two wooden parts we have now. Applying it on the first one, we have
- Applying the rule on the second part, we have
Value(4#3) = 4
So value of (6#2#5) = 6 + 4 = 10.
Now the total cost = 10 + 4 + 10 = 24
In the example chosen above, we didnt have any cut patterns that repeated. So that the costs stored for the cut patterns in the memory were never used.
Now consider this example, wood of length = 10 metres, points a1 = 1 metre, a2 = 3 metres, a3 = 5 metres, a4=6 metres, a5=8 metres
In this example, once we cut the wood at a3=5 metres, we have two woods of the same cut pattern. The pattern is (5#1#3). So it is enough if we calculated the cost for one part. For the other part we can straight away take the cost from the memory.
Now to the recurrence relation,
C(L, a1, a2, ... an) = C(L) + C(L1, a1, a2, ... aL1) + C({L-L1}, {aL1+1 - aL1}, {aL1+2 - aL1}, ... {an - aL1})
where C = Cost
- L = Length of the initial piece of wood. C(L) = L
- a1... an = Points at which the wood has to be cut
- L1 = Point nearest to L/2 where the first cut is made.
- aL1 = Point of cut at length L1
- aL1+1 = The next point after aL1 that is marked for a cut.
I will try to write a C program for the above problem and post it later. Open for comments on the way the solution is explained and if there are other better solutions for this.
1 comment:
hi,
saw your most of the published posts.. interesting.
http://www.embedded.com/2000/0005/0005feat2.htm
something for you.
-thanks
Harish
Post a Comment