0% found this document useful (0 votes)
7 views

Unit1 2

Uploaded by

Bharti Sahu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Unit1 2

Uploaded by

Bharti Sahu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

Analysis and Design of Algorithms(ADA)

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.

Input Process Output


Ingredients Recipe 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

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.
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

1. American approach 2. English approach

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.

Roster Notation Set-builder Notation


Example 1 Example 2
Set A = 11, 12, 21, 22 Set C = x x is an odd integer greater than 1}
Set B = 5, 10, 15, 20, 25 Set D = {x | x ∈ C and x ≤ 11}
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
A = 𝑥 𝑥 ∈ Z and 𝑥 2 − 81 = 0} B = 𝑥 𝑥 is divisible by 2}
A = −9,9 B = {2,4,6, … , }

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 𝐴.
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)}

Reflexive Not Reflexive since (𝟑, 𝟑) ∉ 𝑹


Properties of the Relation
 Symmetric: A relation 𝑅 on a set 𝐴 is called symmetric if (𝑦, 𝑥) ∈ 𝑅 whenever (𝑥, 𝑦) ∈ 𝑅, for
some 𝑥, 𝑦 ∈ 𝐴.

∀𝒙: ∀𝒚: [((𝒙, 𝒚) ∈ 𝑹) → ((𝒚, 𝒙) ∈ 𝑹)]

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 𝑥, 𝑦, 𝑧 ∈ 𝐴.

∀𝒙: ∀𝒚: ∀𝒛[([(𝒙, 𝒚) ∈ 𝑹] ∧ [(𝒚, 𝒛) ∈ 𝑹]) → ((𝒙, 𝒛) ∈ 𝑹)]

Transitive Not Transitive


Example 1 Example 2
A = { 1, 2, 3} and R1 = {(a, b) | a ≤ b} B = 1, 2, 3,4 and
So, R1 = {(1,1), (1,2), (1,3), (2,2), (2,3), (3,3)} R2 = a, b | 𝑤ℎ𝑒𝑟𝑒 𝑏 𝑖𝑠 𝑎 𝑠𝑢𝑐𝑐𝑒𝑠𝑠𝑜𝑟 𝑜𝑓 𝑎
So, R2 = { 1,2 , 2,3 , (3,4)}
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 𝑥 = 𝑧.
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 grows 𝟐𝟎 cm 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.
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:
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

Division Students Students Division


(Domain) (Codomain) (Domain) (Codomain)
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.
 Example: Codomain
𝑓: 𝐴 → 𝐵, 𝑓 𝑥 = 𝑥2
where 𝐴 = {−2, −1,1,2,3,4} and 𝐵 = {1,4,9,16}
𝑓 −2 = 4,
𝑨 𝑩
𝑓 −1 = 1,
𝑓 1 = 1, -𝟐
𝑓 2 = 4,
-𝟏 𝟏
𝑓 3 = 9,
𝑓(4) = 16 𝟏 𝟒
 Range of function 𝑓(𝑥) = {1, 4, 9, 16} = 𝑩 𝟐
𝟗
𝟑
𝟏𝟔
𝟒
Functions Types
 A function 𝑓: 𝐴 → 𝐵 is injective or one-to-one if there do not exist two distinct 𝑥1 and 𝑥2 such
that 𝑓(𝑥1) = 𝑓(𝑥2).
 Example:
The function 𝑓: 𝐴 → 𝐵, 𝑓(𝑥) = 𝑥 + 1 is a one-to-one function,
where 𝐴 = {1, 2, 3, 4} and 𝐵 = {2, 3, 4, 5}

𝟏 𝟐
𝟐 𝟑
𝟑 𝟒

𝟒 𝟓

𝑨 𝑩
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 𝒙=
𝒂

 Two Equations with Two Unknowns


 A system of two linear equations in the two unknowns 𝑥 and 𝑦 is
𝑎1 𝑥 + 𝑏1 𝑦 = 𝑐1
𝑎2 𝑥 + 𝑏2 𝑦 = 𝑐2
 The solution of above can be obtained by the elimination process, whereby reduce the system to a single
equation in only one unknown.
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. p : Fourteen is an even integer.
2. q : Mumbai is the capital city of India.
3. r:0=0
 Following statements are not propositions.
1. Close the door.
2. Where are you?
3. x is greater than 5.
Logical Connectives
 Conjunction ( Ʌ ): The logical  Disjunction (V): The logical  Negation (): 𝑝 , the
connective Conjunction (logical disjunction, or logical OR, is negation of a proposition
AND) is true only when both of true if one or both of the 𝑝, is also a proposition.
the propositions are true. propositions are true.
 Example:
 Example:  Example: p : John studies.
𝑝 : It is raining p∶ 2+2=5
q∶ 1<2  p : John does NOT
𝑞 : It is cold study.
r ∶ 2 + 2 = 5 𝐎𝐑 1 < 2
𝑟 : It is raining AND it is cold  Truth table
 Truth table
 Truth table
𝒑 𝒒 𝒓=𝒑Ʌ𝒒 𝑝 𝑞 𝑟=𝑝V𝑞 𝑝 𝑝
𝑇𝑟𝑢𝑒 𝑇𝑟𝑢𝑒 𝑇𝑟𝑢𝑒 𝑇𝑟𝑢𝑒 𝑇𝑟𝑢𝑒 𝑇𝑟𝑢𝑒 𝑇𝑟𝑢𝑒 𝐹𝑎𝑙𝑠𝑒
𝑇𝑟𝑢𝑒 𝐹𝑎𝑙𝑠𝑒 𝐹𝑎𝑙𝑠𝑒 𝑇𝑟𝑢𝑒 𝐹𝑎𝑙𝑠𝑒 𝑇𝑟𝑢𝑒 𝐹𝑎𝑙𝑠𝑒 𝑇𝑟𝑢𝑒
𝐹𝑎𝑙𝑠𝑒 𝑇𝑟𝑢𝑒 𝐹𝑎𝑙𝑠𝑒 𝐹𝑎𝑙𝑠𝑒 𝑇𝑟𝑢𝑒 𝑇𝑟𝑢𝑒
𝐹𝑎𝑙𝑠𝑒 𝐹𝑎𝑙𝑠𝑒 𝐹𝑎𝑙𝑠𝑒 𝐹𝑎𝑙𝑠𝑒 𝐹𝑎𝑙𝑠𝑒 𝐹𝑎𝑙𝑠𝑒
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:
 ∀x ∶ P x , for all values of x, P(x) is true
∀x ∶ P(x) = x + 1 > x
 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.
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 𝑃(𝑥) = 𝑥/2 < 𝑥
There exists a numerical value for which 𝑥/2 < 𝑥 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.
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.

You might also like