Mikki0

Untitled

Jun 4th, 2021
1,071
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. dfs <- function(v, edges, used, depth, st) {
  2.   used[v] = TRUE
  3.   push(st, v)
  4.   for (i in 1:nrow(edges)) {
  5.     if (edges[i, 1] == v && !used[[edges[i, 2]]]) {
  6.       dfs(edges[i, 2], edges, used, depth + 1, st)
  7.     }
  8.   }
  9.   if (depth > maxdepth) {
  10.     maxdepth <<- depth
  11.     answer <<- rev(unlist(as.list(st)))
  12.     last <<- v
  13.   }
  14.   pop(st)
  15.   used[v] = FALSE
  16. }
  17.  
  18. func <- function(nodes, edges){
  19.   maxdepth <<- -1
  20.   st <- stack()
  21.   for (i in nodes) {
  22.     used <- hash()
  23.     for (j in 1:length(nodes)) {
  24.       used[[nodes[j]]] <- FALSE
  25.     }
  26.     dfs(i, edges, used, 0, st)
  27.     for (j in 1:nrow(edges)) {
  28.       if (edges[j, 1] == last && edges[j, 2] == i) {
  29.         answer <- c(answer, i)
  30.       }
  31.     }
  32.   }
  33.   return(answer)
  34. }
  35.  
  36. library(hash)
  37. library(dequer)
  38. func(c("Marta", "is", "lalka"), rbind(c("Marta", "lalka")))
  39.  
RAW Paste Data