Recently, I thought of the following question relating to "ordering colored balls according to some choice of constraints".
Suppose there are 5 balls:
- Red
- Blue
- Green
- Yellow
- Orange
Normally, there are 5! = 120 ways these balls can be organized (n!).
However, I have the following question:
Suppose we have the following "constraints:
- The "Red" Ball can either be in the first or second position (From Left to Right)
- There must be at least 2 positions between the "Blue" Ball and the "Green" Ball
- The "Yellow" Ball can not be in the last position
Given all these "constraints", is there a mathematical formula that can be used to determine how many ways these 5 balls can now be ordered? That is, out of the 120 ways determined earlier - how many of these 120 ways are now considered "valid" according to the above "constraints"?
Since there only a small number of balls in this question, it's possible to store all possible combinations into a computer and then filter out combinations that don't match the above constraints. For example, here is how this can be done using the R programming language:
# generate all possible combinations
library(combinat)
library(dplyr)
library(data.table)
library(tidyverse)
my_list = c("Red", "Blue", "Green", "Yellow", "Orange")
d = permn(my_list)
all_combinations = as.data.frame(matrix(unlist(d), ncol = 120)) %>%
setNames(paste0("col", 1:120))
# keep combinations that match constraints
tpose = transpose(all_combinations)
tpose %>%
mutate(blue_delete = case_when(V1 == "Blue" & V2 == "Green" ~ TRUE,
V1 == "Blue" & V3 == "Green" ~ TRUE,
V2 == "Blue" & V3 == "Green" ~ TRUE,
V3 == "Blue" & V4 == "Green" ~ TRUE,
V4 == "Blue" & V5 == "Green" ~ TRUE,
TRUE ~ FALSE)) %>%
filter(V3 != "Red" & V4 != "Red" & V5 != "Red",
V5 != "Yellow",
blue_delete == FALSE) %>%
select(-blue_delete)
# preview answer
V1 V2 V3 V4 V5
1 Orange Red Blue Yellow Green
2 Red Orange Blue Yellow Green
3 Red Blue Orange Yellow Green
4 Red Blue Yellow Orange Green
5 Red Blue Yellow Green Orange
6 Red Yellow Blue Orange Green
7 Yellow Red Blue Orange Green
Running the above code, we see that out of the 120 possible ways - only 28 of these ways satisfy the above constraints.
My Question: Are there any "analytical formulas" which can be used to calculate the above answer?
Does anyone know if there any mathematical formulas that can be used calculate that there are 28 combinations for the above problem? Can this be formulated into a linear set of equations that is later solved?
Thanks!
References:
mutate
function, but the 6th option Red Yellow Blue Orange Green is uncorrect because blue and green do not have two positions between them. $\endgroup$