# the following examples show the different ways to encode the
# dependency structure of four items, where item 1 precedes items 2 and 3,
# item 2 precedes item 4, and item 3 precedes item 2
# list with items encoded by their precedence (i precedes all x[[i]])
x <- list(c(2L, 3L), 3L, 4L, integer())
topological_sort(x, dependency_type = "precedes")
stable_topological_sort(x, dependency_type = "precedes")
# list with items encoded by their antecedence (i follows all x[[i]]))
x <- list(integer(), c(1L, 3L), 1L, 2L)
topological_sort(x, dependency_type = "follows")
stable_topological_sort(x, dependency_type = "follows")
# matrix with items encoded by their precedence
x <- matrix(FALSE, ncol = 4, nrow = 4)
x[1L, c(2L, 3L)] <- TRUE
x[2L, 4L] <- TRUE
x[3L, 2L] <- TRUE
topological_sort(x, dependency_type = "precedes")
stable_topological_sort(x, dependency_type = "precedes")
# matrix with items encoded by their antecedence
x <- matrix(FALSE, ncol = 4, nrow = 4)
x[2L, c(1L, 3L)] <- TRUE
x[3L, 1L] <- TRUE
x[4L, 2L] <- TRUE
topological_sort(x, dependency_type = "follows")
stable_topological_sort(x, dependency_type = "follows")
Run the code above in your browser using DataLab