Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Unit-1: Basics of Algorithms: and Mathematics

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 41

Analysis and Design of Algorithms (ADA)

GTU # 3150703

Unit-1:
Basics of Algorithms
and Mathematics

Dr. Gopi Sanghani


Computer Engineering Department
Darshan Institute of Engineering & Technology, Rajkot
gopi.sanghani@darshan.ac.in
9825621471
 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.

#3150703 (ADA)  Unit 1 – Basics of Algorithms and


Dr. Gopi Sanghani 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
Dr. Gopi Sanghani 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


Dr. Gopi Sanghani 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


Dr. Gopi Sanghani 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


Dr. Gopi Sanghani 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 any
fractions. 122 9872
v. Double the number in the right hand column by adding it to
61 19744 19744
itself.
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
#3150703 (ADA)  Unit 1 – Basics of Algorithms and
Dr. Gopi Sanghani 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, shift the Multiply Shift Result
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, shift the (09) * (34) 2 3 0 6 . .
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 (81) * (34) 0 2 7 5 4
is not shifted at all.

1210554
#3150703 (ADA)  Unit 1 – Basics of Algorithms and
Dr. Gopi Sanghani 10
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
   

#3150703 (ADA)  Unit 1 – Basics of Algorithms and


Dr. Gopi Sanghani 12
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


Dr. Gopi Sanghani 13
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=C

𝑩  ⊂ 𝑨   ⊆𝑨
𝑪

#3150703 (ADA)  Unit 1 – Basics of Algorithms and


Dr. Gopi Sanghani 14
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


Dr. Gopi Sanghani 15
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
Dr. Gopi Sanghani 16
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


Dr. Gopi Sanghani 17
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


Dr. Gopi Sanghani 18
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


Dr. Gopi Sanghani 19
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


Dr. Gopi Sanghani 20
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


Dr. Gopi Sanghani 21
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
Dr. Gopi Sanghani 22
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


Dr. Gopi Sanghani 23
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


Dr. Gopi Sanghani 24
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


Dr. Gopi Sanghani 25
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
  and  and
So,
So,

#3150703 (ADA)  Unit 1 – Basics of Algorithms and


Dr. Gopi Sanghani 26
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


Dr. Gopi Sanghani 27
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


Dr. Gopi Sanghani 28
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 Codomain

#3150703 (ADA)  Unit 1 – Basics of Algorithms and


Dr. Gopi Sanghani 29
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)
#3150703 (ADA)  Unit 1 – Basics of Algorithms and
Dr. Gopi Sanghani 30
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
,
where and
 𝑨 𝑩 
 -

 -  𝟏
 𝟏  𝟒
 𝟐
 𝟗
 Range of function  𝟑

 𝟒 𝟏𝟔
 

#3150703 (ADA)  Unit 1 – Basics of Algorithms and


Dr. Gopi Sanghani 31
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


Dr. Gopi Sanghani 32
Functions Types
  If function is both one-to-one and onto then the function is called Bijection function.
 Example:
function
where and

3
 𝑨 𝐁
 

4
5 1 2
6 2 3
3 4
4 5

5 6

#3150703 (ADA)  Unit 1 – Basics of Algorithms and


Dr. Gopi Sanghani 33
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


Dr. Gopi Sanghani 34
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


Dr. Gopi Sanghani 35
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


Dr. Gopi Sanghani 36
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


Dr. Gopi Sanghani 37
Logical Connectives
  Conjunction (): The logical   Disjunction (): 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: : John studies.
: It is raining :John does NOT
: It is cold study.
: It is raining AND it is cold  Truth table
 Truth table  Truth table
= = V
 𝑇𝑟𝑢𝑒  𝑇𝑟𝑢𝑒 𝑇𝑟𝑢𝑒
  𝐹𝑎𝑙𝑠𝑒
 
 𝐹𝑎𝑙𝑠𝑒  𝑇𝑟𝑢𝑒 𝐹𝑎𝑙𝑠𝑒
  𝑇𝑟𝑢𝑒
 
 𝐹𝑎𝑙𝑠𝑒  𝑇𝑟𝑢𝑒
 𝐹𝑎𝑙𝑠𝑒  𝐹𝑎𝑙𝑠𝑒

#3150703 (ADA)  Unit 1 – Basics of Algorithms and


Dr. Gopi Sanghani 38
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


Dr. Gopi Sanghani 39
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


Dr. Gopi Sanghani 40
Thank You!

You might also like