Chapter 01
Chapter 01
Algorithms
(3rd edition)
by Cormen, Leiserson, Rivest & Stein
2
Algorithms
Informally, an algorithm is …
3
Algorithms
Empirically, an algorithm is …
4
Algorithms
The Sorting Problem:
Input: A sequence of n numbers [a1, a2, … , an].
Output: A permutation or reordering [a'1, a'2, … , a'n ] of the input
sequence such that a'1 a'2 … a'n .
Expected
Output: The permutation of the input [26, 31, 41, 41, 58 , 59].
5
Algorithms
Which algorithm is best for a given application
depends on—among other factors
The number of items to be sorted
The extent to which the items are already somewhat
sorted
Possible restrictions on the item values
The architecture of the computer
The kind of storage devices to be used: main
memory, disks, or even tapes.
6
Algorithms
Some definitions …
An algorithm is said to be correct if, for every input
instance, it halts with the correct output.
7
What kinds of problems are solved by
algorithms?
Sorting is by no means the only computational
problem for which algorithms have been developed.
Practical applications of algorithms are ubiquitous
and include the following examples:
8
Gallery of Problems
Algorithms are needed (most of which are novel) to solve the
many problems listed here …
9
Gallery of Problems
E-commerce enables goods and services to be
negotiated and exchanged electronically.
Crucial is the maintenance of privacy and
security for all transactions.
10
Some algorithms
Shortest path algorithm
Given a weighted graph and two
distinguished vertices -- the source
and the destination
-- compute the most efficient way to
get from one to the other
Matrix multiplication algorithm
Given a sequence of conformable
matrices, compute the most efficient
way of forming the product of the
matrix sequence
11
Some algorithms
Convex hull algorithm
Given a set of points on the plane,
compute the smallest convex body
that contains the points
12
Hard problems
Usual measure of efficiency is speed
How long does an algorithm take to produce its
result?
Define formally measures of efficiency
Problems exist that, in all probability, will take
a long time to solve
Exponential complexity
NP-complete problems
Problems exist that are unsolvable
13
Hard problems
NP-complete problems are interesting in and of
themselves
Some of them arise in real applications
Some of them look very similar to problems for
which efficient solutions do exist
Knowing the difference is crucial
Not known whether NP-complete problems
really are as hard as they seem, or, perhaps, the
machinery for solving them efficiently has not
been developed just yet
14
Hard problems
P NP conjecture
Fundamental open problem
in the theory of
computational complexity
Open now for 30+ years
15
Algorithms as a
technology
Even if computers were infinitely fast and memory
was plentiful and free
Study of algorithms still important – still need to
establish algorithm correctness
Since time and space resources are infinite, any correct
algorithm would do
Real-world computers are fast but not infinitely so
Memory is cheap but not unlimited
16
Efficiency
Time and space efficiency are the goal
Algorithms often differ dramatically in their
efficiency
Example: Two sorting algorithms
INSERTION-SORT – time efficiency is c1n2
MERGE-SORT – time efficiency is c1nlogn
For which problem instances would one algorithm be
preferable to the other?
17
Efficiency
factors:
Answer depends on several
Speed of machine performing the computation
Internal clock speed
Shared environment
I/O needed by algorithm
Quality of implementation (coding)
Compiler optimization
Implementation details (e.g., data structures)
Size of problem instance
Most stable parameter – used as independent variable
18
Efficiency
INSERTION-SORT
Implemented by an ace programmer and run on a machine
A that performs 109 instructions per second such that time
efficiency is given by:
tA(n) = 2n2 instructions (i.e., c1=2)
MERGE-SORT
Implemented by a novice programmer and run on a
machine B that performs 107 instructions per second such
that time efficiency is given by:
tB(n) = 50nlogn instructions (i.e., c1=50)
19
Efficiency
Machine A Machine B
Problem Insertion- Merge-
Size Sort Sort
n 2n2/109 50nlogn/107
10,000 0.20 0.66
50,000 5.00 3.90
100,000 20.00 8.30
500,000 500.00 47.33
1,000,000 2,000.00 99.66
5,000,000 50,000.00 556.34
10,000,000 200,000.00 1,162.67
50,000,000 5,000,000.00 6,393.86
20
Efficiency
Graphical comparison
Time Efficiency Comparison
10.00
8.00
Seconds
2.00
0.00
1 9 17 25 33 41 49 57 65
Size of Problem (in 1000s)
21
Algorithms vis-à-vis other technologies
Are algorithms really that important in the face of
dramatic advances in other technologies?
Hardware: super-fast clock speeds, parallelism,
pipelining
Graphical User Interfaces (GUI)
Object Oriented Systems
LANs and WANs
YES! Algorithms are at the core of most
technologies used in contemporary computation
22