首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何改进递归回溯算法

如何改进递归回溯算法
EN

Stack Overflow用户
提问于 2011-11-12 02:51:34
回答 1查看 2K关注 0票数 2

我为我的问题实现了基于回溯的解决方案,这是我在上一篇文章中指定的:Packing items into fixed number of bins

(Bin是一个简单的vector<int>数据类型包装器,带有其他方法,比如sum() )

代码语言:javascript
复制
bool backtrack(vector<int>& items, vector<Bin>& bins, unsigned index, unsigned bin_capacity)
{
    if (bin_capacity - items.front() < 0) return false;

    if (index < items.size())
    {
        //try to put an item into all opened bins
        for(unsigned i = 0; i < bins.size(); ++i)
        {
            if (bins[i].sum() + items[index] + items.back() <= bin_capacity || bin_capacity - bins[i].sum() == items[index])
            {
                bins[i].add(items[index]);
                return backtrack(items, bins, index + 1, bin_capacity);

            }
        }
        //put an item without exceeding maximum number of bins
        if (bins.size() < BINS)
        {
            Bin new_bin = Bin();
            bins.push_back(new_bin);
            bins.back().add(items[index]);

            return backtrack(items, bins, index + 1, bin_capacity);

        }
    }
    else
    {
        //check if solution has been found
        if  (bins.size() == BINS )
        {
            for (unsigned i = 0; i <bins.size(); ++i)
            {
                packed_items.push_back(bins[i]);
            }

            return true;
        }
    }
    return false;
}

尽管此算法运行速度很快,但对于大型数据集,它很容易发生堆栈溢出。

我正在寻找任何想法和建议,如何改善它。

编辑:

我决定尝试使用显式堆栈的迭代方法,但我的解决方案并不像预期的那样工作-有时它会给出不正确的结果。

代码语言:javascript
复制
bool backtrack(vector<int>& items, vector<Bin>& bins, unsigned index, unsigned bin_capacity)
{
      stack<Node> stack;
      Node node, child_node;
      Bin new_bin;
      //init the stack
      node.bins.add(new_bin);
      node.bins.back().add(items[item_index]);
      stack.push(node);
      item_index++;

      while(!stack.empty())
      {
        node = stack.top();
        stack.pop();

        if (item_index < items.size())
        {
            if (node.bins.size() < BINS)
            {
               child_node = node;
               Bin empty;
               child_node.bins.add(empty);
               child_node.bins.back().add(items[item_index]);
               stack.push(child_node);
            }

            int last_index = node.bins.size() - 1;
            for (unsigned i = 0; i < node.bins.size(); i++)
            {
                if (node.bins[last_index - i]->get_sum() + items[item_index]+ items.back() <= bin_capacity || 
                bin_capacity - node.bins[last_index - i]->get_sum() == items[item_index]) 
               {
                   child_node = node;
                   child_node.bins[last_index - i]->push_back(items[item_index]);
                   stack.push(child_node);
                }
            }
            item_index++;
            }
        else
        {
           if (node.bins() == BINS)
           {
               //copy solution
               bins = node.bins;
               return true;
           }
       }
   }
    return false;
}

我们非常感谢您的任何建议。

EN

回答 1

Stack Overflow用户

发布于 2011-11-13 11:30:59

我认为有一种动态编程算法来解决多箱装箱问题,或者至少是一种多项式近似算法。看看herehere吧。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8098562

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档