CSE 14 Lecture 16: Behavior of Functions in Different Classes & PA5 Stuff

31 views11 pages
30 May 2018
School
Course
Professor
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
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 11 pages and 3 million more documents.

Already have an account? Log in
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
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 11 pages and 3 million more documents.

Already have an account? Log in
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
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 11 pages and 3 million more documents.

Already have an account? Log in

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. )

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents

Related Questions