Lec 1
Lec 1
Dongbo Bu
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
1 / 74
The origin of the word “Algorithm”
Figure 1: Muhammad ibn Musa al-Khwarizmi (C. 780—850), a Persian scholar, formerly Latinized as Algoritmi
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
2 / 74
Al-Khwarizmi‘s contributions
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
3 / 74
Algorithm design: the art of computer programming
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
4 / 74
V. Vazirani said:
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
5 / 74
V. Vazirani said: cont′ d
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
6 / 74
Basic algorithmic strategies
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
7 / 74
The first example: calculating the greatest common divisor
(gcd)
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
8 / 74
The first problem: calculating gcd
Definition (gcd)
The greatest common divisor of two integers a and b, when at
least one of them is not zero, is the largest positive integer that
divides the numbers without a remainder.
Example:
The divisors of 54 are: 1, 2, 3, 6, 9, 18, 27, 54
The divisors of 24 are: 1, 2, 3, 4, 6, 8, 12, 24
Thus, gcd(54, 24) = 6.
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
9 / 74
The first problem: calculating gcd
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
10 / 74
Euclidean algorithm
Problem instances
gcd(1949, 101)
gcd(1, 0)
Problem instances
gcd(1949, 101)
gcd(101, 30)
gcd(1, 0)
Problem instances
gcd(1949, 101)
gcd(101, 30)
gcd(30, 11)
gcd(1, 0)
Problem instances
gcd(1949, 101)
gcd(101, 30)
gcd(30, 11)
gcd(1, 0)
Problem instances
gcd(1949, 101)
gcd(101, 30)
gcd(30, 11)
gcd(1, 0)
Node: subproblems
Edge: reduction relationship . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
15 / 74
Euclid algorithm
function Euclid(a, b)
1: if b = 0 then
2: return a;
3: end if
4: return Euclid(b, a mod b);
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
16 / 74
Time complexity analysis
Theorem
Suppose a is a n-bit integer. Euclid(a, b) ends in O(n3 ) time.
Proof.
There are at most 2n recursive calling.
Note that a mod b < a2 .
After two rounds of recursive calling, both a and b shrink at
least a half size.
At each recursive calling, the mod operation costs O(n2 )
time.
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
17 / 74
The second example: traveling salesman problem (TSP)
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
18 / 74
TSP: a concrete example
5
2 3
7
1 3
8
1 4
3
1→4→3→2→1
. . . . . . . . . . . . . . . . . . . .
Tour 6: (distance: 12) 21 / 74
Trial 1: Divide and conquer
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
22 / 74
Decompose the original problem into subproblems
d
6 5
2 6
8 8
e c e c
7 4 7 4
4 3 4 3
a b a b
3 3
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
23 / 74
Consider a tightly-related problem
Let’s consider a tightly-related problem: calculating M(s, S, x),
the minimum distance, starting from city s, visiting each city
in S once and exactly once, and ending at city x.
It is easy to decompose this problem into subproblems, and
the original problem could be easily solved if M(s, S, x) were
determined for all subset S ⊆ V and e ∈ V.
5
2 3
7
1 3
8
1 4
3
For example, since there are 3 cases of the city from which we
return to 1, the shortest tour can be calculated as:
min{ d2,1 + M(1, {3, 4}, 2),
d3,1 + M(1, {2, 4}, 3),
d4,1 + M(1, {2, 3}, 4)}.
.
.
.
.
. . . . .
. . . .
. . . .
. . . .
. . . .
. . . . .
.
.
.
.
.
.
.
.
.
24 / 74
Consider the smallest instance of M(s, S, x)
1 1
M(1, {2}, 3) = d12 + d23 M(1, {3}, 2) = d13 + d32
But how to solve a larger problem, say M(1, {2, 3}, 4)?
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
25 / 74
Divide a large problem into smaller problems
5 5
2 3 2 3
7 7
1 3 1 3
8 8
1 4 1 4
3 3
M(1, {2, 3}, 4) = min{d34 + M(1, {2}, 3), d24 + M(1, {3}, 2)}
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
26 / 74
Sub-instance relationship graph
Problem instances
function TSP(D)
1: return mine∈V,e̸=s M(s, V − {e}, e) + des ;
function M(s, S, x)
1: if S = {v} then
2: M(s, S, x) = dsv + dvx ;
3: return M(s, S, x);
4: end if
5: return mini∈S, i̸=x M(s, S − {i}, i) + dxi ;
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
28 / 74
An example
x
S b c d
{b} - 7 6
{c} 8 - 12
{d} 10 15 -
{b, c} - - 15
{b, d} - 14 -
{c, d} 15 - -
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
29 / 74
Trial 2: Improvement strategy
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
30 / 74
Solution space
5
2 3
7
1 3
8
1 4
3
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
32 / 74
Improvement strategy
function GenericImprovement(V, D)
1: Let s be an initial tour;
2: while TRUE do
3: Select a new tour s′ from the neighbourhood of s;
4: if s′ is shorter than s then
5: s = s′ ;
6: end if
7: if stopping(s) then
8: return s;
9: end if
10: end while
Here, neighbourhood is introduced to describe how to change an
existing tour into a new one;
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
33 / 74
But how to define neighbourhood of a tour?
2 3 2 3
1 4 1 4
s s′
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
34 / 74
An example
d
6 5
2 6
8
e c
7 4
4 3
a b
3
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
35 / 74
Step 1
d
6 5
2 6
8
e c
7 4
4 3
a b
3
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
36 / 74
Step 2: a 2-opt operation improves s ⇒ s′
d d
6 5 6 5
2 6 2 6
8 8
e c e c
7 4 7 4
4 3 4 3
a b a b
3 3
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
37 / 74
Step 3: One more 2-opt operation improves s′ ⇒ s′′
d d
6 5 6 5
2 6 2 6
8 8
e c e c
7 4 7 4
4 3 4 3
a b a b
3 3
Selecting three edge (a, b), (c, d), and (e, f), and applying
2 − OPT and 3 − OPT on them, we will have a total of 7
possible new tours.
The LK-1 technique uses up to sequential 5 − OPT as well as
non-sequential 4 − OPT. . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
40 / 74
Trial 3: “Intelligent” enumeration strategy
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
41 / 74
Solution form
Note that a complete solution can be expressed as a sequence
of n edges. Given a certain order of the m edges, a complete
solution can be represented as X = [x1 , x2 , ..., xm ], where
xi = 1 if the edge i was used in the tour, and xi = 0 otherwise.
For example, the tour a → b → c → d → e → a can be
represented as X = [1, 0, 0, 1, 1, 0, 0, 1, 0, 1].
As every solution is a combination of m items, we can
enumerate all possible solutions.
d
e10 : 6 e8 : 5
e3 : 2 e6 : 6
e9 : 8
e c
e5 : 7 e4 : 4
e2 : 4 e7 : 3
a b
e1 : 3
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
42 / 74
Organizing solutions into a partial solution tree
X =??????????
x1 = 1 0
1????????? 0?????????
x2 = 1 0 1 0
x3 = 0
101?0?????
1 0 1 0 1 0 1 0 1 0 ...
... x4 = 0
X =??????????
x1 = 1 0
1????????? 0?????????
x2 = 1 0 1 0
x3 = 0
101?0?????
1 0 1 0 1 0 1 0 1 0 ...
... x4 = 0
X =??????????
x1 = 1 0
1????????? 0?????????
x2 = 1 0 1 0
x3 = 0
101?0?????
1 0 1 0 1 0 1 0 1 0 ...
... x4 = 0
function GenericBacktrack
1: Let A = {X0 }. //Start with the root node X0 =??...?. Here, A
denotes the active set of nodes that are unexplored
2: best_so_far = ∞;
3: while A ̸= NULL do
4: Choose and remove a node X from A;
5: Select an undetermined item in X, numerate this item, and thus
expand X into nodes X1 , X2 , ..., Xk ;
6: for i = 1 to k do
7: if Xi represents a complete solution then
8: Update best_so_far if Xi has better objective function value;
9: else
10: Insert Xi into A; //Xi needs to be explored further;
11: end if
12: end for
13: end while
14: return best_so_far;
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
46 / 74
An example: Step 1
d
e10 : 6 e8 : 5
e3 : 2 e9 : 8 e6 : 6
e c
e5 : 7 e4 : 4
e2 : 4 e7 : 3 A = {X0 }
a b best_so_far = +∞
e1 : 3
Partial Solution Tree
X =??????????
X0
Initially, the partial solution tree has only one root node, i.e.,
the original problem X0 . The value best_so_far is set as +∞.
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
47 / 74
Step 2
d
e10 : 6 e8 : 5
e3 : 2 e9 : 8 e6 : 6
e c
e5 : 7 e4 : 4
e2 : 4 e7 : 3 A = {X1 , X2 }
a b best_so_far = +∞
e1 : 3
Partial Solution Tree
X =??????????
X0
x1 = 1/0
1????????? 0?????????
X1 X2
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
48 / 74
Step 3
d
e10 : 6 e8 : 5
e3 : 2 e9 : 8 e6 : 6
e c
e5 : 7 e4 : 4
e2 : 4 e7 : 3 A = {X2 , X3 , X4 }
a b best_so_far = +∞
e1 : 3
Partial Solution Tree
X =??????????
X0
x1 = 1/0
1????????? 0?????????
X1 X2
x2 = 1/0
110?0????? 10????????
X3 X4
1????????? 0?????????
X1 X2
x2 = 1/0
110?0????? 10????????
X3 X4
x3 = 1/0
11100????? X 11000?????
X5 X6
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
51 / 74
Calculate lower bound for partial solution: an example
d
e10 : 6 e8 : 5
e3 : 2 e6 : 6
e9 : 8
e c
e5 : 7 e4 : 4
e2 : 4 e7 : 3
a b
e1 : 3
d
e10 : 6 e8 : 5
e3 : 2 e9 : 8 e6 : 6
e c
e5 : 7 e4 : 4
e2 : 4 e7 : 3 A = {X0 }
a b best_so_far = +∞
e1 : 3
Partial Solution Tree
X =??????????
X0 (17.5)
Initially, the partial solution tree has only one root node, i.e.,
the original problem X0 (lower bound: 17.5). The value
best_so_far is set as +∞.
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
54 / 74
Step 2
d
e10 : 6 e8 : 5
e3 : 2 e9 : 8 e6 : 6
e c
e5 : 7 e4 : 4
e2 : 4 e7 : 3 A = {X1 , X2 }
a b best_so_far = +∞
e1 : 3
Partial Solution Tree
X =??????????
X0 (17.5)
x1 = 1/0
1????????? 0?????????
X1 (17.5) X2 (18.5)
d
e10 : 6 e8 : 5
e3 : 2 e9 : 8 e6 : 6
e c
e5 : 7 e4 : 4
e2 : 4 e7 : 3 A = {X2 , X3 , X4 }
a b best_so_far = +∞
e1 : 3
Partial Solution Tree
X =??????????
X0 (17.5)
x1 = 1/0
1????????? 0?????????
X1 (17.5) X2 (18.5)
x2 = 1/0
110?0????? 10????????
X3 (20.5) X4 (18)
d
e10 : 6 e8 : 5
e3 : 2 e9 : 8 e6 : 6
e c
e5 : 7 e4 : 4
e2 : 4 e7 : 3 A = {X2 , X3 , X5 , X6 }
a b best_so_far = +∞
e1 : 3
Partial Solution Tree
X =??????????
X0 (17.5)
x1 = 1/0
1????????? 0?????????
X1 (17.5) X2 (18.5)
x2 = 1/0
110?0????? 10????????
X3 (20.5) X4 (18)
x3 = 1/0
101?0????? 100?1?????
X5 (18) X6 (23)
e2 : 4 e7 : 3 A = {X2 , X3 , X6 }
a b best_so_far = 21
e1 : 3
Partial Solution Tree
X =??????????
X0 (17.5)
x1 = 1/0
1????????? 0?????????
X1 (17.5) X2 (18.5)
x2 = 1/0
110?0????? 10????????
X3 (20.5) X X4 (18)
x3 = 1/0
101?0?????
X5 (18) 100?1?????
x4 = 1/0 X6 (23) X
1011000011 1010001110
X7 (23) X8 (21)
d
e10 : 6 e8 : 5
e3 : 2 e9 : 8 e6 : 6
e c
e5 : 7 e4 : 4
e2 : 4 e7 : 3 A = {X9 }
a b best_so_far = 21
e1 : 3
Partial Solution Tree
X =??????????
X0 (17.5)
x1 = 1/0
1????????? 0?????????
X1 (17.5) X2 (18.5)
x2 = 1/0
110?0????? 10???????? 01???????? 001?1?????
X3 (20.5) X X4 (18) X9 (18.5) X10 (21) X
x3 = 1/0
101?0?????
X5 (18) 100?1?????
x4 = 1/0 X6 (23) X
1011000011 1010001110
X7 (23) X8 (21)
. . . . . . . . . . . . . . . . . . . .
d
e10 : 6 e8 : 5
e3 : 2 e9 : 8 e6 : 6
e c
e5 : 7 e4 : 4
e2 : 4 e7 : 3 A = {X11 }
a b best_so_far = 21
e1 : 3
Partial Solution Tree
X =??????????
X0 (17.5)
x1 = 1/0
1????????? 0?????????
X1 (17.5) X2 (18.5)
x2 = 1/0
110?0????? 10???????? 01???????? 001?1?????
X3 (20.5) X X4 (18) X9 (18.5) X10 (21) X
x3 = 1/0
101?0????? 011?0????? 010?1?????
X5 (18) 100?1????? X11 (18.5) X12 (23.5) X
x4 = 1/0 X6 (23) X
1011000011 1010001110
X7 (23) X8 (21)
. . . . . . . . . . . . . . . . . . . .
X12 should not be added into A, and X11 will be. expanded.
. . . . . . . . . . . . . . . . .
60 / 74
. .
Final step
d
e10 : 6 e8 : 5
e3 : 2 e9 : 8 e6 : 6
e c
e5 : 7 e4 : 4
A = {}
e2 : 4 e7 : 3
a b best_so_far = 19
e1 : 3
Partial Solution Tree
X =??????????
X0 (17.5)
x1 = 1/0
1????????? 0?????????
X1 (17.5) X2 (18.5)
x2 = 1/0
110?0????? 10???????? 01???????? 001?1?????
X3 (20.5) X X4 (18) X9 (18.5) X10 (21) X
x3 = 1/0
101?0????? 011?0????? 010?1?????
X5 (18) 100?1????? X11 (18.5) X12 (23.5) X
x4 = 1/0 X6 (23) X
. . . . . . . . . . . . . . . . . . . .
Note: the tree was pruned to contain only 15 nodes,
. . . making
. . . . . . the
. . . . . . . . . . .
61 / 74
Backtracking strategy: another trial
d
e10 : 6 e8 : 5
e3 : 2 e6 : 6
e9 : 8
e c
e5 : 7 e4 : 4
e2 : 4 e7 : 3
a b
e1 : 3
d
e10 : 6 e8 : 5
e3 : 2 e9 : 8 e6 : 6
e c
e5 : 7 e4 : 4
A = {X0 }
e2 : 4 e7 : 3
a
e1 : 3
b best_so_far = ∞
Partial Solution Tree
X = a???
X0 (17.5)
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
63 / 74
Step 2
d
e10 : 6 e8 : 5
e3 : 2 e9 : 8 e6 : 6
e c
e5 : 7 e4 : 4
A = {X1 , X3 , X4 }
e2 : 4 e7 : 3
a
e1 : 3
b best_so_far = ∞
Partial Solution Tree
X =a???
X0 (17.5)
x2 = b/c/d/e
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
64 / 74
Step 3
d
e10 : 6 e8 : 5
e3 : 2 e9 : 8 e6 : 6
e c
e5 : 7 e4 : 4
A = {X3 , X4 , X5 ,
e2 : 4 e7 : 3
a b X6 , X7 }
Partial Solution Tree e1 : 3
best_so_far = ∞
X = a???
X0 (17.5)
x2 = b/c/d/e
d
e10 : 6 e8 : 5
e3 : 2 e9 : 8 e6 : 6
e c
e5 : 7 e4 : 4
A = {X3 , X4 , X5 , X6 }
e2 : 4 e7 : 3
a
e1 : 3
b best_so_far = 21
Partial Solution Tree
X = a???
X0 (17.5)
x2 = b/c/d/e
abec abed
X8 (21) X9 (21)
d
e10 : 6 e8 : 5
e3 : 2 e9 : 8 e6 : 6
e c
e5 : 7 e4 : 4
A = {X4 , X5 , X6 ,
e2 : 4 e7 : 3
a b X11 , X12 }
e1 : 3
Partial Solution Tree best_so_far = 21
X = a???
X0 (17.5)
x2 = b/c/d/e
abec abed
X8 (21) X9 (21)
67 / 74
Final step
d
e10 : 6 e8 : 5
e3 : 2 e9 : 8 e6 : 6
e c
e5 : 7 e4 : 4
A = {}
e2 : 4 e7 : 3
a
e1 : 3
b best_so_far = 19
Partial Solution Tree
X = a???
X0 (17.5)
x2 = b/c/d/e
algorithm efficient. . . . . . . . . . . . . . . . . . .
68 / 74
. .
Time complexity and space complexity
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
69 / 74
Time complexity and space complexity
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
70 / 74
Running time: we are interested in its growth rate
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
71 / 74
Big O notation
Recall that big O notation is used to describe the error
term in Taylor series, say:
2
ex = 1 + x + x2 + O(x3 ) as x → 0
600
f(x)
g(x)
500
400
300
200
100
0
1 2 3 4 5 6 7 8
Figure 2: Example: f(x) = O(g(x)) as there exists c > 0 (e.g. c = 1) and
x0 = 5 such that f(x) < cg(x) whenever x > x0
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
72 / 74
Big Ω and Big Θ notations