Master Theorem
CSCI3160 Tutorial2
[email protected] ZHANG Jian
Divide and Conquer
Cost satisfies T(n) = aT(n/b) + f(n). But how to know the cost for a
given n? We need to know the closed-form solution from the
recursive expression.
The Master Theorem (for equalities)
• T(n) = aT(n/b) + f(n), c =
• If f (n) = O() for some > 0 then T(n) = Θ()
• If f (n) = Θ() then T(n) = Θ( log n)
• If f (n) = () for some > 0 and if af (n/b) ≤ df (n) for some d < 1 and large
enough n then T(n) = Θ(f (n))
• where and are constants, f(n) is a function, and T(n) is defined on the
nonnegative integers by the recurrence
• where we interpret n/b to mean either or
Practice
• 1.
• 2.
• 3.
How to prove?
• We should not only know that they are true, but also know
why they are true.
Proof
• blackboard sketch
The Master Theorem(for inequalities)
• T(n) ≤ aT(n/b) + f(n), c = logb a
• If f (n) = Θ() for some > 0 then T(n) = O()
• If f (n) = Θ() then T(n) = O( log n)
• If f (n) = Θ() for some > 0
and if af (n/b) ≤ df (n) for some d < 1 and large enough n
then T(n) = O(f (n))
Graph
• Consist of
• Vertex
• Edge
Definition
• A graph G = (V , E) consists of
• a set of vertices V
• a set of edges E
• Each edge is a pair of (u, v), where u,
v belongs to V, which means that
• For directed graph, we distinguish
between edge (u, v) and
edge (v, u); for undirected graph, no such distinction is made
Data structures for graph
• adjacency list
• adjacency matrix
, adjacency matrix: A[n][n]
adjacency list
• advantages:
– new vertices can be added to the graph easily, and they
can be connected with existing nodes simply by adding
elements to the appropriate arrays;
– easy to find all neighbors of a given vertex (and its
degree)
• disadvantages:
– determining whether an edge exists between two vertices
requires O(N) time, where N is the average number of edges
per node
Adjacency matrix
• advantages:
– fast to tell whether an edge exists between any two
vertices i and j (and to get its weight)
• disadvantage:
– consumes a lot of memory on sparse graphs (ones with few
edges)
Hint
• Ex 2.23
• (a) the key is to find the recursive relation between the
original problem and the sub-problems.
• If A has a majority element, it must be the majority
element of or . (why?)
• This helps a lot to find proper candidates!
Hint
• , split A into 2 parts, with the left part k elements, the right part
n-k elements
• case 1: both and have majority elements, denoted by and
respectively
• 1.a: equal
• 1.b: not equal
• case 2: only has the majority element, denoted by
• case 3: only has majority elements, denoted by
• case 4: neither nor have majority elements
Hint
• (b) Algorithm:
1. Pair up the elements arbitrarily to get n/2 pairs(what if odd?)
2. In each pair, discard both of them if they are different; keep one of
them if they are equal equivalent to find majority element in an
array with at most n/2 elements. (Why equivalent?)
3. check if the remained elements are majority elements( note that the
remained element is just a candidate, need to check)
T(n)=T(n/2) + O(n)
Reference
• Cormen, Thomas H., et al. Introduction to algorithms. MIT
press, 2009.
• COMP3711H slides, HKUST
• Thank you