Transcript Chapter 1

Chapter 1
Sections 1.1 – 1.4
pages 1-40
Homework
Read Section 1.4 (recap of data
structures)
 pages 26-37
 Answer the following questions:

• page 38, questions 1a, 1b, 2a, 2b, & 10
• Also, what exactly is a dictionary (not
Webster's)
• Due Wed 1/28 (in class)
Agenda
WTF?
 Euclid’s algorithm
 What is an algorithm
 What is algorithm analysis all about
 Problem types

Algorithm design & analysis process
Understand the Problem
Pick an algorithm design technique
Pick data structures, etc.
Design an algorithm
Prove correctness
Analyze the algorithm
WTF?
Code the algorithm
Euclid’s Algorithm
while n != 0 do
r  m mod n
mn
nr
return m
 Isn’t it a cool algorithm?
 Its very efficient, no?
 How many divisions must it do, as a
function of m or n?

Euclid’s Algorithm
Best case: m mod n equal zero
 Worst case?

• Prime numbers maybe?
• How do you get it to do a lot of
divisions?
Try This
Two prime numbers 29 and 7
 Then try 13 and 8.
 How can you cause the worst case?

Euclid’s Algorithm
Answer: Two consecutive Fibonacci
numbers.
 In the worst case it will require about
L divisions

• where L is the total number of digits in
both n and m.
Proving this is not fun.
 Lets not do it, OK?

Euclid’s Algorithm
If I have two 10 digit numbers,
Euclid’s algorithm will take (at most)
about 20 divisions.
 Whereas, consecutive integer
checking could take 9,999,999,999 X
2 divisions.
 How does Euclid’s algorithm gain
such an advantage?

Euclid’s Algorithm
r  m mod n
 If r is not equal to zero then all the
numbers between m and r are NOT
gcd’s of m and n.
 Its all about understanding the
properties of the problem.

• What is the definition of a gcd?
• What is the definition of mod?
• The two are related.
An Algorithm
A sequence of unambiguous
instructions for solving a problem,
 i.e., for obtaining a required output
for any legitimate input
 in a finite amount of time.

History Lesson

Muhammad ibn Musa al-Khwarizmi
• Famous mathematician from Bagdad
800 A.D
He wrote Al-jabr wa'l muqabala
(from which our modern word
"algebra" comes)
 The English word "algorithm" derives
from the Latin form of al-Khwarizmi's
name

Stupid, irrelevant, personal
anecdote
As an undergraduate, I had Indian
Professor who was an expert on
algorithms and pronounced the word
 A – grow – rid – em’s
 Lets practice saying the word so we
don’t sound like idiots.

Notion of algorithm
problem
algorithm
input
“computer”
output
Example of computational problem:
sorting

Statement of problem:
• Input: A sequence of n numbers <a1, a2, …, an>
• Output: A reordering of the input sequence <a´1, a´2, …,
a´n> so that a´i ≤ a´j whenever i < j

Instance: The sequence <5, 3, 2, 8, 3>

Algorithms:
•
•
•
•
Selection sort
Insertion sort
Merge sort
(many others)
Example


Input: array a[1],…,a[n]
Output: array a sorted in non-decreasing
order
Algorithm:
for i=1 to n
swap a[i] with smallest of a[i],…a[n]

Algorithm design strategies

Brute force


Divide and conquer

Decrease and
conquer

Transform and
conquer



Greedy approach
Dynamic
programming
Backtracking and
Branch and bound
Space and time
tradeoffs
Analysis of Algorithms

How good is the algorithm?
• Correctness
• Time efficiency
• Space efficiency

Does there exist a better algorithm?
• Lower bounds
• Optimality
Some Well-known Computational
Problems










Sorting
Searching
Shortest paths in a graph
Minimum spanning tree
Primality testing
Traveling salesman problem
Knapsack problem
Chess
Towers of Hanoi
Program termination
Why do we care?










Variations of the solutions to
these problems are used to…
 Schedule flights
Sorting
 Route Internet traffic
Searching
 Make Google work
Shortest paths in a
 Prevent the stock-market from
graph
crashing
Minimum spanning tree
 Assemble the human genome
Primality testing
 Encrypt data
Traveling salesman
 Save UPS millions of dollars
problem
 Track terrorist cell
Knapsack problem
communication on the Internet
Chess
 Make spell checkers work
Towers of Hanoi
 help keep you from going
Program termination
through the walls in games like
Halo and CounterStrike
Basic Issues Related to Algorithms
How to design algorithms
 How to express algorithms
 Proving correctness
 Efficiency

• Theoretical analysis
• Empirical analysis (experiments)

Optimality
What is an algorithm?

1.
Recipe, process, method, technique, procedure,
routine,… with following requirements:
Finiteness

2.
Definiteness

3.
valid inputs are clearly specified
Output

5.
rigorously and unambiguously specified
Input

4.
terminates after a finite number of steps
can be proved to produce the correct output given a valid
input
Effectiveness

steps are sufficiently simple and basic
Why study algorithms?

Theoretical importance
• the core of computer science

Practical importance
• A practitioner’s toolkit of known algorithms
• Framework for designing and analyzing
algorithms for new problems
Important Types of Problems
Sorting & Searching
 String matching & pattern matching
 Graph Problems
 Combinatorial Problems
 Geometric Problems
 Numerical Problems

Sorting & Searching

Intrinsically related
• Sorting can make searching easier
sort key – an important attribute
used to sort data
 stable sort – preserves the relative
order of any two equal elements
 in place sorting – doesn’t require
extra memory
 search key – not necessarily unique

String matching & pattern matching



Amazingly well-studied
Surprisingly difficult problem for certain
variations
Tons of applications:
• Approximate matching used in spell checkers
• Pattern matching used to find genes in DNA
• Or, used to find patterns in the stock market
Graph Problems

Graphs can be used to model all sorts of
real-world environments, scenarios, and
systems.
• Relationship modeling using graphs is very
powerful



Representing the real-world as a graph
can allow computers to solve all sorts of
problems.
Graph related algorithms are being used
to detect terrorist cell communication via
the Internet.
Hell, the Internet itself is a massive graph.
Combinatorial Problems



Certain problems, like optimal scheduling
or routing, can be reduced to finding the
optimal parameters among the set of all
possible parameters
But, the number of different parameter
combinations can become very large, too
large.
Reducing the combinatorial space of
problems is a very important strategy in
algorithms
Geometric Problems
These problems can be as simple as
finding the intersection point of two
lines to
 Finding the Delaunay Triangulation of
the smallest convex polygon
containing a given set of points.
 These types of problems arise from
modeling all sorts of real-world
entities.

Numerical Problems
Problems involving mathematical
objects
 solving equations
 computing integrals
 efficiently evaluating functions
 i.e., really boring stuff

Data Structures





The assigned reading is all about common
data structures used in algorithms
Data structures are just one of many tools
that can be used to improve algorithms
Perhaps they are the most important tool
Do the reading and homework
This material should not be new.
Homework
Read Section 1.4 (recap of data
structures)
 pages 26-37
 Answer the following questions:

• page 38, questions 1a, 1b, 2a, 2b, & 10
• Also, what exactly is a dictionary (not
Webster's)
• Due Wed 1/28 (in class)