Skip to content

Lecture 6: Mathematical Structures and Functions - CSCI 381 Goldberg

Cost of Exhaustive Searching (Brute-Force Method)

Section titled “Cost of Exhaustive Searching (Brute-Force Method)”

Here, the Time Complexity equals the size of the search space.

Initially, mathematical structures are stored in un/ordered sets.

How many subsets are there of a given set?

Suppose a set S={E0,,En1}S = \{E_0, \ldots, E_{n-1}\} - EE for elements, and in C/C++ based languages the first index is 0.

n=S(cardinality)n = |S| \quad \text{(cardinality)}

A method to encode such a set with its subsets is by using a one-dimensional array/vector:

E0E_0E1E_1\cdotsEn1E_{n-1}
110 0 01

Sub/set Si={E0,E1,En1}S_i = \{E_0, E_1, E_{n-1}\}

Any subset SiS_i (of a set of size nn) can simply be encoded by boolean values stored in the above vector.

For example, subset Si={E0,E1,En1}S_i = \{E_0, E_1, E_{n-1}\}:

11000122=490000002=01111112=2n11100012_2 = 49 \qquad 000\ldots000_2 = 0 \qquad 111\ldots111_2 = 2^n - 1

The small 2 after the digits denotes base 2.

So counting the number of subsets is equivalent to determining how many bitstrings (arrays of only 0/1 values) of size nn there are. As mentioned many times before, this finite string of length nn can be encoded by natural numbers ranging from 00 (when the vector contains all 0’s) to 2n12^n - 1 (when the vector contains all 1’s). So a simple for loop enumerating the natural numbers from 00 to 2n12^n - 1 can each be decoded into the binary representation of each such natural number, which automatically represents a unique subset of an initial set SS of size nn.

In summary, there are 2n2^n such subsets, which is also PS(S)|PS(S)| where PSPS = powerset.

Structure 2: Relations(hips) from a Set S1S_1 to a Set S2S_2 - 2mn2^{mn}

Section titled “Structure 2: Relations(hips) from a Set S1S_1S1​ to a Set S2S_2S2​ - 2mn2^{mn}2mn”

Conventionally, m=S1m = |S_1| and n=S2n = |S_2|.

S1 S2
A1 B1
A2 --> B2
Ai .
. .
Am Bn

A graph is a “graph”ical representation of a relation. Standard (default) relations have two sets, whereas standard (default) graphs are from one set to itself. But this is only a default convention - relations can be over one set and graphs can be over two sets. A graph from set AA to a set BB is called bipartite.

Note: For diagramming purposes, the two sets appear of the same length (size) but mathematically of course of any size. This is indicated by the fact that S1=m|S_1| = m, S2=n|S_2| = n, with most probably mnm \neq n.

The Cartesian Product is ONE such relationship/mapping but contains the maximum number of connections FROM/TO:

S1×S2={A1,B1,,Ai,Bj,,Am,Bn}S_1 \times S_2 = \{\langle A_1, B_1 \rangle, \ldots, \langle A_i, B_j \rangle, \ldots, \langle A_m, B_n \rangle\} S1×S2=S1S2=mn|S_1 \times S_2| = |S_1| \cdot |S_2| = mn

Now, an arbitrary relation RR from S1S_1 to S2S_2 will be a subset of S1×S2S_1 \times S_2. S1×S2S_1 \times S_2 has ALL possible connections from S1S_1 to S2S_2; a random subset has SOME of the possible connections. SOME is a subset of ALL. Therefore:

RS1×S2R \subseteq S_1 \times S_2

Since RR is one subset of S1×S2S_1 \times S_2, then ALL relations are ALL subsets of S1×S2S_1 \times S_2, which is PS(S1×S2)PS(S_1 \times S_2). Based on Structure 1:

PS(S1×S2)=2S1×S2=2mn|PS(S_1 \times S_2)| = 2^{|S_1 \times S_2|} = 2^{mn}

In sets, the relation with the most number of connections is the Cartesian Product A×BA \times B, and in its corresponding graph, the graph with all possible edges is called Complete, noted by the letter KK for Komplete. (Complete, but not to be confused with other usages of CC.) Km,nK_{m,n} if two sets and KnK_n if over one set.

Structure 3: Directed Graphs over a Set SS - 2n22^{n^2}

Section titled “Structure 3: Directed Graphs over a Set SSS - 2n22^{n^2}2n2”

Graph G=(V,E)G = (V, E) where VV is the vertex set (aka nodes) and EE is the edge set (aka arcs).

n=V,EV×Vn = |V|, \qquad E \subseteq V \times V

(Compare with Structure 2 with RR as EE; here S1=S2=VS_1 = S_2 = V.) In fact, a graph is a “graph”ical representation of a relation.

The conventional defaults for relations and directed graphs are slightly different but they both are “two sides” of the same coin. In the case of relations (Structure 2), the default is a mapping from a set S1S_1 to a set S2S_2 (i.e. two sets involved), but the default for directed graphs is a mapping over a single set VV. Now the two are the same because there is no restriction on S1S_1 and S2S_2, and in fact we can have S1=S2S_1 = S_2, in which case relations are identically directed graphs. Not just that m=nm = n but that Ai=BiA_i = B_i - the elements are the same.

Therefore, we can use Structure 2 to count the number of directed graphs over VV, n=Vn = |V|. Namely, set m=nm = n and S1=S2S_1 = S_2. Then, the number of directed graphs over a single set VV:

PS(S1×S2)=2S1×S2=2mnwhere S1=S2=V|PS(S_1 \times S_2)| = 2^{|S_1 \times S_2|} = 2^{mn} \quad \text{where } S_1 = S_2 = V PS(V×V)=2V×V=2n2so m=n|PS(V \times V)| = 2^{|V \times V|} = 2^{n^2} \quad \text{so } m = n

Consider a relatively “small” vertex set VV with n=10n = 10 nodes/vertices. The number of different directed graphs over this set V=2n2=2102=2100V = 2^{n^2} = 2^{10^2} = 2^{100} (WOW!!!!). (That is larger than the claimed number of molecules in the universe.)

(The number of undirected graphs is left as a homework exercise.)

Structure 4: Partial Mapping from a Set S1S_1 to a Set S2S_2 - (n+1)m(n+1)^m

Section titled “Structure 4: Partial Mapping from a Set S1S_1S1​ to a Set S2S_2S2​ - (n+1)m(n+1)^m(n+1)m”
S1 S2
A1 B1
A2 --> B2
Ai Bj
. .
Am Bn

In a partial mapping, each element AiA_i has independently nn possible BjB_j elements to choose from to map into (though, at most one), but in addition, each element AiA_i has the extra right to not go at all (this is what causes the mapping to be “partial” or undefined). The partial mapping can only make one of these choices for each element AiA_i.

Each AiA_i therefore has n+1n + 1 choices:

(n+1)A1×(n+1)A2××(n+1)Ai××(n+1)Am1×(n+1)Am\underbrace{(n+1)}_{A_1} \times \underbrace{(n+1)}_{A_2} \times \cdots \times \underbrace{(n+1)}_{A_i} \times \cdots \times \underbrace{(n+1)}_{A_{m-1}} \times \underbrace{(n+1)}_{A_m}

In basic probability theory (and basic analysis of algorithms), the independent choices are multiplied together. The number of partial mappings from a set S1S_1 of size mm to a set S2S_2 of size nn equals (n+1)m(n+1)^m.

Structure 5: Total Mapping (Function) from a Set S1S_1 to a Set S2S_2 - nmn^m

Section titled “Structure 5: Total Mapping (Function) from a Set S1S_1S1​ to a Set S2S_2S2​ - nmn^mnm”
S1 S2
A1 B1
A2 --> B2
Ai Bj
. .
Am Bn

In a total mapping, each element AiA_i has independently nn possible BjB_j elements to choose from to map into, but each element AiA_i must go to exactly one element BjB_j. Note that all AiA_i can go to the same BjB_j.

Exactly one is not the same as uniquely one (next structure). Exactly one is purely a quantitative issue. Uniquely one is also qualitative.

Total functions that all map to the same element are called Constant Functions. For example F(x)=5F(x) = 5.

Each AiA_i has nn choices:

nA1×nA2××nAi××nAm1×nAm\underbrace{n}_{A_1} \times \underbrace{n}_{A_2} \times \cdots \times \underbrace{n}_{A_i} \times \cdots \times \underbrace{n}_{A_{m-1}} \times \underbrace{n}_{A_m}

In basic probability theory (and basic analysis of algorithms), the independent choices are multiplied together. The number of total mappings from a set S1S_1 of size mm to a set S2S_2 of size nn equals nmn^m.

Structure 6: Injective Mapping (1-1 Function) from a Set S1S_1 to a Set S2S_2 - n!(nm)!\frac{n!}{(n-m)!}

Section titled “Structure 6: Injective Mapping (1-1 Function) from a Set S1S_1S1​ to a Set S2S_2S2​ - n!(n−m)!\frac{n!}{(n-m)!}(n−m)!n!​”

aka Permutations

S1 S2
A1 B1
A2 --> B2
. .
. .
. .
Am Bn

Injective mappings are also Total. Based on the pigeonhole principle, the uniqueness property of 1-1 mappings requires therefore that mnm \leq n.

In a 1-1 mapping, each element AiA_i must uniquely choose one of the remaining possible BjB_j to map into, and each element AiA_i must go to some BjB_j (since 1-1 is also Total).

To see why the number of choices decreases, imagine a waiter at a party carrying a tray of 100 different shaped cakes - each one unique. The first person the waiter approaches has 100 choices and takes one. The second person now only has 99 choices, the third has 98, and so on. Because of the uniqueness requirement, whatever cake a person takes is no longer available to the next person. The same logic applies here: A1A_1 makes its choice first (wlog), which removes one BjB_j from availability, leaving A2A_2 with n1n-1 choices, and so on.

Each AiA_i gets fewer choices as prior elements have already claimed a BjB_j:

nA1×(n1)A2××(ni+1)Ai××(nm+2)Am1×(nm+1)Am\underbrace{n}_{A_1} \times \underbrace{(n-1)}_{A_2} \times \cdots \times \underbrace{(n-i+1)}_{A_i} \times \cdots \times \underbrace{(n-m+2)}_{A_{m-1}} \times \underbrace{(n-m+1)}_{A_m}

In basic probability theory (and basic analysis of algorithms), the independent choices are multiplied together. The number of injective mappings from a set S1S_1 of size mm to a set S2S_2 of size nn equals:

n(n1)(n2)(ni+1)(nm+1)n \cdot (n-1) \cdot (n-2) \cdots (n-i+1) \cdots (n-m+1)

This sort of looks like a defective factorial function. So, the literature uses an algebraic trick to count the number of such 1-1 mappings - multiply by 1:

[n(n1)(n2)(ni+1)(nm+1)]×(nm)!(nm)!=n!(nm)!\bigl[n \cdot (n-1) \cdot (n-2) \cdots (n-i+1) \cdots (n-m+1)\bigr] \times \frac{(n-m)!}{(n-m)!} = \frac{n!}{(n-m)!}

This is ALSO the number of ways to permute a subset of a set. As above, mnm \leq n. Thus, we can view the 1-1 mapping as a two-step process:

  • (Select or) Choose mm of the nn elements in set S2S_2. This is C(n,m)C(n,m), aka (nm)\binom{n}{m}, aka nCmnCm. Note: the convention for combinatorics is that the LARGER size is written first, here nn (since mnm \leq n), even though in terms of its cousin the 1-1 mapping, the set of size mm is visited/used first. Likewise for Permute(n,m)\text{Permute}(n,m) even though we select (“deal with”) mm first. C(n,m)C(n,m) = Combinations.
  • Count the number of permutations of a set of size mm. How many permutations of size mm? m!m!

In summary, what we have here is the following:

Permute(n,m)=C(n,m)×m!=n!(nm)!m!×m!=n!(nm)!\text{Permute}(n,m) = C(n,m) \times m! = \frac{n!}{(n-m)! \cdot m!} \times m! = \frac{n!}{(n-m)!}

Thus, a 1-1 mapping is identical to permuting a subset of elements from a larger set.

Structure 7: Surjective Mapping (Onto Function) from a Set S1S_1 to a Set S2S_2 - see below

Section titled “Structure 7: Surjective Mapping (Onto Function) from a Set S1S_1S1​ to a Set S2S_2S2​ - see below”

Every element in S2S_2 is reached.

S1 S2
A1 B1
A2 --> B2
. .
. .
. .
Am Bn

Onto mappings are also Total, so no AiA_i can go to more than one BjB_j. All elements of S2S_2 are connected to (“reached”) by the elements of set S1S_1. Therefore, mnm \geq n.

No one knows how to directly calculate the number of surjective mappings. Instead, the approach utilized is to count the number of total functions that are NOT onto:

Total(m,n)=ONTO(m,n)+NON_ONTO(m,n)\text{Total}(m,n) = \text{ONTO}(m,n) + \text{NON\_ONTO}(m,n) ONTO(m,n)=Total(m,n)NON_ONTO(m,n)=nmNON_ONTO(m,n)\text{ONTO}(m,n) = \text{Total}(m,n) - \text{NON\_ONTO}(m,n) = n^m - \text{NON\_ONTO}(m,n)

(cf. Structure 5 above.)

The complication that occurs is due to the Inclusion-Exclusion (I-E) principle, which deals with offsetting the possibility of counting the same elements more than once.

For example, suppose the math students club and the computer science society decide to have an end of year party (only students allowed). MATH=50|\text{MATH}| = 50 and CS=50|\text{CS}| = 50. Every student attends the party. How many students in total attended the party?

The first glance approach would be MATH+CS=50+50=100|\text{MATH}| + |\text{CS}| = 50 + 50 = 100 students. But this is not necessarily correct, because this ignores the possibility that the same student(s) is a double major. Thus, the correct answer is 50x10050 \leq x \leq 100.

I-E applied:

AB=A+BAB|A \cup B| = |A| + |B| - |A \cap B| ABC=A+B+CABACBC+ABC|A \cup B \cup C| = |A| + |B| + |C| - |A \cap B| - |A \cap C| - |B \cap C| + |A \cap B \cap C|

Pascal’s triangle has an interesting connection to I-E - the combinatorics involved in the I-E principle are the coefficients present in Pascal’s triangle:

1211331\begin{array}{ccccccc} & 1 & & 2 & & 1 & \\ 1 & & 3 & & 3 & & 1 \end{array}

These are rows of Pascal’s triangle.

Applying I-E to Count (Non-)Surjective Mappings

Section titled “Applying I-E to Count (Non-)Surjective Mappings”
S1 S2
A1 B1
A2 --> B2
. .
. .
. .
. Bn
.
Am

(Note that mnm \geq n.)

How many ways can we prevent the general total mapping from becoming surjective? (wlog means “without loss of generality”: the theorem does not depend on which element(s) we use; we choose the elements simply for the ease of discussing them.)

  • We will not allow any element to reach (wlog) B1B_1 - (one BiB_i element)
  • We will not allow any element to reach (wlog) B1B_1 or B2B_2 - (two BiB_i elements)
  • \vdots
  • We will not allow any element to reach (wlog) B1,B2,,Bn1B_1, B_2, \ldots, B_{n-1} - (n1n-1 BiB_i elements)

Once we have stopped the total mapping from becoming surjective, the total mapping is then free to map to whichever remaining BjB_j it wants.

Putting this all together:

  • How many ways can we prevent the total mapping from reaching ii elements of S2S_2? C(n,i)C(n,i)
  • How many BjB_j remain after preventing the total mapping from reaching those ii elements? nin - i
  • How many total functions are there from S1S_1 (all of the AA‘s) to the remaining nin-i free BB elements? (ni)m(n-i)^m

How does the I-E principle apply here? When you prevented the mapping from reaching one element BjB_j of S2S_2, this also includes the situation of preventing it from reaching two elements of S2S_2, as follows:

  • (forced) The first of the two BB elements is prevented because you explicitly prevented it.
  • (free) The second of the two BB elements can also be prevented because the remaining total function had no obligation to go to this second element.

This latter situation is then numerically equivalent to one of the cases where you explicitly prevented the mapping from reaching two of the BjB_j. Thus, the I-E principle applies and we need to put in the ±\pm correction factor.

Putting this all together:

ONTO(m,n)=nmi=1n1(1)i+1C(n,i)(ni)m\text{ONTO}(m,n) = n^m - \sum_{i=1}^{n-1} (-1)^{i+1} \cdot C(n,i) \cdot (n-i)^m

where the sum accounts for: Possible NON_ONTO, I-E correction, Choosing BiB_i‘s, and TOTAL(m,ni)\text{TOTAL}(m, n-i).

Therefore:

NON_ONTO(m,n)=i=1n1(1)i+1Cin(ni)m\text{NON\_ONTO}(m,n) = \sum_{i=1}^{n-1} (-1)^{i+1} C_i^n (n-i)^m ONTO(m,n)=TOTAL(m,n)NON_ONTO(m,n)=nmi=1n1(1)i+1Cin(ni)m\text{ONTO}(m,n) = \text{TOTAL}(m,n) - \text{NON\_ONTO}(m,n) = n^m - \sum_{i=1}^{n-1} (-1)^{i+1} C_i^n (n-i)^m

Structure 8: Partitions and How They Relate to Surjective Mappings - ONTO(m,n)n!\frac{\text{ONTO}(m,n)}{n!}

Section titled “Structure 8: Partitions and How They Relate to Surjective Mappings - ONTO(m,n)n!\frac{\text{ONTO}(m,n)}{n!}n!ONTO(m,n)​”

A partition of a set S={e1,,en}S = \{e_1, \ldots, e_n\} is a subdivision into kk subsets SiS_i such that Partition(S)={S1,S2,,Sk}\text{Partition}(S) = \{S_1, S_2, \ldots, S_k\} with the following conditions:

  • Each SiS_i is nonempty - (every box purchased is being used)
  • SiS_i must be a subset of SS - (no extraneous element crept in) (*)
  • Si=S\bigcup S_i = S - (all original elements have been packed)
  • SiSj=S_i \cap S_j = \emptyset unless i=ji = j - (mutually disjoint)

(*) Assumes SS is unordered and hence has only unique elements and no duplicates.

The above assumed that a) the owner of the original set decided ahead of time how many boxes (or subsets) to use (here, kk). A different scenario is that it is up to b) the packers to decide on their own how many boxes to use. These scenarios get slightly different names in discrete mathematics:

  • a) k-partition: PARTITION(S,k)\text{PARTITION}(S, k) - kk, the number of pieces (subsets) in the partition, is fixed ahead of time.
  • b) partition: PARTITION(S)\text{PARTITION}(S) - no initial restriction is placed on the number of boxes.

Connection Between k-Partitions and ONTO Mappings

Section titled “Connection Between k-Partitions and ONTO Mappings”

There is a fascinating connection between k-partitions and ONTO mappings. Consider:

S1 S2
a1 b1
a2 b2
a3 --> b3
a4 b4
a5
a6
a7
a8
a9
a10

Here, k=S2=4k = |S_2| = 4.

For example, one surjective mapping vs. a second surjective mapping:

Surjective mapping 1:

  • B1B_1 reached by {a1,a2,a3,a4}\{a_1, a_2, a_3, a_4\}
  • B2B_2 reached by {a5,a6,a7}\{a_5, a_6, a_7\}
  • B3B_3 reached by {a8,a9}\{a_8, a_9\}
  • B4B_4 reached by {a10}\{a_{10}\}

Surjective mapping 2:

  • B4B_4 reached by {a1,a2,a3,a4}\{a_1, a_2, a_3, a_4\}
  • B3B_3 reached by {a5,a6,a7}\{a_5, a_6, a_7\}
  • B2B_2 reached by {a8,a9}\{a_8, a_9\}
  • B1B_1 reached by {a10}\{a_{10}\}

In terms of ONTO, these are different ONTO mappings. But in terms of partitions, they are the same - since the ORDER of the boxes does not matter, only which elements are packed with which elements. In both cases, the same elements are with the same elements as before. This then becomes the same difference between Combination and Permutation, so that the counting differs by n!n!.

Conclusion:

PARTITION(m,n)=ONTO(m,n)n!\text{PARTITION}(m, n) = \frac{\text{ONTO}(m, n)}{n!}

Note: n!n! is the number of ways of ordering nn elements (here the S2S_2 boxes that the S1S_1 elements are packed into). Dividing removes the consideration of order, whereas multiplying by n!n! would add in the consideration of order.

The Bell number is the number of partitions for a set of a given size. The above is for k-partitions. So, the Bell number is the summation over all possible kk:

Bell(m)=n=1mPartition(m,n)\text{Bell}(m) = \sum_{n=1}^{m} \text{Partition}(m, n)

(Partition(m,n)\text{Partition}(m,n) are generically called “k-partitions” even though here k=nk = n.)

Structure 9: Bijections - Also Known as 1-1 Correspondence - n!n!

Section titled “Structure 9: Bijections - Also Known as 1-1 Correspondence - n!n!n!”

A bijection (aka 1-1 correspondence) is a (total) function that is both 1-1 (injective) and onto (surjective). Its importance is that it allows for the definition of an inverse - inverse means that the relation holds true in the other direction as well.

S1 S2
a1 b1
a2 --> b2
. .
Ai Bj
. .
am bn
m = |S1| n = |S2|

Recall that both injective and surjective have constraints on the sizes of the sets:

  • Injective: mnm \leq n
  • Surjective: mnm \geq n
  • Therefore, Bijective: m=nm = n

Discrete Mathematics then has a theorem that proves that an alternative (but equivalent) definition for bijective can be: 1-1 (injective) and equal sized sets (m=nm = n).

So, recall that the number of injective mappings equals n!(nm)!\frac{n!}{(n-m)!}. But here m=nm = n, so the number of bijections equals:

n!(nn)!=n!0!=n!since 0!=1\frac{n!}{(n-n)!} = \frac{n!}{0!} = n! \quad \text{since } 0! = 1

Why Can’t an Injective Mapping Alone Already Have an Inverse?

Section titled “Why Can’t an Injective Mapping Alone Already Have an Inverse?”
S1 S2
a1 b1 |
a2 --> b2 | (reached by S1)
. . |
am bm |
bm+1
.
.
bn
m = |S1| n = |S2|, m <= n

Simply, can’t we argue that the manner in which we mapped from an element in S1S_1 to an element in S2S_2 should provide the exact manner of getting back? Imagine for simplicity AiBjA_i \to B_j. Couldn’t the bijection simply be BjAiB_j \to A_i?

The official answer is that since S2S_2 can be bigger in size, there will possibly be some extra BjB_j elements that are not involved in this mapping. This makes the complete mapping of set S2S_2 back to S1S_1 a partial function.

A counter-argument is that the extra elements of S2S_2 were never reached by the elements from S1S_1 and therefore should not be part of our discussion of going back (“inverse”). In numerical analysis, this argument is presented and based on this, the mapping back (the inverse) uses only the subset of elements from S2S_2 that were initially reached by the elements of S1S_1. This type of inverse is termed a pseudo-inverse.

The faceplate of a scientific calculator illustrates partial functions in practice. Several buttons yield undefined (invalid) results for certain inputs:

ButtonInvalid InputWindows ResponseNote
(a)1/x1/x00Cannot divide by zero
(b)x\sqrt{\phantom{x}}1-1Invalid inputSquare root function on any negative value
(c)log\log00Invalid inputOr any negative value - typically natural number base
(d)ln\ln00Invalid inputOr any negative value - base ee
(e)//00Cannot divide by zeroWhen the denominator is zero (÷\div)
(f)xyx^yx=y=0x=y=011*This is technically wrong (see below)
(g)arcsin\arcsin22Invalid inputThe “inverse sin” function. Not defined for any xx where x>1\lvert x \rvert > 1
(h)arccos\arccos22Invalid inputThe “inverse cos” function. Not defined for any xx where x>1\lvert x \rvert > 1
(i)tan\tan9090 or 270270Invalid inputValue is in degrees
(j)!!1-1Invalid inputOr any negative value

*Regarding xyx^y where x=y=0x = y = 0, there are two different rules that clash:

  • 00 raised to any power equals 00.
  • Any number raised to power 00 equals 11.

Technically, 000^0 is undefined - and so undefined due to the contradiction that it is called indeterminate. Computer theory needed to define 000^0 as equal to 11 in order for certain mathematical recursions to work, so Martin Davis (the modern founder of computer theory) chose rule 2 and assigned 00=10^0 = 1.