Divide and Conquer - Paradigm and Call Tree
In questa pagina 4
The first design paradigm: split a large instance into smaller subinstances of the same problem, solve them, and combine the solutions. Recursive algorithms are its realizations. The problem is (Computational Problems, Algorithms and Cost ModelA computational problem is a total, not necessarily single-valued relation between instances and solutions; an algorithm is a function from instances to solutions and solves the problem if its output is always related to its input. Correctness means agreement with the specification; complexity is the cost as a function of input size, by default in the worst case, with unit-cost instructions chosen meaningfully.Computational Problems, Algorithms and Cost Model →).
Substructure property
Definition. For it is a triple :
- a threshold and an explicit description of the solutions of base instances ();
- a Divide function mapping an instance with to subinstances ;
- a Conquer function building a solution from a sequence of solutions.
For every non-base with :
- for all (subinstances strictly smaller: guarantees termination);
- if then (guarantees correctness of the recombination).
With a direct method for base instances and algorithms , for and we get the algorithm. Metacode:
D&C(i) {i in I}
[B]: if |i| <= n0 then *solve directly*; return
[D]: <i_1, ..., i_k> <- A_D(i) // divide
[R]: for j <- 1 to k do s_j <- D&C(i_j) // recurse
[C]: return A_C(s_1, ..., s_k) // conquerThe correctness of the property implies the correctness of the algorithm: the proof mirrors the property.
The call tree
The execution on is the call tree: the root is the main instance, the children of a node are the subinstances produced by its Divide, the leaves are base instances. In this course all internal nodes have the same arity.
- Divide expands the tree root to leaves (top-down); Conquer contracts it leaves to root (bottom-up).
- When a subtree is contracted, all its information is destroyed: D&C has no memory. If the same subinstance appears at several places, its subtree is expanded and contracted several times, possibly with disastrous cost (e.g. Fibonacci); this is the weakness that dynamic programming fixes.
- The order of expansion is irrelevant. In practice recursion uses the system stack and visits the tree in preorder, keeping memory low.
Paradigmatic example: maximum of an array
Problem. Given , integers, find the maximum. Substructure. For split into two non-empty, as balanced as possible, subarrays; the maximum is the larger of the two maxima. For it is the element itself (). A subinstance is , of size .
MAX(A, i, j)
[B]: if i = j then return A[i]
[D]: k <- floor((i + j)/2)
[R]: m1 <- MAX(A, i, k)
m2 <- MAX(A, k + 1, j)
[C]: if m1 >= m2 then return m1 else return m2Correctness
Induction on the size : [B] base, [HP] correct for smaller sizes, [TH] Divide gives strictly smaller subinstances and Conquer gives a solution.
- [B] : MAX returns , the only element.
- [TH] Divide. For , , hence . Then and , and both are . By [HP] are correct.
- [TH] Conquer. , and the maximum of a finite union is the maximum of the maxima: .
Complexity: the recurrence
Only comparisons between elements cost . Base: ; Divide: ; Conquer: . The subinstances have sizes and : The argument of must be an integer: writing is legitimate only for (it is not enough that be even: leads to ).
Unfolding, : It stops when , i.e. ; with and : Check by induction on the parameter : ; .
Recurrence tree of MAX
For the call tree is: root ; children and (identical, so the work is replicated: no memory); leaves . Attaching to each node its work (cost of its Divide plus Conquer) gives the recurrence tree:
(4) 1
/ \
(2) 1 (2) 1
/ \ / \
(1) 0 (1) 0 (1) 0 (1) 0, the sum of all works. For general the tree is not complete, but (1) every internal node has two children and work ; (2) there are exactly leaves (each call partitions its subarray in two disjoint parts), all of work . A binary tree is full if every internal node has exactly children (not to be confused with complete: all leaves at the same level).
Theorem. A full binary tree with leaves has internal nodes.
Proof (induction on ). [B] : no internal nodes. [HP] true for . [TH] a full tree with leaves has an internal node of maximum level; its children cannot be internal, so they are two leaves. Removing them gives a full tree with leaves (two leaves disappear, becomes a leaf) and one internal node fewer; by [HP] it has internal nodes, so the original has .
Hence for every (and ).
Work depending on the instance. For MERGESORT counting only key comparisons, merging stops comparing as soon as one run is exhausted, so Conquer varies with the instance but is bounded by (sum of lengths) ; the worst-case recurrence is (Recurrence Relations - Recursion Tree and General FormulaThe recurrence tree has the shape of the call tree of the worst instance and T(n) is the sum of the works of its nodes. For the family T(n) = s(n) T(f(n)) + w(n) with base T0 for n <= n0, counting nodes per level with the iterates of f and the level f*(n,n0) gives the closed formula T(n) = sum over levels of [product of s] w + [product of s] T0, proved by induction and applied to merge sort, Hanoi and square-root recurrences.Recurrence Relations - Recursion Tree and General Formula →).
Versione ripasso
- Substructure : explicit solutions for ; with ; with . Metacode: [B] base, [D] divide, [R] recurse, [C] conquer.
- Call tree: root = main instance, leaves = base instances; Divide goes down, Conquer goes up. No memory: equal subinstances are recomputed (remedy: dynamic programming). Expansion order irrelevant; recursion visits in preorder.
- MAX: , two calls on , , one final comparison. Correctness by induction on : Divide, gives sizes ; Conquer, max of a union is max of maxima.
- Recurrence (only element comparisons cost): , . Without rounding only for (not just even ). Unfolding: , , so .
- Recurrence tree: node = size, beside it the work (Divide+Conquer); = sum of all works. In MAX: internal nodes with children and work , leaves of work .
- Theorem: a full binary tree (every internal node has children) with leaves has internal nodes; induction removing the two leaf children of an internal node of maximum level. Hence for all .
- Instance-dependent work: merge sort, merge costs at most comparisons.
- Common mistakes: writing for general ; confusing full and complete trees; leaving as the induction parameter without saying it is ; assuming work depends only on size.