Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Number of specific sequences in vector

Given two vectors: 'pattern' and 'trail'. Question: How often occurs 'pattern' in 'trail'? Example:

pattern <- c(1,2,3)

trail <- c(7,1,4,2,9,2,3)

Correct solution: 2 (i.e., 1,2,3 and 1,2,3; "2" occurs twice in the middle).

I tried:

getPerformance <- function(pattern,trail) {
  tmp <- 0
  for(i in 1:length(pattern)) {
    for(j in 1:length(trail)) {
      if(pattern[i]==trail[j]) {
        
        if(i<length(pattern)) {
          sum(pattern[i:length(pattern)]) 
        }
        tmp <- 1 * getPerformance(pattern[i:length(pattern)],trail[j:length(trail)])
      }
    }
  }
  return(tmp)
}

But this function does not terminate. Of course, non-recursive solutions are welcome. Thanks for any help!

like image 664
Michael227 Avatar asked Dec 08 '22 12:12

Michael227


2 Answers

n_subseq = function(trail, pattern) {
  # generate all subsets of the elements of `trail` in `pattern`
  # of `length(pattern)`
  # preserving order (as combn does)
  # that are all equal to `pattern`
  sum(combn(
    x = trail[trail %in% pattern],
    m = length(pattern),
    FUN = function(x) all(x == pattern)
  ))
}

n_subseq(trail = c(7, 1, 4, 2, 9, 2, 3), pattern = 1:3)
# [1] 2

n_subseq(c(1, 2, 2, 3, 3), 1:3)
# [1] 4
like image 74
Gregor Thomas Avatar answered Dec 25 '22 03:12

Gregor Thomas


First, we can ignore elements that don't appear in pattern:

tt = trail[trail %in% pattern]

Then, I'd do this recursive solution:

count_patt = function(p, v){
  # stop if done searching
  if (length(p) == 0L) return(0L)

  # find matches
  w  = which(v == p[1L])

  # report matches if done searching
  if (length(p) == 1L) return(length(w))

  # otherwise, search for subsequent matches    
  pn = p[-1L]
  sum(vapply(w, function(wi) count_patt(pn, tail(v, -wi)), FUN.VALUE = 0L))
}

count_patt(pattern, tt)
# [1] 2

Another recursive idea:

count_patt2 = function(p, v){
  # succeed if there's nothing to search for
  if (length(p) == 0L) return(1L)

  # find match
  w = match(p[1L], v)

  # fail if not found
  if (is.na(w)) return(0L)

  # if found, define rest of searchable vector
  tv = tail(v, -w)

  # count if same pattern is found later
  count_same = count_patt(p, tv)

  # or if rest of pattern is found later
  count_next = count_patt(p[-1L], tv)

  count_same + count_next
}

count_patt2(pattern, trail)
# [1] 2

If elements of pattern are distinct, I think this also works:

v = na.omit(match(trail, pattern))
prod(table(v[v == cummax(v)]))*(length(pattern) == length(v)) 
# [1] 2

A simple benchmark (so far only including @Gregor's function):

set.seed(1)
v0 = 1:9
nv = 200L
np = 5L

vec  = sample(v0, nv, replace=TRUE)
patt = sample(v0, np, replace=TRUE)

system.time(res_count2 <- count_patt2(patt, vec))
#    user  system elapsed 
#    0.56    0.00    0.56
system.time(res_count1 <- count_patt(patt, vec))
#    user  system elapsed 
#    0.60    0.00    0.61 
system.time(res_subseq <- n_subseq(vec, patt))
#    user  system elapsed 
#   25.89    0.15   26.16 

length(unique(c(res_subseq, res_count1, res_count2))) == 1L
# [1] TRUE

Comments. I find Gregor's res_subseq more readable than mine. I am sure there are more efficient recursive solutions.

like image 33
Frank Avatar answered Dec 25 '22 03:12

Frank