I have to write a C program (for my Discrete Mathematics assignment) that finds the number of onto functions from set A (|A| = m) to set B (|B|=n) and to display all those functions. Number of onto functions I calculated using the code:
for(k=0; k<n; k++)
x = x + pow( -1, k) * combination( n, n - k ) * pow( ( n - k ), m);
Where combination is a function that finds the Number of possible combinations.
For example if A = {1,2,3}, B={a,b,c} then the number of onto functions evaluated from the formula is
3^3 – 3(2^3) + 3 = 6.
One possible solution is f = {(1,a),(2,b),(3,c)} [I know this is a solution].
But my problem is: How to display each and every solution!?
This is just a trivial example. But if m and n values are increased (provided m>=n) then the number of possible onto functions increases exponentially!!
For example if m=7 and n=4 there are 8400 functions!
I can’t think of any method to display each and every function that exists between A and B.
I answered a similar problem sometime ago but m and n were equal
m = n.(You must think recursively to solve this), by your comment I think the possible answers are:{(1,a)(2,b)(3,c)}, {(2,a)(3,b)(1,c)}, {(3,a)(1,b)(2,c)}, {(3,a)(2,b)(1,c)}, {(2,a)(1,b)(3,c)} and {(1,a)(3,b)(2,c)}then this is my recipe:Set 2 arrays with their initial value let’s call them
lettersandnumbers.Choose one of the arrays to be your pivot, I chose the
letters, it will be statical.Rotate the dynamic array counter-clockwise or clockwise as you wish, you must print the
i element of numberswith thei element of letters.So you get at this point:
{(1,a)(2,b)(3,c)}, {(2,a)(3,b)(1,c)}, {(3,a)(1,b)(2,c)}, 3 are missing.Swap the
i elementwith then elementof the dynamic array.Repeat the step 3.
So you get the missed subsets:
{(3,a)(2,b)(1,c)}, {(2,a)(1,b)(3,c)} and {(1,a)(3,b)(2,c)}.If you have more than 3 example 4. Easy:
1234(rotate N times where N is the number of variables and print with each movement), swap 1 and 4 ->4231(Rotate and Print), swap 2 and 3 ->4321(Rotate and Print), swap 4 and 1 –>1324(Rotate and Print).I hope this helped.