Unit 4: Dynamic programming and dictionary data structures Flashcards
Divide and conquer
Recurrence
A recurrence consists of:
- Initial conditions T(n) = cₙ
for some small n
- Recursive equation that describes T(n)
in terms of n
and T(m)
for some m<n
.
Examples of recurrences:
Fibonacci numbers
F(0) = 0
F(1) = 1
F(n) = F(n - 1) + F(n - 2) for all n>1
Examples of recurrences:
Towers of Hanoi
T(0) = 0
T(n) = 2 . T(n - 1) + 1 for all n > 0
Examples of recurrences:
quick sort
T(1) = 0
T(n) = n + T(n - 1) for all n > 0
Master method applies to recurrences of the form:
T(n) = a T( n / b ) + f(n)
where a≥1
and b≥1
are constants and f(n) > 0
for almost all n
.
Master method
Critical exponent
log_b a
Master theorem
g(n)
g(n) = n^{c_crit}
Informally, g(n)
is related to the work of obtaining the subproblems.
Master theorem
<<
and >>
<<
and >>
stand for smaller / larger by a polinomial cap O(n^ϵ)
for some ϵ>0
Master theorem
3 Cases
Case 1:f(n) = O(n^c)
, where c < c_crit
» T(n)
= Θ(g(n))
Case 2:f(n) =Θ(g(n) log^k n)
for k≥0
» T(n)
= Θ(f(n) log n)
Case 3:f(n) = Ω(n^c)
, where c < c_crit
» T(n)
= Θ(f(n))
T(n) = a T(n / b) + f(n)
Dynamic Programming - Principals / Foundations
Dynamic programming
Divide the problem into a sequence (hierarchy) of (overlapping) subproblems and compute and store solutions for larger and larger subproblems with the help of the saved solutions.
Dynamic Programming - Principals / Foundations
Bellman’s principal of optimality
An optimal policy (set of decisions) has the property that whatever the initial state and decisions are, the remaining decisions must constitute an optimal policy with the regard to the state resulting from the first decision.
Shortest path (negative edge weights)
Bellman’s equations
Let G=(V, E)
be a directed graph without negative cycles and edge weights w : E → Q
and t ∈ V
.
The following holds for the length OPT(v)
of a shortest (simple) v-t
path P
in G
:
- OPT(t) = 0
- OPT(v) = min_{ u ∈ V\{u} } { w(v, u) + OPT(u) }
, ∀ v ∈ V, v ≠ t.
Dictionary
A dictionary is a set of elements of a common type that supports the following operations:
- Search
- Insert
- Removal
Every element has a key k
and some associated data.
We assume that k ∈ N
.
Every element is uniquely identified by its key.
Search Trees
in general
Search trees exist in many variants and usually allow the efficient implementation of the following operations:
- Insertion of a new element.
- Searching an element (using the key).
- Removal of an element (using the key).
- Sequential iteration over all elements.
- Find a smallest / largest element.
- Find the next smallest / largest element.
Rooted trees
Depth of a node
The depth of a node is the number of edges on the path from the root to the node.
The depth is unique since such a path exists and is unique.
The depth of the root is 0.
The set of nodes having the same depth in a tree is also refered to as a level of a tree.