Module 1 Algorithms PDF
Module 1 Algorithms PDF
Proofs
Chapter 3: Algorithms
© 2019 McGraw-Hill Education. All rights reserved. Authorized only for instructor use in the classroom. No reproduction or further distribution permitted without the prior written consent of McGraw-Hill Education.
Chapter Summary
Algorithms
• Example Algorithms
• Algorithmic Paradigms
Growth of Functions
• Big-O and other Notation
Complexity of Algorithms
Properties of Algorithms
Algorithms for Searching and Sorting
Greedy Algorithms
Halting Problem
• Sorting algorithms are useful to illustrate the basic notions of computer science.
In Section 3.3, we’ll study the amount of time required to sort a list using the
sorting algorithms covered in this section.
© 2019 McGraw-Hill Education
Bubble Sort 1
At the first pass the largest element has been put into the correct
position
At the end of the second pass, the 2nd largest element has been
put into the correct position.
In each subsequent pass, an additional element is put in the
correct position.
Jump to long description
© 2019 McGraw-Hill Education
Insertion Sort 1
Insertion sort begins with the 2nd element. It compares the 2nd
element with the 1st and puts it before the first if it is not larger.
Show that the change making algorithm for U.S. coins is optimal.
Lemma 1: If n is a positive integer, then n cents in change using
quarters, dimes, nickels, and pennies, using the fewest coins
possible has at most 2 dimes, 1 nickel, 4 pennies, and cannot have
2 dimes and a nickel. The total amount of change in dimes,
nickels, and pennies must not exceed 24 cents.
Proof: By contradiction
• If we had 3 dimes, we could replace them with a quarter and a nickel.
• If we had 2 nickels, we could replace them with 1 dime.
• If we had 5 pennies, we could replace them with a nickel.
• If we had 2 dimes and 1 nickel, we could replace them with a quarter.
• The allowable combinations, have a maximum value of 24 cents; 2 dimes
and 4 pennies.
© 2019 McGraw-Hill Education
Proving Optimality for U.S. Coins 2
| f ( x) | C | g ( x) |
f ( x) is O ( g ( x )
• But this is an abuse of the equals sign since the meaning is that there is an
inequality relating the values of f and g, for sufficiently large values of x.
Usually, we will drop the absolute value sign since we will always
deal with functions that take on positive values.
© 2019 McGraw-Hill Education
Using the Definition of Big-O Notation 1
0 x2 + 2 x + 1 x2 + 2 x2 + x2 = 4 x2
( )
f ( x) = x 2 +2 x + 1 is O x 2
Both f ( x) = x 2 +2 x + 1 and g ( x) = x 2
are such that f ( x) is O ( g ( x)) and g ( x) is O ( f ( x)) .
We say that the two functions are of the same order. (More on
this later)
If f ( x) is O ( g ( x ) ) and h(x) is larger than g(x) for all positive real
numbers, then f ( x) is O ( h ( x ) ) .
( )
n ! is O n n taking C = 1 and k = 1.
If f1 ( x) is O ( g1 ( x ) ) and f 2 ( x ) is O ( g 2 ( x ) ) then
( f1 + f 2 ) ( x) (
is O max (|g1 ( x ) |,| g 2 ( x ) |) . )
• By the definition of big-O notation, there are constants C1,C2 ,k1,k2 such that
| f1 ( x) C1 | g1 ( x ) | when x k1 and f 2 ( x ) C2 | g 2 ( x ) | when x k2 .
| ( f1 + f 2 ) ( x ) | = | f1 ( x ) + f 2 ( x ) | by the triangle inequality |a + b| | a | + | b|
| f1 ( x) | + | f 2 ( x ) |
| f1 ( x) | + | f 2 ( x ) | C1 | g1 ( x ) | + C 2 | g 2 ( x ) |
C1 | g ( x ) | + C 2 | g ( x ) | where g ( x) = max (| g1 ( x ) |,| g 2 ( x ) |)
= ( C1 + C2 ) | g ( x) |
= C | g ( x) | where C = C1 + C2
f 7 ( n) = 2n n 2 + 1 ( ) f1 ( n) = (1.5)
n
( next largest, an exponential function )
f 4 ( n) = 2n ( grows faster than one above since 2 > 1.5)
f8 ( n) = n + n ( log n)
3 2
(
f 7 ( n) = 2n n 2 + 1 ) (grows faster than above because of the n 2 +1 factor)
f9 ( n) = 10000 f10 ( n) = n ! (n ! grows faster thancn for every c)
f10 ( n) = n !
© 2019 McGraw-Hill Education
Big-Omega Notation 1
Example: Show that the sum of the first n positive integers is Θ(n2).
Solution: Let
• We have already shown that f(n) is O(n2).
• To show that f(n) is Ω(n2), we need a positive constant C such that
f(n) > Cn2 for sufficiently large n. Summing only the terms greater
than n/2 we obtain the inequality
1+ 2 + ( )
+ n n / 2 + n / 2 + 1 + +n
n / 2 + n / 2 + + n / 2
( )
= n − n / 2 + 1 n / 2
( n / 2)( n / 2) = n 2 / 4
• Taking C =[ ¼, f(n) > Cn2 for all positive integers n. Hence, f(n) is
Ω(n2), and we can conclude that f(n) is Θ(n2).
© 2019 McGraw-Hill Education
Big-Theta Notation 3
Hence, 3 x + 8 x log x is ( x ) .
2 2
Note: There is no need to compute the square root, since the square of the
distance between two points is smallest when the distance is smallest.
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
The algorithm loops through n(n −1)/2 pairs of points, computes the
value
(xj − xi)2 + (yj − yi)2 and compares it with the minimum, etc. So, the
algorithm uses Θ(n2) arithmetic and comparison operations.
We will develop an algorithm with O(n log n) worst-case complexity in
Section 8.3.
© 2019 McGraw-Hill Education
Understanding the Complexity of
Algorithms 1
n 𝐥𝐨𝐠 n n n 𝐥𝐨𝐠 n n2 2n n!
The P versus NP problem asks whether the class P = NP? Are there problems
whose solutions can be checked in polynomial time, but can not be solved in
polynomial time?
• Note that just because no one has found a polynomial time algorithm is different
from showing that the problem can not be solved by a polynomial time algorithm.
If a polynomial time algorithm for any of the problems in the NP complete class
were found, then that algorithm could be used to obtain a polynomial time
algorithm for every problem in the NP complete class.
• Satisfiability (in Section 1.3) is an NP complete problem.
It is generally believed that P≠NP since no one has been able to find a
polynomial time algorithm for any of the problems in the NP complete class.
The problem of P versus NP remains one of the most famous unsolved
problems in mathematics (including theoretical computer science). The Clay
Mathematics Institute has offered a prize of $1,000,000 for a solution.
© 2019 McGraw-Hill Education
Appendix of Image Long
Descriptions
There are four passes of using a bubble sort for a column of numbers. There are 5
columns in the first pass. In the first column, the numbers from the top to the bottom
are 3, 2, 4, 1, and 5. Numbers 3 and 2 are interchanged. In the second column, the
numbers are 2, 3, 4, 1, and 5. Numbers 3 and 4 are in the correct order. In the third
column, numbers 4 and 1 are interchanged. In the fourth column, the numbers are 2,
3, 1, 4, and 5. Numbers 4 and 5 are in the correct order. There are 3 columns in the
second pass. In the first column, the numbers are 2, 3, 1, 4, and 5. Numbers 2 and 3
are in the correct order, number 5 is guaranteed to be in the correct order. In the
second column, numbers 3 and 1 are interchanged, number 5 is guaranteed to be in
the correct order. In the third column, the numbers are 2, 1, 3, 4, and 5. Numbers 3
and 4 are in the correct order, and number 5 is guaranteed to be in the correct order.
There are 2 columns in the third pass. In the first column, the numbers are 2, 1, 3, 4,
and 5. Numbers 2 and 1 are interchanged, numbers 4 and 5 are guaranteed to be in
the correct order. In the second column, the numbers are 1, 2, 3, 4, and 5. Numbers 2
and 3 are in the correct order, numbers 4 and 5 are guaranteed to be in the correct
order. There is one column in the fourth pass. The numbers are 1, 2, 3, 4, and 5.
Numbers 1 and 2 are in the correct order, numbers 3, 4 and 5 are guaranteed to be in
the correct order.s
Jump to the image
© 2019 McGraw-Hill Education
Halting Problem - Appendix 1
Appendix
The first curve is given by the equation x squared. The
second curve is given by the equation x squared plus
two x plus one. The third curve is given by the equation
4 times x squared. The first and the second curves
intersect at the point 1, 4. After the intersection point,
the second curve is between the first and the third
curves and highlighted.
Appendix
The first curve is Cg(x). The second curve is f(x). The
third curve is g(x). The first and the second curves
intersect at x = k. After the intersection point, the
second curve is between the first and the third curves
and highlighted.