| CodeGuru Home | VC++ / MFC / C++ | .NET / C# | Visual Basic | Newsletters | VB Forums | Developer.com |
|
|||||||
| Algorithms & Data Structures Discuss algorithms & data structures. Topics are not specific to any programming language. |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Algorithm for finding out the cheapst combination
Hello, I have a few sets which are like
SET A(1,2,3,11,10) - $30 SET B(2,5,8) - $20 SET C(6) -$25 SET D(6,8) -$30 SET E(7,5) -$20 SET F(5,6,7,8,9,10) -$60 . . . and so on... All are random, Now consider sets D,E and F. I want to buy the cheapest combination for a set, SET Q(7,8,6,5) the answer should be SET D + SET E, not the SET F Please link... thanks |
|
#2
|
|||
|
|||
|
Re: Algorithm for finding out the cheapst combination
That's how I would solve the problem
![]() I hope it will help you. given variables: Code:
int count = 4;
SET[] dataArray;
string[] needArray = new String[count] {5, 6, 7, 8};
global variables: Code:
int price; int cheapestPrice; bool[] needBool = new bool[count]; string[] tempArray = new string[count]; string[] resultArray = new string[count]; Code:
void get cheapestCombination() {
price = 0;
cheapestPrice = 999999;
for (i = 0; i < count; i++)
needBool[i] = true;
for (i = 0; i < count; i++)
tempArray[i] = NULL;
for (i = 0; i < count; i++)
resultArray[i] = NULL;
findSet(0, 0);
}
Code:
void findSET(int start, int nesting) {
for (i = start; i < dataArray.lenth; i++)
bool containBool = false;
for (j = 0; j < count; j++) {
if (needBool[i] == true && dataArray[i] contains needArray[j]) {
containBool = true;
needBool[j] = false;
}
}
if (containBool == true) {
containBool = false;
tempArray[nesting] = i;
price += data.price;
}
if (nesting < count-1)
findSet(i+1, nesting+1);
if (nesting = 0) {
countProgress = 0;
for (int j = 0; j < count; j++) {
if (!needBool[j])
countProgress++;
}
if (countProgress == count && price < cheapestPrice) {
cheapestPrice = price;
resultArray = tempArray;
}
}
}
If you have any questions, send me an email to nulpen@online.de |
![]() |
| Bookmarks |
|
||||||
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|