0% found this document useful (0 votes)
10 views

Data Structure Lect9 Week 3

Uploaded by

Hasnain Nisar
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)
10 views

Data Structure Lect9 Week 3

Uploaded by

Hasnain Nisar
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/ 10

Discrete Structure

Lecture 9
Topics
 Complexity of the Algorithm.

 Worst Case Complexity

 Average Case Complexity

 Euclidean’s Algorithm
 Examples

 Bezout’s Theorm

 GCD Linear Combination


Complexity of Algorithm
Complexity of the Linear Search Algorithms.
The number of comparison used by the linear search Algorithm will be taken as a
measures as a time complexity.
 At each step of the loop in the algorithm, two comparisons are performed—
one i ≤ n, to see whether the end of the list has been reached and one x ≤ ai,
to compare the element x with a term of the list.
 Finally, one more comparison i ≤ n is made outside the loop. Consequently, if
x = ai , 2i + 1 comparisons are used.
 The most comparisons, 2n + 2, are required when the element is not in the
list.
Worst Case Analysis

The worst-case performance of an algorithm, we mean the largest number of


operations needed to solve the given problem using this algorithm on input of specified
size.

Worst-case analysis tells us how many operations an algorithm requires to guarantee


that it will produce a solution.

Hence, a linear search requires (n) comparisons in the worst case, because 2n + 2 is (n).
Average Case
 The average number of operations used to solve the problem over all possible
inputs of a given size is found in this type of analysis.
 Average case time complexity analysis is usually much more complicated than
worst-case analysis.
3 + 5 + 7 +···+ (2n + 1) = 2(1 + 2 + 3 +···+ n) + n
n n
Using the formula
1 + 2 + 3 +···+ n = n(n + 1)/ 2 .
Hence, the average number of comparisons used by the linear search algorithm
(when x is known to be in the list) is
2[n(n + 1)/2] + 1 = n + 2
n
Euclidean’s Algorithm
Computing the greatest common divisor of two integers directly from the prime
factorizations of these integers is inefficient.
The reason is that it is time-consuming to find prime factorizations.
We will give a more efficient method of finding the greatest common divisor,
called the Euclidean algorithm

Let a and b be positive integers. Let a = b q + r, where a, b, q, and r are integers.

Then gcd (a, b) = gcd ( b, r).


Example of Euclidean’s
Find gcd(91, 287).
First, divide 287, the larger of the two integers, by 91, the smaller, to obtain
287 = 91 · 3 + 14
Any divisor of 91 and 287 must also be a divisor of 287 − 91 · 3 = 14.
Also, any divisor of 91 and 14 must also be a divisor of 287 = 91 · 3 + 14.
Hence, the greatest common divisor of 91 and 287 is the same as the greatest common
divisor of 91 and 14
Next, divide 91 by 14 to obtain 91 = 14 · 6 + 7.
Because any common divisor of 91 and 14 also divides 91 − 14 · 6 = 7 and any common
divisor of 14 and 7 divides 91,
it follows that gcd(91, 14) = gcd(14, 7).
Continue by dividing 14 by 7, to obtain 14 = 7 · 2.
Because 7 divides 14, it follows that gcd(14, 7) = 7.
Furthermore, because gcd(287, 91) = gcd(91, 14) = gcd(14, 7) = 7
Example 2 of Euclidean’s
Find the greatest common divisor of 414 and 662 using the Euclidean algorithm.
Solution:
Successive uses of the division algorithm give:

662 = 414 · 1 + 248


414 = 248 · 1 + 166
248 = 166 · 1 + 82
166 = 82 · 2 + 2
82 = 2 · 41
Hence, gcd(414, 662) = 2, because 2 is the last nonzero remainder.
Bezout’s Theorm
If a and b are positive integers, then there exist integers s and t such that
gcd(a, b) = sa + tb.

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

gcd(6, 14) = 2,
and 2 = (−2) · 6 + 1 · 14.
GCD as a Linear Combination
An important result we will use throughout the remainder of this section is that
the greatest common divisor of two integers a and b can be expressed in the
form
sa + tb,
where s and t are integers.
In other words, gcd(a, b) can be expressed as a linear combination with integer
coefficients of a and b.

For example, gcd(6, 14) = 2, and 2 = (−2) · 6 + 1 · 14.

You might also like