Unit1 2
Unit1 2
Unit-1
Basics of Algorithms and
Mathematics
Outline
Looping
▪ Introduction to Algorithm
• Definition
• Characteristics
• Types
• Simple Multiplication Methods
▪ Mathematics for Algorithmic Sets
• Set Theory
• Functions and Relations
• Vectors and Matrices
• Linear Inequalities and Linear Equations
• Logic and Quantifiers
Introduction to Algorithm
What is an Algorithm?
A step-by-step procedure, to solve the different kinds of problems.
Suppose, we want to make a Chocolate Cake.
An unambiguous sequence of computational steps that transform the input into the output.
What is an Algorithm?
A process or a set of rules to be followed to achieve desired output, especially by a computer.
Input
An algorithm is any well-defined computational procedure that takes some value, or a set of
values as input and produces some value, or a set of values as output.
Characteristics of An Algorithm
Finiteness: An algorithm must always terminate after a finite number of steps.
Definiteness: Each step of an algorithm must be precisely defined.
Input: An algorithm has zero or more inputs.
Output: An algorithm must have at least one desirable output.
Effectiveness: All the operations to be performed in the algorithm must be sufficiently basic so
that they can, in principle be done exactly and in a finite length of time.
Types of Algorithm
Simple recursive algorithms
Backtracking algorithms
Divide and conquer algorithms
Dynamic programming algorithms
Greedy algorithms
Branch and bound algorithms
Brute force algorithms
Randomized algorithms
Simple Multiplication Methods
981 981
1 23 4 1 23 4
3924 981
2943 1962
1962 2943
981 3924
1210554 1210554
Simple Multiplication Methods
3. à 𝒍𝒂 𝒓𝒖𝒔𝒔𝒆 multiplication
i. Write the multiplicand and multiplier side by side. 981 1234 1234
ii. Make two columns, one under each operand. 490 2468
iii. Repeat step iv and v until the number in the left column is 1.
245 4936 4936
iv. Divide the number in the left hand column by 2, ignoring any
fractions. 122 9872
v. Double the number in the right hand column by adding it to 61 19744
itself. 19744
vi. Next cross out each row where the number in the left hand 30 39488
column is even.
15 78976 78976
vii. Finally add up the numbers that remain in the right hand
column. 7 157952 157952
3 315904 315904
1 631808 631808
1210554
Simple Multiplication Methods
4. Multiplication by divide and conquer
Both the multiplicand and the multiplier must have the same number of digits and this
number be a power of 2. If not then it can be done by adding zeros on the left if
necessary.
Multiplicand 0981
i. Multiply left half of the multiplicand by left half of multiplier and
shift the result by no. of digits of multiplier i.e. 4. Multiplier 1234
ii. Multiply left half of the multiplicand by right half of the multiplier, Multiply Shift Result
shift the result by half the number of digits of multiplier i.e. 2.
(09) * (12) 4 10 8 . . . .
iii. Multiply right half of the multiplicand by left half of the multiplier, (09) * (34) 2 3 0 6 . .
shift the result by half the number of digits of multiplier i.e. 2.
(81) * (12) 2 9 7 2 . .
iv. Multiply right half of the multiplicand by right half of the multiplier
the result is not shifted at all. (81) * (34) 0 2 7 5 4
1210554
Exercises
Multiply following values using divide and conquer method.
1. 4567×6543
2. 31415975×8182818
Mathematics for Algorithmic Sets
Set Theory
A set is an unordered collection of distinct objects.
The objects in a set are called elements or members of the set.
Example 1 Example 2
A = 𝑥 𝑥 ∈ Z and 𝑥 2 − 81 = 0} B = 𝑥 𝑥 is divisible by 2}
A = −9,9 B = {2,4,6, … , }
Subset: For two sets 𝐴 and 𝐶, we say that 𝐶 is a subset of 𝐴, written as 𝐶⊆𝐴, if each member
of set 𝐶 is also a member of set 𝐴.
Set Theory
Proper Subset: A proper subset of a set 𝐴 is a subset of 𝐴 that is not equal to 𝐴.
Example 1 Example 2
If 𝐴 = {1,3,5} and 𝐵 = 1,5 If 𝐴 = {1,3,5} and 𝐶 = 1,3,5
Then set 𝐵 is a proper subset of 𝐴. Then set C is a subset of 𝐴, but it is not
a proper subset of 𝐴 since 𝐶 = 𝐴.
A
B A=C
𝑩⊂𝑨 𝑪⊆𝑨
Set Theory
Power Set: Let 𝐴 be the set. The power set of 𝐴, written as 𝑃(𝐴), is the set of all subsets of 𝐴.
Example:
A = {0, 1} then the power set of A is {{}, {0}, {1}, {0, 1}}
Cardinality of set: The cardinality of a set denotes the number of elements in a set. The
cardinality of a set 𝑆 is denoted by 𝑛(𝑆) or |𝑆|.
Examples:
1. If 𝑆 is a set of English alphabets the 𝑛(𝑆) = |𝑆| = 26
2. The cardinality of infinite set 𝑋 denoted as 𝑋 = ∞
3. The empty set denoted as ∅ is the unique set whose cardinality is 𝟎.
Set Theory
Complement: The complement of a set 𝐴 is the set 𝐴’ that contains every element of the
Universal set U but not in A.
𝐴′ = {𝑥 | 𝑥 ∈𝑈 𝑎𝑛𝑑 𝑥∉𝐴}
Example:
Consider 𝑈 = {1, 3, 5, 7, 9} and 𝐴 = 1, 5
Then 𝐴′ = {3, 7, 9}
A’
A
U
Set Operations
Union: The union of two different sets 𝐴 and 𝐵 is the set of all distinct elements of sets 𝐴 and
𝐵.
𝑨 ∪ 𝑩 = {𝒙 | 𝒙 ∈ 𝑨 𝒐𝒓 𝒙 ∈ 𝑩}
Example:
Consider 𝐴 = {1, 3, 5, 7, 9} and 𝐵 = {1, 2, 3, 4, 5}
Then 𝐴 ∪ 𝐵 = {1, 2, 3, 4, 5, 7, 9}
A B
Set Operations
Intersection: The intersection of two sets 𝐴 and 𝐵 is the set that contains all elements of 𝐴
that also belong to 𝐵 but no other elements.
𝑨 ∩ 𝑩 = {𝒙 | 𝒙 ∈ 𝑨 𝒂𝒏𝒅 𝒙 ∈ 𝑩}
Example:
Consider 𝐴 = {1, 3, 5, 7, 9} and 𝐵 = {1, 2, 3, 4, 5}
Then 𝐴 ∩ 𝐵 = {1, 3, 5}
A B
Set Operations
Set Difference: The set difference 𝐴 − 𝐵 of two sets 𝐴 and 𝐵 is the set of elements that are
in 𝑨 but not in 𝑩.
𝑨 – 𝑩 = {𝒙 | 𝒙 ∈ 𝑨 𝒂𝒏𝒅 𝒙 ∉ 𝑩}
Example:
Consider 𝐴 = {1, 3, 5, 7, 9} and 𝐵 = {1, 2, 3, 4, 5}
Then 𝐴 − 𝐵 = {7, 9}
A B
Set Operations
Symmetric Difference: The symmetric difference 𝐴 ⊖ 𝐵 of two sets 𝐴 and 𝐵 is the elements
that are in 𝑨 but not in 𝑩 and the elements that are in 𝑩 but not in 𝑨.
𝑨 – 𝑩 = {𝒙 | 𝒙 ∈ 𝑨 𝒂𝒏𝒅 𝒙 ∉ 𝑩}
Example:
Consider, 𝐴 = {1, 3, 5, 7, 9} and 𝐵 = {1, 2, 3, 4, 5}
Then 𝐴 ⊖ 𝐵 = {7, 9, 2, 4}
A B
Set Operations
Sequences: A sequence of objects is a list of objects in some order.
Example: the sequence 7, 21, 57 would be written as (7, 21, 57)
In a set the order does not matter but in a sequence it does.
Repetition is not permitted in a set but repetition is permitted in a sequence. So, (7, 7, 21, 57) is
different from (7, 21, 57).
Tuples: Finite sequences are called tuples.
Examples:
1. (7, 21) 2-tuple or pair
2. (7, 21, 57) 3-tuple
3. (7, 21, ..., k ) k-tuple
Set Operations
Cartesian Product: The Cartesian product 𝐴 × 𝐵 of two sets 𝐴 and 𝐵 is the set of all ordered
pairs (𝒂, 𝒃) where 𝑎 ∈ 𝐴 and 𝑏 ∈ 𝐵.
𝑨 × 𝑩 = {(𝒂, 𝒃) | 𝒂 ∈ 𝑨 𝒂𝒏𝒅 𝒃 ∈ 𝑩}
Example:
𝐴
{1,2,3} (1, 𝑥) (1, 𝑦)
(2, 𝑥) (2, 𝑦)
(3, 𝑥) (3, 𝑦)
𝐵
{𝑥, 𝑦}
𝐴×𝐵
Relation
Let 𝐴 and 𝐵 be two sets. Any subset 𝑹 of their Cartesian product 𝐴 × 𝐵 is a relation.
A relation defines the relationship between values of sets.
It is defined between the x-values and y-values of the ordered pairs.
The set of all x-values is called the domain, and the set of all y-values is called the range.
Properties of the Relation
Reflexive: Let 𝐴 be a set, and let 𝑅 be a binary relation on 𝐴. Relation 𝑅 is reflexive if,
Example 1 Example 2
A = {1, 2} and R1 = {(a, b) | a ≤ b} B = 1,2,3 , and
so, R1 = 1,1 , 1,2 , 2,2 R2 = {(1,1), (1,2), (2,1), (2,2), (3,1)}
Symmetric Asymmetric
Example 1 Example 2
A = {1,2,3} and R1 = {(a, b)|a ≠ b} B = { 1, 2, 3} and R2 = {(a, b) | a ≤ b}
R1 = {(1,2), (1,3), (2,1), (2,3), (3,1), (3,2)} So, R2 = {(1,1), (1,2), (1,3), (2,2), (2,3), (3,3)}
Properties of the Relation
Transitive: A relation 𝑅 on a set 𝐴, is called transitive if whenever (𝑥, 𝑦) ∈ 𝑅 and (𝑦, 𝑧) ∈ 𝑅,
then (𝑥, 𝑧) ∈ 𝑅, for 𝑥, 𝑦, 𝑧 ∈ 𝐴.
Codomain
Example:
1
𝑓: 𝐴→𝐵, 𝑓(𝑥) = 2𝑥 + 1 2
Domain 𝑓(1) = 2(1) + 1 = 3 1 3
4
𝑓(2) = 2(2) + 1 = 5 2 5
𝑓 3 =2 3 +1=7 6
3
7
𝑓(4) = 2(4) + 1 = 9
4 8
The Range of the function 𝑓 𝑥 = 3, 5, 7, 9 9
10
Domain Codomain
Relation & Function
Relation 1 Relation 2
Is not a function since elements of Is a function since elements of domain
domain point to multiple elements of point to only one element of codomain.
codomain.
Ana
Mit
Sam
CX Yug Ana CX
CY Jen Yug CY
CZ Tom Ram CZ
Ram Mit
Neel
𝟏 𝟐
𝟐 𝟑
𝟑 𝟒
𝟒 𝟓
𝑨 𝑩
Functions Types
If function is both one-to-one and onto then the function is called Bijection function.
Example:
function 𝑓 ∶ 𝐴 → 𝐵, 𝑓(𝑥) = 𝑥 2
where 𝐴 = {1,2,3,4} and 𝐵 = {1,4,9,16}
𝑓(1) = 1
𝑓(2) = 4
𝑓(3) = 9 𝐁
𝑨
𝑓(4) = 16
1 1
2 4
3 9
4 16
Vectors and Matrices
A vector, 𝑢, means a list (or 𝑛-tuple) of numbers:
𝒖 = (𝒖𝟏 , 𝒖𝟐 , . . . , 𝒖𝒏 )
Where 𝑢𝑖 are called the components of 𝑢.
If all the 𝑢𝑖 are zero, then 𝑢 is called the zero vector.
Vector operations : Addition, Subtraction, Scalar Multiplication
Matrix A, means a rectangular array of numbers.
1 2 3
3x3 Matrix: 4 5 6
7 8 9
Matrix operations: Addition, Subtraction, Multiplication
Linear Inequalities
Inequalities: The term inequality is applied to any statement involving one of the symbols <, >,
≤, ≥.
Examples of inequalities are:
1. x≥1
2. x + y + 2z > 16
3. p2 + q2 ≤ 1/2
4. a2 + ab > 1
Linear Equations
Linear equation with one Unknown
𝒃
𝒂𝒙 = 𝒃 Solution 𝒙=
𝒂