Algorithms For Networks Notes v2 Lecture2
Algorithms For Networks Notes v2 Lecture2
Algorithms For Networks Notes v2 Lecture2
logs n−1
X (q/2)log2 n − 1 c
T (n) ≤ (q/2)i cn = cn ≤ n(q/2)log2 n (2.1)
i=0
q/2 − 1 q/2 − 1
= c2 nnlog2 q−1 = c2 nlog2 q . (2.2)
11
12 CHAPTER 2. LECTURE 2: DIVIDE AND CONQUER AND DYNAMIC PROGRAMMING
where we have used that alog b = blog a . We can then state the following theorem.
Theorem 6. Any T (n) satisfying T (n) ≤ qT (n/2) + cn for q > 2 is bounded by O(nlog2 q ).
For example, if you need to divide the problem in q = 3 subproblems, the running time will
be O(nlog2 3 ) = O(n1.59 ). Note, if you have a quadratic brute force approach, then dividing the
problem into four subproblems does not help.
For n digit numbers, following elementary school methods, the running time is O(n2 ). (We
multiply one number by one digit at a time, for all n digits.) For simplicity, we consider numbers
of same number of digits.
Let us try an alternative method, that leads to Divide and Conquer. We write the numbers as
x1 2n/2 + x2 , where x1 is the higher order n/2 bits, x2 is the lower order n/2 bits.
• calculate x1 y1 , x0 y0
Greedy. Build up a solution incrementally, by step wise optimization according to some local
criterion.
Dynamic programming. Break up a problem into a series of overlapping subproblems, and build
up solutions to larger and larger subproblems.
We do some preprocessing: order requests according to finishing time, and record the last
compatible request as p(i).
Recursive thinking: let us consider the optimal solution O, and the last interval. There are two
options.
2. n ∈
/ O: then O is the same as the optimal solutions for requests {1, ...n − 1}.
Theorem 8. The running time of M-Compute-Opt(n) is O(n), assuming that the intervals are
sorted by their finish times.
Proof. We look for a suitable progress measure that decreases by one each time M-Compute-Opt
is called. The number of entries that are empty is such a measure.
If we also need the optimal set, and not only the maximum weight, then we need additional
post processing, Find-Solution(j). See algorithm in book.
Proof. This is a recursive solution which calls strictly lower values, that is, completes in O(n)
time.
Iterative solution
The key tool for the efficient recursive solution was the maintaining of the array M of already
computed values. But then nothing stops us to directly compute the entries of M , which leads to
an iterative algorithm, Iterative-Compute-Opt. See book.
1. There are a polynomial number of subproblems. Reasonable requirement to keep the running
time polynomial. (We had two for the interval scheduling.)
2. The solution can be easily computed from the solutions to the subproblems. Again, to make
sure the algorithm remains polynomial. (It was the maximum operation for interval schedul-
ing.)
3. There is a natural ordering of subproblems from smallest to largest. This is needed to be able
to use memorization or iteration. (It was the number of intervals considered, according to
increasing finishing times.)
14 CHAPTER 2. LECTURE 2: DIVIDE AND CONQUER AND DYNAMIC PROGRAMMING
Solution approaches: greedy solutions with starting from the heaviest or lightest item obviously
do not work. So we try with dynamic programming. Let us assume we ordered the requests.
Unfortunately the recursion as in the interval scheduling does not work, if item n is part of the
optimal solution, all the others may be part of it as well, only the limit W is decreased. Since we
need to keep track of the remaining space, we introduce one more variable:
X X
OP T (i, w) = max wj | wj ≤ w
S⊆{1...i}
j∈S j∈S
• if n ∈
/ O then OP T (n, W ) = OP T (n − 1, W ),
• if n ∈ O then OP T (n, W ) = wn + OP T (n − 1, W − wn ),
or
Theorem 11. The Subset-Sum(n,W) algorithm correctly computes the optimal value of the problem
and runs in O(nW ) time.
We call such algorithms pseudo-polynomial, as the running time depends on the largest integer
involved in the problem. These algorithms get less practical as this number grows large.
Theorem 12. Given the table M , the optimal set S can be backtracked in O(n) time.
We can use exactly the same approach as for the subset sum problem, with slightly different
recursion rule, but the same matrix M :
• if n ∈
/ O then OP T (n, W ) = OP T (n − 1, W ),
• if n ∈ O then OP T (n, W ) = vn + OP T (n − 1, W − wn ),
or
Problem. Shortest path with negative weights: Given G = (V, E), and weights {cij }. Decide
if G has a negative cycle. If the graphs has no negative cycle, find a path P from s to t with
minimum total cost.
We can easily see some wrong approaches. Dijsktra does not work, since now we can not easily
declare the set of discovered nodes. Another idea would be to add a large constant to each weights,
so that each of them becomes positive. But this penalize long paths more that short paths, and
the shortest path may change. Let us see a dynamic programming approach. We will end up
with the Bellman-Ford Algorithm, which is one of the first applications of dynamic programming.
Remember, what we need:
Lemma 9. If G does not have negative cycles, then the shortest paths have at most n-1 edges
(|V | = n).
We need two tricks from earlier discussions: we will use more than two subproblems, and we
will have two variables: i for using at most i edges, and v as the start of a path to t Then we know
the following about the optimal path P , representing OP T (i, v), that is, shortest path from v to t
with maximum i edges:
• if P is uses i edges, and the first edge is (v, w), then OP T (i, v) = cvw + OP T (i − 1, w),
The iterative shortest path algorithm fills in an M matrix of n × n. To fill in an entry, it needs
to compare n previous entries. See algorithm in book.
Theorem 14. The Shortest-Path algorithm correctly computes the minimum cost of an s-t path in
a graph with no negative cycles, and runs in O(n3 ) time.
Remark. The algorithm can be implemented in O(mn) time, where n2 is changed to m since only
w − v pairs with edges need to be evaluated. Similarly, the space requirements can be decreased to
O(n).
This shortest-path algorithm is relevant even for graphs with only positive weights, due to the
possibility of distributed implementation. In the distributed implementation each node pushes its
new shortest path values to its neighbors, neighbors update their own values, etc. Algorithms
with synchronous and asynchronous implementation in the book. The synchronous distributed
implementation is the official Bellman-Ford algorithm.
Lemma 10. The augmented graph G0 has negative cycle such that there is a path from the cycle
to the sink t iff G has negative cycle.
Proof. Forward direction (If G0 has negative cycle, then G has too): Since no edge leaves t, the
negative cycle can not go through t, and needs to be there in G as well.
Backward direction (If G has negative cycle, then G0 has too): If G has a negative cycle, it will
not disappear due to the augmentation. Also, a path needs to go to t, since there is an edge from
all nodes to t.
• Sequence alignment
• Parsing
Dynamic programming:
• Similarly to divide and conquer we build up solution from subproblems
• Design steps: recursion, recursion with memorization, iterative solution