IT221 Discrete Math

Unit 6: Counting, Combinations and Permutations

RBatzinger

2025-10-21

Final Topics

  • 1: Automata
  • 2: Sequences
  • 3: Counting Combinations and Permutations
  • 4: Graphs and Trees
  • 5: Regular Expressions and State Machines

1: Automata

Starting image

Automata Outline

  +         +
 +#+  -->  + +
  +         +

 +++       +++
 +#+  -->  + +
 +++       +++

Outline version

 5 dot pattern                                    9-dot pattern
 

Conway’s Game of Life ( Cellular Automata)

  • Each cell with one or no neighbors dies, as if by solitude.

     ++            ++
    #     -->     .
  • Each cell with four or more neighbors dies, as if by overpopulation.

    ++         ++
    #+    -->  .+
     +         ++ 
  • Each cell with two or three neighbors survives.

     +          +
    #+    -->  #+ 
  • For a space that is empty or unpopulated: Each cell with three neighbors becomes populated.

     +           +
     +    -->   #+
     +           +

https://playgameoflife.com

2: Sequences

Arithmetic

\[a_n = a_{n-1} + d\]

\[a_n = a_1 + d(n-1)\]

\[\small s_n = \sum \left(a_1 + a_1 + \cdots + a_n\right)= \frac{n (a_1 + a_n)}{2}\]

Geometric

\[x_n = a(r^{n-1})= r x_{n-1}\]

  • Sum of units \(x_1 \cdots x_n\)

\[s_n = \sum \left(x_1 + x_2+ \cdots x_n\right)= \frac{a(1-r^n) }{(1-r)}\]

  • Sum of units $x_1 x_r< 1 $

\[s_n = \sum \left(x_1 + x_2+ \cdots + x_n\right)= \frac{a}{(1-r)}\]

  • Note: \(r \ne 1\) and \(r \gt 1\) will never converge.

Polynomial of power \(n\)

  • General Polynomial

\[x = a_0 + a_1 x+ a_2 x^2+ ... + a_{n}x^n\]

  • Let’s use a random polynomial to general

\[y=2x^3-4x\]

  • Calculate the first 7 entries of the sequence.
-2  8  42  112  230  408  658  
  • Calculate derivative differentials to determine power of the polynomial

     x   y    Diff  Diff2 Diff3
     1   -2   
          >  10 
     2    8        >  24
          >  34           > 12
     3   42        >  36
          >  70           > 12
     4  112        >  48
          >  118          > 12
     5  230        >  60
          >  178          > 12
     6  408        >  72
          >  250
     7  658
  • Set up to determine the coefficients

\[\eqalign{ x:& &ax^3 + bx^2 + cx + d &=& y\\ 1:& &a + b + c + d &=& -2\\ 2:& & 8a + 4b + 2c +d &=&8\\ 3:& & 27a + 9b + 3c + d&=&42\\ 4:& & 64a + 16b + 4c + d&=&112\\ }\]

  • Convert to a solution matrix

\[\left[\matrix{1&1&1&1&|& -2\\ 8 & 4 & 2 & 1 &|&8\\ 27 & 9 & 3& 1&|&42\\ 64 & 16 & 4 & 1&|&112\\ }\right]\]

  • Use Gaussian elimination to reduce the lower echelon

\[\left[\matrix{1&1&1&1&|& -2\\ 0 & 1 & \frac{3}{2} & \frac{7}{4}&|&-6\\ 0 & 0 & 1& \frac{11}{6}&|&-4\\ 0 & 0 & 0 & 1&|&0\\ }\right]\]

  • Use back substitution to solve the unknowns

\[\left[\matrix{1&0&0&0&|& 2\\ 0 & 1 & 0 & 0&|&0\\ 0 & 0 & 1& 0&|&-4\\ 0 & 0 & 0 & 1&|&0\\ }\right]\]

  • Convert to the polynomial

\[y=2x^3-4x\]

Use Integration to determine the sum

\[\int_1^7 2x^3 - 4x dx= 1102\]

Confirmation by Trapezoida Rule

3: Counting Combinations and Permutations

Permutations

  • an arrangement of objects in a specific order.

  • Order matters: each permutation represents a different permutation.

  • Permutations of \(n\) Objects Taken \(n\) at a Time

\[P(n,n) = n!\]

  • Permutations of \(n\) Objects Taken \(k\) at a Time

\[P(n,k)= \frac{n!}{(n-k)!}\]

  • Permutations with Repetition (Non-Distinct Objects)

The number of ways to arrange \(n\) objects where there are \(n_1\) identical objects of type 1, \(n_2\) identical objects of type 2, and so on.

\[\frac{n!}{n_1! n_2! \dots n_r!}\]

Combinations (Order Doesn’t Matter)

  • A combination is a selection of objects where the order of selection is irrelevant.

Example: Choosing runs like \({1, 2, 3}\), \({2,1,3}\) and \({3,1,2}\) out of a set of 100 numbers

  • Combinations of \(n\) Objects taken \(k\) at a Time

\[C(n, k) = \binom{n}{k} = \frac{P(n, k)}{k!} = \frac{n!}{k!(n-k)!}\]

Permutations vs Combinations

\[\small\matrix{ Feature & Permutations & Combinations\\ \hline \hbox{Order}&\hbox{Matters (Arrangement)}&\hbox{Doesn't Matter (Selection)}\\ \hbox{Question}&\hbox{How many arrangements?}&\hbox{How many groups/subsets?}\\ \hbox{Formula} & P(n, k) = \frac{n!}{(n-k)!} & C(n, k) = \frac{n!}{k!(n-k)!}\\ \hbox{Result}&\hbox{Generally larger number}&\hbox{Generally smaller number}\\ \hbox{Mnemonic}&\hbox{Position (or Placement)}&\hbox{Committee (or Choose)}\\ }\]

Bars and Stars:

Distribution of \(n\) items among \(k\) bins

  • We need n stars (the objects)

  • We need k-1 bars (to create k bins)

  • Total positions: n + k - 1

  • The number of ways =

\[\binom{n+k-1}{k-1} = \binom{n+k-1}{n}\]

  • Example: 3 cookies distribute among 2 children

     ***|
     **|*
     *|**
     |***

\[\binom{3+2-1}{2-1}= \binom{4}{1}= \frac{4!}{3!1!}=4\]

Pigeon Hole Principle

There are 10 pigeons and 9 holes:

There is at least one hole with 1 or more pigeons.

If there are \(P\) individuals in the group and \(k\) boxes:

There are at least one box that will contain at least \(\left\lceil \frac{P}{k}\right\rceil\) objects

Chopsticks in a drawer

5 pairs of chopstick are thrown into a drawer and mixed up. With the draw of each chopstick what is the probability that a match is made?

\[\eqalign{Draw&&Permutation&&Sum&\quad Acc &Sum&&10000\\ 1& & \frac{0}{10} &=& 0.000 && 0.000\\ 2& & \frac{10\times 1}{10\times9} &=& 0.111&& 0.111 &&1126\\ 3 & & \frac{10\times 8\times 2}{10\times9\times 8} &=& 0.222&& 0.333& \\ 4 & & \frac{10\times 8\times 6\times3}{10\times9\times 8\times7} &=& 0.286 && 0.508\\ 5 & & \frac{10\times 8\times 6\times4\times 4}{10\times9\times 8\times7\times 6} &=& 0.254&& 0.762\\ 6 & & \frac{10\times 8\times 6\times4\times 2\times 5}{10\times9\times 8\times7\times 6\times 5} &=& 0.127&&1.000\\}\]

Experiment: 10 chopsticks (5prs) selected to make 1 pair

How many chopsticks do you need to pick before you definitely have a matching pair?

\[\tiny \matrix{ Sticks & Unpaired&&Calc & Not & Single & Doble & Triple \\ Picked & Permut& &Fraction & Paired & Pairs & Pairs & Pairs \\ 0 &\frac{10}{10}&=&1.000 &10000\pm 0.0 & 0& 0 & 0 \\ 1 &\frac{10}{10}&=&1.000 &10000\pm 0.0 &0 & 0 & 0 \\ 2 &\frac{10\cdot8}{10\cdot9}&=&0.889 &8890.8\pm24.7&1109.2\pm24.7& 0 & 0 \\ 3 &\frac{10\cdot8\cdot6}{10\cdot9\cdot8}&=&0.667 &6644.0\pm51.1&3358.0\pm51.1 & 0 & 0 \\ 4 &\frac{10\cdot8\cdot6\cdot4}{10\cdot9\cdot8\cdot7}&=&0.381 &3810.6\pm74.6 & 5718.2\pm66.6 &471.2\pm 26.3 & 0 \\ 5 &\frac{10\cdot8\cdot6\cdot4\cdot2}{10\cdot9\cdot8\cdot7\cdot6}&=&0.127 &1292.2\pm 18.3 & 6311.0\pm 65.3 & 2396.9\pm67.1 & 0 \\ 6 &\frac{10\cdot8\cdot6\cdot4\cdot2\cdot0}{10\cdot9\cdot8\cdot7\cdot6\cdot5}&=&0 & 0\pm 0& 3812\pm 51.9 & 5692.6\pm 28.7 & 495.4\pm 32.8\\ }\]

p0 p1 p2 p3 p4 paired unpaired NA
10000 0 0 0 0 0 10000 0
8868 1132 0 0 0 1132 8868 0
6620 3380 0 0 0 3380 6620 0
3772 5761 467 0 0 6228 3772 0
1178 6466 2356 0 0 8822 1178 0
0 3783 5733 484 0 10000 0 0

Simulation of 10000 tries

Manhattan Distances

Manhattan Distances

\[\eqalign{ R&=& |x_2 - x_1|&=&4&&\#\ Hortizonal\ Moves\\ U &=& |y_2 - y_1|&=&2&&\#\ Vertical\ Moves\\ L&=& R + U &=& 6 &&\#\ Moves\ in\ the\ Shortest\ path\\ }\]

Number of Paths

\[N= {R+U\choose U} = {R + U\choose R} = \frac{6!}{4!2!} = \frac{6\cdot5}{2!}=15\]

4: Graphs

Structures

  • linked nodes: A list of neighbors
  a = Node.new(0)
  b = Node.new(72)
  c = Node.new(144)
  d = Node.new(216)
  e = Node.new(288)
  f = Node.new(nil)

  a.neighbors([b,c,d,e,f])
  b.neighbors([a,c,d,e])
  c.neighbors([a,b,d,e])
  d.neighbors([a,b,c,e])
  e.neighbors([a,b,c,d])
  f.neighbors([a])
  • Connectivity Matrix:

    a matrix to map connections to the next

\[\matrix{&\tiny a &\tiny b&\tiny c &\tiny d&\tiny e &\tiny f\\ \tiny a &0&1&1&1&1&1\\ \tiny b &1&0&1&1&1&0\\ \tiny c &1&1&0&1&1&0\\ \tiny d &1&1&1&0&1&0\\ \tiny e &1&1&1&1&0&0\\ \tiny f &1&0&0&0&0&0\\ }\]

  • Connectivity matrix records number of steps needed
     [,1] [,2] [,3] [,4]
[1,]    0    1    1    1
[2,]    1    0    1    0
[3,]    1    1    0    1
[4,]    1    0    1    0
     [,1] [,2] [,3] [,4]
[1,]    3    1    2    1
[2,]    1    2    1    2
[3,]    2    1    3    1
[4,]    1    2    1    2

Unweighted Undirected Graph

Weighted Undirected Graph

Directed Graph

Weighted Directed Graph

Chromatic number of a graph

Assign colors to vertices ensuring no two connected vertices share the same color—a concept called proper coloring.

  • Start by coloring the vertex with the highest degree.

  • Continue coloring other vertices, using the fewest colors possible without violating the adjacency rule.

  • The chromatic number is the total number of unique colors used once all vertices are colored properly.

Some notes about chromatic numbers

  • The chromatic number of a wheel graph is 3 if the number of vertices is even, and 4 if the number of vertices is odd.

  • The chromatic number of a star graph is 2

Euler’s test for planarity

Planar if:

\[v-e+f = 2\] *

\[\forall v\ge 3:\left\{\begin{matrix} e\le 3v-6 &\qquad planar\\ e\gt 3 v-6 &\qquad nonplanar\\ \end{matrix}\right.\]

\[\begin{matrix} shape & v & e & f & eulers & Modified\\ butterfly & 5 & 6 & 3 & 2 & 6\lt9\\ K_4 & 4 & 6 & 4 & 2 & 6 = 6\\ K_5 & 5 & 10 & 6& 1 &10 \gt 9\\ K3,3 & 6 & 9 & 6 &3 &9 \gt 12\\ \end{matrix}\]

Node

  • A container object that includes
  • Data structure can be either
    • scalar value:
    • attributes of a record
    • links to data container
  • Dynamic links to other nodes
    • Forward links (going deeper into the structure)
    • Back links (backing out of a structure)

Ruby code

class Node
   attr_accessors :value, :leftnode; :rightnode

   def initialize(value,left,right)
      @value = value
      @leftnode = left
      @rightnode = right
   end
   
   def to_s
      @value.to_s
   end
end

6: Trees

  • Root- (only one)
  • Each node has branches (subtrees)
  • By convention small branch on the left

Inserting a new node

def add_node(root, value)
  if root.nil?
    return Node.new(value)
  end

  if value < root.value
    root.left = add_node(root.left, value)
  else
    root.right = add_node(root.right, value)
  end

  root
end

Printing a binary tree

def print_tree(node)
  return if node.nil?

  print_tree(node.left)
  puts node.value
  print_tree(node.right)
end

7: Regular Expressions and State Machines

Basic operators

Objects

. (dot): Matches any single character except newline.

[] : Matches any one character inside the brackets; can specify ranges like [a-z].

| : Logical OR operator, matches either the expression before or after the |.

() : Groups expressions and captures the matched portion.

\\ : Escape character to treat special characters as literals or introduce special sequences.

Position

^ : Matches the beginning of a string.

$ : Matches the end of a string.

Recurrence

* : Matches zero or more occurrences of the preceding element (greedy).

+ : Matches one or more occurrences of the preceding element (greedy).

? : Matches zero or one occurrence of the preceding element (makes quantifiers lazy when used after them).

{n} : Matches exactly n occurrences of the preceding element.

{n,} : Matches n or more occurrences of the preceding element.

{n,m} : Matches between n and m occurrences of the preceding element.

Simulator for regular expressions

https://rubular.com/

     [,1] [,2] [,3] [,4]
[1,]    1    0    1    0
[2,]    0    1    0    1
[3,]    0    1    0    1
[4,]    1    0    1    1