fork download
  1. input <- c(
  2. "a1=a2", "b1=b2", "b3=b2", "c1=c2", "e1=e2", "a3=a4", "c3=c4", "e1=e3",
  3. "a2=a4", "c3=c1", "b3=a4", "c2=d1", "a4=a5", "d2=c1", "b4=b3", "d3=c3"
  4. )
  5.  
  6. eq <- strsplit(input, "=")
  7. id <- unique(unlist(eq))
  8. n.id <- length(id)
  9.  
  10. A <- matrix(0, n.id, n.id)
  11. rownames(A) <- colnames(A) <- id
  12. E <- t(matrix(unlist(eq), 2))
  13. A[E] <- A[E[, 2:1]] <- 1
  14.  
  15. group <- rep(0, n.id)
  16. n.group <- 0
  17. for (i in 1:n.id) {
  18. if (group[i]) next
  19. group[i] <- n.group <- n.group + 1
  20. while (length(i)) {
  21. j <- which(colSums(A[i, , drop = FALSE]) != 0)
  22. i <- j[!group[j]]
  23. group[i] <- n.group
  24. }
  25. }
  26.  
  27. for (i in 1:n.group) cat("[", toString(id[group == i]), "]\n", sep = "")
Success #stdin #stdout 0.29s 40624KB
stdin
Standard input is empty
stdout
[a1, a2, b1, b2, b3, a3, a4, a5, b4]
[c1, c2, c3, c4, d1, d2, d3]
[e1, e2, e3]