0

I'm dealing with a problem, trying to make a recursive method that will test all possible combinations and show me the "cheapest" one.

The task is that:

  • I have an Array[n] numbers, which was dynamically allocated and filled. {1, 3, 4, 2} for example. The array is defined as
    int *array;
  • I fond out the size of the array and putted in the variable Size.
  • Each 2 numbers in the array indicate the top and bottom of a segment, so in this case the array has 3 segments. The segments are: A = 1 3 ,B = 3 4 ,C = 4 2 .
  • I have to join them in the order they are. I can start joining from the last two to the first one or I can start from the center to the sides but I have to choose the "cheapest" way.
  • Every time i join two segments: A = 1 3 and B = 3 4 . I calculate the cost of it this way: (1 * 4) + 3. I multiply the numbers at the ends of the segments and Sum to the the number between the segments.
  • The input will always be 2 or more segments meaning 3 or more numbers in the Array.

Practically what I need it to do is:

For two segments: array= "1 2 3"
A={1,2},B={2,3} ---- (1*3)+2 -> Cost = 5

For three segments: array= "30 20 25 10"
A={30,20},B={20,25},C={25,10} ----
case1 - [(30*25)+20] + [(30*10)+25] -> Cost = 1095
case2 - [(20*10)+25] + [(30*10)+20] /> Cost = 545

So we can clearly see that the second cost is better so we return that value. For more segments the number of calculations will drastically increase but that's ok for what I need.

I've been trying many times in different ways but I'm getting confused with all the pointers and also that I'm not good with recursion functions. I don't know if I was specific enough, if something isn't clear I'll be happy to explain.

I sure dont need the whole code, maybe some explanaition or an idea about how to do it would be appresiated :)

  • Do you want to know which is the cheapest cost only or also the associated "order"? Does it have to be efficient? – igon Nov 28 '12 at 15:14
  • No i only need to know the lower Cost. I dont need to know anything else, that makes it easier. About efficiency, Not exactly, i would be happy if it just worked even being it the hard way. – František Kvintus Nov 28 '12 at 15:27

1 Answers1

0

Let A be our array of n numbers, A[j] the jth element and A[i,k] a subarray of A with the ith up to the kth element, including. Index start counting at 0, so that A = A[0, n-1]. What is the result of the join operation? For example A = { 1, 2, 3 } has two segments 1,2 and 2,3 I assume correctly that joined together that will be 1,3?

Since each join biparts A in two parts you can recurse by testing all possible joins of A, namely n-2 possibilities, choosing the one with minimal cost. Maybe this equations gives you an idea.

/* cost of join two segments of A ending respective starting at j */
cost(A,j) = ( A[j-1]*A[j+1] ) + A[j]

 /* minmal cost of joining(A) when joining at j */
mincost(A,j) = mincost( A[0,j-1] ) + cost(A,j) + mincost( A[j+1,n-1] )

/* test all possible join, use minimal one */
mincost(A) = min[j]: mincost(A,j)

End recursion when n=3, use cost(A,1).

For a useable algorithm you will probably have to cache mincost(A[x,y]) this is called dynamic programming.

In C A[j] can be translated to *(A+j) where as A[i,k] does not directly correspond to a C construct, but it could be roughly translated to the following

 struct Array { 
          int * A; 
          int index_first;
          int index_last;
 };
Lothar
  • 860
  • 6
  • 21
  • Im not sure what do you mean by A. But what i understand is that A is the first number of the Array and i test it with the rest of them? I do not see how would this try to join segment 1 with 2 , segment 3 with 4 and finally join segment 12 with 34.. Im probably missunderstanding the meaning of the A. – František Kvintus Nov 28 '12 at 19:21
  • My bet, by accident I changed your `Array` into `A`. – Lothar Nov 28 '12 at 21:04
  • Oh i get it better now. The way before confused me. Well im gonna try and let you know. – František Kvintus Nov 28 '12 at 21:37