Sunteți pe pagina 1din 47

2150703

Analysis and Design


of Algorithms (ADA)

Unit-1
Basics of
Algorithms and
Mathematics
Dr. Gopi Sanghani
9825621471

gopi.sanghani@darshan.ac.
in
Topics to be Covered
 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

Basics
Basics of
of Algorithms
Algorithms and
and Mathematics
Mathematics 22 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Introduction to Algorithms

3
What is an Algorithm?
 An algorithm can be broadly defined as;
• A process or a set of rules to be followed to achieve
desired output, especially by a computer.
• A step-by-step procedure, to solve the different kinds of
problems.
• An unambiguous sequence of computational steps that
transform the input into the 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.

Basics
Basics of
of Algorithms
Algorithms and
and Mathematics
Mathematics 44 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Characteristics of an Algorithm
 Precision: Each step of an algorithm must be precisely defined.
 Input: An algorithm accepts zero or more inputs.
 Output: An algorithm must generate at least one desirable output.
 Finiteness: An algorithm must always terminate after a finite
number of steps.
 Effectiveness: All the operations to be performed in the algorithm
must be essential and sufficiently basic.
 Generality: The algorithm should be expressed in a generic form
and must be applicable to a set of all possible inputs.

Basics
Basics of
of Algorithms
Algorithms and
and Mathematics
Mathematics 55 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
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

Basics
Basics of
of Algorithms
Algorithms and
and Mathematics
Mathematics 66 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
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

Basics
Basics of
of Algorithms
Algorithms and
and Mathematics
Mathematics 77 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Simple Multiplication Methods
multiplication 981 1234 1234
1. Write the multiplicand and multiplier
490 2468
side by side.
2. Make two columns, one under each 245 4936 4936
operand. 122 9872
3. Repeat step 4 and 5 until the number in
61 19744 19744
the left column is 1.
4. Divide the number in the left hand 30 39488
column by 2, ignoring any fractions. 15 78976 78976
5. Double the number in the right hand
column by adding it to itself. 7 157952 157952
6. Next cross out each row where the 3 315904 315904
number in the left hand column is even. 1 631808 631808
7. Finally add up the numbers that remain
in the right hand column. 1210554
Basics
Basics of
of Algorithms
Algorithms and
and Mathematics
Mathematics 88 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
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.

Basics
Basics of
of Algorithms
Algorithms and
and Mathematics
Mathematics 99 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Simple Multiplication Methods
4. Multiplication by divide and conquer
i. Multiply left half of the multiplicand by Multiplicand = 0 9 8 1
left half of multiplier and shift the Multiplier = 1 2 3 4
result by no. of digits of multiplier i.e.
4. Multiply Shift Result
ii. Multiply left half of the multiplicand by
right half of the multiplier, shift the
(09) * (12) 4 108. . . .
result by half the number of digits of (09) * (34) 2 306. .
multiplier i.e. 2.
(81) * (12) 2 972. .
iii. Multiply right half of the multiplicand
by left half of the multiplier, shift the (81) * (34) 0 2754
result by half the number of digits of
multiplier i.e. 2. 1210554
iv. Multiply right half of the multiplicand
by right half of the multiplier the result
is not shifted at all.

Basics
Basics of
of Algorithms
Algorithms and
and Mathematics
Mathematics 10
10 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Exercises
 Multiply following values using divide and conquer method.

Basics
Basics of
of Algorithms
Algorithms and
and Mathematics
Mathematics 11
11 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
To multiply with
Multiply Shift Result

Multiply Shift Result


To multiply with
Multiply Shift Result

Multiply Shift Result


Mathematics for Algorithmic Sets

14
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

Roster Notation
Example 2

Set-builder
Notation

Basics
Basics of
of Algorithms
Algorithms and
and Mathematics
Mathematics 15
15 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Set Theory
 Finite & Infinite sets: A set is finite if it contains a finite number of
elements, otherwise it is an infinite set.
 Examples:
1. Finite Set

Set is an infinite set

 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 an infinite set denoted as
3. The empty set denoted as is the unique set whose cardinality is .

Basics
Basics of
of Algorithms
Algorithms and
and Mathematics
Mathematics 16
16 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Set Theory
 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 �.
 Proper Subset: A proper subset of a set is a subset of that is not
equal to
 Example:
• If and set is a proper subset of .
• Set is a subset of , but it is not a proper subset of since .

A
B A=C

Basics
Basics of
of Algorithms
Algorithms and
and Mathematics
Mathematics 17
17 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
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

Basics
Basics of
of Algorithms
Algorithms and
and Mathematics
Mathematics 18
18 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
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
Basics
Basics of
of Algorithms
Algorithms and
and Mathematics
Mathematics 19
19 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
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

Basics
Basics of
of Algorithms
Algorithms and
and Mathematics
Mathematics 20
20 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
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

Basics
Basics of
of Algorithms
Algorithms and
and Mathematics
Mathematics 21
21 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
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

Basics
Basics of
of Algorithms
Algorithms and
and Mathematics
Mathematics 22
22 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Set Operations
 Symmetric Difference: The symmetric difference of two sets and
is the set of elements that are in but not in and the elements
that are in but not in .

 Example:
• Consider, and
Then

A B

Basics
Basics of
of Algorithms
Algorithms and
and Mathematics
Mathematics 23
23 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
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

Basics
Basics of
of Algorithms
Algorithms and
and Mathematics
Mathematics 24
24 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Set Operations
 Cartesian Product: The Cartesian product of two sets and is the
set of all ordered pairs where and .

 Example:

Basics
Basics of
of Algorithms
Algorithms and
and Mathematics
Mathematics 25
25 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Relation
 Let and be two sets. Any subset of their Cartesian product is a
relation.
 A relation defines the relationship between sets of values.
 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.

Basics
Basics of
of Algorithms
Algorithms and
and Mathematics
Mathematics 26
26 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Properties of the Relation
 Reflexive: Let be a set, and let be a binary relation on . Relation
is reflexive if,

 Examples:
Example 1 Example 2

and

So,

Not Reflexive
Reflexive since

Basics
Basics of
of Algorithms
Algorithms and
and Mathematics
Mathematics 27
27 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Properties of the Relation
 Symmetric: A relation on a set is called symmetric if whenever ,
for some .

 Examples:

Example 1

Symmetric
Example 2
and
So,
Asymmetric
Basics
Basics of
of Algorithms
Algorithms and
and Mathematics
Mathematics 28
28 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Properties of the Relation
 Transitive: A relation � on a set �, is called transitive if whenever
(�,�)∈� and (�,�)∈�, then (�,�)∈�, for �, �, �∈�.

 Examples:

Example 1

Transitive

Basics
Basics of
of Algorithms
Algorithms and
and Mathematics
Mathematics 29
29 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
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 .

Basics
Basics of
of Algorithms
Algorithms and
and Mathematics
Mathematics 30
30 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Functions
 Relationship between two sets of numbers is known as a function.
 Function is a special kind of relation.
 A number in one set is mapped to a 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.

Basics
Basics of
of Algorithms
Algorithms and
and Mathematics
Mathematics 31
31 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
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.
 Example: Codomain 2
3
4
Domain 5
6
7
8
9
10
 The range of the function
Domain Codomain

Basics
Basics of
of Algorithms
Algorithms and
and Mathematics
Mathematics 32
32 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
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 Ram CZ
Ram Mit
Neel

Division Students Students Division


(Domain) (Codomain) (Domain) (Codomain)
Basics
Basics of
of Algorithms
Algorithms and
and Mathematics
Mathematics 33
33 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Functions
 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

Basics
Basics of
of Algorithms
Algorithms and
and Mathematics
Mathematics 34
34 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Functions
 A function is injective or one-to-one if there do not exist two
distinct andsuch that
 The function , is a one-to-one function,
where and

Basics
Basics of
of Algorithms
Algorithms and
and Mathematics
Mathematics 35
35 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Functions
 If function is both one-to-one and onto then the function is called
Bijection function.
 Example:
function
where and

1 1

2 4

9
3
16
4

Basics
Basics of
of Algorithms
Algorithms and
and Mathematics
Mathematics 36
36 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
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

Basics
Basics of
of Algorithms
Algorithms and
and Mathematics
Mathematics 37
37 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Linear Inequalities
 Inequalities: The term inequality is applied to any statement
involving one of the symbols <, >, ≤, ≥.
 Examples of inequalities are:
1.

Basics
Basics of
of Algorithms
Algorithms and
and Mathematics
Mathematics 38
38 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
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.

Basics
Basics of
of Algorithms
Algorithms and
and Mathematics
Mathematics 39
39 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
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 .

Basics
Basics of
of Algorithms
Algorithms and
and Mathematics
Mathematics 40
40 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Logical Connectives
 Conjunction (): The logical connective Conjunction (logical AND) is
true only when both of the propositions are true.
 Example:
: It is raining
: It is cold
: It is raining AND it is cold
 Truth table
=

Basics
Basics of
of Algorithms
Algorithms and
and Mathematics
Mathematics 41
41 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Logical Connectives
 Disjunction (V): The logical disjunction, or logical OR, is true if one
or both of the propositions are true.
 Example:

 Truth table
= V

Basics
Basics of
of Algorithms
Algorithms and
and Mathematics
Mathematics 42
42 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Logical Connectives
 Negation (): , the negation of a proposition , is also a
proposition.
 Example:
: John studies.
: John does NOT study.
 Truth table

Basics
Basics of
of Algorithms
Algorithms and
and Mathematics
Mathematics 43
43 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
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.

Basics
Basics of
of Algorithms
Algorithms and
and Mathematics
Mathematics 44
44 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
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.

Basics
Basics of
of Algorithms
Algorithms and
and Mathematics
Mathematics 45
45 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
GTU Questions
1. Define the term: Quantifier.
2. Define Algorithm. How algorithm differs from flow chart?
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.

Basics
Basics of
of Algorithms
Algorithms and
and Mathematics
Mathematics 46
46 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Thank
you!