Professional Documents
Culture Documents
Chapter-2: Algorithms, Integers and Matrices
Chapter-2: Algorithms, Integers and Matrices
Chapter-2: Algorithms, Integers and Matrices
• 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
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
• 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
• 24 is a multiple of 4
• 3 | 7 True or False ? False
DIVISION ALGORITHM
• 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 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 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.
• 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
• 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:
• 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.
• 198 = 3 54 + 36
• 54 = 1 36 + 18
• 36 = 2 18
• 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
• 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
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,
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) ?
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
• 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
• 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
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
• 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 ).