Hi all!
I am trying to implement a code that allows to count and create all possible combinations of a set of pairs, with the condition that no element is repeated between pairs.
That is, given two sets:
A={1,2,3,...,nA}
B={1,2,3,...,nB}
I know that the set A×B:={(a,b):a∈A,b∈B} has nA∗nB elements. But I'm interested in family of sets:
C⊂A×B , C={(a,b)∈A×B:a≠a′, and ,b≠b∀(a,b),(a′,b′)∈C} .
I'm interested in that set C with the maximum number of elements (given A and B ), and how to implement a code to create all the elements of this set.
For example, given A={1,2} , B={1,2} , I have this code:
scalar na=2
scalar nb=2
set obs `=na*nb'
egen A=seq(), from(1) to(`=na') block(`=nb')
egen B=seq(), from(1) to(`=nb') block(1)
that return in every row each elements of A×B . But suppose I take a subset of this set, C ⊂ A×B (i.e. a sample of this dataframe), my goal is to find all possible sets of pairs (a,b) ∈ C , such that between these pairs no elements are repeated.
In the example above, the set A×B is (in data format, "value" variable is defined next ):
A B value
1 1 x
1 2 y
2 1 z
2 2 w
if I take the following sample:
A B value
1 1 x
1 2 y
2 1 z
All sets of interest are:
var3 (string) var4_1 (num) var4_2(num)
"1-1" x 0
"1-2" y 0
"2-1" z 0
"1-2,2-1" y z
In particular, I'm interested in building this last data, with the variables "var4", "var4_1", "var4_2", ..., "var4_g", with g = maximum number of pairs in a set C .
I hope you can guide me in the procedure to build the code.
Best,
Jorge.
I am trying to implement a code that allows to count and create all possible combinations of a set of pairs, with the condition that no element is repeated between pairs.
That is, given two sets:
A={1,2,3,...,nA}
B={1,2,3,...,nB}
I know that the set A×B:={(a,b):a∈A,b∈B} has nA∗nB elements. But I'm interested in family of sets:
C⊂A×B , C={(a,b)∈A×B:a≠a′, and ,b≠b∀(a,b),(a′,b′)∈C} .
I'm interested in that set C with the maximum number of elements (given A and B ), and how to implement a code to create all the elements of this set.
For example, given A={1,2} , B={1,2} , I have this code:
scalar na=2
scalar nb=2
set obs `=na*nb'
egen A=seq(), from(1) to(`=na') block(`=nb')
egen B=seq(), from(1) to(`=nb') block(1)
that return in every row each elements of A×B . But suppose I take a subset of this set, C ⊂ A×B (i.e. a sample of this dataframe), my goal is to find all possible sets of pairs (a,b) ∈ C , such that between these pairs no elements are repeated.
In the example above, the set A×B is (in data format, "value" variable is defined next ):
A B value
1 1 x
1 2 y
2 1 z
2 2 w
if I take the following sample:
A B value
1 1 x
1 2 y
2 1 z
All sets of interest are:
var3 (string) var4_1 (num) var4_2(num)
"1-1" x 0
"1-2" y 0
"2-1" z 0
"1-2,2-1" y z
In particular, I'm interested in building this last data, with the variables "var4", "var4_1", "var4_2", ..., "var4_g", with g = maximum number of pairs in a set C .
I hope you can guide me in the procedure to build the code.
Best,
Jorge.
Comment