CSE 14 Lecture 16: Behavior of Functions in Different Classes & PA5 Stuff
CMPS12A Lecture 16: Behavior of Functions in Different Classes & PA5 Stuff
Programming Assignment 5 deals with permutations and finding a way to solve the Queen
problem in chess using permutations.
For the function of nextPermutation():
n = 6
0 1 2 3 4 5 6
/ 4 6 5 3 2 1
pivot successsor
(We start at the end of the indices and down by one, which would be the value 2. We then compare 2 to
the last value in the indices, which is 1, if 2 > 1. Since it is, we move to the left (down by one) to 3 and
compare that to 2, which 3 is greater, so we move to the left again. It is only when we get to the last two
indices when we compare 4 > 6, which is not, thus 4 is the pivot. The successor is then the last value we
compared that was ok, which was 5 when we compared it with 6.)
Then, swap the pivot and successor
0 1 2 3 4 5 6
/ 5 6 4 3 2 1
ī¾The piīot is ī
¶oī iī
¶ the suīīessors last iī
¶deī aī
¶d the suīīessor is ī
¶oī iī
¶ the piīotās iī
¶deī, thus
our new pivot and successor)
Then, reverse everything after new pivot
0 1 2 3 4 5 6
/ 5 1 2 3 4 6
For the function isSolution():
We need to iterate through īariaīles i aī
¶d j iī
¶ a īaī that īou doī
¶āt īouī
¶t for the tiī
µes īou
compared them at positions already counted for. Thus, you always need to keep i bigger than j
since it makes sense
find more resources at oneclass.com
find more resources at oneclass.com
1 <= i < j <= n
n would be the max number we would go up to and n(n-1) / 2 would be the number of
permutations possible with number n.
if n = 4:
1 2 2 3 3 4
1 3 2 4
1 4
if n = 5:
1 2 2 3 3 4 4 5 #comp
1 3 2 4 3 5 = 10 = 5 *(4 / 2)
1 4 2 5
1 5
To design isSolution(), Professor Taī
¶talo proīided a īode that is īasiīallī the īheartī of
isSolution():
Example:
class UnorderedPairs{
public static void main(String[] agrs){
int n = 5;
int k = n*(n-1)/2;
int i, j;
System.out.print("the "+k+" 2-element subsets of {");
for(i=1; i<n; i++)
find more resources at oneclass.com
find more resources at oneclass.com
System.out.print(i+",");
System.out.println(n+"} are: ");
for(i=1; i<n; i++){
for(j=i+1; j<=n; j++){
System.out.println("{"+i+","+j+"}");
}
System.out.println();
}
System.out.println();
}
}
To explain this program, it basically prints out all permutations up to the integer n, which in this
case is 5. The variable k basically tells us the number of 2-element subsets up to the number n,
which in this case will be 10, as shown above this programming code. The first for loop prints
out all the numbers up to one less than n inside a bracket { and then once out of the loop,
prints out the n number with the closing bracket }. The second for loop then prints out all the 2-
element permutations, starting with {1, 2}, {1,3 }, {1, 4}, {1, 5}, followed by a blank line, and
then goes to {2, 3}, {2, 4}, {2, 5}, and so on. Here is an output of it:
OUTPUT
{1, 2}
{1, 3}
{1, 4}
{1, 5}
{2, 3}
{2, 4}
{2, 5}
{3, 4}
find more resources at oneclass.com
find more resources at oneclass.com
Document Summary
Cmps12a lecture 16: behavior of functions in different classes & pa5 stuff. Programming assignment 5 deals with permutations and finding a way to solve the queen problem in chess using permutations. 0 pivot successsor (we start at the end of the indices and down by one, which would be the value 2. We then compare 2 to the last value in the indices, which is 1, if 2 > 1. Since it is, we move to the left (down by one) to 3 and compare that to 2, which 3 is greater, so we move to the left again. It is only when we get to the last two indices when we compare 4 > 6, which is not, thus 4 is the pivot. The successor is then the last value we compared that was ok, which was 5 when we compared it with 6. )