近似裝箱問題(兩種脫機算法實現)

【0】README

0.1) 本文總結於 數據結構與算法分析, 源代碼均爲原創, 旨在 理解 「近似裝箱問題(兩種脫機算法實現)」 的idea 並用源代碼加以實現;
0.2) 近似裝箱問題的兩種聯機算法 分別是: 首次適合遞減算法 和 最佳適合遞減算法 , 咱們將依次給出源代碼實現+算法描述;
0.3)聯機算法+脫機算法
git

  • version1)聯機裝箱問題: 在這種問題中, 必須將每一件物品放入一個箱子後才處理下一件物品;(英語口語考試, 作完上一題,才能進入下一題做答)
  • version2)脫機裝箱問題:在一個脫機裝箱算法中, 咱們作任何事情 都須要等到全部的輸入數據全被讀入後才進行;(通常的考試,你只須要在規定的時間作完題目便可,作題順序不是咱們所關心的)
    0.3)不得不提的是: 個人源代碼中,桶的容量設爲了10,而不是1:緣由是 因爲 C語言的double類型的精度沒法準確表示 小數值(它的有效位數是15~16位),因此,會使得最後的算法結果錯誤, 固然了對於容量爲1的版本,我也寫了源代碼,參見:https://github.com/pacosonTang/dataStructure-algorithmAnalysis/tree/master/chapter10/p274_firstFitDecreaseOne, 有興趣的朋友,能夠down 下來,運行並測試一下;

【1】脫機算法相關

1.0)聯機算法的主要問題:在於將大項物品裝箱困難, 特別是當他們在輸入的晚期出現的時候, 
1.1)圍繞這個問題的天然方法:將各項物品排序,把最大的物品放在最早,此時咱們能夠應用首次適合算法或最佳適合算法,分別獲得 「首次適合遞減算法」 和 」最佳適合遞減算法」;
1.2)看個荔枝(能夠產生的最優解):
這裏寫圖片描述
1.3)咱們能夠證實的結論有(Conclusion):
github

  • C1)若是一種最優裝箱法使用M 個箱子, 那麼首次適合遞減算法使用的箱子數目絕對不超過 (4M+1)/3;
  • C2)首先, 全部重量大於1/3 的項將被放入前M個箱子內, 這意味着, 在外加的箱子中全部各項的重量頂可能是 1/3;
  • C3)在外加的箱子中 , 物品的項數最可能是 M-1;
  • C4)結合C2 和 C3, 咱們發現, 外加的箱子最多可能須要 |(M-1)/3|(不小於(M-1)/3 的最小整數)個;

【2】source code + printing results(first fit decrease alg)

2.1)download source code: https://github.com/pacosonTang/dataStructure-algorithmAnalysis/tree/master/chapter10/p274_firstFitDecreaseTen
2.2)source code at a glance:(for complete code , please click the given link above)
算法

int main()
{
    ElementTypePtr tempBox;
    int tempKey;
    int key[7] = {2, 5, 4, 7, 1, 3, 8}; 
    int i;

    size = 7;
    initBox(size);
    surplus = buildBasicArray(size, 10); // building surplus array to store surplus value
    tempBox = buildSingleElement();
    bh = initBinaryHeap(size + 1);

    for(i=0; i<size; i++)
    {   
        tempBox->key = key[i];
        insertHeap(*tempBox, bh);
    }// building binary heap over
        
    printBinaryHeap(bh);
    printf("\n");
    printf("\n===the sequence deleting minimum from binary heap===\n");
    while(!isEmpty(bh)) 
    {
        tempKey = deleteMin(bh)->key;
        printf("%d -> ", tempKey);
        firstFixDecrease(tempKey);
    }
    printf("\n");
    printBox(size);
    return 0;
}

void firstFixDecrease(int key)
{
    int i;
    ElementTypePtr box;
    ElementTypePtr temp;

    box = buildSingleElement();
    box->key = key; // build single box with key over

    for(i=0; i<size; i++)
    {
        if(surplus[i] < key)
            continue;   
        else
            break;
    }
    temp = boxes[i] ;
    while(temp->next)                   
        temp = temp->next;
    temp->next = box;
    surplus[i] -= key;  
}

2.3)printing results:
這裏寫圖片描述
數據結構


【3】source code + printing results(best fit decrease alg)

3.1)download source code: https://github.com/pacosonTang/dataStructure-algorithmAnalysis/tree/master/chapter10/p274_bestFitDecreaseTen
3.2)source code at a glance:(for complete code , please click the given link above)
ide

int main()
{
    ElementTypePtr tempBox;
    int tempKey;
    int key[7] = {2, 5, 4, 7, 1, 3, 8}; 
    int i;

    size = 7;
    initBox(size);
    surplus = buildBasicArray(size, 10); // building surplus array to store surplus value
    tempBox = buildSingleElement();
    bh = initBinaryHeap(size + 1);

    for(i=0; i<size; i++)
    {   
        tempBox->key = key[i];
        insertHeap(*tempBox, bh);
    }// building binary heap over
        
    printBinaryHeap(bh);
    printf("\n");
    printf("\n===the sequence deleting minimum from binary heap===\n");
    while(!isEmpty(bh)) 
    {
        tempKey = deleteMin(bh)->key;
        printf("%d -> ", tempKey);
        bestFixDecrease(tempKey);
    }
    printf("\n");
    printBox(size);
    return 0;
}

void bestFixDecrease(int key)
{
    int i;
    ElementTypePtr box;
    ElementTypePtr temp;
    int minimum;
    int miniIndex;

    box = buildSingleElement();
    box->key = key; // build single box with key over
    miniIndex = 0;
    minimum = 10;

    for(i=0; i<size; i++)
    {
        if(surplus[i] < key)
            continue;
        if(surplus[i] - key < minimum)
        {
            minimum = surplus[i] - key;
            miniIndex = i;
        }
    }

    temp = boxes[miniIndex] ;
    while(temp->next)                   
        temp = temp->next;
    temp->next = box;
    surplus[miniIndex] -= key;  
}

3.3)printing results:
這裏寫圖片描述
測試

相關文章
相關標籤/搜索