Introduction To Algorithm: Unit-1 Basics of Algorithms and

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Analysis and Design of Algorithms(ADA)

GTU # 3150703
✓ Outline
Looping
▪ Introduction to Algorithm
Unit-1 • Definition
Basics of Algorithms and • Characteristics

Mathematics • Types
• Simple Multiplication Methods
▪ Mathematics for Algorithmic Sets
• Set Theory
• Functions and Relations
• Vectors and Matrices
Rupesh Vaishnav
Computer Engineering Department • Linear Inequalities and Linear Equations
Darshan Institute of Engineering & Technology, Rajkot
rupesh.vaishnav@darshan.ac.in
• Logic and Quantifiers
9428037452

What is an Algorithm?
A step-by-step procedure, to solve the different kinds of problems.
Suppose, we want to make a Chocolate Cake.

Introduction to Algorithm 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


Rupesh Vaishnav 4
Mathematics
What is an Algorithm? Characteristics of An Algorithm
A process or a set of rules to be followed to achieve desired output, especially by a computer. Finiteness: An algorithm must always terminate after a finite number of steps.
Input 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.

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 #3150703 (ADA) ⬥ Unit 1 – Basics of Algorithms and
Rupesh Vaishnav 5 Rupesh Vaishnav 6
Mathematics Mathematics

Types of Algorithm Simple Multiplication Methods


Simple recursive algorithms
Backtracking algorithms 1. American approach 2. English approach
Divide and conquer algorithms
981 981
Dynamic programming algorithms
1 23 4 1 23 4
Greedy algorithms
Branch and bound algorithms 3924 981
Brute force algorithms 2943 1962
Randomized algorithms 2943
1962
981 3924

1210554 1210554

#3150703 (ADA) ⬥ Unit 1 – Basics of Algorithms and #3150703 (ADA) ⬥ Unit 1 – Basics of Algorithms and
Rupesh Vaishnav 7 Rupesh Vaishnav 8
Mathematics Mathematics
Simple Multiplication Methods Simple Multiplication Methods
3. à 𝒍𝒂 𝒓𝒖𝒔𝒔𝒆 multiplication 4. Multiplication by divide and conquer
i. Write the multiplicand and multiplier side by side. 981 1234 1234
Both the multiplicand and the multiplier must have the same number of digits and this
ii. Make two columns, one under each operand. 490 2468 number be a power of 2. If not then it can be done by adding zeros on the left if
iii. Repeat step iv and v until the number in the left column is 1.
iv. Divide the number in the left hand column by 2, ignoring any
245 4936 4936 necessary.
Multiplicand 0981
fractions. 122 9872 i. Multiply left half of the multiplicand by left half of multiplier and
v. Double the number in the right hand column by adding it to shift the result by no. of digits of multiplier i.e. 4. Multiplier 1234
61 19744 19744
itself.
vi. Next cross out each row where the number in the left hand 30 39488 ii. Multiply left half of the multiplicand by right half of the multiplier, Multiply Shift Result
column is even. shift the result by half the number of digits of multiplier i.e. 2.
15 78976 78976 (09) * (12) 4 10 8 . . . .
vii. Finally add up the numbers that remain in the right hand
column. iii. Multiply right half of the multiplicand by left half of the multiplier, (09) * (34) 2 3 0 6 . .
7 157952 157952 shift the result by half the number of digits of multiplier i.e. 2.
3 315904 315904 (81) * (12) 2 9 7 2 . .
iv. Multiply right half of the multiplicand by right half of the multiplier
1 631808 631808 the result is not shifted at all. (81) * (34) 0 2 7 5 4

1210554 1210554
#3150703 (ADA) ⬥ Unit 1 – Basics of Algorithms and #3150703 (ADA) ⬥ Unit 1 – Basics of Algorithms and
Rupesh Vaishnav 9 Rupesh Vaishnav 10
Mathematics Mathematics

Exercises
Multiply following values using divide and conquer method. Multiply Shift Result
1. 4567×6543
2. 31415975×8182818

Multiply Shift Result

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


Rupesh Vaishnav 11 Rupesh Vaishnav #3150703 (ADA) ⬥ Unit 1 – Basics of Algorithms and Mathematics 12
Mathematics
Multiply Shift Result

Multiply Shift Result


Mathematics for Algorithmic Sets

Rupesh Vaishnav #3150703 (ADA) ⬥ Unit 1 – Basics of Algorithms and Mathematics 13

Set Theory Set Theory


A set is an unordered collection of distinct objects. Finite & Infinite sets: A set is finite if it contains a finite number of elements, otherwise it is an
The objects in a set are called elements or members of the set. infinite set.

Example 1 Example 2
Roster Notation Set-builder Notation
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 𝐴.

#3150703 (ADA) ⬥ Unit 1 – Basics of Algorithms and #3150703 (ADA) ⬥ Unit 1 – Basics of Algorithms and
Rupesh Vaishnav 15 Rupesh Vaishnav 16
Mathematics Mathematics
Set Theory Set Theory

Example 1 Example 2

A
B A=C

#3150703 (ADA) ⬥ Unit 1 – Basics of Algorithms and #3150703 (ADA) ⬥ Unit 1 – Basics of Algorithms and
Rupesh Vaishnav 17 Rupesh Vaishnav 18
Mathematics Mathematics

Set Theory Set Operations

𝐴′ = {𝑥 | 𝑥 ∈𝑈 𝑎𝑛𝑑 𝑥∉𝐴}

A’
A •A
•B
U
#3150703 (ADA) ⬥ Unit 1 – Basics of Algorithms and #3150703 (ADA) ⬥ Unit 1 – Basics of Algorithms and
Rupesh Vaishnav 19 Rupesh Vaishnav 20
Mathematics Mathematics
Set Operations Set Operations

A B A B

#3150703 (ADA) ⬥ Unit 1 – Basics of Algorithms and #3150703 (ADA) ⬥ Unit 1 – Basics of Algorithms and
Rupesh Vaishnav 21 Rupesh Vaishnav 22
Mathematics Mathematics

Set Operations 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

A B

#3150703 (ADA) ⬥ Unit 1 – Basics of Algorithms and #3150703 (ADA) ⬥ Unit 1 – Basics of Algorithms and
Rupesh Vaishnav 23 Rupesh Vaishnav 24
Mathematics Mathematics
Set Operations Relation

#3150703 (ADA) ⬥ Unit 1 – Basics of Algorithms and #3150703 (ADA) ⬥ Unit 1 – Basics of Algorithms and
Rupesh Vaishnav 25 Rupesh Vaishnav 26
Mathematics Mathematics

Properties of the Relation Properties of the Relation

Example 1 Example 2
Symmetric Asymmetric
Example 1 Example 2

Reflexive

#3150703 (ADA) ⬥ Unit 1 – Basics of Algorithms and #3150703 (ADA) ⬥ Unit 1 – Basics of Algorithms and
Rupesh Vaishnav 27 Rupesh Vaishnav 28
Mathematics Mathematics
Properties of the Relation Equivalence Relation

Transitive Not Transitive


Example 1 Example 2

#3150703 (ADA) ⬥ Unit 1 – Basics of Algorithms and #3150703 (ADA) ⬥ Unit 1 – Basics of Algorithms and
Rupesh Vaishnav 29 Rupesh Vaishnav 30
Mathematics Mathematics

Functions Function Notations

Codomain

Domain

Domain Codomain

#3150703 (ADA) ⬥ Unit 1 – Basics of Algorithms and #3150703 (ADA) ⬥ Unit 1 – Basics of Algorithms and
Rupesh Vaishnav 31 Rupesh Vaishnav 32
Mathematics Mathematics
Relation & Function Functions Types
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
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 #3150703 (ADA) ⬥ Unit 1 – Basics of Algorithms and
Rupesh Vaishnav 33 Rupesh Vaishnav 34
Mathematics Mathematics

Functions Types Functions Types

1 1
2 4

3 9

4 16

#3150703 (ADA) ⬥ Unit 1 – Basics of Algorithms and #3150703 (ADA) ⬥ Unit 1 – Basics of Algorithms and
Rupesh Vaishnav 35 Rupesh Vaishnav 36
Mathematics Mathematics
Vectors and Matrices Linear Inequalities

#3150703 (ADA) ⬥ Unit 1 – Basics of Algorithms and #3150703 (ADA) ⬥ Unit 1 – Basics of Algorithms and
Rupesh Vaishnav 37 Rupesh Vaishnav 38
Mathematics Mathematics

Linear Equations Logic

Solution

#3150703 (ADA) ⬥ Unit 1 – Basics of Algorithms and #3150703 (ADA) ⬥ Unit 1 – Basics of Algorithms and
Rupesh Vaishnav 39 Rupesh Vaishnav 40
Mathematics Mathematics
Logical Connectives Logical Quantifiers

#3150703 (ADA) ⬥ Unit 1 – Basics of Algorithms and #3150703 (ADA) ⬥ Unit 1 – Basics of Algorithms and
Rupesh Vaishnav 41 Rupesh Vaishnav 42
Mathematics Mathematics

Logical Quantifiers 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 #3150703 (ADA) ⬥ Unit 1 – Basics of Algorithms and
Rupesh Vaishnav 43 Rupesh Vaishnav 44
Mathematics Mathematics

You might also like