CodeGuru Forums -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com


Newest CodeGuru.com Articles:

  • Deploying Windows Server 2008 with System Center
  • Remote Desktop Protocol Performance Improvements in Windows Server 2008 R2 and Windows 7
  • The Microsoft Dynamics CRM Security Model
  • SQL Server Modeling Services with Microsoft Visual Studio 2010 Beta 2

  • Search CodeGuru:
     



    Go Back   CodeGuru Forums > General Discussion > Algorithms & Data Structures
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    Algorithms & Data Structures Discuss algorithms & data structures. Topics are not specific to any programming language.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1    
    Old October 9th, 2009, 03:25 AM
    raghuramkb raghuramkb is offline
    Junior Member
     
    Join Date: Mar 2007
    Posts: 0
    raghuramkb is an unknown quantity at this point (<10)
    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
    Reply With Quote
      #2    
    Old November 10th, 2009, 10:01 AM
    Comand Comand is offline
    Junior Member
     
    Join Date: Mar 2009
    Posts: 7
    Comand is an unknown quantity at this point (<10)
    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
    Reply With Quote
    Reply

    Bookmarks
    Go Back   CodeGuru Forums > General Discussion > Algorithms & Data Structures


    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 02:42 PM.



    Acceptable Use Policy


    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.