G5 Numerics Part II
G5 Numerics Part II
Numerics II
A Deeper Dive into CP Math
2
Lecture Objectives
● Understanding the principles of hashing, its applications, and
how hash collisions occur and are managed.
● Exploring advanced concepts in modular arithmetic in greater
depth.
● Developing combinatorial reasoning skills for solving problems
related to arrangements, selections, and distributions.
● Understanding Extended Euclidean Algorithm.
3
Lecture Outline
● Prerequisites
● Hashing and Hash Collisions
● Modular Arithmetic
● Combinatorics and Probability
● Extended Euclidean Algorithm
● Quote of the Day
4
Prerequisites
● Math I
○ Basic Understanding of Modular Arithmetic
○ Euclidean Algorithm
● Bit Manipulation
● Time and Space Complexity Analysis
5
Hashing
6
What is Hashing?
● Examples:
○ Password Storage
○ Python’s built-in hash function
Hashing
Hash Collision
hash(x1) = hash(x2)
Hash Collision
Modular Hashing
● Modular Hashing is mapping a key k into one of m slots by taking
the remainder of k divided by m.
hash(k) = k % m
Modular Arithmetic
13
Modular Division
Modular Division
Proof
Implementation of Euler’s totient function
20
Binary Exponentiation
21
Binary Exponentiation
Binary Exponentiation
Binary Exponentiation
Binary Exponentiation
● Implementation Steps
Binary Exponentiation
● Implementation Steps
class ModularArithmetic:
def add(self, a, b, p):
return ((a % p) + (b % p)) % p
Combinatorics
“Combinatorics is the art of counting without counting." - Andrzej Schinzel
30
● Example:
○ Suppose you have 6 roads and 3 railways from Addis
Ababa to Berland. How many possible ways do you
have to go Berland?
○ Number of ways = 6 roads + 3 railways = 9.
32
● Example:
34
The substring should start from ‘d’, ‘b’, ‘a’ inorder to include
from ‘d’.
39
• In general,
(i + 1) * (n - i)
Permutations
47
Permutation
Permutation
n! = n * (n - 1) * (n - 2) * … * (1)
n! = n * (n - 1) !
1! = 1
49
Permutation
P(n, r) = n! / (n - r)!
50
Permutation
Answer
3! * 2! * 1! ways
Therefore: 6! = x * 3! * 2!
=> x = 6! / (3! * 2!)
53
Permutation
If we need to arrange n objects where n1, n2, n3 .. nr are
Combinations
55
Combinations
matter.
56
Combinations
Combinations
• Every group of 3 will be counted 3! = 6 times. Example: ABC
■ ABC ■ BCA
■ ACB ■ CAB
■ BAC ■ CBA
58
Combinations
Combinations
• The total number of groups that can be formed is:
60
Combinations - Formula
61
for (right).
● Path: R R R R R R U U U U
● That is, go all the way right (6 R’S) then all the way
up (4 U’s)
63
RRRRUUUURR
64
Combinations - Approach
Path: R R R R R R R R R R
But Wait!
● We need to remove the redundancies:
finally we get:
Probability
70
Probability
● Sample space:
• Denoted by S.
Probability
● Event Space:
⚬ Denoted by E.
Probability
Rules of Probability
• Rule 1: The probability P(A) of any event A satisfies 0 <= P(A) <= 1
Probability - Exercise
Conditional Probability
event will occur given the knowledge that an event B has already
Conditional Probability
Extended Euclidean
Algorithm
78
Practice Problems
References
CP-Algorithms
CP-Algorithms
81