蟻は立方体の角に置かれ、移動できません。クモは反対側の角から始まり、等しい確率で立方体のエッジに沿って任意の方向移動できます。平均して、クモがアリに到達するために必要な歩数は?1 / 3
(これは宿題ではなく、インタビューの質問でした。)
蟻は立方体の角に置かれ、移動できません。クモは反対側の角から始まり、等しい確率で立方体のエッジに沿って任意の方向移動できます。平均して、クモがアリに到達するために必要な歩数は?1 / 3
(これは宿題ではなく、インタビューの質問でした。)
回答:
各状態がクモとアリの間の距離を表すマルコフ連鎖として問題をモデル化することをお勧めします。このケースでは4つの可能な状態を有する距離として、私がすることができ、{ 0 、1 、2 、3 }。
クモが立方体の反対側の角にあるとき、それはアリから3ステップの距離にあります。状態です。
遷移行列構築。
立方体を描くと、状態にいるときに、すべての動きがクモとアリの間の距離を2ステップに短縮することがわかります。したがって、状態S 3にいるとき、確率1で状態S 2に移動します。
状態いるときは、そこから到着したエッジを使用して状態S 3に戻ることができます。または、他の2つのエッジを選択した場合、距離を1ステップのみに減らすことができます。我々は状態にあるときに、S 2我々は状態に移動することができ、S 1を確率2/3とし、状態にS 3確率が1/3と。
状態いるとき、3つの可能なエッジの1つを使用して状態移動できます。他の2つを使用する場合、状態戻ります。したがって、状態いるとき、確率1/3で状態、確率2/3で状態移動できます。
状態到達すると、それが私たちの目標なのでそこにとどまります。は吸収状態です。
これは、3つの過渡状態(、S 2、S 1)と1つの吸収状態(S 0)を持つ吸収マルコフ連鎖です。
理論によれば、過渡状態とr吸収状態を持つマルコフ連鎖の遷移行列は、次のように書き換えることができます 。P = [ Q t R 0 r × t I r ]
ここで、は、ある過渡状態から別の過渡状態に遷移する確率を示すt × tマトリックスであり、Rは、t過渡状態の1つからr吸収の1つに遷移する確率を持つt × rマトリックスです状態。恒等行列I rは、r吸収状態のいずれかに到達すると、その状態から離れる遷移がないことを示しています。すべてゼロの行列0 r × tは、rからの遷移がないと解釈できます。過渡状態のいずれかに吸収状態。
Q tのエントリは、正確に1ステップで状態iから状態jに遷移する確率を表します。kステップの確率を取得するに は、Q k tの(i 、j )エントリが必要です。すべてのkを合計すると、(i 、j )エントリに、過渡状態iから開始した後の過渡状態jへの予想訪問数を含む行列が得られます。
吸収されるまでのステップ数を取得するには、各行の値を合計するだけです。これは
ここで、はすべての成分が1に等しい列ベクトルです。
これをケースに適用してみましょう。
我々のケースで我々は、上述のように = 3の過渡状態及びR = 1つの吸収状態、したがって: Q T = [ 0 1 0 1 / 3 0 2 / 3 0 2 / 3 0 ]
予想される訪問数のマトリックスは
この行列は次のように解釈できます。状態から始まり、S 0に吸収される前に、平均してS 3 2.5回、S 2 4.5回、S 1 3回訪問します。
状態から状態S 0までの予想されるステップ数は、次のベクトルの最初のコンポーネントによって与えられます。
の2番目と3番目のコンポーネントは、それぞれS 2とS 1から開始した場合のS 0への予想ステップ数です。
Let be the number of expected steps. Let be the number of expected steps from any corner adjacent to the origin of the spider and ditto for the ant.
Then and . Since
we get that . So implying that and .
We get our answer as .
If we draw the cube with coordinates then is the starting position of the spider and the position of the ant.
The spider can move to either , or .
By the symmetry of the cube these must have the same number of expected steps to the ant, denoted by . From , we can either return to the origin (with probability ) or (with probability ) we can go to one of the points , , depending on which state we are in.
.
One nice abstraction to think of it is this:
Think of the Position of the Ant as and Spider , now each move the spider can make will essentially switch exactly one of the three components from or . So the question becomes:
If I randomly switch bits in (1,1,1) after how many steps in average do I get 0,0,0
We see the shortest way is 3 switches. Since it doesn't matter with which bit I start the probability of that happening is 1 * 2/3 * 1/3 = 2/9
. If we make 1 mistake (switch one bit back to 1) we will need 5 steps. And the chances of making a mistake are 7/9 - if we want to make only one mistake, we have to get from there back and do everything right again - so the chance of making exactly 1 mistake resulting in 5 steps is 7/9 * 2/9
and the chance of making 2 mistakes aka 7 steps is (7/9)² * 2/9
and so on.
So the formula for the expected average number of steps is:
n
is the current integer in that infinite sum.
Just to compliment tiagotvv's answer:
I don't naturally think of these kinds of problems as matrices (even though they are). I have to draw it out, which I've done below. You can see that there are 3 places to move from S, all of which are As. From any A, you can either return to the S, or move to one of two Bs. From any B, you can move to the E, or to one of two As. This all translates to the transition matrix given by tiagotvv, which can also be drawn in graph form.
Because I am terrible at math, I would just try to simulate your problem. You can do this with the markovchain package in R.
library(markovchain)
library(ggplot2)
# Create a markovchain object, given the states and their transition matrix
mcCube <- new("markovchain",
states = c("S", "A", "B", "E"),
transitionMatrix = matrix(data = c(0, 1, 0, 0,
1/3, 0, 2/3, 0,
0, 2/3, 0, 1/3,
0, 0, 0, 1),
byrow = T, nrow = 4),
name = "cube")
# The following code calcuates the probability of landing on E after taking
# between 1 and 100 steps from the start, given the above set of transition
# probabilities.
start <- c(1, 0, 0, 0)
list <- list()
for (i in 1:100){
list[[i]] <- (start * mcCube^i)[4]
}
a <- do.call(rbind, list)
data <- data.frame(propE = a,
steps = c(1:100))
ggplot(data, aes(x = steps, y = propE)) +
geom_line(size = 1) +
ylab("Probability you reached the spider") +
xlab("Number of steps taken") +
theme_bw() +
theme(panel.grid.minor = element_blank())
# This code simulates 1000 different applications of the markov chain where you
# take 1000 steps, and records the step at which you landed on E
list <- list()
for (i in 1:1000) {
b <- rmarkovchain(n = 1000, object = mcCube, t0 = "S", include.t0 = T)
list[[i]] <- 1001 - length(b[b == "E"])
}
data <- as.data.frame(do.call(rbind, list))
ggplot(data, aes(x = V1)) +
geom_density(fill = "grey50", alpha = 0.5) +
geom_vline(aes(xintercept = mean(V1))) +
ylab("Density") +
xlab("Number of steps to reach E") +
theme_bw() +
theme(panel.grid.minor = element_blank())
mean(data$V1) # ~10 is the average number of steps to reach E in this set of
# simulations
tiagotvv's answer can be calcuated in R as:
q = matrix(c(0, 1, 0,
1/3, 0, 2/3,
0, 2/3, 0), byrow = T, nrow = 3)
(solve(diag(3) - q) %*% c(1, 1, 1))[1] # = 10
Parity considerations give a very clean solution, using surprisingly simple machinery: no Markov chains, no iterated expectations, and only high school level summations. The basic idea is that if the spider has moved an even number of times in the direction, it has returned to its original coordinate so can't be at the ant's position. If it has moved an odd number of times in the direction, then its coordinate matches the ant's. Only if it has moved an odd number of times in all three directions will it match the , and coordinates of the ant.
Initially the spider has made zero moves in any of the three directions, so the parity for each direction is even. All three parities need to be flipped to reach the ant.
After the spider's first move (let's label that direction ), exactly one direction has odd parity and the other two ( and ) are even. To catch the ant, only those two parities need to be reversed. Since that can't be achieved in an odd number of subsequent moves, from now on we consider pairs of moves. There are nine possible combinations for the first paired move:
We need to move in the and directions to reach the ant after one paired move, and two out of nine combinations will achieve this: and would ensure all three parities are odd.
The other seven combinations leave one odd and two even parities. The three repeated moves, , or , leave all parities unchanged so we still require one and one movement to reach the ant. The other pairs contain two distinct moves, including one in the direction. This switches the parity of and one of the other parities (either or ) so we are still left with one odd and two even parities. For instance the pair leaves us needing one more and one more to reach the ant: an equivalent situation (after relabelling of axes) to where we were before. We can then analyse the next paired move in the same way.
In general paired moves start with one odd and two even parities, and will either end with three odd parities (with probability ) and the immediate capture of the ant, or with one odd and two even parities (with probability ) which returns us to the same situation.
Let be the number of paired moves required to reach the ant. Clearly follows the geometric distribution on the support with probability of success so has mean . Let be the total number of moves required, including the initial move and the subsequent paired moves. Then so, applying linearity of expectations, .
Alternatively you might note and apply the well-known formula for the mean of a discrete distribution taking only non-negative integer values, . This gives which is a geometric series with first term and common ratio so has sum . We can then take as before.
Comparison to Markov chain solutions
How might I have spotted this from the Markov chain transition matrix? Using @DLDahly's notation, the states in the transition matrix correspond to my description of the number of the number of directions with odd parity.
The one-step transition matrix is
The first row show us that after one movement, the spider is guaranteed to be in state A (one odd and two even parities). The two-step transition matrix is:
The second row shows us that once the spider has entered state A, in two moves time it has either returned to state A with probability or has reached state E (all odd parities) and captured the ant, with probabilty . So having reached state A, we see from the two-step transition matrix that the number of two-step moves required can be analysed using the geometric distribution as above. This isn't how I found my solution, but it is sometimes worth calculating the first few powers of the transition matrix to see if a useful pattern like this can be exploited. I have occasionally found this to give simpler solutions than having to invert a matrix or perform an eigendecomposition by hand - admittedly something that is only really relevant in an exam or interview situation.
あなたの質問に数値で答える短いJavaプログラムを書きました。クモの移動は本当にランダムです。つまり、アリに到達する前に周期的に移動することもできます。
ただし、「反対のコーナー」という用語を定義しなかったため、2つの異なるシナリオがあります。同じ平面を横切るように、または立方体を横切るように向かいます。最初のシナリオでは、最短パスは2ステップであり、2番目のシナリオでは3ステップです。
私は1億回の繰り返しを使用しましたが、結果は次のとおりです。
-- First scenario --
Steps sum: 900019866
Repeats: 100000000
Avg. step count: 9.00019866
-- Second scenario --
Steps sum: 1000000836
Repeats: 100000000
Avg. step count: 10.00000836
ソースコード:
import java.util.Random;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.IntStream;
public class ProbabilityQuizSpider {
// Edges of the cube
private static final int[][] EDGES = new int[][] {
{1, 3, 7}, // corner 0
{0, 2, 4}, // corner 1
{1, 3, 5}, // corner 2
{0, 2, 6}, // corner 3
{1, 5, 7}, // corner 4
{2, 4, 6}, // corner 5
{3, 5, 7}, // corner 6
{0, 4, 6} // corner 7
};
private static final int START = 0; // Spider
private static final int FINISH = 5; // Ant
private static final int REPEATS = (int) Math.pow(10, 8);
public static void main(String[] args) {
final Random r = new Random();
final AtomicLong stepsSum = new AtomicLong();
IntStream.range(0, REPEATS).parallel().forEach(i -> {
int currentPoint = START;
int steps = 0;
do {
// Randomly traverse to next point
currentPoint = EDGES[currentPoint][r.nextInt(3)];
// Increase number of steps
steps++;
} while(currentPoint != FINISH);
stepsSum.addAndGet(steps);
});
// Results
System.out.println("Steps sum: " + stepsSum.get());
System.out.println("Repeats: " + REPEATS);
System.out.println("Avg. step count: " + (((double) stepsSum.get()) / ((double) REPEATS)));
}
}
編集:スクリプトのタイプミスを修正(および結果も更新)
{0, 2, 6}
. I have re-run the program and got the following result: 10.00000836 steps for traversing from corner #0 to corner #5 (body diagonal of the cube). This is also consistent with @Hunaphu.
モンテカルロシミュレーションで難問を解決しました()および取得 。
私が使用したRコードは次のとおりです。
ant = c(0,0,0) # ant's coordinates
sim = 1e4 # number of MC simulations
steps = numeric() # initialize array of steps
for (i in 1:sim)
{
spider = c(1,1,1) # spider's coordinates
count = 0 # initialize step counter
# while ant's coordinates == spider's coordinates
while (!isTRUE(all.equal(ant, spider)))
{
# random walk in one of three dimensions
xyz = trunc(runif(1,1,4))
# let the spider move
if (spider[xyz] == 1)
{
spider[xyz] = 0
} else if (spider[xyz] == 0)
{
spider[xyz] = 1
}
# add one step
count = count + 1
}
# add the number of step occurred in the ith iteration
steps = c(steps, count)
# print i and number of steps occurred
cat("\n", i, " ::: ", count)
}
# print the mean of steps
(mean(steps))
R
関数を活用して、これを1秒未満に高速化できます。n.sim <- 1e6; x <- matrix(runif(n.sim*3), ncol=3); moves <- x >= pmax(x[, 1], x[, 2], x[, 3]); positions <- apply(moves, 2, cumsum) %% 2; types <- rowSums(positions); vertices <- types[types==0 | types==3]; transitions <- cumsum(diff(vertices) != 0); n.sim / transitions[length(transitions)]
「しかし、あなたは「反対のコーナー」という用語を定義しなかった」と言及するとき、alescは正しい軌道に乗っていると思います。質問に何かが欠けていない限り、正しい答えはなく、仮定に基づいた答えだけです。キューブサイズは定義されていませんIE 10立方フィート、1000立方フィートなど。Antサイズは定義されていませんIE小さな庭、大工、ジャイアントレッドなど、スパイダータイプは定義されていません(ステップサイズを決定するため)IE小さな庭、タランチュラなど。変数。答えは0ステップまたは未定/無限のステップ数です。