Written byKrung Sinapiromsaran
July 2557
Aside from
You will need to learn using
T.H. Cormen et al. Introduction to Algorithms. MIT Press, 2001.
A lot of theorems and examples
Contains a lot of good references
Very useful for Computer Scientists
Need to have one (paper back or electronics)
K. Mehlhorn and P. Sanders. Algorithms and Data Structures: The Basic Toolbox. Springer, 2008.
B.R. Preiss. Data Structures and Algorithms with Object-oriented Design Patterns in Java. Wiley, 1999. (+ other Java-specific texts)
R. Sedgewick. Algorithms. Addison Wesley, 1988.
S.S. Skiena. The Algorithm Design Manual. Springer, 2008.
D.E. Knuth. The Art of Computer Programming. Addison Wesley, 1997. (In 3 volumes)
“If you think you’re a really good programmer, read Knuth’s Art of Computer Programming. You should definitely send me a resume if you can read the whole thing.”
Bill Gates
Muhammad ibn Mūsā al-Khwārizmī
“algorism” referred to rules of performing arithmetic using Arabic numerals
Evolved into “algorithm” and came to mean a definite procedure for solving a problem or performing a task
“the idea behind any reasonable computer program”
Something like a recipe, process, method, technique, procedure, routine etc
No formal definition has been accepted for all branches of Computer Science
According to our criteria, is the following statements valid algorithm?
Pancake batter
1. Sift the flour into a mixing bowl
2. Add pinch of salt
3. Beat the eggs lightly
4. Add to the flour and mix well in
5. Leave to rest for at least 1 hour or even overnight
According to our criteria, is the following statements valid algorithm?
Pancake batter
1. Sift the flour into a mixing bowl
2. Add pinch of salt
3. Beat the eggs lightly
4. Add to the flour and mix well in
5. Leave to rest for at least 1 hour or even overnight
According to our criteria, is the following statements valid algorithm?
Solving Chess
1. Construct the tree of all possible valid game states
2. Mark states as “won” if the player to move can win this turn or “lost” if the player to move loses regardless of move chosen
3. Mark states as “lost” if all successors are marked “won”
4. Mark states as “won” if at least one successor is marked “lost”
5. Repeat 3 and 4 until no additional states are marked
6. If first move is marked “won”, then return “white always wins”
According to our criteria, is the following statements valid algorithm?
Solving Chess
1. Construct the tree of all possible valid game states
2. Mark states as “won” if the player to move can win this turn or “lost” if player to move loses regardless of move chosen
3. Mark states as “lost” if all successors are marked “won”
4. Mark states as “won” if at least one successor is marked “lost”
5. Repeat 3 and 4 until no additional states are marked
6. If first move is marked “won”, then return “white always wins”
According to our criteria, is the following statements valid algorithm?
Find the greatest common divisor of positive integers m and n
1. If m < n, exchange m and n
2. Divide m by n and let r be the remainder
3. If r = 0, terminate and return n as the answer
4. Set m = n and n = r, return to 1
According to our criteria, is the following statements valid algorithm?
Find the greatest common divisor of positive integers m and n
1. If m < n, exchange m and n
2. Divide m by n and let r be the remainder
3. If r = 0, terminate and return n as the answer
4. Set m = n and n = r, return to 1
According to our criteria, is the following statements valid algorithm?
Find the nth prime number
1. Find a function f(n) which computes the nth prime number
2. Apply the function to n and return the result
According to our criteria, is the following statements valid algorithm?
Find the nth prime number
1. Find a function f(n) which computes the nth prime number
2. Apply the function to n and return the result
In practice we want algorithms that are:
Q: Why don’t we always use English? Why don’t we program computers in English?
We will use a mixture of English and pseudocode.
The algorithm for finding the greatest common divisor given above can be expressed in pseudocode as follows:
Function GCD(m, n:N): N
1. If m < n then
2. (m, n) := (n, m)
3. while(n > 0) do
4. r := m mod n
5. (m, n) := (n, r)
6. Endwhile
7. Return m
xxxxxxxxxx
def mygcd(m, n):
if m < n: m, n = n, m
r = m % n
if r == 0: return(n)
return(mygcd(n, r))
print(mygcd(12, 18))
As algorists, we need tools to:
A very common (and useful) informal way to specify a problem is to list its inputs and outputs:
Sorting:
Input:A sequence of n numbers; a_1, a_2, ..., a_n
Output:A permutation, i_1, i_2, ..., i_n of the input sequence such that
a_{i_1} \leq a_{i_2} \leq ... \leq a_{i_n}
Function GCD(m; n:N):N
1. assert (m > 0) & (n > 0)
2. if m < n then
3. (m; n) := (n, m)
4. while n > 0 do
5. r := m mod n
6. (m; n) := (n; r)
7. endwhile
8. assert (r|m) & (r|n) & (all i, i|m & i|n → i < r)
9. return m
Function sum(n:N):N
1. assert (n > 0)
2. s := 0
3. for i := 1 to n do
4. s := s + i
5. invariant s = sum j from 1 to i of j
6. endfor
7. assert s = sum i from 1 to n of i
8. return s
Robot arm must visit a set of contact points on a circuit board to solder each point Need to choose an order in which the contact points will be visited We want to minimise time taken, i.e. shortest path Shorter tour = faster production = cheaper to produce = more profit
Input: A set P of n points
Output: The shortest cycle tour that visits each point
Function NearestNeighbor(P: Set of points)
1. Pick and visit an initial point p[0] in P
2. i := 0
3. while(P is not empty)do
4. i := i + 1
5. P := P – {p[i-1]}
6. Let p[i] be the closet point to p[i-1]
7. Visit p[i]
8. endwhile
9. Return to p[0] from p[i]
Look encouraging
An instance where nearest neighbor gives a suboptimal solution
A better (optimal) solution for the same instance:
As algorists, we need tools to:
The theoretical study of computer program performance and resource usages.