Lazy Larry was assigned to create a bubble sort algorithm, but he didn't have the time to do it properly. He designed the following algorithm instead:
For each iteration of the algorithm:
1
Choose two distinct elements in the list uniformly at random.
Exchange the placements of those elements in the list.
Check to see if the list is now sorted. If it is, end the algorithm. Otherwise, do another iteration.
Lazy Larry tests his algorithm on the following list:
capybara, dingo, aardvark, bandicoot
What is the expected value of the number of iterations of Larry's algorithm to alphabetize the list (A to Z)?
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
How can we generalize this result?
Log in to reply
Well the states of the Markov chain are the cycle types of S 4 (identity, 2 -cycles, 3 -cycles, 4 -cycles, 2 × 2 -cycles). We determine the transition probabilities by considering what happens to a particular cycle type when composed with a transposition. There are 6 possible transpositions, and we need to check all the options. For example, if we start with a 2 -cycle:
These ideas give you the transition probabilities from Andy's State P.
We could play the same game with any other permutation group S n , but the options would get increasingly complicated. There is some ease to be gained from the fact that an odd permutation state can only transition to an even permutation state, and conversely.
good problem
Not understand
Heh. Nice "sorting" algorithm.
A slightly easier version of the problem: instead of taking a random transposition from the group of permutations on four objects, just take a random member of the group, i.e. randomly shuffle the order of the list.
It might seem like this is a slower approach, but it turns out to be just a tad bit faster in expected stopping time. (And computing the expected stopping time is much simpler.)
Log in to reply
A lot easier. You want the expectation of the geometric distribution G e o ( 2 4 1 ) , which is 2 4 . There is no need to introduce Markov chains.
Log in to reply
Exactly correct. No matter what the sequence is at any point there is exactly one permutation that will immediately sort it.
%MATLAB CODE n = 1e5; operations = zeros(1,n); for i = 1:n disp(i) list = [3 4 1 2]; while any(diff(list) < 0) order = randperm(4); tmp = list(order(1)); list(order(1)) = list(order(2)); list(order(2)) = tmp; operations(i) = operations(i) + 1; end end mean(operations) == ~ 27
So the answer is probably 27
Larry's algorithm
Below you will find my code doing a brute-force Monte-Carlo simulation. The code uses multi-threading.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
|
Problem Loading...
Note Loading...
Set Loading...
There are 4 ! = 2 4 permutations of the list, and each is possible with Larry's algorithm. This is a lot to analyze, but fortunately, we can organize these permutations into a set of "states":
State P: Exactly two of the elements in the list are out of order. Example:
bandicoot, aardvark, capybara, dingo
State Q: All four of the elements in the list are out of order, but they can be placed in order by switching two pairs. This is the state the list is in before Larry runs the algorithm:
capybara, dingo, aardvark, bandicoot
State R: Exactly three of the elements in the list are out of order. Example:
capybara, aardvark, bandicoot, dingo
State S: Exactly four of the elements are out of order, and the order cannot be restored by switching two pairs. Example:
bandicoot, capybara, dingo, aardvark
State T: All four of the elements are in order:
aardvark, bandicoot, capybara, dingo
Now we can analyze how we can move between states with each iteration of the algorithm. There are 6 possible switches with each iteration. Suppose we are in state P, like the example given above:
bandicoot, aardvark, capybara, dingo
There is 1 switch that will bring us to state Q:
bandicoot, aardvark, capybara, dingo
⇒bandicoot, aardvark, dingo, capybara
There are 4 switches that will bring us to state R:
bandicoot, aardvark, capybara, dingo
⇒capybara, aardvark, bandicoot, dingo
bandicoot, aardvark, capybara, dingo
⇒dingo, aardvark, capybara, bandicoot
bandicoot, aardvark, capybara, dingo
⇒bandicoot, capybara, aardvark, dingo
bandicoot, aardvark, capybara, dingo
⇒bandicoot, dingo, capybara, aardvark
There is 1 switch that will bring us to state T:
bandicoot, aardvark, capybara, dingo
⇒aardvark, bandicoot, capybara, dingo
This gives us probabilities of 6 1 , 3 2 , and 6 1 to go from state P to states Q, R, and T, respectively. The other states can be likewise analyzed. Below is a graph showing their relationships:
Let X be the number of iterations until we reach state T. Then we have the expected values:
p = E [ X ∣ P ] q = E [ X ∣ Q ] r = E [ X ∣ R ] s = E [ X ∣ S ]
We can write a system of equations using linearity of expectation .
p q r s = 1 + 6 1 q + 3 2 r = 1 + 3 1 p + 3 2 s = 1 + 2 1 p + 2 1 s = 1 + 3 1 q + 3 2 r
Solving this system gives q = 2 7 , and so the expected value of the number of iterations from the initial list is 2 7 .