I am trying to write a program that can generate permutations using the Johnson-Trotter method with varying number of elements. I am still confused on how to get the permutations exactly. For 5 elements, I can only get this far and then I get stuck. I am not asking for all of them just a few more so I can get the pattern down.
1 2 3 4 5
1 2 3 5 4
1 2 5 3 4
1 5 2 3 4
5 1 2 3 4
I found the following page:
http://introcs.cs.princeton.edu/java/23recursion/JohnsonTrotter.java.html
Try running this Java program step by step, it will surely be of great help...
I have used the Johnson-Trotter algorithm in a card-melding game. The princeton.edu link mentioned above is widely quoted, but pretty confusing. And the code I've seen is either recursive (yuck) or inefficient. I rewrote it as an iterator, and it works great in my game. See my other post here: https://stackoverflow.com/a/28241384/4266886 with (very short) code. Let me know if I can help further.
Related
I have modified the question on n-puzzle. In this scenario, the puzzle has two blanks instead of one blank.
Initial State
3 5 1
4 6 -
7 2 -
Goal State
- 1 7
3 2 -
5 6 4
Is there any algorithm that I can use for this?
All existing algorithms that solve the regular sliding tile puzzle (such as A* or IDA*) can solve this variant as well. The puzzle with multiple blanks is equivalent to a pattern database for the sliding-tile puzzle - the exact solution to the puzzle with some pieces replaced with blanks can be used as a heuristic for the original puzzle with only a single blank.
(To be precise they are equivalent to additive pattern databases. You can combine several together and add their heuristic values as long as the action cost of swapping two blanks is 0 and none of the tiles are duplicated.)
Firstly, apologies for not using the correct terminology here. I don't actually know the correct terms, and thus have failed miserably to find a solution. Please accept my example as the question, and I'll update the question accordingly if somebody can enlighten me (or delete and read the actual solution where it exists).
So far my research has only yielded comparisons of single bits within the whole, rather than showing all.
Given a set of integers:
ELEMENT
-----------
Bricks 1
Plaster 2
Cement 4
Concrete 8
I have a result set that provides how these materials are used:
MIXTURE ELEMENTS
----------------------
MixtureFoo 3
MixtureBar 7
MixtureBaz 11
I need to show the final set of mixtures, but with each constituent element listed that is used in the respective mixture:
MIXTURE ELEMENTS ELEMENT
------------------------------
MixtureFoo 3 1
MixtureFoo 3 2
MixtureBar 7 1
MixtureBar 7 2
MixtureBar 7 4
MixtureBaz 11 1
MixtureBaz 11 2
MixtureBaz 11 8
You could use bitwise operations:
SELECT *
FROM t
JOIN ELEMENT e
ON t.ELEMENTS & e.w = e.w
ORDER BY MIXTURE, w;
db<>fiddle demo
I have made a loop that creates a variable, expectedgpa.
So now I have 1,000 variables for each observation, labeled expectedgpa1, expectedgpa2...expectedgpa1000.
I want to get the average and standard deviation for all the expectedgpas for each observation.
So if I have this
Joe 1 2 1 2 4
Sally 2 4 2 4 3
Larry 3 3 3 3 3
I want a variable returned that gives
Joe 2
Sally 3
Larry 3
Any help?
First, for future questions:
Please post code showing what you've tried. Your question shows no research effort.
Second, to clarify the terminology:
You created 1000 variables, each
one corresponding to some expected gpa. Each observation corresponds
to a different person. You want, as a result, three variables. One with the person's id
and another two with the the mean and sd of the gpa (by person).
This is my interpretation, at least.
One solution involves reshaping your data:
clear all
set more off
input ///
str5 id exgpa1 exgpa2 exgpa3 exgpa4 exgpa5
Joe 1 2 1 2 4
Sally 2 4 2 4 3
Larry 3 3 3 3 3
end
list
reshape long exgpa, i(id) j(exgpaid)
collapse (mean) mexgpa=exgpa (sd) sdexgpa=exgpa, by(id)
list
Instead of collapse, you can also run by id: summarize exgpa after the reshape, but this doesn't create new variables.
See help reshape, help collapse and help summarize for details.
You should not have created 1000 new variables without a strategy for how you were going to analyse them!
You could also use egen functions rowmean() and rowsd() and keep the same data structure.
A review of working "rowwise" in Stata is accessible at http://www.stata-journal.com/sjpdf.html?articlenum=pr0046
I'm having trouble with my n-puzzle solver. Thought it was working, but it turns out it is solving insoluble puzzles. I've tried to trace it, but that's a lot of tracing and so far I see no cheating. I think I understand the algorithm for determining solubility, and my implementation agrees with the odd/even parity of some examples from the web... that is to say, when I count up the number of tiles after a given tile that are smaller than it, for every tile, and then add the row index of the blank tile, I get the same odd or even number as others have gotten.
So a thought that has occurred to me. In my model of, say, the 8-puzzle, my solution state is:
_ 1 2
3 4 5
6 7 8
Rather than
1 2 3
8 _ 4
7 6 5
Or
1 2 3
4 5 6
7 8 _
As it is in some other formulations. Could this be affecting which puzzles are soluble and which are not?
Thanks!
z.
In general, yes: If a configuration is solvable to the standard solution, it will not be solvable to an unsolvable configuration.
In particular, it depends on the exact configuration you're using as a solution. You will need to check to see if you can solve from that configuration to the standard one.
EDIT: This of it this way:
Let A be the standard solution.
Let B be your preferred solution.
Let C be your starting configuration.
If you can get from A to B, and you can get from C to A, then you can get from C to B.
But if you can't get from A to B, and you can get from C to A, then you can't get from C to B.
I came across an algorithmic problem to find out the number of inversion pairs in an array in O(nlogn) time. I got the solution to this. But, my question is that what is the real-life application of this problem? Like I want to know some applications where we need to know the inversion pairs.
One example is the fifteen puzzle. If you want to randomly shuffle a grid of numbers, can you tell at a glance if
1 14 5 _
7 3 2 12
6 9 13 15
4 10 8 11
can be solved by sliding moves or not? The parity of the permutation will tell you that it is not.
Here is the use of inversion count in real life..
suppose you want to know how similar two list are..based on ranking..
on any movie site..two wishlist of movies are compared and few of them who are similar , are shown to users who have same choice.
Same logic applies to shopping list on any shopping website.. for recommending shopping items based on his activity..