Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

A riddle from the Riddler where N cars going at random (iid) speeds drive a road with a slow and a fast lane, each car choosing the fast lane iff any of the cars ahead in the slow lane is slowerthan them. With the question of the average number of car convoys.

If there were one single lane, the problem would be to determine how many times a smaller realisation and it has been solved in a much older Riddler. Namely

$\sum_{i=1}^N 1\big/ i$

In the two-lane system, the slow one only gathers cars with speeds lowest than the current speed, i.e. a decreasing sequence of speeds, creating single car convoys.  The fast lane thus captures cars that are above the current minimum in the sequence, which, as it converges to the global minimum at some points, means that all following cars are found in the fast lane. i thought this would bring the number of convoys close to twice the above logarithmic sum (which sealed my destiny during an entrance oral exam 40 years ago!), but there are actually more of them for N large enough , which may be due to the possibility of the slow lane to capture more moderate speed cars in the beginning… The above compares the number of convoys for one lane (in red) and two (in gold), as well as the remarkable fit when regressing these numbers against log(N).

Here is the R code I used for that simulation

convoy=function(N,T=1e5){
for(t in 1:T){
speed=runif(N)
slow=fast=NULL
slow=speed[1]
for(i in 2:N){
if(speed[i]<min(slow))slow=c(slow,speed[i])
else fast=c(fast,speed[i])}
F=F+length(slow)
if(length(fast)>0)F=F+1+sum(fast[-1]<cummin(fast)[-length(fast)])}
return(F/T)}


R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.