Analysis and Design of
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 Process Output
Ingredients Recip Cak
e e
An unambiguous sequence of computational steps that transform the input into the output.
#3150703 (ADA) Unit 1 – Basics of Algorithms and
Rupesh Vaishnav 4
What is an Algorithm?
A process or a set of rules to be followed to achieve desired output, especially by a computer.
Input
Algorithm Program Output
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.
#3150703 (ADA) Unit 1 – Basics of Algorithms and
Rupesh Vaishnav 5
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.
#3150703 (ADA) Unit 1 – Basics of Algorithms and
Rupesh Vaishnav 6
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
#3150703 (ADA) Unit 1 – Basics of Algorithms and
Rupesh Vaishnav 7
Simple Multiplication Methods
1. American approach 2. English approach
981 981
1 23 4 1 23 4
3924 981
2943 1962
1962 2943
981 3924
1210554 1210554
#3150703 (ADA) Unit 1 – Basics of Algorithms and
Rupesh Vaishnav 8
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 122 9872
any fractions.
61 19744 19744
v. Double the number in the right hand column by adding it to
itself. 30 39488
vi. Next cross out each row where the number in the left hand
column is even. 15 78976 78976
vii. Finally add up the numbers that remain in the right hand 7 157952 157952
column.
3 315904 315904
1 631808 631808
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
#3150703 (ADA) Unit 1 – Basics of Algorithms and
Rupesh Vaishnav 11
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.
Roster Set-builder
Notation Notation
Example 1 Example 2
#3150703 (ADA) Unit 1 – Basics of Algorithms and
Rupesh Vaishnav 15
Set Theory
Finite & Infinite sets: A set is finite if it contains a finite number of elements, otherwise it is
an infinite set.
Example 1 Example 2
Set is a finite set Set is an infinite set
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 𝐴.
#3150703 (ADA) Unit 1 – Basics of Algorithms and
Rupesh Vaishnav 16
Set Theory
Proper Subset: A proper subset of a set is a subset of that is not equal to
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
𝑩⊂ 𝑨 𝑪⊆ 𝑨
#3150703 (ADA) Unit 1 – Basics of Algorithms and
Rupesh Vaishnav 17
Set Theory
Power Set: Let be the set. The power set of , written as , is the set of all subsets of .
Example:
then the power set of
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 .
#3150703 (ADA) Unit 1 – Basics of Algorithms and
Rupesh Vaishnav 18
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 and
Then
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 .
𝑨∪ 𝑩={𝒙∨𝒙∈ 𝑨𝒐𝒓 𝒙 ∈𝑩}
Example:
Consider and
Then
A B
#3150703 (ADA) Unit 1 – Basics of Algorithms and
Rupesh Vaishnav 20
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 and
Then
A B
#3150703 (ADA) Unit 1 – Basics of Algorithms and
Rupesh Vaishnav 21
Set Operations
Set Difference: The set difference of two sets and is the set of elements that are in but not
in .
𝑨– 𝑩={𝒙∨𝒙∈ 𝑨𝒂𝒏𝒅𝒙∉ 𝑩}
Example:
Consider and
Then
A B
#3150703 (ADA) Unit 1 – Basics of Algorithms and
Rupesh Vaishnav 22
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, and
Then
A B
#3150703 (ADA) Unit 1 – Basics of Algorithms and
Rupesh Vaishnav 23
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
#3150703 (ADA) Unit 1 – Basics of Algorithms and
Rupesh Vaishnav 24
Set Operations
Cartesian Product: The Cartesian product of two sets and is the set of all ordered pairs
where and .
𝑨× 𝑩={(𝒂,𝒃)∨𝒂∈ 𝑨𝒂𝒏𝒅𝒃∈ 𝑩}
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.
#3150703 (ADA) Unit 1 – Basics of Algorithms and
Rupesh Vaishnav 26
Properties of the Relation
Reflexive: Let be a set, and let be a binary relation on . Relation is reflexive if,
∀ 𝒙:[(𝒙∈ 𝑨)→((𝒙,𝒙)∈ 𝑹)]
Example 1 Example 2
and and
so,
Reflexive Not Reflexive since
#3150703 (ADA) Unit 1 – Basics of Algorithms and
Rupesh Vaishnav 27
Properties of the Relation
Symmetric: A relation on a set is called symmetric if whenever , for some .
∀ 𝒙:∀ 𝒚 :[((𝒙 ,𝒚)∈𝑹)→((𝒚 ,𝒙)∈𝑹)]
Symmetric Asymmetric
Example 1 Example 2
and and
So,
#3150703 (ADA) Unit 1 – Basics of Algorithms and
Rupesh Vaishnav 28
Properties of the Relation
Transitive: A relation on a set , is called transitive if whenever and , then , for .
∀ 𝒙:∀ 𝒚 :∀ 𝒛[([(𝒙,𝒚)∈𝑹]∧[(𝒚 ,𝒛)∈𝑹])→((𝒙,𝒛)∈𝑹)]
Not
Transitive Transitive
Example 1 Example 2
and and
So,
So,
#3150703 (ADA) Unit 1 – Basics of Algorithms and
Rupesh Vaishnav 29
Equivalence Relation
Equivalence Relation declares or shows some kind of equality or equivalence.
If the relation satisfies all three properties reflexive, symmetric and transitive then it is
called an Equivalence Relation.
Equality ‘=’ relation is the equivalence relation because equality proves the required
conditions.
1. Reflexive: is true for all values of .
2. Symmetric: and is true for all values of and .
3. Transitive: if and is true for all values then we can say that .
#3150703 (ADA) Unit 1 – Basics of Algorithms and
Rupesh Vaishnav 30
Functions
Relationship between two sets of numbers is known as a function.
Function is the special kind of relation in which there is only one output for each input.
A number in one set is mapped to number in another set by the function.
Example: this tree growscm every year, so the height of the tree is related to its age using the
function :
So, if the age is years,
then height is cm
is like saying 10 is related to 200.
Here, age is called Domain and height is called Codomain.
#3150703 (ADA) Unit 1 – Basics of Algorithms and
Rupesh Vaishnav 31
Function Notations
Domain: Values given as input to the function is called the domain of the function.
Codomain: Values that may possibly come out of a function is the codomain.
Range: Actual values that come out of a function is a 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
#3150703 (ADA) Unit 1 – Basics of Algorithms and
Rupesh Vaishnav 36
Vectors and Matrices
A vector, , means a list (or -tuple) of numbers:
Whereare 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.
3x3 Matrix
Matrix operations: Addition, Subtraction, Multiplication
#3150703 (ADA) Unit 1 – Basics of Algorithms and
Rupesh Vaishnav 37
Linear Inequalities
Inequalities: The term inequality is applied to any statement involving one of the symbols <,
>, ≤, ≥.
Examples of inequalities are:
1.
#3150703 (ADA) Unit 1 – Basics of Algorithms and
Rupesh Vaishnav 38
Linear Equations
Linear equation with one Unknown
𝒃
𝒂𝒙=𝒃 Solution 𝒙=
𝒂
Two Equations with Two Unknowns
A system of two linear equations in the two unknowns and is
The solution of above can be obtained by the elimination process, whereby reduce the system to a single
equation in only one unknown.
#3150703 (ADA) Unit 1 – Basics of Algorithms and
Rupesh Vaishnav 39
Logic
Declarative statement that is sufficiently objective, meaningful and precise to have a truth
value (true or false) is known as proposition.
Proposition examples:
1. : Fourteen is an even integer.
2. : Mumbai is the capital city of India.
3. :0=0
Following statements are not propositions.
1. Close the door.
2. Where are you?
3. is greater than .
#3150703 (ADA) Unit 1 – Basics of Algorithms and
Rupesh Vaishnav 40
Logical Connectives
Conjunction (): The logical Disjunction (V): The Negation (): , the
connective Conjunction logical disjunction, or negation of a proposition ,
(logical AND) is true only logical OR, is true if one or is also a proposition.
when both of the propositions both of the propositions are
are true. true. Example:
: John studies.
Example: Example:
: John does NOT
: It is raining study.
: It is cold Truth table
: It is raining AND it is cold
Truth table = Truth table = V
𝑇𝑟𝑢𝑒 𝑇𝑟𝑢𝑒 𝑇𝑟𝑢𝑒 𝐹𝑎𝑙𝑠𝑒
𝐹𝑎𝑙𝑠𝑒 𝑇𝑟𝑢𝑒 𝐹𝑎𝑙𝑠𝑒 𝑇𝑟𝑢𝑒
𝐹𝑎𝑙𝑠𝑒 𝑇𝑟𝑢𝑒
𝐹𝑎𝑙𝑠𝑒 𝐹𝑎𝑙𝑠𝑒
#3150703 (ADA) Unit 1 – Basics of Algorithms and
Rupesh Vaishnav 41
Logical Quantifiers
Universal Quantifier (denoted as for all): is the preposition, if gives expected result for all
values of in the universe of discourse then the universal quantification of is denoted by,
Examples:
for all values of is true
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.
#3150703 (ADA) Unit 1 – Basics of Algorithms and
Rupesh Vaishnav 42
Logical Quantifiers
Existential Quantifier (denoted as for some): is the preposition, if there exits an element in
the universe of discourse such that is giving expected result then the Existential
Quantification of is represented by,
Example:
Let
There exists a numerical value for which is true
Thus, is true
In order to show an existential quantification is true, it must be shown true for only ONE
value.
In order to show an existential quantification is false, it must be show false for ALL values.
#3150703 (ADA) Unit 1 – Basics of Algorithms and
Rupesh Vaishnav 43
GTU Questions
1. Define the term: Quantifier
2. Define the term: Algorithm
3. What is vector? Which operations are performed on vector?
4. List types of algorithms.
5. Discuss key characteristics of algorithm.
6. Explain Equivalence Relation with example.
#3150703 (ADA) Unit 1 – Basics of Algorithms and
Rupesh Vaishnav 44