Knapsack Problem priority queue branch and restriction algorithm

Source: Internet
Author: User

This is probably an exercise question in the algorithm class.

The so-called backpack problem can be described as follows: a thief steals a safe deposit box and finds there are n kinds of items of different sizes and values in the cabinet, but a thief only has a backpack with a volume of m to carry things. The problem with a backpack is to find out a combination of stolen things, so as to maximize the total value of stolen items.

There are many ways to solve this problem. This program uses the branch and limit method to solve it.

/*
Copyright (c) 2006, Liu aigui, Aigui.LIU@ihep.ac.cn, computing center of ihu, Beijing, China
*/

// 0/1 knapsack problem priority queue branch Limit algorithm
# Include "stdafx. H"
# Include "iostream. H"
Struct node {// node table node Data Structure
Node * parent; // parent node pointer
Node * Next; // subsequent node pointer (used for priority queue)
Int level; // The number of nodes
Int tag; // indicates the left and right children
Int Cu; // available space of the backpack
Int PE; // valid benefit value of the loaded item
Int Lb; // lower bound value of a node
Float UB; // upper bound value of the node
};
Class lcknap {// priority queue backpack class
PRIVATE:
Node * head; // The Head of the Active Node queue.
Node * ans, * E; // solution node, Root Node
Int * P, * w; // bag value, weight array pointer
Int M, LBB, Cap, Prof; // The sum of the backpack capacity, lower limit, remaining capacity, and current value
Int N; // number of items
Float L; // value of the loaded item
Float E, UBB; // small positive integer parameter and upper value limit
Public:
Lcknap (int * PP, int * ww, int mm, int NN, float EE); // Constructor
~ Lcknap (); // destructor
Void lubound (int rw, int CP, int K, Int & LBB, float & UBB); // calculates the upper and lower limits.
Node * newnode (node * parent, int level, int T, int cap, int prof, float UB, int lb); // generate a new node
Void enqueue (node * I); // Add node I to the priority queue
Void dequeue (node * I); // delete node I from the priority queue
Node * nextlivenode (); // The next extended Node
Void print (); // print the result
Void lcknap (); // solve the Knapsack Problem
};
Lcknap: lcknap (int * PP, int * ww, int mm, int NN, float ee)
{// Constructor
Int I;
// Initialization parameters
N = nn;
M = mm;
E = EE;
P = new int [N];
W = new int [N];
For (I = 0; I <n; I ++)
{
P [I] = PP [I];
W [I] = WW [I];
}
Head = new (node );
Head-> next = NULL;
L = 0;
Ans = new (node );
}
Lcknap ::~ Lcknap ()
{// Destructor
Delete head;
Delete P;
Delete W;
Delete ans;
}
Void lcknap: lubound (int rw, int CP, int K, Int & LBB, float & UBB)
{// Calculate the upper limit
Int I, j, C;
LBB = CP;
C = RW;
For (I = K; I <n; I ++)
{
If (C <W [I])
{
UBB = (float) (LBB + C * P [I]/W [I]);
For (j = I + 1; j <n; j ++)
{
If (C> = W [J])
{
C = C-W [J];
LBB + = P [J];
}
}
Return;
}
C = C-W [I];
LBB + = P [I];
}
UBB = (float) LBB;
Return;
}
Node * lcknap: newnode (node * parent, int level, int T, int cap, int prof, float UB, int lb)
{// Generate a new node
Node * I = new (node );
I-> parent = parent;
I-> next = NULL;
I-> level = level;
I-> tag = T;
I-> Cu = cap;
I-> Pe = Prof;
I-> UB = UB;
I-> lB = LB;
Return (I );
}
Void lcknap: enqueue (node * I)
{// Add node I to the priority queue
I-> next = head-> next;
Head-> next = I;
}
Void lcknap: dequeue (node * I)
{// Delete node I from the priority queue
Node * pre = head, * P = head-> next;
While (P! = I)
{
Pre = P;
P = p-> next;
}
Pre-> next = p-> next;
}
Node * lcknap: nextlivenode ()
{// Next extended node (maximum LB node with lower limit)
Node * P = head-> next, * choice = P;
Int lB = p-> Lb;
While (P)
{
If (p-> LB> lb)
{
Choice = P;
}
P = p-> next;
}
Return (choice );
}
Void lcknap: Print ()
{// Print the result
Int I;
Cout <"value of optimal filling is:" <L <Endl;
Cout <"objects in knapsack are :";
For (I = N; I> = 1; I --)
{
If (ANS-> tag = 1)
{
Cout <'X' <I <'';
}
Ans = ANS-> parent;
}
Cout <Endl;
}
Void lcknap: lcknap ()
{// Solve the Knapsack Problem
Int I;
Node * E = new (node); // Root Node
E-> parent = NULL;
E-> next = NULL;
E-> level = 0;
E-> Cu = m;
E-> Pe = 0;
E-> tag = 0;
Lubound (M, LBB, UBB); // calculates the upper and lower limits of the root node.
L = LBB-E;
E-> lB = LBB;
E-> UB = UBB;
While (e-> UB> L) // upper bound of the current expansion node <End of current solution
{
I = e-> level;
CAP = e-> Cu;
Prof = e-> PE;
If (I = N) // solution node
{
If (Prof> L)
{
L = (float) Prof; // Solution
Ans = E;
}
}
Else // E has two sons
{
If (Cap> = W [I]) // The left son is feasible.
{
Enqueue (newnode (E, I + 1, 1, cap-W [I], Prof + P [I], e-> UB, e-> lb ));
}
Lubound (Cap, Prof, I + 1, LBB, UBB); // recalculate the Upper and Lower Bounds
If (UBB> L) // The right son is feasible.
{
Enqueue (newnode (E, I + 1, 0, Cap, Prof, UBB, LBB ));
If (L <LBB-e) L = LBB-E;
}
}
If (Head-> next = NULL) // The queue is empty or UB> L ends.
{
Break;
}
Else
{
E = nextlivenode (); // the next extension node.
Dequeue (E); // delete a node from the queue
}
} // Endwhile
Print ();
}
// Main program
Void main (INT argc, char * argv [])
{
Float E;
Int P [4] = {10, 10, 12, 18}, W [4] = {2, 4, 6, 9}; // 4 backpack
Int PP [16] = {10, 12, 9, 15, 13, 12, 10, 14, 9, 7, 19, 18, 15, 12, 11, 10}; // 16 backpack
Int WW [16] = {2, 5, 5, 6, 5 };
E = (float) 0.0001;
Lcknap * kn1_= new lcknap (p, W, 15, 4, e );
Lcknap * knap16 = new lcknap (PP, WW, 40, 16, e );
Knap4-> lcknap ();
Knap16-> lcknap ();

Delete knits;
Delete knap16;
}
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.