Lexicographic (or dictionary) Ordering
The permutation precedes(mendahului) the permutation of , if for some , with , and In other words, a permutation of the set of the n smallest positive integers precedes (in lexicographic order) a second permutation if the number in this permutation in the first position where the two permutations disagree is smaller than the number in that position in the second permutation.Dengan kata lain, permutasi dari himpunan bilangan bulat terkecil terkecil mendahului (dalam urutan leksikografis) permutasi kedua jika nomor dalam permutasi ini di posisi pertama di mana dua permutasi tidak sesuai lebih kecil dari angka di posisi itu di kedua permutasi.
Example
The permutation 23415 of the set precedes the permutation 23514, because these permutations agree in the first two positions, but the number in the third position in the first permutation, 4, is smaller than the number in the third position in the second permutation, 5. Similarly, the permutation 41532 precedes 52143.Permutasi 23415 dari himpunan mendahului permutasi 23514, karena permutasi ini sama dalam dua posisi pertama, tetapi angka di posisi ketiga dalam permutasi pertama, 4, lebih kecil daripada nomor di posisi ketiga dalam permutasi kedua, 5. Demikian pula, permutasi 41532 mendahului 52143.
Algorithm based on Lexicographic
Find the integers and with and ,
The next larger permutation in lexicographic order is obtained by putting in the jth position the least integer among that is greater than and listing in increasing order the rest of the integers in positions to .
The next larger permutation in lexicographic order is obtained by putting in the jth position the least integer among that is greater than and listing in increasing order the rest of the integers in positions to .
Example
What is the next permutation in lexicographic order after 362541?Solution:
The last pair of integers and where is and .
The least integer to the right of 2 that is greater than 2 in the permutation is .
Hence, 4 is placed in the third position.
Then the integers 2, 5, and 1 are placed in order in the last three positions, giving 125 as the last three positions of the permutation. Hence, the next permutation is 364125.
The least integer to the right of 2 that is greater than 2 in the permutation is .
Hence, 4 is placed in the third position.
Then the integers 2, 5, and 1 are placed in order in the last three positions, giving 125 as the last three positions of the permutation. Hence, the next permutation is 364125.
Algortithm Generating the Next Permutation in Lexicographic Order
procedure next permutation( : permutation of
not equal to )
while
is the largest subscript with
while
is the smallest integer greater than to the right of
Interchange and
while
interchange and
this puts the tail end of the permutation after the th position in increasing order is now the next permutation
not equal to )
while
is the largest subscript with
while
is the smallest integer greater than to the right of
Interchange and
while
interchange and
this puts the tail end of the permutation after the th position in increasing order is now the next permutation
Generating Combinations
An -combination can be represented by a sequence containing the elements in the subset in increasing order.Kombinasi dapat direpresentasikan oleh urutan yang berisi elemen dalam subset dalam urutan meningkat.The -combinations can be listed using lexicographic order on these sequences.
Kombinasi dapat dicantumkan menggunakan urutan leksikografis pada urutan ini.In this lexicographic ordering, the first -combination is and the last -combination is .
Dalam urutan leksikografis ini, kombinasi pertama adalah dan kombinasi terakhir adalah .The next -combination after can be obtained in the following way: First, locate the last element in the sequence such that .
Kombinasi berikutnya setelah dapat diperoleh dengan cara berikut: Pertama, temukan elemen terakhir dalam urutan sehingga .Then, replace with and with for .
Kemudian, ganti dengan dan dengan untuk .It is left for the reader to show that this produces the next larger -combination in lexicographic order.
Dibiarkan untuk pembaca menunjukkan bahwa ini menghasilkan kombinasi yang lebih besar berikutnya dalam urutan leksikografis.
Example
Find the next larger 4-combination of the set after .Solution:
The last term among the terms with , and such that is .
To obtain the next larger 4-combination, increment by 1 to obtain .
Then set and .
Hence the next larger 4-combination is .
To obtain the next larger 4-combination, increment by 1 to obtain .
Then set and .
Hence the next larger 4-combination is .
Algortithm Generating the Next r-Combination in Lexicographic Order
procedure next r-combination ( : proper subset of
not equal to with
)
while
for to
is now the next combination
not equal to with
)
while
for to
is now the next combination