Chapter 3 The Efficiency of Algorithms. Figure 3.3 A Choice of Algorithms.
Chapter 3: The Efficiency of Algorithms - 國立中興大學 · 2013-09-29 · Chapter 3: The...
Transcript of Chapter 3: The Efficiency of Algorithms - 國立中興大學 · 2013-09-29 · Chapter 3: The...
Chapter 3: The Efficiency of
Algorithms
Invitation to Computer Science,
C++ Version, 6-th Edition
Invitation to Computer Science, C++ Version, 6E 2
Objectives
In this chapter, you will learn about
Attributes of algorithms
Measuring efficiency
Analysis of algorithms
When things get out of hand
Invitation to Computer Science, C++ Version, 6E 3
Introduction
Desirable characteristics in an algorithm
Correctness
Ease of understanding (clarity)
Elegance
Efficiency
Invitation to Computer Science, C++ Version, 6E 4
Attributes of Algorithms
Correctness
Does the algorithm solve the problem it is designed for?
Does the algorithm solve the problem correctly?
Ease of understanding (clarity)
How easy is it to understand or alter the algorithm?
Important for program maintenance
Invitation to Computer Science, C++ Version, 6E 5
Attributes of Algorithms (con’t)
Elegance
How clever is the algorithm?
Sometimes elegance and ease of understanding work at cross-purposes
Efficiency
How much time and/or space does the algorithm require when executed?
Perhaps the most important desirable attribute
Efficiency of Algorithms
How long does it take an algorithm to run? Depends on computer Depends on input data
Benchmarking Run same algorithm and same input on difference
machines Runs same algorithm on same machine using difference
input
Need a method of measuring inherent efficiency of algorithm, independent of machine or input
Count how many times “work unit” is executed
Invitation to Computer Science, C++ Version, 6E 6
Invitation to Computer Science, C++ Version, 6E 7
Measuring Efficiency
Analysis of algorithms
Study of the efficiency of various algorithms
Efficiency measured as a function relating size of input to time or space used
For one input size, best case, worst case, and average case behavior must be considered
The notation captures the order of magnitude of the efficiency function (work units)
Invitation to Computer Science, C++ Version, 6E 8
Sequential Search
Search for NAME among a list of n names
Start at the beginning and compare NAME to each entry until a match is found
9
Figure 3.1 Sequential Search Algorithm
http://note-for-it.blogspot.com/
comparison
Invitation to Computer Science, C++ Version, 6E
Invitation to Computer Science, C++ Version, 6E 10
Sequential Search (con’t)
Count the comparisons of the NAME being searched against a name in the list
Central unit of work
Used for efficiency analysis
For lists with n entries
Best case
NAME is the first name in the list
1 comparison
(1)
Invitation to Computer Science, C++ Version, 6E 11
Sequential Search (con’t)
Worst case
NAME is the last name in the list
NAME is not in the list
n comparisons
(n)
Average case
Roughly n/2 comparisons
(n)
Invitation to Computer Science, C++ Version, 6E 12
Sequential Search (con’t)
Space efficiency
Uses essentially no more memory storage than original input requires
Very space efficient
n
Invitation to Computer Science, C++ Version, 6E 13
Order of Magnitude: Order n
As n grows large, order of magnitude dominates running time, minimizing effect of coefficients and lower-order terms
All functions that have a linear shape are considered equivalent
Order of magnitude n
Written (n)
Functions vary as a constant times n
Linear (n)
n
10n
100n
Invitation to Computer Science, C++ Version, 6E 14
Figure 3.4
Work = cn for Various Values of c
Quick Quiz
1. In the sequential search algorithm, the minimum amount of work is done if NAME is the ____ name in the list.
2. The ____ case of an algorithm requires the minimum amount of work. 3. The ____ case of an algorithm requires the maximum amount of work.
4. The selection sort algorithm does the same amount of work no matter how
the numbers are initially arranged. True or False?
5. n grows at a much faster rate than n2. True or False?
Answer: first
Answer: best
Answer: worst
Answer: True
Answer: false
Invitation to Computer Science, C++ Version, 6E 15
Invitation to Computer Science, C++ Version, 6E 16
Selection Sort
Sorting
Take a sequence of n values and rearrange them into order
Selection sort algorithm
Repeatedly searches for the largest value in a section of the data
Moves that value into its correct position in a sorted section of the list
Uses the Find Largest algorithm
17
marker
http://users.informatik.uni-halle.de/~jopsi/dinf203/chap6.shtml
Figure 3.6 Selection Sort Algorithm
comparison
Invitation to Computer Science, C++ Version, 6E 18
Selection Sort (con’t)
Count comparisons of largest so far against other values
Find Largest, given m values, does m-1 comparisons
Selection sort calls Find Largest (step 4) n times,
Each time with a smaller list of values
Cost = n-1 + (n-2) + … + 2 + 1 = n(n-1)/2
Invitation to Computer Science, C++ Version, 6E 19
Selection Sort (con’t)
Time efficiency
Comparisons: n(n-1)/2
Exchanges: n (swapping largest into place)
Overall: (n2), for best, worst, average cases
Space efficiency
Space for the input sequence, plus a constant number of local variables ( n)
Invitation to Computer Science, C++ Version, 6E 20
Order of Magnitude – Order n2
All functions with highest-order term cn2 have similar shape
An algorithm that does cn2 work for any constant c is order of magnitude n2, or (n2)
21
Order of Magnitude – Order n2 (con’t)
Anything that is (n2) will eventually have larger values than anything that is (n), no matter what the constants are
An algorithm that runs in time (n) will outperform
one that runs in (n2)
Invitation to Computer Science, C++ Version, 6E
Invitation to Computer Science, C++ Version, 6E 22
Figure 3.10 Work = cn2 for Various Values of c
Invitation to Computer Science, C++ Version, 6E 23
Figure 3.11
A Comparison of n and n2
Invitation to Computer Science, C++ Version, 6E 24
Analysis of Algorithms
Multiple algorithms for one task may be compared for efficiency and other desirable attributes
Data cleanup problem
Search problem
Pattern matching
Invitation to Computer Science, C++ Version, 6E 25
Data Cleanup Algorithms
Given a collection of numbers, find and remove all zeros
Possible algorithms
Shuffle-left
Copy-over
Converging-pointers
Invitation to Computer Science, C++ Version, 6E 26
The Shuffle-Left Algorithm
Scan list from left to right
When a zero is found, shift all values to its right one slot to the left
Invitation to Computer Science, C++ Version, 6E 27
Figure 3.14 The Shuffle-Left Algorithm for Data Cleanup
examinations
of list values
shifts
Invitation to Computer Science, C++ Version, 6E 28
Invitation to Computer Science, C++ Version, 6E 29
Invitation to Computer Science, C++ Version, 6E 30
The Shuffle-Left Algorithm (con’t)
Time efficiency
Count examinations of list values and shifts
Best case
No shifts, n examinations
(n)
Worst case
Shift at each pass, n passes
n2 shifts plus n examinations
(n2)
Invitation to Computer Science, C++ Version, 6E 31
The Shuffle-Left Algorithm (con’t)
Space efficiency
n slots for n values, plus a few local variables
(n)
Invitation to Computer Science, C++ Version, 6E 32
The Copy-Over Algorithm
Use a second list
Copy over each nonzero element in turn
Time efficiency
Count examinations and copies
Best case
All zeros
n examinations and 0 copies
(n)
Invitation to Computer Science, C++ Version, 6E 33
Figure 3.15 The Copy-Over Algorithm for Data Cleanup
Invitation to Computer Science, C++ Version, 6E 34
The Copy-Over Algorithm (con’t)
Time efficiency (continued)
Worst case
No zeros
n examinations and n copies
(n)
Space efficiency
2n slots for n values, plus a few extraneous variables
Invitation to Computer Science, C++ Version, 6E 35
The Copy-Over Algorithm (con’t)
Time/space tradeoff
Algorithms that solve the same problem offer a tradeoff
One algorithm uses more time and less memory
Its alternative uses less time and more memory
Invitation to Computer Science, C++ Version, 6E 36
The Converging-Pointers Algorithm
Swap zero values from left with values from right until pointers converge in the middle
Time efficiency
Count examinations and swaps
Best case
n examinations, no swaps
(n)
37
Figure 3.16 The Converging-Pointers Algorithm for Data Cleanup
Invitation to Computer Science, C++ Version, 6E
Invitation to Computer Science, C++ Version, 6E 38
The Converging-Pointers Algorithm
(con’t)
Time efficiency (continued)
Worst case
n examinations, n swaps
(n)
Space efficiency
n slots for the values, plus a few extra variables
Invitation to Computer Science, C++ Version, 6E 39
Figure 3.17 Analysis of Three Data Cleanup Algorithms
Invitation to Computer Science, C++ Version, 6E 40
Binary Search Algorithm
Given ordered data
Search for NAME by comparing to middle element
If not a match, restrict search to either lower or upper half only
Each pass eliminates half the data
Example of Binary Search
Invitation to Computer Science, C++ Version, 6E 41
Invitation to Computer Science, C++ Version, 6E 42
Figure 3.18 Binary Search Algorithm (list must be sorted)
Invitation to Computer Science, C++ Version, 6E 43
Binary search tree for a 7-element list
Invitation to Computer Science, C++ Version, 6E 44
Binary Search Algorithm (con’t)
Efficiency
Best case
1 comparison
(1)
Worst case
lg n comparisons
lg n: The number of times n can be divided by two before reaching 1
(lg n)
Invitation to Computer Science, C++ Version, 6E 45
Binary Search Algorithm (con’t)
Tradeoff
Sequential search
Slower, but works on unordered data
Binary search
Faster (much faster), but data must be sorted first
Invitation to Computer Science, C++ Version, 6E 46
Figure 3.21 A Comparison of n and lg n
Invitation to Computer Science, C++ Version, 6E 47
Pattern-Matching Algorithm
Analysis involves two measures of input size
m: length of pattern string
n: length of text string
Unit of work
Comparison of a pattern character with a text character
Invitation to Computer Science, C++ Version, 6E 48
Pattern-Matching Algorithm (con’t)
Efficiency
Best case
Pattern does not match at all
n - m + 1 comparisons
(n)
Worst case
Pattern almost matches at each point
(m -1)(n - m + 1) comparisons
(m x n)
Invitation to Computer Science, C++ Version, 6E 49
Figure 3.22 Order-of-Magnitude Time Efficiency Summary
Quick Quiz
1. In the shuffle-left algorithm, the best case occurs when the list has no 0 values. True or False?
2. The best case for the converging-pointers algorithm is a list of all 0 entries. True or False?
3. The binary search algorithm works only on a list that has already been ____.
Answer: True
Answer: False
Answer: sorted
Invitation to Computer Science, C++ Version, 6E 50
Invitation to Computer Science, C++ Version, 6E 51
When Things Get Out of Hand
Polynomial bound algorithms
Work done is no worse than a constant multiple of n2
Intractable algorithms
Run in worse than polynomial time
Examples
Hamiltonian circuit
Bin-packing
When Things Get Out Of Hand:
Hamiltonian Circuits
A path that begins and ends at the same node and goes through all other nodes exactly once following the edges.
A B
C D
Invitation to Computer Science, C++ Version, 6E 52
Invitation to Computer Science, C++ Version, 6E 53
Hamiltonian circuits among all paths from A
When Things Get Out Of Hand:
Bin Packing Problem
Given:
an unlimited number of bins with volume 1 unit
N objects with volume between 0.0 and 1.0
Find:
The minimum number of bins to store the n objects
裝箱問題演算法
Invitation to Computer Science, C++ Version, 6E 54
Greedy Approximation Algorithm
First-fit algorithm 1. The algorithm processes the items in arbitrary order.
2. For each item, it attempts to place the item in the first bin that can accommodate the item.
3. If no bin is found, it opens a new bin and puts the item within the new bin.
Invitation to Computer Science, C++ Version, 6E 55
=>This algorithm achieves an approximation factor of 2
Invitation to Computer Science, C++ Version, 6E 56
When Things Get Out of Hand (con’t)
Exponential algorithm
(2n)
More work than any polynomial in n
Approximation algorithms
Run in polynomial time but do not give optimal solutions
Invitation to Computer Science, C++ Version, 6E 57
Figure 3.25 Comparisons of lg n, n, n2 , and 2n
Invitation to Computer Science, C++ Version, 6E 58
Figure 3.27 A Comparison of Four Orders of Magnitude
Invitation to Computer Science, C++ Version, 6E 59
Summary of Level 1
Level 1 (Chapters 2 and 3) explored algorithms
Chapter 2
Pseudocode
Sequential, conditional, and iterative operations
Algorithmic solutions to various practical problems
Chapter 3
Desirable properties for algorithms
Time and space efficiencies of a number of algorithms
Invitation to Computer Science, C++ Version, 6E 60
Summary
Desirable attributes in algorithms
Correctness
Ease of understanding (clarity)
Elegance
Efficiency
Efficiency—an algorithm’s careful use of resources—is extremely important
Invitation to Computer Science, C++ Version, 6E 61
Summary (continued)
To compare the efficiency of two algorithms that do the same task
Consider the number of steps each algorithm requires
Efficiency focuses on order of magnitude