Chapter 3
Chapter 3
Copyright © McGraw-Hill Education. All rights reserved. No reproduction or distribution 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
Section 3.1
Section Summary
Properties of Algorithms
Algorithms for Searching and Sorting
Greedy Algorithms
Halting Problem
Problems and Algorithms
In many domains there are key general problems that
ask for output with specific properties when given
valid input.
The first step is to precisely state the problem, using
the appropriate structures to specify the input and the
desired output.
We then solve the general problem by specifying the
steps of a procedure that takes a valid input and
produces the desired output. This procedure is called
an algorithm.
Algorithms Abu Ja’far Mohammed Ibin Musa Al-Khowarizmi
(780-850)
Definition: An algorithm is a finite set of precise
instructions for performing a computation or for solving a
problem.
Example: Describe an algorithm for finding the maximum
value in a finite sequence of integers.
Solution: Perform the following steps:
1. Set the temporary maximum equal to the first integer in the
sequence.
2. Compare the next integer in the sequence to the temporary
maximum.
If it is larger than the temporary maximum, set the temporary
maximum equal to this integer.
3. Repeat the previous step if there are more integers. If not, stop.
4. When the algorithm terminates, the temporary maximum is the
largest integer in the sequence.
Specifying Algorithms
Algorithms can be specified in different ways. Their steps can be
described in English or in pseudocode.
Pseudocode is an intermediate step between an English language
description of the steps and a coding of these steps using a
programming language.
The form of pseudocode we use is specified in Appendix 3. It uses
some of the structures found in popular languages such as C++ and
Java.
Programmers can use the description of an algorithm in pseudocode to
construct a program in a particular language.
Pseudocode helps us analyze the time required to solve a problem
using an algorithm, independent of the actual programming language
used to implement algorithm.
Properties of Algorithms
Input: An algorithm has input values from a specified set.
Output: From the input values, the algorithm produces the
output values from a specified set. The output values are
the solution.
Correctness: An algorithm should produce the correct
output values for each set of input values.
Finiteness: An algorithm should produce the output after a
finite number of steps for any input.
Effectiveness: It must be possible to perform each step of
the algorithm correctly and in a finite amount of time.
Generality: The algorithm should work for all problems of
the desired form.
Finding the Maximum Element in a
Finite Sequence
The algorithm in pseudocode:
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.
Insertion Sort
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.
procedure insertion sort
•Next the 3rd element is put into (a1,…,an:
the correct position among the real numbers with n ≥ 2)
first 3 elements. for j := 2 to n
•In each subsequent pass, the n+1st i := 1
element is put into its correct while aj > ai
position among the first n+1
i := i + 1
elements.
•Linear search is used to find the
m := aj
correct position. for k := 0 to j − i − 1
aj-k := aj-k-1
ai := m
{Now a1,…,an is in increasing order}
Insertion Sort
Example: Show all the steps of insertion sort with the
input: 3 2 4 1 5
i. 2 3 4 1 5 (first two positions are interchanged)
ii. 2 3 4 1 5 (third element remains in its position)
iii. 1 2 3 4 5 (fourth is placed at beginning)
iv. 1 2 3 4 5 (fifth element remains in its position)
Greedy Algorithms
Optimization problems minimize or maximize some parameter over all possible
inputs.
Among the many optimization problems we will study are:
Finding a route between two cities with the smallest total mileage.
Determining how to encode messages using the fewest possible bits.
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” choice at each step. Making the “best choice” at each step 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.
The greedy approach to solving problems is an example of an algorithmic
paradigm, which is a general approach for designing an algorithm. We return to
algorithmic paradigms in Section 3.3.
Greedy Algorithms: Making Change
Example: Design a greedy algorithm for making change (in U.S.
money) of n cents with the following coins: quarters (25 cents),
dimes (10 cents), nickels (5 cents), and pennies (1 cent) , using
the least total number of coins.
Idea: At each step choose the coin with the largest possible value
that does not exceed the amount of change left.
1. If n = 67 cents, first choose a quarter leaving 67−25 = 42 cents.
Then choose another quarter leaving 42 −25 = 17 cents
2. Then choose 1 dime, leaving 17 − 10 = 7 cents.
3. Choose 1 nickel, leaving 7 – 5 = 2 cents.
4. Choose a penny, leaving one cent. Choose another penny leaving 0
cents.
Greedy Change-Making Algorithm
Solution: Greedy change-making algorithm for n cents. The
algorithm works with any coin denominations c1, c2, …,cr .
procedure change(c1, c2, …, cr: values of coins, where c1> c2> … > cr ;
n: a positive integer)
for i := 1 to r
di := 0 [di counts the coins of denomination ci]
while n ≥ ci
di := di + 1 [add a coin of denomination ci]
n = n - ci
[di counts the coins ci]
Start: 9:00 AM
Talk 1
Talk 2
Start: 9:45 AM
End :9:45 AM
End: 10:00 AM Talk 3
End: 11:00 AM
For many applications, the goal is to select the function g(x) in O(g(x))
as small as possible (up to multiplication by a constant, of course).
Using the Definition of Big-O Notation
Example: Show that 7x2 is O(x3).
Solution: When x > 7, 7x2 < x3. Take C =1 and k = 7
as witnesses to establish that 7x2 is O(x3).
(Would C = 7 and k = 1 work?)
Example: Show that n2 is not O(n).
Solution: Suppose there are constants C and k for
which n2 ≤ Cn, whenever n > k. Then (by dividing
both sides of n2 ≤ Cn) by n, then n ≤ C must hold for
all n > k. A contradiction!
Big-O Estimates for Polynomials
Example: Let
where are real numbers with an ≠0.
Then f(x) is O(xn). Uses triangle inequality,
an exercise in Section 1.8.
n n-1
Proof: |f(x)| = |anx + an-1 x + ∙∙∙ + a1x + a0|1
Continued →
Big-O Estimates for some
Important Functions
Example: Use big-O notation to estimate log n!
Solution: Given that (previous slide)
then .
Hence, log(n!) is O(n∙log(n)) taking C = 1 and k = 1.
Display of Growth of Functions
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 f2 (x) ≤ C2|g2(x) | when x > k2 .
|( f1 + f2 )(x)| = |f1(x) + f2(x)|
≤ |f1 (x)| + |f2 (x)| by the triangle inequality |a + b| ≤ |a| + |b|
|f1 (x)| + |f2 (x)| ≤ C1|g1(x) | + C2|g2(x) |
≤ C1|g(x) | + C2|g(x) | where g(x) = max(|g1(x)|,|g2(x)|)
= (C1 + C2) |g(x)|
= C|g(x)| where C = C1 + C2
Therefore |( f1 + f2 )(x)| ≤ C|g(x)| whenever x > k, where k = max(k1,k2).
Ordering Functions by Order of Growth
Put the functions below in order so that each function is
big-O of the next function on the list.
f1(n) = (1.5)n We solve this exercise by successively finding the function that
grows slowest among all those left on the list.
f2(n) = 8n +17n +111
3 2
• f (n) = 10000 (constant, does not increase with n)
f3(n) = (log n ) 2 9
•f (n) = (log n )3
2
(grows next slowest)
f5(n) = log (log n)
•f (n) = n (log n) (next largest, (log n) factor smaller than any power of n)
6
2 3 3
f6(n) = n (log n)
2 3
•f (n) = 8n +17n +111 (tied with the one below)
2
3 2
n
f7(n) = 2 (n +1)
2
•f (n) = n + n(log n)
8
3 2
(tied with the one above)
f10(n) = n! •f (n) = 2 (n +1) (grows faster than above because of the n +1 factor)
7
n 2 2
Continued →
Computing the Closest Pair of
Points by Brute-Force
Algorithm for finding the closest pair in a set of n points.
procedure closest pair((x1, y1), (x2, y2), … ,(xn, yn): xi, yi real numbers)
min = ∞
for i := 1 to n
for j := 1 to i
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(log n) worst-case complexity in Section
8.3.
Understanding the Complexity of
Algorithms
Understanding the Complexity of
Algorithms
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.