Algorithm: Search for Subsets and merge them by weight and order (Assistant Algorithm for goods Lang problem)

Source: Internet
Author: User

Assume that the number of values from 1 to 8 is 4.

1) Minimum values (1, 2, 3, 4)

2) assume that the current (A, B, C, D), if we can determine the combination of the Weight Value and the combination that is just greater than it, it is not difficult to find all the combinations by weight and order

Data Structure

Two structures are required: selects and remains. Selects is the selected child set and remains is the number to be considered.

They are lists and need to be sorted. First, the elements are placed as follows:

 

Algorithm

1) Use the smallest element in remains to find the element in selects that is better than it can.

2) If the two elements are found, exchange them.

3) if it cannot be found, delete the element from remains and take the next element to continue searching (that is, to 1 ))

 

------------ What can be found

Selects

1 2 3 4
5 6 6 8

Remains

 

Selects

1 2 3 5
4 6 6 8

Remains

------------ Cannot be found

 

Selects

2 3 4 5
1 6 7 8

Remains

 

Selects

2 3 4 5
6 7 8

Remains

(1. You don't have to worry about it. Adding selects will inevitably reduce the weight)

========================= SpecificProgramBelow ============================

Package com. PNP. findnextnumber;

Import java. util. arraylist;
Import java. util. collections;

Public class nextweighsum {

Arraylist <integer> remains = new arraylist <integer> ();
Arraylist <integer> selects = new arraylist <integer> ();
Int total_count = 10;
Int select_count = 4;

Void Init (){
For (INT I = 0; I <total_count; I ++)
Remains. Add (I + 1 );
Collections. Sort (remains );
For (INT I = 0; I <select_count; I ++)
Selects. Add (remains. Remove (0 ));
}

/*
* Selects give the subset, need to return the next subset which the weight sum is just larger than current subset
*/
Boolean selectnext (){
While (remains. Size ()> 0 ){
Int cur = remains. Get (0 );
Int Pos = collections. binarysearch (selects, cur );
If (Pos <0) // binarysearch (-(insertion point)-1)
Pos = (-Pos)-1;
Else {
System. Err. Print ("not allow equal elements ");
System. Exit (-1); // not allow equal elements
}

If (Pos = 0) {// That means current element is less that any in selects, we won't need to consider this ELEM
Remains. Remove (0 );
Continue;
}
Else {
Int insert_pos = pos-1;
Remains. Set (0, selects. Get (insert_pos ));
Selects. Set (insert_pos, cur );
System. Out. Print ("an Select ---");
Print (selects );
Return true;
}
}
Return false;
}

Void selectall (){
While (selectnext ())
;
}

Static void print (arraylist <integer> List ){
Int sum = 0;
For (INT I = 0; I <list. Size (); I ++ ){
Sum + = list. Get (I );
System. Out. Print ("" + list. Get (I ));
}
System. Out. println ("sum:" + sum );
}

Public static void main (string [] ARGs ){
Nextweighsum M = new nextweighsum ();
M. INIT ();
M. selectall ();
}

}

This algorithm is intended to be used in the question of goods Lang. The question of goods Lang is the set of options and the smallest edge. It is used to test whether it constitutes a path. If it constitutes a path, the problem is solved. Otherwise, it will test the next one.

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.