Schneider - Ch03 - Inv To CS 8e
Schneider - Ch03 - Inv To CS 8e
Schneider - Ch03 - Inv To CS 8e
The Efficiency of
Algorithms
Learning Objectives
• Describe algorithm attributes and why they are important
• Explain the purpose of efficiency analysis and apply it to new
algorithms to determine the order of magnitude of their time
efficiencies
• Describe, illustrate, and use the algorithms from the chapter,
including: sequential and binary search, selection sort, data
cleanup algorithms, pattern-matching
• Explain which orders of magnitude grow faster or slower than
others
• Describe what a “suspected intractable” problem is, giving
one or more examples, and the purpose of approximation
algorithms that partially solve them
Introduction
• What makes one algorithm better than another?
– How can we judge and compare algorithms?
• Metaphor: purchasing a car vs. attributes of interest
– Ease of handling
– Style
– Fuel efficiency
– Ease of understanding
– Elegance
– Time/space efficiency
Attributes of Algorithms (1 of 3)
• Correctness
– Is the problem specified correctly?
– Does the algorithm produce the correct result?
• Example: pattern matching
– Problem spec: “given pattern p and text t, determine
the location, if any, of pattern p occurring in text t”
– Algorithm correct: does it always work?
Attributes of Algorithms (2 of 3)
• Ease of understanding: Can somebody other than
the author easily understand it?
– Examples: Checking correctness and program
maintenance
• Elegance: using a clever or nonobvious approach
– Example: Gauss’s summing of 1 + 2 + … + 100
• Attributes may conflict: elegance often conflicts with
ease of understanding
• Attributes may reinforce each other: ease of
understanding supports correctness
Attributes of Algorithms (3 of 3)
• Efficiency: an algorithm’s use of time and space
resources
– Timing an algorithm is not always useful
– Confounding factors: machine speed, size of input
• Benchmarking: timing an algorithm on standard
data sets
– Testing hardware and operating system, etc.
– Testing real-world performance limits
Measuring Efficiency Sequential Search
(1 of 4)
• Analysis of algorithms: the study of the efficiency
of algorithms
• Searching: the task of finding a specific value in a
list of values, or deciding it is not there
• Sequential search algorithm (from Chapter 2)
– Given a target value and a random list of values, find
the location of the target in the list, if it occurs, by
checking each value in the list in turn
Measuring Efficiency Sequential Search
(2 of 4)
Measuring Efficiency Sequential Search
(3 of 4)
• Central unit of work, operations most important for
the task, and occurring frequently
• In sequential search, comparison of the target
NUMBER to each number in the list
• Given a big input list:
– Best case is smallest amount of work algorithm does
– Worst case is greatest amount of work algorithm does
– Average case depends on likelihood of different
scenarios occurring
Measuring Efficiency Sequential Search
(4 of 4)
• Best case: target found with the first comparison
• Worst case: target never found or the last value
• Average case: if each value is equally likely to be
searched, work done varies from 1 to n, averages to n/2
Figure 3.2
FIGURE 3.27
n
Order 10 50 1,000
100
Ig n 0.0003 sec 0.0006 sec 0.0007 sec 0.001 sec
n 0.001 sec 0.005 sec 0.01 sec 0.1 sec
n2 0.01 sec 0.25 sec 1 sec 1.67 min
2n 0.1024 sec 3,570 years 4 × 1016 centuries Too big to compute!!