j
using the following line:
Rewrite this line to improve performance. Time each improvement and present your findings in a table format. (HINT: Addition is much faster than multiplication.)j = i % 2 * p[i];
For extra credit, remove the nested while loop and compute the second index value i. Try replacing the nested loop with a fractal equation that depends upon the established range and the first index value generated. (A very large integer or binary operations will also work in this case.)
Write an algorithm to accurately time each Example for five different lengths of permutation sets. Be consistent for each Example and do not use a time less than a second more than once. (If you gather times for N as 10, 11, 12, 13, and 14 for QuickPerm (formally EXAMPLE_01), you must use the same values for the remaining four Examples.) Also, predict the amount of time it would take to permute 50, 75, and 100 items. Present your findings in a table format and save them. Repeat this process using a different CPU speed and compare your findings. (Include an accurate description of the advertised speed of each computer that you used for this exercise.)
10! 9! 8! 7! 6! 5! 4! 3! 2! 1! 0! <- Factorial Base 3628800 362880 40320 5040 720 120 24 06 02 01 01 <- Positional Value p[10] p[09] p[08] p[07] p[06] p[05] p[04] p[03] p[02] p[01] p[00] <- Base N Odometer Array -------------------------------------------------------------------- {01} {00} {00} {07} {03} {02} {04} {00} {01} {01} {00} <- Base N Odometer Reading
(1 * 10!) + (7 * 7!) + (3 * 6!) + (2 * 5!) + (4 * 4!) + (1 * 2!) + (1 * 1!)
------------------------------------------------- p[9] p[8] p[7] p[6] p[5] p[4] p[3] p[2] p[1] p[0] ------------------------------------------------- {00} {04} {04} {06} {01} {00} {02} {02} {01} {00} {09} {00} {01} {03} {00} {01} {03} {02} {01} {00} {00} {00} {00} {00} {00} {00} {00} {00} {00} {00} {08} {08} {07} {06} {05} {04} {03} {02} {01} {00} {09} {08} {07} {06} {05} {04} {03} {02} {01} {00} {09} {08} {05} {03} {02} {04} {03} {02} {01} {00}
Rewrite QuickPerm (EXAMPLE_01) and implement a binary storage method for both indices. Provide a fundamental analysis for all storage requirements. (Try describing a relationship between each binary number segment and prime numbers.)
For instance, assume you are modifying QuickPerm and the user's password is stored in the character string a[]. Consider expanding upon the following C++ statements to encode a data character from a file's IO stream:...
swap(a[j], a[i]);
read(data_ch);
new_ch = a[j] ^ CRC(a) ^ data_ch;
save(new_ch);
...
- Create a large stack to store index pairs. Index generation occurs within the stack class as needed during idle periods. Background stack operations (FIFO) including index pair generations are transparent to surrounding processes. (Reference exercises 1 & 15 above for implementation details.)
- Incorporate a greedy algorithm strategy. For instance, the distance of all cities along the tour including a return path can be initially arranged in ascending order based on the current city (or starting point), then permute the list of cities and recalculate distance(s) until a shortest path is discovered within a reasonable time. Adding the first new city from the best-discovered tour held above is one step forward toward the overall goal. The new discovered city becomes the current city (or starting point) and the process repeats itself until all cities are explored. Consider all cities that must be part of your view within a reasonable time-frame. Initially consider a wide radius centered upon the home city utilizing the QuickPerm algorithm to create two pathways that will eventually connect to complete the tour. Head and tail strategies may also be considered for this exercise.
- Implement time management procedures. For large tours, determine a maximum permutation time and when to utilize the best-discovered tour held. Fortunately the permutation presented in QuickPerm focuses upon the head of the array and initially avoids obvious irrational tours. (Reference exercise 5 above.)
- Algorithm 1: Count each combination without actually generating them (or simply without using recursion compute a previous generation count solely based upon the Base N Odometer reading). BIG HINT?
- Algorithm 2: Quickly compute the nth permutation without generating (n!-1) combinations. Your algorithm must follow the output order from the QuickPerm algorithm (formally Example_01) and compute the exact index order using the array p[N] without swapping elements within the target array a[]. (Hint: Use Algorithm 1 above and the array p[N] to predict the nth permutation. Also recall the p[N] controller array's behavior is analogous to an incremental base N odometer, where digit p[0] is zero, digit p[1] counts in base 2 (binary), digit p[2] counts in base 3, ..., and digit p[N-1] counts in base N.)
NOTE: Since the initial publication of this exercise in 1991, several attempts were made to solve Algorithm 2 as described above. Some attempts were very successful and obviously correct, but unfortunately many algorithms found on the Internet today (such as the recent article by Dr. James McCaffrey) mistakenly represent the nth permutation initially as a very limited integer parameter. These algorithms clearly fail for a large n within permutation sets where the length of the linear list or target exceeds 12 elements.
Accepted solutions to this exercise manually set the Base-N-Odometer then calculate the nth permutation solely based upon the new Base-N-Odometer reading. Utilize the same parameter concept as found in common Factorial functions to set the Base-N-Odometer without calculating a large factorial number(s) as a result. Research the QuickPerm Reversal Algorithm (formally Example 03) and answer Permutation Exercise 9 (above) before attempting to solve this exercise.
- Describe the influence of N's size when N < 12 and when N >= 12.
- Calculate a CRC number from an opposite direction. Compare the outcome to the previous method implemented and report noticeable correlations (or patterns).
- Using the MetaPerm class' public variables j, i, q, and r, alternate swapping elements between the head and the tail of the target array a[N]. Measure the statistical significance (or describe the duplicate sets produced). How would your measurements change by using two or more targets initially?
- Create a permutation algorithm that cycles or thrashes a new target array and compare it to the RandPerm algorithm developed above. (Hint: Individually increment the Base N Odometer's digits cyclically or increment the odometer by a number greater than one.)
- Describe the benefits (pros and cons) of using a permutation based random number generator instead of using a common random number generator based upon time. Consider several lottery number machines that can automatically pick a number sequence for the customer. Imagine some of these machines are based upon a permutation whereas others are based upon time. What happens when a several customers purchase a ticket at the exact "machine" time? What if there was a single primary index controller array (or just one base N odometer) that was shared for all permutation based machines? A large casino can also utilize a Base N Odometer(s), instead of random events based on time, to control large payouts without loosing the appearance of randomness or luck...
- Write an essay accurately describing the vital significance of a permutation based random number generator within Artificial Intelligent programs. (HINT: What's the computer doing when it's not trying to solve a problem?)
- Can you guess my "lucky" number? (Click here for the answer.)