Lecture 10-Fffcomplexity of Algorithms

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

EE 206000

Discrete Mathematics

Lecture 10
ComplexityofAlgorithms
Jen-Ming Wu
Inst. of Communications Engineering
Dept. of Electrical Engineering
National Tsing Hua University
Email: [email protected]
Spring,2015
40

ComplexityofAlgorithms
Howcantheefficiencyofanalgorithmbeanalyzed?
me( mecomplexity)
memory(spacecomplexity)
1.Timecomplexity:ananalysisofthetimerequiredtosolvea
problemofaparticularsize.
a) Describedintermsofthenumberofoperationsrequired
insteadof actualcomputertime
b) Operation:comparison,addition,multiplication,division
c) Platformindependentmeasure
d) Worstcasecomplexity,averagecasecomplexity
2.Spacecomplexity:ananalysisofthecomputermemory
requiredtosolveaproblemofaparticularsize.(Datastructure)
41

ComplexityofAlgorithms
Example 1. Describe the time complexity of Find the max
element in a finite set of integers (specified as Algorithm 1).

Solution: The number of comparisons will be used as the


measure of the time complexity of the algorithm.
42

ComplexityofAlgorithms
Solution (cntd):
Set max a1
i 2 : compare(2, n), compare(max, a 2), set max a 2 or not
i 3 : compare(3, n), compare(max, a 3), set max a 3 or not
......
i n : compare(n, n), compare(max, an), set max an or not
i n 1 : compare(n 1, n), exit the loop
There are 2(n-1 )+1 = 2n-1 comparisons are used. Hence, the time
complexity is (n). Note that for this algorithm the numbers of
comparisons is independent of particular input of n numbers.
43

ComplexityofAlgorithms
Worst-case Complexity: By 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.
Average-case Complexity: 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.
44

ComplexityofAlgorithms
Example 2. Describe the time complexity of the linear search
algorithm(specified as Algorithm 2).

45

ComplexityofAlgorithms
Solution:
worst-case:
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. Hence, if x ai, 2i 1 comparisons are used. At most 2n 2
comparisons are required when the element is not in the list.
At most 2n 2 comparisons are required when the element is not
in the list. In this case, 2n comparisons are used to determine that
x is not ai, an additional comparison is used to exit the loop, and
one comparison is made outside the loop. Consequently, a linear
search requires (n) comparisons in the worst case.
46

ComplexityofAlgorithms
Example 3. Describe the average-case time complexity of the
linear search algorithm, assuming that x is in the list.

Solution : average - case :


If x is the ith term of the list, a total of 2i 1 comparisons are
needed. Hence, the average number of comparisons used equals
[3 5 7 ... (2n 1)] n n 2
which is (n).

47

ComplexityofAlgorithms
Example 4. Describe the time complexity of the binary search
algorithm(specified as Algorithm 3).

48

ComplexityofAlgorithms
Solution (Binary Search):

Assume n 2k elements in the list. (If n is not a power of 2, the list

can be considered part of a larger list with 2k 1 elements.)


At each stage of the algorithm, there are two comparisons:
(1) seeing whether the restricted list has more than one term,
(2) determining whether x is greater than the middle term.

2k comparisons

2 k elements
20 element
When one term is left in the list, one comparison
tells that there are no additional terms left, and
one more comparison to determine if this term is x.
Hence, at most 2 log n 2 comparisons are required to
perform a binary search. The worst-case complexity is (log n) .49

ComplexityofAlgorithms
Example 5. Describe the worst-case complexity of the bubble
sort (specified as Algorithm 4) in terms of the number of
comparisons made.

Solution:
During each pass the bubble sort successively compares
adjacent elements, interchanging them if necessary.
When the ith pass begins, the i - 1 largest elements are
guaranteed to be in the correct positions. During this pass,
n-i comparisons are used.

50

ComplexityofAlgorithms
Solution (Bubble Sort):

...

(n-1) comparisons

(n-2) comparisons

1 comparison

(n-1) (n-2) ... 2 1 (n-1)n / 2


Hence, it has (n 2 ) worst - case complexity.

51

ComplexityofAlgorithms
Example 6. What is the worst-case complexity of the insertion
sort (specified as Algorithm 5) in terms of the number of
comparisons made?

52

ComplexityofAlgorithms
Solution (Insertion Sort):
The insertion sort inserts the jth element into the correct
position among the first j-1 elements that have already
been put into the correct order. It does this by using a
linear search technique. Consequently, in the worst case,
j comparisons are required to insert the jth element into
the correct position.
...
2 comparisons

3 comparisons

n comparisons

2 3 ... n [n(n 1) / 2]-1


The insertion sort has worst - case complexity (n 2 ) .

53

ComplexityofAlgorithms
An algorithmic paradigm is a general approach based on a
particular concept that can be used to construct algorithms for
solving a variety of problems. Some of the algorithms we have
already studied are based on an algorithmic paradigm known
as brute force.
In a brute-force algorithm, a problem is solved in the most
straightforward manner based on the statement of the problem
and the definitions of terms. Brute-force algorithms are
designed to solve problems without regard to the computing
resources required. In some brute-force algorithms the solution
to a problem is found by examining every possible solution,
looking for the best possible.
54

ComplexityofAlgorithms
For example, Algorithm 1 for finding the maximum number in
a sequence is a brute-force algorithm because it examines each
of the n numbers in a sequence to find the maximum term. The
algorithm for finding the sum of n numbers by adding one
additional number at a time is also a brute-force algorithm.

55

ComplexityofAlgorithms
Example 7. Construct a brute-force algorithm for finding the
closest pair of points in a set of n points in the plane.
Solution:
Algorithm 1. Brute-Force Algorithm for Closest Pair of Points
procedure closest-pair (( x1,y1), ( x 2 ,y 2),..., ( xn,yn ) : pairs of real numbers)
min
for i : 2 to n
for j : 1 to i 1
if ( xj xi ) 2 ( yj-yi ) 2 min then
min : ( xj xi ) 2 ( yj-yi ) 2
closest pair : (( xi, yi ), ( xj , yj ))
return closest pair
56

Commonly Used Terminology

57

EndofAlgorithmsLectureSeries

58

You might also like