ข้อเสนอดังต่อไปนี้ย่อมสมบูรณ์แบบ:
zucchini <- function(st, en, mingap=1)
{
i <- order(st, en-st);
st <- st[i];
en <- en[i];
last <- r <- 1
while( sum( ok <- (st > (en[last] + mingap)) ) > 0 )
{
last <- which(ok)[1];
r <- append(r, last);
}
if( length(r) == length(st) )
return( list(c = list(st[r], en[r]), n = 1 ));
ne <- zucchini( st[-r], en[-r]);
return(list( c = c(list(st[r], en[r]), ne$c), n = ne$n+1));
}
coliflore <- function(st, en, mingap = 1)
{
zu <- zucchini(st, en, mingap);
plot.new();
plot.window( xlim=c(min(st), max(en)), ylim = c(0, zu$n+1));
box(); axis(1);
for(i in seq(1, 2*zu$n, 2))
{
x1 <- zu$c[[i]];
x2 <- zu$c[[i+1]];
for(j in 1:length(x1))
rect( x1[j], (i+1)/2, x2[j], (i+1)/2+0.5, col="gray", border=NA );
}
}
การประยุกต์ใช้:
> st <- runif(20,0,50)
> en <- st + runif(20, 5,20)
> st
[1] 25.571385 17.074676 4.564936 27.247745 23.832638 11.045469 2.845222
[8] 2.824046 23.319625 19.684993 42.610242 48.185618 47.748637 39.813871
[15] 9.235512 40.299425 13.797027 21.079956 31.638772 24.152991
> en
[1] 35.43667 32.20029 19.37133 44.30378 35.73845 16.63794 11.52551 16.06469
[9] 32.22477 26.05563 49.51284 67.77664 67.27914 49.35472 28.27657 50.49421
[17] 27.29273 37.87611 48.76251 39.89335
> coliflore(st, en)
สวัสดีปีใหม่!