ধরে নেওয়া যাক যে আমরা মান দেওয়া হচ্ছে সেগুলি নিয়ে অভিধানিক ক্রমের কথা বলছি, দুটি সাধারণ পন্থা রয়েছে যা আপনি ব্যবহার করতে পারেন:
- উপাদানগুলির একটি ক্রমকে পরের অনুমানে রূপান্তর করুন (যেমন শ্রীভাতসার পোস্ট করেছেন), বা
- 0 থেকে উপরের
n
দিকে গণনা করার সময় সরাসরি th তর অনুক্রমের গণনা করুন n
।
তাদের জন্য (আমার মত ;-) যারা সি ++ নেটিভ হিসাবে কথা বলেন না, তাদের নীচের সিউডো কোড থেকে 1 টি প্রয়োগ করা যেতে পারে, বামে সূচক শূন্যের সাথে একটি অ্যারের শূন্য-ভিত্তিক সূচিকরণ ধরে নেওয়া (কিছু অন্যান্য কাঠামো স্থির করে) যেমন একটি তালিকা হিসাবে, "অনুশীলন হিসাবে বাকি আছে" ;-):
1. scan the array from right-to-left (indices descending from N-1 to 0)
1.1. if the current element is less than its right-hand neighbor,
call the current element the pivot,
and stop scanning
1.2. if the left end is reached without finding a pivot,
reverse the array and return
(the permutation was the lexicographically last, so its time to start over)
2. scan the array from right-to-left again,
to find the rightmost element larger than the pivot
(call that one the successor)
3. swap the pivot and the successor
4. reverse the portion of the array to the right of where the pivot was found
5. return
এখানে সিএডিবির বর্তমান অনুমানের সাথে শুরু করে একটি উদাহরণ রয়েছে:
1. scanning from the right finds A as the pivot in position 1
2. scanning again finds B as the successor in position 3
3. swapping pivot and successor gives CBDA
4. reversing everything following position 1 (i.e. positions 2..3) gives CBAD
5. CBAD is the next permutation after CADB
দ্বিতীয় পদ্ধতির ( n
তম অনুগতির প্রত্যক্ষ গণনা ) জন্য মনে রাখবেন যে উপাদানগুলির N!
ক্রমশক্তি রয়েছে N
। অতএব, আপনি যদি N
উপাদানগুলিকে অনুমতি দিচ্ছেন, তবে প্রথম (N-1)!
আদেশটি অবশ্যই ক্ষুদ্রতম উপাদানের সাথে শুরু হওয়া উচিত, পরবর্তী (N-1)!
আদেশগুলি দ্বিতীয় ক্ষুদ্রতম দিয়ে শুরু হওয়া উচিত এবং এই জাতীয়। এটি নিম্নলিখিত পুনরাবৃত্তিমূলক পদ্ধতির দিকে পরিচালিত করে (আবার সিউডো কোডে, 0 থেকে ক্রমান্বয়ে এবং অবস্থানগুলি সংখ্যাযুক্ত):
To find permutation x of array A, where A has N elements:
0. if A has one element, return it
1. set p to ( x / (N-1)! ) mod N
2. the desired permutation will be A[p] followed by
permutation ( x mod (N-1)! )
of the elements remaining in A after position p is removed
সুতরাং, উদাহরণস্বরূপ, এবিসিডি এর 13 তম অনুমান নিম্নলিখিত হিসাবে পাওয়া যায়:
perm 13 of ABCD: {p = (13 / 3!) mod 4 = (13 / 6) mod 4 = 2
C followed by perm 1 of ABD {because 13 mod 3! = 13 mod 6 = 1}
perm 1 of ABD: {p = (1 / 2!) mod 3 = (1 / 2) mod 2 = 0
A followed by perm 1 of BD {because 1 mod 2! = 1 mod 2 = 1}
perm 1 of BD: {p = (1 / 1!) mod 2 = (1 / 1) mod 2 = 1
D followed by perm 0 of B {because 1 mod 1! = 1 mod 1 = 0}
B (because there's only one element)
DB
ADB
CADB
ঘটনাচক্রে, উপাদানগুলির "অপসারণ" বুলিয়ানগুলির সমান্তরাল অ্যারে দ্বারা প্রতিনিধিত্ব করা যেতে পারে যা নির্দেশ করে যে কোন উপাদানগুলি এখনও উপলব্ধ, তাই প্রতিটি পুনরাবৃত্তির কলটিতে একটি নতুন অ্যারে তৈরি করা প্রয়োজন হয় না।
সুতরাং, এবিসিডি এর অনুক্রমের পুনরাবৃত্তি করতে, কেবল 0 থেকে 23 (4! -1) পর্যন্ত গণনা করুন এবং সরাসরি সম্পর্কিত ক্রমান্বন গণনা করুন।