0% found this document useful (0 votes)
8 views58 pages

CH 1

CH 1 OF ADA

Uploaded by

Eva Watts'
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views58 pages

CH 1

CH 1 OF ADA

Uploaded by

Eva Watts'
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 58

Analysis and Design of Algorithms

Subject Code:3150703
Subject credit:5

Prepared by:
Prof. Nishaba Jadeja
Teaching Scheme
Teaching Scheme Credits Examination Marks

Theory Marks Practical Marks Total


L T P C Marks
ESE
(E) PA
(M) ESE (V) PA (I)

4 0 2 5 70 30 30 20 150

[L-Lecture;
T – Tutorial/Teacher Guided Theory Practice;
P - Practical;
C – Credit,
ESE - End Semester Examination,
PA – Progressive Assessment]
Evaluation Methodology
Theory Exam Marks Marks Conversion
GTU External Exam 70 NA

GTU MidSem Exam 70 30

Internal Exam Marks Activity Marks


Conversion
Assignment 05

Internal 20
Class Test 1 & 2,3 10

Lab performance 05
Google Classroom Code

2665apc
https://classroom.google.com/c/NjkzMjIxODIyMTc1?cjc=2665apc
Syllabus
• Total syllabus is covered in 09 topics.
• ADA-3150703 syllabus.pdf
CH-1
Basics of Algorithms and
Mathematics
Outlines
 What is an algorithm?
 Mathematics for Algorithmic Sets,
 Functions and Relations,
 Vectors and Matrices,
 Linear Inequalities and
 Linear Equations
What is Algorithm ???????
 An algorithm, named for the ninth-century Persian mathematician
al-Khowarizm, is simply a set of rules for carrying out some
calculation, either by hand or, more usually, on a machine.
 Definition :
It is a set of instructions of how to carry out a process. An
algorithm list the steps that must be followed to complete the
process and to reach the solution.
 Different types of algorithms :
Probabilistic of Algorithm,
Approximate Algorithm,
Heuristics Algorithm

 Hence a cooking recipe can be consider to be an algorithm if it


describe precisely how to make a certain dish, giving exact
quantities to use and detailed instruction for how long to cook it.
Example : To make a tea

 Step-1
Take a pan, and add water,
tea , and sugar to it

 Step-2
Put the burner on low flame
 Step-3
Heat the tea until it turns
a reddish brown colour

 Step-4

Take Basil leaves and


ginger flakes, and crush
them together
 Step-5
Add milk

 Step-6
Increase the flame
 Step-7
Stop the burner when the
milk rises to the top of
the pan.

Step-8
Filter all the solid out
 Properties of the algorithm
1) Finiteness: - an algorithm terminates after a finite numbers of steps.
 Properties of the algorithm
1) Finiteness: - an algorithm terminates after a finite numbers of steps.

2) Definiteness: - each step in algorithm is unambiguous. This


means that the action specified by the step cannot be
interpreted (explain the meaning of) in multiple ways & can be
performed without any confusion.
 Properties of the algorithm
1) Finiteness: - an algorithm terminates after a finite numbers of steps.

2) Definiteness: - each step in algorithm is unambiguous. This means that


the action specified by the step cannot be interpreted (explain the meaning
of) in multiple ways & can be performed without any confusion.

3) Input:- an algorithm accepts zero or more inputs


 Properties of the algorithm
1) Finiteness: - an algorithm terminates after a finite numbers of steps.

2) Definiteness: - each step in algorithm is unambiguous. This means that


the action specified by the step cannot be interpreted (explain the meaning
of) in multiple ways & can be performed without any confusion.

3) Input:- an algorithm accepts zero or more inputs

4) Output:- it produces at least one output.


 Properties of the algorithm
1) Finiteness: - an algorithm terminates after a finite numbers of steps.

2) Definiteness: - each step in algorithm is unambiguous. This means that


the action specified by the step cannot be interpreted (explain the meaning
of) in multiple ways & can be performed without any confusion.

3) Input:- an algorithm accepts zero or more inputs

4) Output:- it produces at least one output.

5) Effectiveness:- it consists of basic instructions that are


realizable. This means that the instructions can be performed
by using the given inputs in a finite amount of time.
 Properties of the algorithm
1) Finiteness: - an algorithm terminates after a finite numbers of steps.

2) Definiteness: - each step in algorithm is unambiguous. This means that


the action specified by the step cannot be interpreted (explain the meaning
of) in multiple ways & can be performed without any confusion.

3) Input:- an algorithm accepts zero or more inputs

4) Output:- it produces at least one output.

5) Effectiveness:- it consists of basic instructions that are realizable. This


means that the instructions can be performed by using the given inputs in a
finite amount of time.

6) Should be written in simple English


 Properties of the algorithm
1) Finiteness: - an algorithm terminates after a finite numbers of steps.

2) Definiteness: - each step in algorithm is unambiguous. This means that


the action specified by the step cannot be interpreted (explain the meaning
of) in multiple ways & can be performed without any confusion.

3) Input:- an algorithm accepts zero or more inputs

4) Output:- it produces at least one output.

5) Effectiveness:- it consists of basic instructions that are realizable. This


means that the instructions can be performed by using the given inputs in a
finite amount of time.

6) Should be written in simple English

7) Should provide the correct solutions


 Properties of the algorithm
1) Finiteness: - an algorithm terminates after a finite numbers of steps.

2) Definiteness: - each step in algorithm is unambiguous. This means that


the action specified by the step cannot be interpreted (explain the meaning
of) in multiple ways & can be performed without any confusion.

3) Input:- an algorithm accepts zero or more inputs

4) Output:- it produces at least one output.

5) Effectiveness:- it consists of basic instructions that are realizable. This


means that the instructions can be performed by using the given inputs in a
finite amount of time.

6) Should be written in simple English

7) Should provide the correct solutions

8) Every statement should be perfect


Issues in writing an algorithm
 How to devise algorithms?
◦ Create using algorithmic design strategy
 How to validate algorithms?
◦ Checking whether algorithm computes correct answer for all possible
inputs.
 How to analyze algorithms?
◦ Computing time and storage, performance analysis.
 How to test a program?
◦ Debugging and performance measuring.
How to write an algorithm?
 Algorithm name (p1,p2,.....,pn)

This keyword Here write the Write parameters (if


should be name of an any)
written first algorithm

 Write following things in heading section:


//Problem Description:
//Input:
//Output:
 While statement can be written as:
While (condition) do
{
statement 1
statement 2
.
.
.
statement n
}
 General form for writing loop is:
for variable  valuei to valuen do
{
statement 1
statement 2
.
.
.
statement n
}
Example of an algorithm
 Write an algorithm to count the sum of n
numbers.
Algorithm sum (1,n)
//Problem Description: This algorithm is for finding
the sum of given n numbers.
//Input: 1 to n numbers
//Output: The sum of n numbers
result 0
for i1 to n do ii+1
result  result +1
return result
Designing of Algorithm

Natural Lang
Psuedo Code
Flowchart
Mathematics for algorithm

Set :
 Definition :
◦ Set is a collection of objects. These objects are called
elements of the set. So, A set is an unordered collection
of distinct element.
◦ All elements are enclosed within curly brackets ‘{‘ and
‘}’ and every element is separated by commas.
◦ If ‘a’ is an element of set A then we say that a€A and if
‘a’ is not an elements of A then we say that a € A.
◦ Normally set is denoted by capital letter. The set can be
represented using three methods.
 1) Listing Method
 2) Describing properties
 3) Recursive Method
Cont . . .
 1. Listing Method
The elements are listed in the set.
Ex. A set of elements which is less than 5
{0 , 1 , 2 , 3 , 4}
 2. Describing Properties
The properties of elements of a set defined the set.
Ex. A set of vowels
A = { a , e , i , o , u}
 3. Recursion method
The recursion occurs to define the elements of the set.
Ex. A = {x / x is square of n} where n<=10
A = {0 , 1 , 4 , 9 , 16, 25 , …….. 100}
Finite-Infinite Set
 A set is finite if it contains a finite number of elements; otherwise, the set
is infinite.

Cardinality
 If X is a finite set, |x| , the cardinality of X, denotes the number of
elements in X.
 If X is infinite set, we may write |X| = .
 The empty set, denoted O, is the unique set whose cardinality is 0.

Power Set
 The power set is a set of all the subsets of its elements.
Ex. A= {1 , 2 , 3}
Q = { O , {1} , {2} , {3} , {1,2} , {1,3}, {2,3} , {1,2,3}}
 X Y “ X is subset of Y ”
Each elements of X is belongs to Y.

 X Y “ X is a proper subset of Y ”
Means that X Y and moreover that there is at
least one element in Y that does not belongs to X.

Equal Set
The two sets are said to be equal (A=B) if A B and
B A.
Operations on Sets
1) Union 2) Intersection 3) Difference 4) Complement
 1) Union
AUB is a union operation – If A={1,2,3} , B={1,2,4}
then AUB = {1, 2, 3, 4}
 2) Intersection
A∩B is intersection operation – If A={1,2,3} , B={2,3,4}
then A∩B = {2 , 3}
 3) Difference
A-B is the difference operation – If A={1,2,3} ,B={2,3,4}
then A-B = {1} means elements which are in set A but not in set B
 4) Complement of A
Complement of (A) = U – A where U is universal set
Ex. U={10,20,30,40,50} and A={10,20}
Complement(A) = {30 , 40 , 50}
 Cartesian Product of Two Set
Ex.
A = {a , b} and B = {0 , 1 , 2}
A X B = {{a,0},{a,1},{a,2},{b,0},{b,1},{b,2}}
Explain Function & Relation with Example

 Let X and Y be two sets.


 Any subset p of their Cartesian product
X x Y is a relation.
 When x X and y Y, we say that x is in
relation with y according to p, denoted x p
y, if and only if (x,y) p.
 Consider any relation f between X and Y.
The relation is called a function if, for each
x X, there exists one and only one y
Y such that (x,y) f.
Cont…
 For example:
A = {1,2}; B = {3,4}
A × B = {1,2} × {3,4}
= {(1,3), (1,4), (2,3), (2,4)}
P = {(1,4) , (2,3)}

Here A has relation with B according to P.


This relation is called function because for each
element of A there exist one and only one
element of B which is belongs to P.
 Injective (One To One),
 Surjective (Onto),
 Bijective.

 Injective (One To One) : A function f : X Y is


injective if there do not exist two distinct x1, x2 X
such that
f(x1) = f(x2).
 Injective (One To One),
 Surjective (Onto),
 Bijective.

 Surjective (Onto) : It is surjective if for each y Y


there exist at least one x X such that f(x) = y.
 Injective (One To One),
 Surjective (Onto),
 Bijective.

 Bijective : It is bijective if it is both injective and


surjective.
Cont…

 Propositional calculation:
A Boolean (or propositional) variable may only take
one of this two values(true or false).
If p is a Boolean variable, we write “p is true” , or
simply “p” , to mean “p=true”
Let p and q be two Boolean variables.
conjunction p q or p and q ,
disjunction p q or p or q ,
negation p or not p,
if p then q p q
p q either both true or both false.
 Interval :
An interval is a set of real numbers lying between
two bonds.
Let a and b be two real numbers such that a<=b.

The open interval (a,b) denotes


{x R / a < x < b}
Closed interval [a,b] denotes
{x R / a <= x <= b}
Semi Open Intervals
(a,b] = { x R / a < x <= b} and
[a,b) = { x R / a < = x < b}
Vectors and Matrices
 A vector, u, means a list (or n-tuple) of numbers:
u = (u1, u2, . . . , un)
where ui are called the components of u.

Zero Vector :
If all the ui are zero i.e., ui = 0, then u is called the zero
vector.

Equal Vector :
Given vectors u and v are equal i.e., u = v, if they have
the same number of components and if corresponding
components are equal.
 Addition of Two Vectors
If two vectors, u and v, have the number of components, their sum,
u + v, is the vector obtained by adding corresponding components from
u and v.
u + v = (u1, u2, . . . , un) + (v1, v2, . . . , vn)
= (u1 + v1 , u2 + v2 , . . . , un + vn)

 Multiplication of a vector by a Scalar


The product of a scalar k and a vector u i.e., ku, is the vector obtained
by multiplying each component of u by k:
ku = k(u1, u2, . . . , un)
= ku1, ku2, . . . , kun
Dot Product and Norm

The dot product or inner product of vectors u = (u1, u2, . . . , un) and
v = (v1, v2, . . . , vn) is denoted by u.v and defined by
u.v = u1v1 + u2v2 + . . . + unvn

The norm or length of a vector, u, is denoted by ||u|| and defined by


Matrices
Matrix, A, means a rectangular array of numbers

A =

The m horizontal n-tuples are called the rows of A, and the n


vertical m-tuples, its columns.

Note that the element aij, called the ij-entry, appear in the ith row
and the jth column.

In algorithmic (study of algorithms), we like to write a matrix A


as A(aij).
 Column Vector
A matrix with only one column is called a column vector
 Zero Matrix
A matrix whose entries are all zero is called a zero matrix and
denoted by 0.
 Matrix Addition
Let A and B be two matrices of the same size. The sum of A and
B is written as A + B and obtained by adding corresponding
elements from A and B.

A+B =

=
 Scalar Multiplication
 The product of a scalar k and a matrix A, written kA or Ak, is
the matrix obtained by multiplying each element of A by k.

kA = k

If K = 3
 Properties of Matrix under Addition and
Multiplication

Let A, B, and C be matrices of same size and let k and l be


scalars. Then

1. A+B=B+A
2. A+0 = 0 +A=A
3. A + (-A) = (-A) + A = 0
4. k(A + B) = kA + kB
5. (k + l)A = kA + lA
6. (kl)A = k(lA)
Matrix Multiplication
 Suppose A and B are two matrices such that the number of
columns of A is equal to number of rows of B. Say matrix A is
an m×p matrix and matrix B is a p×n matrix. Then the product
of A and B is the m×n matrix whose ij-entry is obtained by
multiplying the elements of the ith row of a by the
corresponding elements of the jth column of B and then adding
them.

 Properties of Matrix Multiplication


Let A, B, and C be matrices and let k be a scalar. Then

1. (AB)C = A(BC)
2. A(B+C) = AB + AC
3. (B+C)A = BA + CA
4. k (AB) = (k A)B = A(k B)
 Transpose
 The transpose of a matrix A is obtained by writing the row of A, in
order, as columns and denoted by AT.

It is not hard to see that if A is an m×n matrix, then AT is an n×m


matrix.

For example if A =

then AT =
 Square Matrix
 If the number of rows and the number of columns of any
matrix are the same, we say matrix is a square matrix, i.e., a
square matrix has same number of rows and columns.
 A square matrix with n rows and n columns is said to be
order n and is called an n-square matrix.

 Unit Matrix
 The n-square matrix with 1's along the main diagonal and
0's elsewhere is called the unit matrix and usually denoted
by I.
 Determinants
 To each n-square matrix A = (aij), we assign a
specific number called the determinants of A and
denoted as
|A| = del(A)

The determinant of order one is |a11| = a11


The determinant of order two is

= a11a22 - a12a21
 The determinant of order three is

= a11(a22a33 - a23a32) – a12(a21a33 - a31a23) +


a13(a21a32 -a22a31)

= a11a22a33+ a12a23a31 + a13a21a32 - a13a22a31 -


a12a21a33 - a11a23a32
Linear Inequality
 In mathematics, linear inequality is a statement
containing < , > <= , >=.
Example : x + y > 14

Solution to Linear Inequality :


 The solution of linear inequality is based on number of
variables.

 The solution to one variable inequality :


3x+7<=10, the value of x gives true statement.
For example x=1
Cont. . .
 The solution to two variable inequality:
3x+7y-5 <=17, the value of x and y should be such that the
statement remains true. The solution to above inequality
is (2,2) because
= 3(2) + 7(2) - 5<= 17
= 6 + 14 - 5<= 17

 The solution to three variable inequality:


3x+2y-7z <=5, the value of x, y and z should be such that
the statement remains true. The solution to above
inequality is (2,2,1).
Because
= 3(2)+2(2)-7(1)
= 3 <= 5.
Properties of Inequality
1. If x<=y and y<=z then x<=z
2. If x <= y and c is some positive number then
x+c <= y+c
3. If x <= y and c is negative, then xc >= yc
Linear Equations
 The linear equation is an equation containing n
unknowns. A linear with one unknown can be given in
standard form:
ax = b
Where x is unknown and a and b are constants. The
solution of such equation will be,
x = b/a
The linear equation with two unknowns can be given in
standard form as
ax+by = c
Where x,y are unknowns and a, b, c are constant.
Two linear equations with two unknowns
a1x + b1y = c1
a2x + b2y = c2

Step-1 : Multiply two equation by any two numbers


such that resulting coefficients of at least one term will
be negative of each other.

Step-2 : Perform addition of two equations.

Example :
5x + 10y = 15
3x + 2y = 5
Cont . .
 Example
Gauss Elimination Method
GTU QUESTIONS

 Define algorithm. Discuss key characteristics of algorithms. –3 marks


 What is an algorithm? Why analysis of algorithm is required? –3 marks

You might also like