Lecture 1
Lecture 1
Foundations
• Design and Analysis of Algorithms, an Example (Ch 1).
The lecture notes/slides are adapted from those associated with the text book by J. Kleinberg and E.
Tardos.
1
CMPT405/705 Foundations Qianping Gu
2
CMPT405/705 Foundations Qianping Gu
• Each employer has a preference order on the applicants; each student has a
preference order on employers.
3
CMPT405/705 Foundations Qianping Gu
4
CMPT405/705 Foundations Qianping Gu
5
CMPT405/705 Foundations Qianping Gu
6
CMPT405/705 Foundations Qianping Gu
7
CMPT405/705 Foundations Qianping Gu
8
CMPT405/705 Foundations Qianping Gu
• Instability
Given a matching S ⊆ M × W , (m, w) ∈ (M × W ) \ S is unstable w.r.t.
(with respect to) S if
9
CMPT405/705 Foundations Qianping Gu
2 2 fm (w) g w (m) 2
m w m 1 w
1 1 2
1
2 1
1 2
m’ w’ m’ w’
1 2 2 1
2 2 2 2 2 1 m 2 1 w
m w m w m w
1 1 1 2 1 2
1 1
2 1 2 1 1 2 1 2
m’ w’ m’ w’ m’ w’ m’ w’
1 2 1 2 2 1 2 1
Stable matching Stable matching
Stable matching Unstable matching
S={(m,w),(m’,w’)} S’={(m,w’),(m’,w)}
S={(m,w),(m’,w’)} S={(m,w’),(m’,w)}
fm (w)>f m (w’), gw (m)>g w (m’)
10
CMPT405/705 Foundations Qianping Gu
Design algorithm
2. For every unpaired m, let Wm = {w|w ∈ W and m has not tried to pair} and
let w ∈ Wm with the largest fm (w);
11
CMPT405/705 Foundations Qianping Gu
12
CMPT405/705 Foundations Qianping Gu
2 1 2 1 2 1 2 1
m’ w’ m’ w’ m’ w’ m’ w’
1 2 1 2 1 2 1 2
fm (w) g (m) Choose (m’,w), S={m’,w} Choose (m,w), w is paired Choose (m’,w’),
w
but w prefers m, S={m,w} S={(m,w),(m’,w’)}
Wm ={w,w’} Wm’ ={w,w’}
Choose (m’,w), w is paired
but w prefers m, Wm’ ={w’}
13
CMPT405/705 Foundations Qianping Gu
Analyze algorithm
Proof. Let P (t) be the set of pairs (m, w) s.t. m has tried to pair by the end of
2
iteration t of the while loop. Then |P (t + 1)| > |P (t)|. Since there are n pairs in
M × W.
• |P (t)| is a progress measure which specifies that each step of the algorithm
makes the algorithm closer to termination.
14
CMPT405/705 Foundations Qianping Gu
15
CMPT405/705 Foundations Qianping Gu
Discussion
• For a same input instance, different executions of the algorithm give the same
∗
stable matching S which has the following properties:
∗
– For (m, w ) ∈ S ∗ and (m, w) in any stable matching S ,
fm (w∗ ) ≥ fm (w).
∗
– For (m , w) ∈ S ∗ and (m, w) in any stable matching S ,
gw (m∗ ) ≤ gw (m).
16
CMPT405/705 Foundations Qianping Gu
Computation Models
Word RAM (Random Access Memory)
Output
17
CMPT405/705 Foundations Qianping Gu
Turing Machines
• A tape divided into cells, each cell has a symbol from a finite set.
• A set of finite machine states.
• A set of finite instructions which define that given a machine state qi and a
symbol aj , the next step action of the machine.
#:1
# a b
0:1
18
CMPT405/705 Foundations Qianping Gu
Computational Tractability
• An algorithm is a poly-time (polynomial running time) algorithm if there are
constants c > 0 and d > 0 s.t. for every input of size n, the running time of the
d
algorithm is bounded above by cn primitive computational steps.
• A poly-time algorithm has the desirable scaling property: When the input size
doubles, the running time increases by at most a constant factor.
19
CMPT405/705 Foundations Qianping Gu
20
CMPT405/705 Foundations Qianping Gu
• Foucs on the dominating term for the growth, igonor constant factors and
low-order terms.
21
CMPT405/705 Foundations Qianping Gu
• Big-Oh notation, f (n) is O(g(n)) if there exist constants c > 0 and n0 ≥ 0 s.t.
0 ≤ f (n) ≤ c · g(n) for all n ≥ n0 .
For upper bounds on time/space complexity.
f (n) = 5n2 + 7n + 4, f (n) is O(n2 ); f (n) not O(nk ) for any 0 ≤ k < 2 nor
O(n log n).
22
CMPT405/705 Foundations Qianping Gu
f (n) = 5n2 + 7n + 4, f (n) is Ω(nk ) for any k ≤ 2 and Ω(n log n); f (n) is
k
not Ω(n ) for any k > 2.
23
CMPT405/705 Foundations Qianping Gu
24
CMPT405/705 Foundations Qianping Gu
• Little-oh notation, f (n) is o(g(n)) if for any constant c > 0, there exists a
constant n0 > 0 s.t. 0 ≤ f (n) < cg(n) for all n ≥ n0 (or
limn→∞ f (n)/g(n) = 0).
f (n) = n2 + 7n + 4 is o(nk ) for k > 2.
• Little-omega notation, f (n) is ω(g(n)) if for any constant c > 0, there exists a
constant n0 > 0 s.t. 0 ≤ cg(n) < f (n) for all n ≥ n0 (or
limn→∞ f (n)/g(n) = ∞).
f (n) = n2 + 7n + 4 is ω(nk ) for k < 2.
25
CMPT405/705 Foundations Qianping Gu
• If f (n) is O(f1 (n)) and g(n) is O(g1 (n)), then f (n) · g(n) is
O(f1 (n) · g1 (n)), and f (n) + g(n) is O(max{f1 (n), g1 (n)}).
• If f (n) is O(f1 (n)) and f1 (n) is O(f2 (n)) then f (n) is O(f2 (n)).
• Big-Oh with multiple variables, f (m, n) is O(g(m, n)) if there exist constants
c > 0, m0 ≥ 0 and n0 ≥ 0 s.t. f (m, n) ≤ c · g(m, n) for all m ≥ m0 and
n ≥ n0 .
26
CMPT405/705 Foundations Qianping Gu
Inverse Ackermann time O(α(n)) some operations in disjoint set data structure
Linear time O(n) merge two sorted lists into one sorted list
27
CMPT405/705 Foundations Qianping Gu
AKS primality test: M. Agrawal, N. Kayal and N. Saxena [2002, IIT Kanpur] gave an algorithm
which, given integer N of n bits, decide if N is a prime number in O(nk ) (O((log N )k ))
time, k is a constant.
Factorizing integer: given integer N of n bits, find the prime factors of N . It is open whether
this problem admits an algorithm with poly-time in n or NP-hard. Naive algorithm takes
√
O( N ) = O(2n/2 ) time, heuristic of O(N 1/4 ) = O(2n/4 ) time is known.
If factorizing integer can be solved in poly-time in n (the number of bits of the integer) then
28
CMPT405/705 Foundations Qianping Gu
29
CMPT405/705 Foundations Qianping Gu
30
CMPT405/705 Foundations Qianping Gu
Graphs
Undirected graphs
• Graph G(V, E), V is a set of nodes (vertices), E is a set of edges, edge {u, v}
connects nodes u and v and called an edge between u and v .
• If there are more than one edge between u and v , these edges are called
multiple edges. An edge {u, u} connects node u itself is called a self loop.
• Simple graph, does not have multiple edge nor self loop.
31
CMPT405/705 Foundations Qianping Gu
1 7
V={1,2,3,4,5,6,7,8}
2 3 E={ {1,2},{1,3},{2,3},{2,4},{2,5}
{3,5},{3,7},{3,8},{4,5},{5,6},{7,8} }
4 5 8
n=8, m=11
6
A simple undirected graph
32
CMPT405/705 Foundations Qianping Gu
• Space, O(n2 ).
• Time, O(1) to check if {u, v} is an edge; O(n2 ) time to identify all edges.
33
CMPT405/705 Foundations Qianping Gu
• Adjacency Lists: for every node u of G, all nodes adjacent to u are put in a list.
34
CMPT405/705 Foundations Qianping Gu
1 7
2 3
4 5 8
35
CMPT405/705 Foundations Qianping Gu
Trees
36
CMPT405/705 Foundations Qianping Gu
• Graph traversal, breadth first search (BFS) and depth first search (DFS).
• For each i, Li = {nodes with the minimum path length exactly i from s}.
37
CMPT405/705 Foundations Qianping Gu
BFS example
38
CMPT405/705 Foundations Qianping Gu
Bipartite graphs
• Bipartite G, nodes of G can be partitioned into two subsets V1 and V2 s.t. for
every edge e of G, one end node of e is in V1 and the other is in V2 .
39
CMPT405/705 Foundations Qianping Gu
Test bipartiteness
40
CMPT405/705 Foundations Qianping Gu
Directed Graphs
• Digraph G(V, E), V is a set of nodes (vertices), E is a set of arcs (directed
edges), arc (u, v) is from node u to node v .
• If there are more than one arc from u to v , these arcs are called multiple arcs.
An arc (u, u) from u to itself is called a self loop.
• Simple digraph, does not have multiple arcs nor self loop.
indeg(u)=2
u outdeg(u)=3
41
CMPT405/705 Foundations Qianping Gu
1 7
V={1,2,3,4,5,6,7,8}
2 3 A={ (1,2),(3,1),(2,3),(4,2),(2,5)
(3,5),(7,3),(3,8),(5,4),(6,5),(8,7) }
4 5 8
n=8, m=11
6
A simple directed graph
42
CMPT405/705 Foundations Qianping Gu
1 2 3 4 5 6 7 8 1 2
1 7
1 0 1 0 0 0 0 0 0 2 3 5
2 0 0 1 0 1 0 0 0
2 3 3 1 0 0 0 1 0 0 1 3 1 5 8
4 0 1 0 0 0 0 0 0 4 2
5 0 0 0 1 0 0 0 0 5 4
4 5 8
6 0 0 0 0 1 0 0 0
6 5
7 0 0 1 0 0 0 0 0
8 0 0 0 0 0 0 1 0 7 3
6
8 7
Adjacency matrix Adjacency list
43
CMPT405/705 Foundations Qianping Gu
1 7
2 3
4 5 8
44
CMPT405/705 Foundations Qianping Gu
Digraph search
• Nodes s and t are mutual reachable if each of them is reachable from the other.
u u
s s
v v
G G 1st BFS on G 2nd BFS on G
45
CMPT405/705 Foundations Qianping Gu
A topological order
46
CMPT405/705 Foundations Qianping Gu
47
CMPT405/705 Foundations Qianping Gu
• Assume you have n friends, some of your friend may dislike some others, and
you want to invite as many of your friends as possible s.t. no two friends invited
dislike each other.
48
CMPT405/705 Foundations Qianping Gu
• Consider a two players game problem: Two companies A and B want to open
similar stores (e.g., provide fast food) in n zones. Each zone has a value of
potential profit for having a store. There are zone regulations that each zone can
have at most one store and if some zone has a store, any zone adjacent to the
zone can not have any store. A and B alternate in choosing a zone to open a
store, starting from A. If each company can reach a predefined profit target?
49
CMPT405/705 Foundations Qianping Gu
1 2 3 4 5 6
10 5 6 9 7 8
50