- 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.
- Some notes on this definition:
- Non-ambiguity of instructions
- Valid range of inputs specified
- Algorithms can be represented in different ways
- Several algorithms can exist to solve the same problem.
- Algorithms for the same problem can be based on very different ideas and can solve the problem with dramatically different speeds.
- An example: GCD
- Given two non-negative integers m and n, find the largest integer g which divides both m and n evenly.
- The book examines three methods for computing gcd
- Euclid's Algorithm
EUCLID(m, n)
- while n ≠ 0 do
- r ← m mod n
- m ← n
- n ← r
- return m
- The algorithm language used above
- no variable declarations
- Indentation is used to specify scope of a control structure
- so lines 2,3, and 4 are the body of the loop.
- Keywords in bold.
- Use c/c++ style keywords, plus some
- Assignment operator: ←
- The idea is to convey the algorithm, not be executable code.
- Executable code tends contain too many details, the algorithm is obscured.
- A brute force algorithm
BRUTE-GCD(m,n)
- g ← MIN(m,n);
- while m%g ≠ 0 or n%g ≠ 0 do
- g ← g-1
- return g
- Will this algorithm terminate?
- Will this algorithm work?
- Is this better/worse than Euclid's algorithm?
- A Middle School Algorithm:
MIDDLE-SCHOOL(m, n)
- f1 ← FACTOR(m)
- f2 ← FACTOR(n)
- common ← COMMON-FACTORS(f1, f2)
- g ← 1
- i ← 1
- while i ≤ sizeof(common) do
- g ← g * common[i]
- return g
- Will this algorithm terminate?
- Will it produce the correct answer (work)?
- Is it better/worse than the previous two?
- Are there any hidden costs in this algorithm?
- Are there any ambiguous steps?
- To factor, we need to know the prime numbers.
- There is an ancient function known as the sieve of Eratosthenes to do this.
SIEVE(n)
- for p ← 2 to n do
- A[p] ← true
- for p ← 2 to ⌊sqrt(n)⌋ do
- if A[p]
- j ← p * p
- while j ≤ n do
- A[j] ← false
- j ← j + p
- i ← 0
- for p ← 2 to n do
- if A[p]
- L[i] ← p
- i ← i+1
- return L
- Run this algorithm on 27
- Line 1-2 builds a list of potential primes (all numbers)
- Lines 3-8 cross off all non-prime numbers, starting with 2.
- Cross off all of the numbers divisible by 2 (since it will be true)
- Search for the next number not crossed off
- Cross off all numbers divisible by this number.
- Line 5 starts at p*p, why not p*2?
- Lines 3 only goes to sqrt(n), why?
- Lines 10-13 extract the primes from the list