Chapter-2: Algorithms, Integers and Matrices

You might also like

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

CHAPTER-2

ALGORITHMS, INTEGERS AND MATRICES


ALGORITHM

• An algorithm is the foundation of computer programming.


• An algorithm is a finite sequence of precise instructions for performing a computation or for solving
a problem. Most generally, an algorithm just means definite procedure for performing some sort of
task.

• a procedure that follows a sequence of steps that leads to the desired sequence of steps that leads
to the desired answer. Such a sequence is called an answer. Such a sequence is called an algorithm
• An algorithm can also be described using a computer language. However, when that is done, only
those instructions permitted in the language can be used.
• Pseudocode provides an intermediate step between an English language description of an algorithm
and an implementation of this algorithm in a programming language
EXECUTING

• Executing an Algorithm When you start up a piece of software, we say the program or its
algorithm are being run or executed by the computer.
• Given a description of an algorithm, you can also execute it by hand, by working through
all of its steps on paper.
• Before WWII, computer meant a person whose job was to run algorithms!
PROPERTIES OF ALGORITHM

• Input. An algorithm has input values from a specified set.


• Output. From each set of input values an algorithm produces output values from a specified set. The output
values are the solution to the problem.
• Definiteness. The steps of an algorithm must be defined precisely.
• Correctness. An algorithm should produce the correct output values for each set of input values.
• Finiteness. An algorithm should produce the desired output after a finite (but perhaps large) number of
steps for any input in the set.
• Effectiveness. It must be possible to perform each step of an algorithm exactly and in a finite amount of
time.
• Generality. The procedure should be applicable for all problems of the desired form, not just for a
particular set of input values.
EXAMPLE
SEARCHING ALGORITHMS

The problem of locating an element in an ordered list occurs in many contexts Problems of this kind
are called searching problems.
Types of Searching:

• Linear search
• Binary Search
LINEAR SEARCH

• Sequential search
• The linear search algorithm begins by
comparing x and a1. When x = a1, the
solution is the location of a1, namely, 1.
When x = a1, compare x with a2. If x =
a2, the solution is the location of a2,
namely, 2. When x = a2, compare x with
a3. Continue this process, comparing x
successively with each term of the list
until a match is found,
EXAMPLE
BINARY SEARCH

• This algorithm can be used when the list has terms occurring in order of increasing size.
• It proceeds by comparing the element to be located to the middle term of the list. The list is then
split into two smaller sub-lists of the same size, or where one of these smaller lists has one fewer
term than the other.
• The search continues by restricting the search to the appropriate sub-list based on the comparison of
the element to be located and the middle term.
• binary search algorithm is much more efficient than the linear search algorithm
ALGORITHM
SORTING

• Sorting is putting these elements into a list in which the elements are in increasing order. For
instance, sorting the list 7, 2, 1, 4, 5, 9 produces the list 1, 2, 4, 5, 7, 9. Sorting the list d, h, c, a, f
(using alphabetical order) produces the list a, c, d, f, h.
Types of Sorting
• Bubble sort
• Insertion Sort
BUBBLE SORT

• The bubble sort is one of the simplest sorting algorithms, but not one of the most efficient. It puts a
list into increasing order by successively comparing adjacent elements.
• interchanging a larger element with a smaller one following it, starting at the beginning of the list,
for a full pass.
• We iterate this procedure until the sort is complete.
• In the bubble sort, the smaller elements “bubble” to the top as they are interchanged with larger
elements. The larger elements “sink” to the bottom.
ALGORITHM & EXAMPLE
INSERTION SORT

• the insertion sort begins with the second element.


• The insertion sort compares this second element with the first element and inserts it before the first
element if it does not exceed the first element and after the first element if it exceeds the first
element.
• At this point, the first two elements are in the correct order. The third element is then compared with
the first element, and if it is larger than the first element, it is compared with the second element; it
is inserted into the correct position among the first three elements.
EXAMPLE
GREEDY ALGORITHM

• Optimization problems minimize or maximize some parameter over all possible inputs.
Examples of optimization problems:
1. Finding a route between two cities with the smallest total mileage.
2. Determining how to encode messages using the fewest possible bits.
3. Finding the fiber links between network nodes using the least amount of fiber.
• Optimization problems can often be solved using a greedy algorithm, which makes the “best”
(by a local criterion) choice at each step. This does not necessarily produce an optimal solution
to the overall problem, but in many instances, it does. After specifying what the “best choice” at
each step is, we try to prove that this approach always produces an optimal solution, or find a
counterexample to show that it does not.
EXAMPLE
DIVISION

• If a and b are integers with a = 0, we say that a divides b if there is an integer c such that b = ac, or
equivalently, if b a is an integer. When a divides b we say that a is a factor or divisor of b, and that
b is a multiple of a. The notation a | b denotes that a divides b. We write a | b when a does not
divide b.
• Assume 2 integers a and b, such that a =/ 0 (a is not equal 0). We say that a divides b if there is an
integer c such that b = ac. If a divides b we say that a is a factor of b and that b is multiple of a. The
fact that a divides b is denoted as a | b.
EXAMPLE

• 4 | 24 True or False ? True


• 4 is a factor of 24

• 24 is a multiple of 4
• 3 | 7 True or False ? False
DIVISION ALGORITHM

• When an integer is divided by a positive integer, there is a quotient and a remainder.


THE DIVISION ALGORITHM Let a be an integer and d a positive integer. Then there are unique
integers q and r, with 0 ≤ r < d, such that a = dq + r.

• In the equality given in the division algorithm, d is called the divisor, a is called the dividend, q is
called the quotient, and r is called the remainder. This notation is used to express the quotient and
remainder: q = a div d, r = a mod d.
EXAMPLE

• What are the quotient and remainder when 101 is divided by 11?
Solution: We have

• 101 = 11 ・ 9 + 2.
• Hence, the quotient when 101 is divided by 11 is 9 = 101 div 11, and the remainder is
• 2 = 101 mod 11.
MODULAR ARITHEMTIC

• If a and b are integers and m is a positive integer, then a is congruent to b modulo m if m divides a − b.
We use the notation a ≡ b (mod m) to indicate that a is congruent to b modulo m. We say that a ≡ b (mod
m) is a congruence and that m is its modulus (plural moduli). If a and b are not congruent modulo m, we
write a ≡ b (mod m).

• Let a and b be integers, and let m be a positive integer. Then a ≡ b (mod m) if and only if a mod m = b
mod m.

Determine whether 17 is congruent to 5 modulo 6 and whether 24 and 14 are congruent modulo 6.

Solution: Because 6 divides 17 − 5 = 12, we see that 17 ≡ 5 (mod 6). However, because 24 − 14 = 10 is not
divisible by 6, we see that 24 ≡ 14 (mod 6).
PRIMES

An integer p greater than 1 is called prime if the only positive factors of p are 1 and p. A positive
integer that is greater than 1 and is not prime is called composite.
• Example : The integer 7 is prime because its only positive factors are 1 and 7, whereas the integer 9
is composite because it is divisible by 3.
THE FUNDAMENTAL THEOREM OF ARITHMETIC Every integer greater than 1 can be written
uniquely as a prime or as the product of two or more primes where the prime factors are written in
order of nondecreasing size.
FACTORIZATION EXAMPLE

The prime factorizations of 100, 641, 999, and 1024 are given by
• 100 = 2 ・ 2 ・ 5 ・ 5 = 2252,

• 641 = 641,
• 999 = 3 ・ 3 ・ 3 ・ 37 = 33 ・ 37,
• 1024 = 2 ・ 2 ・ 2 ・ 2 ・ 2 ・ 2 ・ 2 ・ 2 ・ 2 ・ 2 = 210.
TRAIL DIVISION

• If n is a composite integer, then n has a prime divisor less than or equal to √n.
• an integer is prime if it is not divisible by any prime less than or equal to its square root. This leads
to the brute-force algorithm known as trial division.

• To use trial division we divide n by all primes not exceeding √n and conclude that n is prime ,if it is
not divisible by any of these primes.
Example: Show that 101 is prime.
Solution: The only primes not exceeding √101 are 2, 3, 5, and 7. Because 101 is not divisible by 2, 3,
5, or 7 (the quotient of 101 and each of these integers is not an integer), it follows that 101 is prime.
SIEVE OF ERATOSTHENES

• The sieve of Eratosthenes is used to find all


primes not exceeding a specified positive
integer.
• There are infinitely many primes.
CONTD..

• the largest prime known has been an integer of the special form 2p − 1, where p is also prime. Such
primes are called Mersenne primes
Example:

The numbers 22 − 1 = 3, 23 − 1 = 7, 25 − 1 = 31 and 27 − 1 = 127 are Mersenne primes, while 211 − 1 =


2047 is not a Mersenne prime because 2047 = 23 ・ 89.
GCD

• The largest integer that divides both of two integers is called the greatest common divisor of these
integers.
• Let a and b be integers, not both zero. The largest integer d such that d | a and d | b is called the
greatest common divisor of a and b.
• The greatest common divisor of a and b is denoted by gcd (a, b).
Example:What is the greatest common divisor of 24 and 36?
The positive common divisors of 24 and 36 are 1, 2, 3, 4, 6, and 12. Hence, gcd(24, 36) = 12.
• The integers a and b are relatively prime if their greatest common divisor is 1.

Example:What is the greatest common divisor of 17 and 22?


The integers 17 and 22 have no positive common divisors other than 1, so that gcd(17, 22) = 1.
• The integers a1, a2, . . . , an are pairwise relatively prime if gcd(ai, aj ) = 1 whenever 1 ≤ i < j ≤ n.
Example:Determine whether the integers 10, 17, and 21 are pairwise relatively prime and
whether the integers 10, 19, and 24 are pairwise relatively prime.

• Because gcd(10, 17) = 1, gcd(10, 21) = 1, and gcd(17, 21) = 1, we conclude that 10, 17, and 21 are
pairwise relatively prime. Because gcd(10, 24) = 2 > 1, we see that 10, 19, and 24 are not pairwise
relatively prime.
LCM

• The least common multiple of the positive integers a and b is the smallest positive integer that is
divisible by both a and b, denoted by lcm(a; b).
Examples: lcm(2; 10) = 10, lcm(5; 7) = 35, lcm(4; 6) = 12.

• Let a and b be positive integers and let p1; p2….. pn be all the primes that appear in the prime
factorization of a or b, so that
EXAMPLE

Determine the lcm ( 3528, 1296 ).


The prime factorization of 3528 = 23 * 32 * 72.
The prime factorization of 1296 = 24 * 34.
lcm ( 3528, 1296 ) = 2max(3,4) * 3max(2,4) * 7max(2,0) = 24 * 34 * 72 = 63504
EUCLIDEAN ALGORITHM

• Computing the greatest common divisor of two integers directly from the prime factorizations of
these integers is inefficient.
• Time Consuming
• Euclidean algorithm
We want to calculate the gcd(91; 287).
Applying the division algorithm to 287 and 91, we get
287 = 91 3 + 14:
Any common divisor d of 287 and 91 must also be a divisor of 14, because
d|287 and d|91 implies d|(287- 91. 3) = 14.
EUCLIDEAN ALGORITHM CONTD…

• Also, any common divisor of 91 and 14 must also be a divisor of 287. So, gcd(287; 91)=gcd(91; 14).
Great! We've just decreased one of the numbers. Continue the process by dividing 91 by
• 91 = 14 6 + 7:

• Again, we conclude gcd(91; 14)=gcd(14; 7), and divide 14 by 7:


• 14 = 7 2 + 0
• Because 7 divides 14 we know gcd(14; 7) = 7. Therefore,

• gcd(287; 91) =gcd(91; 14) =gcd(14; 7) = 7.14:


ALGORITHM

• Let a = bq + r, where a, b, q, and r are integers. Then gcd(a, b) = gcd(b, r).


• suppose that d divides both a and b. Then it follows that d also divides a − bq = r. any common divisor of
a and b is also a common divisor of b and r.
• suppose that d divides both b and r. Then d also divides bq + r = a. Hence, any common divisor of b and r
is also a common divisor of a and b.
• Consequently, gcd(a, b) = gcd(b, r)
gcd(a, b) = gcd(r0, r1) = gcd(r1, r2) = ・ ・ ・ = gcd(rn−2, rn−1)
= gcd(rn−1, rn) = gcd(rn, 0) = rn.
the greatest common divisor is the last nonzero remainder in the sequence of divisions
EXAMPLE
GCD AS LINEAR COMBINATIONS

• the greatest common divisor of two integers a and b can be expressed in the form sa + tb, where s
and t are integers.
• gcd(a, b) can be expressed as a linear combination with integer coefficients of a and b.

Example:gcd(6, 14) = 2, and 2 = (−2) ・ 6 + 1 ・ 14.


BÉZOUT’S THEOREM If a and b are positive integers, then there exist integers s and t such that
gcd(a, b) = sa + tb.
If a and b are positive integers, then integers s and t such that gcd(a, b) = sa + tb are called Bézout
coefficients of a and b Also, the equation gcd(a, b) = sa + tb is called Bézout’s identity
EXTENDED ECULIDEAN

• Consider the steps of the Euclidean algorithm for gcd(252; 198):


• 252 = 1 198 + 54

• 198 = 3 54 + 36
• 54 = 1 36 + 18
• 36 = 2 18

gcd(252; 198)=18 as linear combination of 54 and 36


• 18 = 54 − 1 ・ 36.
• The second division tells us that

• 36 = 198 − 3 ・ 54.
• Substituting this expression for 36 into the previous equation, we can express 18 as a linear
• combination of 54 and 198.We have
• 18 = 54 − 1 ・ 36 = 54 − 1 ・ (198 − 3 ・ 54) = 4 ・ 54 − 1 ・ 198.
• The first division tells us that
• 54 = 252 − 1 ・ 198.
• Substituting this expression for 54 into the previous equation, we can express 18 as a linear
• combination of 252 and 198.We conclude that
• 18 = 4 ・ (252 − 1 ・ 198) − 1 ・ 198 = 4 ・ 252 − 5 ・ 198,
• completing the solution
LINEAR CONGRUENCE

• is a congruence of the form ax  b ( mod m ), where m is a positive integer, a and b are


integers, and x is a variable.
• How can we solve it, i.e. find all integers x that satisfy it?

• One possible method is to multiply both sides of the congruence by an inverse a of a (mod m) if
one such inverse exists: a is an inverse of a (mod m) if aa 1 (mod m).

• If a and m are relatively prime integers and m > 1, then an inverse of a modulo m exists.
INVERSE MODULO

Find an inverse of 3 modulo 7.

Since gcd ( 3, 7 ) = 1, then 3 and 7 are relatively prime.


According to Theorem , 3 modulo 7 has an inverse.
According to the Euclidean algorithm, 7 = 3 * 2 + 1.
It follows that 7 – 3 * 2 = 1.
Since – 2 * 3 = – 6  1 mod 7 because 7 | – 6 – 1 = – 7
then – 2 is an inverse of 3 modulo 7.
EXAMPLE

• What are the solutions of the linear


congruence 3x  4 ( mod 7 )
• Substitute back into the original linear congruence to
check that 6 is a solution:

• which shows that all such x satisfy the congruence. We


conclude that the solutions to the congruence are the
integers x such that x ≡ 6 (mod 7), namely, 6, 13, 20, . . .
and −1, −8,−15, . . . .
CHINESE REMINDER THEOREM

• Chinese Mathematician asked in the first century:


• There are certain things whose number is unknown. When divided by 3, the remainder is 2; when
divided by 5 the remainder is 3; and when divided by 7, the remainder is 2. What

• will be the number of things?


• This puzzle is asking for the solution of the following system of congruences:
• In order to construct a simultaneous solution, let Mk = m=mk. Note that gcd(mk;Mk) = 1. So there
exists yk inverse of Mk modulo mk.

first let m = 3 ・ 5 ・ 7 = 105, M1 = m/3 = 35,M2 = m/5 = 21, and M3 = m/7 = 15.We see that 2 is an
inverse of M1 = 35 modulo 3, because 35 ・ 2 ≡ 2 ・ 2 ≡ 1 (mod 3); 1 is an inverse of M2 = 21
modulo 5, because 21 ≡1 (mod 5); and 1 is an inverse of M3 = 15 (mod 7), because 15 ≡ 1 (mod 7).
The solutions to this system are those x such that
x ≡ a1M1y1 + a2M2y2 + a3M3y3 = 2 ・ 35 ・ 2 + 3 ・ 21 ・ 1 + 2 ・ 15 ・ 1
= 233 ≡ 23 (mod 105).
INTEGERS AND
ALGORITHM
INTEGERS

• Integers can be expressed using any integer greater than one as a base
• decimal (base 10), representations,

• binary (base 2),


• octal (base 8), and
• hexadecimal (base 16) representations
REPRESENTATION OF INTEGERS

The representation of n given in Theorem is called the base b expansion of n. The base b
expansion of n is denoted by (akak−1 . . . a1a0)b.
EXAMPLE
BINARY EXPANSIONS

• Choosing 2 as the base gives binary expansions of integers. In binary notation each digit is either a
0 or a 1. In other words, the binary expansion of an integer is just a bit string. Binary expansions are
used by computers to represent and do arithmetic with integers.
OCTAL AND HEXADECIMAL

• Among the most important bases in computer science are base 2, base 8, and base 16. Base 8
expansions are called octal expansions and base 16 expansions are hexadecimal expansions.
• The hexadecimal digits used are 0,1,2,…,9,A,B,C,D,E and F where the letters A through F represent
the digits corresponding to the numbers 10 through 15.
• Example: What is the decimal expansion of the hexadecimal expansion of(2AE0B) ?

• Each hexadecimal digit can be represented using four bits


BASE CONVERSION

• Divide n by b to obtain a quotient and a remainder, i.e. n = bq0 + a0 where 0  a0 < b.


• The remainder a0 is the rightmost digit in the base b expansion of n.
• Divide q0 by b to obtain q0 = bq1 + a1 where 0  a1 < b.
• a1 is the second digit from the right in the base b expansion of n.
• Continue the process, successively dividing the quotients by b, obtaining additional
base b digits as the remainders.
• This process terminates when we obtain a quotient equal to 0
EXAMPLE
REPRESENTATIONS
ALGORITHMS FOR INTEGER OPERATIONS

The algorithms for performing operations with integers using their binary expansions are extremely
important in computer arithmetic.

• Addition
• Multiplication
The binary expansions of a and b are

a = (an−1an−2 . . . a1a0)2, b = (bn−1bn−2 . . . b1b0)2,


ADDITION ALGORITHM

• This method proceeds by adding pairs of binary digits together with carries, when they occur, to
compute the sum of two integer.
• To add a and b, first add their rightmost bits. This gives
a0 + b0 = c0 ・ 2 + s0
• where s0 is the rightmost bit in the binary expansion of a + b and c0 is the carry, which is either 0
or 1. Then add the next pair of bits and the carry.
a1 + b1 + c0 = c1 ・ 2 + s1
• Continue this process, adding the corresponding bits in the two binary expansions and the carry, to
determine the next bit from the right in the binary expansion of a + b.
• The leading bit of the sum is sn = cn−1.
EXAMPLE
MULTIPLICATION ALGORITHM

• the multiplication of two n-bit integers a and b.

• We can compute ab using this equation. We first note that abj = a if bj = 1 and abj = 0 if bj = 0. Each
time we multiply a term by 2, we shift its binary expansion one place to the left and add a zero at
the tail end of the expansion.
• Finally, we obtain ab by adding the n integers abj 2j, j = 0, 1, 2, . . . , n − 1.
EXAMPLE
ALGORITHM FOR DIV AND MOD

• Given integers a and d, d > 0, we can find q = a div d and r = a mod d.


• when a is positive we subtract d from a as many times as necessary until what is left is less than d.

• subtraction is the quotient.


• left over after all these subtractions is the remainder
MOD EXPONENTIATION

• to find bn mod m efficiently, where b, n, and m are large


integers.
• to first compute bn and then find its remainder when divided by
m because bn will be a huge number.
• Instead, we can use an algorithm that employs the binary
expansion of the exponent n.

• This shows that to compute bn, we need only compute the


values of b, b2, (b2)2 = b4, (b4)2 = b8, . . . , b2k
EXAMPLE
FIND 3644 MOD 645

• binary expansion of 644, which is (1010000100)2


MATRICES

Matrix
is a rectangular array of numbers with m rows and n columns called an m x n matrix.
Square Matrix
is a matrix with the same number of rows as columns.
Equal Matrix
are two matrices with the same number of rows and the same number of columns
and the corresponding entries in every position are equal.
MATRIX NOTATION

• The (i, j )th element or entry of A


is the element aij , that is, the
number in the ith row and j th
column of A.
• A convenient shorthand notation
for expressing the matrix A is to
write A = [aij], which indicates
that A is the matrix with its (i, j )th
element equal to aij.
MATRIX
CONTD…

Row Matrix :A matrix with only one row.


• [1 x n] matrix
Column Matrix :A matrix with only one column.
• [m x 1] matrix
EQUAL MATRICES

• Two matrices A = [aij] and B = [bij] are said to be equal (A = B) iff each element of
A is equal to the corresponding element of B, i.e., aij = bij for 1 <= i <=m, 1<=
j<=n.
MATRIX ARITHEMATIC

Matrix Addition:
Let A = [ aij ] and B = [ bij ] be m x n matrices. The sum of A and B, denoted by A + B, is the
m x n matrix that has aij + bij as its ( i, j )th element. In other words, A + B = [ aij + bij ].
CONTD….

Matrix Multiplication
Let A be an m x k matrix and B be a k x
n matrix. The product of A and B, denoted by AB,
is the m x n matrix with its ( i, j )th entry equal to
the sum of the products of the corresponding
elements from the ith row of A and the jth column
of B.
In other words, if AB = [ cij ], then cij = ai1b1j + ai2b2j + . .
. + aikbkj.’
• Matrix multiplication is not commutative.
IDENTITY

Identity Matrix
of order n is the n x n matrix In = [ dij ], where dij = 1 if i = j and dij = 0 if i  j.
TRANSPOSE

• Let A = [aij ] be an m × n matrix. The transpose of A, denoted by At, is the n × m matrix obtained by
interchanging the rows and columns of A. In other words, if At = [bij ], then bij = aji for i = 1, 2, . . . ,
n and j = 1, 2, . . . , m.
ZERO-ONE MATRICES

Zero-One Matrix
is a matrix with entries that are either 0 or 1.
Join
of Amxn = [ aij ] and Bmxn = [ bij ] zero-one matrices,
denoted by A \/ B, is the zero-one matrix with ( i, j )th entry aij \/ bij.
Meet
of Amxn = [ aij ] and Bmxn = [ bij ] zero-one matrices,
denoted by A /\ B, is the zero-one matrix with ( i, j )th entry aij /\ bij.
Boolean Product
of Amxk = [ aij ] and Bkxn = [ bij ], denoted by A  B, is the m x n matrix with ( i, j )th entry cij
where cij = ( ai1 /\ b1j ) \/ ( ai2 /\ b2j ) \/ . . . \/ ( ain /\ bnj ).

You might also like