Unit-1 Basic Algorithm
Unit-1 Basic Algorithm
Algorithms(ADA)
GTU # 3150703
Unit-1
Basics of Algorithms and
Mathematics
Rupesh Vaishnav
Computer Engineering Department
Darshan Institute of Engineering & Technology, Rajkot
[email protected]
9428037452
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.
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.
981 981
1 23 4 1 23 4
3924 981
2943 1962
1962 2943
981 3924
1210554 1210554
1210554
#3150703 (ADA) Unit 1 – Basics of Algorithms and
Rupesh Vaishnav 9
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 108. . . .
iii. Multiply right half of the multiplicand by left half of the multiplier, (09) * (34) 2 306. .
shift the result by half the number of digits of multiplier i.e. 2.
(81) * (12) 2 972. .
iv. Multiply right half of the multiplicand by right half of the (81) * (34) 0 2754
multiplier the result is not shifted at all.
1210554
#3150703 (ADA) Unit 1 – Basics of Algorithms and
Rupesh Vaishnav 10
Exercises
Multiply following values using divide and conquer method.
1. 4567×6543
2. 31415975×8182818
Roster Set-builder
Notation Notation
Example 1 Example 2
Example 1 Example 2
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 𝐴.
Example 1 Example 2
If and If and
Then set is a proper subset of . Then set C is a subset of , but it is not a
proper subset of since .
A A=
B C
𝑩⊂ 𝑨 𝑪⊆ 𝑨
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
2. The cardinality of infinite set denoted as
3. The empty set denoted as is the unique set whose cardinality is .
A
’
A
U
#3150703 (ADA) Unit 1 – Basics of Algorithms and
Rupesh Vaishnav 19
Set Operations
Union: The union of two different sets and is the set of all distinct elements of sets and .
A B
A B
𝑨– 𝑩={𝒙∨𝒙∈ 𝑨𝒂𝒏𝒅𝒙∉ 𝑩}
Example:
Consider and
Then
A B
𝑨– 𝑩={𝒙∨𝒙∈ 𝑨𝒂𝒏𝒅𝒙∉ 𝑩}
Example:
Consider, and
Then
A B
𝑨× 𝑩={(𝒂,𝒃)∨𝒂∈ 𝑨𝒂𝒏𝒅𝒃∈ 𝑩}
Example:
(1 , 𝑥 ) (1 , 𝑦 )
(2 , 𝑥) (2 , 𝑦 )
(3 , 𝑥) (3 , 𝑦)
𝐴× 𝐵
#3150703 (ADA) Unit 1 – Basics of Algorithms and
Rupesh Vaishnav 25
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.
Codomain
Example:
2
Domain 1 3
4
2 5
6
3 7
4 8
9
10
The Range of the function
Domain Codomai
n
#3150703 (ADA) Unit 1 – Basics of Algorithms and
Rupesh Vaishnav 32
Relation & Function
Relation 1 Relation 2
Is not a function since elements of Is a function since elements of
domain point to multiple elements of domain point to only one element of
codomain. codomain.
Ana
Mit
Sam
CX Yug Ana CX
CY Jen Yug CY
CZ Tom Ra CZ
Ra m
m
Mit
Neel
Division Students Students Division
(Domain) (Codomain) (Domain) (Codomain)
#3150703 (ADA) Unit 1 – Basics of Algorithms and
Rupesh Vaishnav 33
Functions Types
If the range of function and codomain of function are equal then the function is said to be
onto or surjective or surjection.
Codomai
Example: n
,
where and
𝑨 𝑩
-
- 𝟏
𝟏 𝟒
𝟐
𝟗
Range of function 𝟑
𝟒 𝟏𝟔
#3150703 (ADA) Unit 1 – Basics of Algorithms and
Rupesh Vaishnav 34
Functions Types
A function is injective or one-to-one if there do not exist two distinct andsuch that
Example:
The function , is a one-to-one function,
where and
𝟏 𝟐
𝟐 𝟑
𝟑 𝟒
𝟒 𝟓
𝑨 𝑩
#3150703 (ADA) Unit 1 – Basics of Algorithms and
Rupesh Vaishnav 35
Functions Types
If function is both one-to-one and onto then the function is called Bijection function.
Example:
function
where and
𝑨 𝐁
1 1
2 4
3 9
4 16
𝒃
𝒂𝒙=𝒃 Solution 𝒙=
𝒂
The solution of above can be obtained by the elimination process, whereby reduce the system to a single
equation in only one unknown.
In order to prove that a universal quantification is false, it must be shown to be false for only
ONE case.
In order to prove that a universal quantification is true, it must be shown true for ALL cases.