Salta al contenuto
Note per Studenti Divide and Conquer - Paradigm and Call Tree Report

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 Π⊆I×S\Pi\subseteq I\times S (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 Π⊆I×S\Pi\subseteq I\times S it is a triple (n0,D,C)(n_0,D,C):

  • a threshold n0∈Nn_0\in\mathbb N and an explicit description of the solutions of base instances (∣i∣≤n0\lvert i\rvert\le n_0);
  • a Divide function D:I→I⋆D:I\to I^\star mapping an instance with ∣i∣>n0\lvert i\rvert>n_0 to subinstances ⟨i1,…,ik⟩\langle i_1,\dots,i_k\rangle;
  • a Conquer function C:S⋆→SC:S^\star\to S building a solution from a sequence of solutions.

For every non-base ii with D(i)=⟨i1,…,ik⟩D(i)=\langle i_1,\dots,i_k\rangle:

  1. ∣ij∣<∣i∣\lvert i_j\rvert<\lvert i\rvert for all jj (subinstances strictly smaller: guarantees termination);
  2. if i1 Π s1,…,ik Π ski_1\,\Pi\,s_1,\dots,i_k\,\Pi\,s_k then i Π C(⟨s1,…,sk⟩)i\,\Pi\,C(\langle s_1,\dots,s_k\rangle) (guarantees correctness of the recombination).

With a direct method for base instances and algorithms ADA_D, ACA_C for DD and CC 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)                 // conquer

The correctness of the property implies the correctness of the algorithm: the proof mirrors the property.

The call tree

The execution on ii 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 A[1..n]A[1..n], n≥1n\ge1 integers, find the maximum. Substructure. For n>1n>1 split into two non-empty, as balanced as possible, subarrays; the maximum is the larger of the two maxima. For n=1n=1 it is the element itself (n0=1n_0=1). A subinstance is A[i..j]A[i..j], of size j−i+1j-i+1.

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 m2

Correctness

Induction on the size n=j−i+1n=j-i+1: [B] base, [HP] correct for smaller sizes, [TH] Divide gives strictly smaller subinstances and Conquer gives a solution.

  • [B] n=1⇒j=in=1\Rightarrow j=i: MAX returns A[i]A[i], the only element.
  • [TH] Divide. For n>1n>1, i<ji<j, hence i−1<k<ji-1<k<j. Then ∣i1∣=k−i+1<j−i+1=n\lvert i_1\rvert=k-i+1<j-i+1=n and ∣i2∣=j−k<j−i+1=n\lvert i_2\rvert=j-k<j-i+1=n, and both are ≥1\ge1. By [HP] m1,m2m_1,m_2 are correct.
  • [TH] Conquer. A=A1∪A2\mathcal A=\mathcal A_1\cup\mathcal A_2, and the maximum of a finite union is the maximum of the maxima: max⁡{m1,m2}=max⁡A\max\{m_1,m_2\}=\max\mathcal A. □\square

Complexity: the recurrence

Only comparisons between elements cost 11. Base: 00; Divide: 00; Conquer: 11. The subinstances have sizes ⌈n/2⌉\lceil n/2\rceil and ⌊n/2⌋\lfloor n/2\rfloor: T(n)={0n=1T(⌊n/2⌋)+T(⌈n/2⌉)+1n>1.T(n)=\begin{cases}0&n=1\\T(\lfloor n/2\rfloor)+T(\lceil n/2\rceil)+1&n>1.\end{cases} The argument of TT must be an integer: writing 2T(n/2)+12T(n/2)+1 is legitimate only for n=2kn=2^k (it is not enough that nn be even: n=6n=6 leads to T(3)T(3)).

Unfolding, n=2kn=2^k: T(n)=2T(n/2)+1=22T(n/22)+2+1=⋯=2iT(n/2i)+∑j=0i−12j.T(n)=2T(n/2)+1=2^2T(n/2^2)+2+1=\dots=2^iT(n/2^i)+\sum_{j=0}^{i-1}2^j. It stops when n/2i=1n/2^i=1, i.e. i=log⁡2ni=\log_2n; with T(1)=0T(1)=0 and ∑j=0m−1aj=am−1a−1\sum_{j=0}^{m-1}a^j=\frac{a^m-1}{a-1}: T(n)=n T(1)+(2log⁡2n−1)=n−1.T(n)=n\,T(1)+(2^{\log_2n}-1)=n-1. Check by induction on the parameter k=log⁡2nk=\log_2n: T(1)=0=n−1T(1)=0=n-1; T(n)=2(n/2−1)+1=n−1T(n)=2(n/2-1)+1=n-1.

Recurrence tree of MAX

For A=[1,7,1,7]A=[1,7,1,7] the call tree is: root [1 7 1 7][1\,7\,1\,7]; children [1 7][1\,7] and [1 7][1\,7] (identical, so the work is replicated: no memory); leaves [1],[7],[1],[7][1],[7],[1],[7]. 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

T(4)=3T(4)=3, the sum of all works. For general nn the tree is not complete, but (1) every internal node has two children and work 11; (2) there are exactly nn leaves (each call partitions its subarray in two disjoint parts), all of work 00. A binary tree is full if every internal node has exactly 22 children (not to be confused with complete: all leaves at the same level).

Theorem. A full binary tree with nn leaves has n−1n-1 internal nodes.

Proof (induction on nn). [B] n=1n=1: no internal nodes. [HP] true for 1≤k<n1\le k<n. [TH] a full tree with n>1n>1 leaves has an internal node vv of maximum level; its children cannot be internal, so they are two leaves. Removing them gives a full tree with n−1n-1 leaves (two leaves disappear, vv becomes a leaf) and one internal node fewer; by [HP] it has n−2n-2 internal nodes, so the original has n−1n-1. □\square

Hence T(n)=n−1T(n)=n-1 for every n≥1n\ge1 (and Θ(n)\Theta(n)).

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) −1-1; the worst-case recurrence is T(n)=2T(n/2)+n−1T(n)=2T(n/2)+n-1 (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 (n0,D,C)(n_0,D,C): explicit solutions for ∣i∣≤n0\lvert i\rvert\le n_0; D:I→I⋆D:I\to I^\star with ∣ij∣<∣i∣\lvert i_j\rvert<\lvert i\rvert; C:S⋆→SC:S^\star\to S with ij Π sj⇒i Π C(⟨s1..sk⟩)i_j\,\Pi\,s_j\Rightarrow i\,\Pi\,C(\langle s_1..s_k\rangle). 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: k=⌊(i+j)/2⌋k=\lfloor(i+j)/2\rfloor, two calls on A[i..k]A[i..k], A[k+1..j]A[k+1..j], one final comparison. Correctness by induction on n=j−i+1n=j-i+1: Divide, i−1<k<ji-1<k<j gives sizes <n<n; Conquer, max of a union is max of maxima.
  • Recurrence (only element comparisons cost): T(n)=T(⌊n/2⌋)+T(⌈n/2⌉)+1T(n)=T(\lfloor n/2\rfloor)+T(\lceil n/2\rceil)+1, T(1)=0T(1)=0. Without rounding only for n=2kn=2^k (not just even nn). Unfolding: 2iT(n/2i)+∑j<i2j2^iT(n/2^i)+\sum_{j<i}2^j, i=log⁡2ni=\log_2n, so T(n)=n−1T(n)=n-1.
  • Recurrence tree: node = size, beside it the work (Divide+Conquer); T(n)T(n) = sum of all works. In MAX: internal nodes with 22 children and work 11, nn leaves of work 00.
  • Theorem: a full binary tree (every internal node has 22 children) with nn leaves has n−1n-1 internal nodes; induction removing the two leaf children of an internal node of maximum level. Hence T(n)=n−1T(n)=n-1 for all nn.
  • Instance-dependent work: merge sort, merge costs at most n−1n-1 comparisons.
  • Common mistakes: writing 2T(n/2)+12T(n/2)+1 for general nn; confusing full and complete trees; leaving nn as the induction parameter without saying it is log⁡2n\log_2n; assuming work depends only on size.

Teoria collegata