算法导论 复习

Hash Table

Dynamic Sets

  • Elements have a key and satellite data
  • support queries:Search(S,k),Maximum(S),Successor(S,x),Predecessor(S,x)
  • support modifying operation: Insert(S,x), Del

Hash Tables

  1. Direct-access tables
  • keys are drawn from set U ⊆ { 0 , 1 , . . . , m − 1 } U\subseteq\{0,1,...,m-1\} U{0,1,...,m1} and distinct, set up an array T[m]
    T [ k ] = { x ,    i f   x ∈ K   a n d   k e y [ x ] = k , N I L ,    o t h e r w i s e T[k]=\begin{cases}x, \quad \ \ & if\ x\in K\ and\ key[x]=k,\\ NIL,\quad \ \ &otherwise\end{cases} T[k]={x,  NIL,  if xK and key[x]=k,otherwise
    Θ ( 1 ) \Theta (1) Θ(1) time
  • limitation:range of key can be very large, while set K of keys actually stored maybe <<U, most of the space allocated for T is wasted.
  1. Resolving collisions by chaining
  • Link records in the same slot in a list
  • Worst case: Θ ( n ) \Theta(n) Θ(n)
  • Average case:
    let n = #keys, m= #slots, load factor T α = n m \alpha=\frac{n}{m} α=mn= average #keys/slot
    Search cost: expected unsuccessful search = Θ ( 1 + α ) =\Theta(1+\alpha ) =Θ(1+α)
    expected search time = Θ ( 1 )   i f   α = O ( 1 ) ( n = O ( m ) ) =\Theta(1)\quad \ if\ \alpha=O(1)(n=O(m)) =Θ(1) if α=O(1)(n=O(m))
  1. Choosing hash functions
  • distribute the keys uniformly into the slots of the table
  • regularity in the key distribution should not affect this uniformly
    (1) Division method: h ( k ) = k   m o d   m h(k)=k\ mod\ m h(k)=k mod m
    Deficiency: Don’t pick an m has a small remainder d. A preponderance of(数量上的优势) keys that are conguent(相配的,全等的) modulo d can adversely affect uniformly. If m = 2 r m=2^r m=2r, the the hash doesnot even depend on all its bits.
    Proper m: prime and not too close to a power of 2 or 10 and not otherwise used prominently in the computing environment. eg. m=701 for n=2000
    (2)Multiplication method: m = 2 r    h ( k ) = ( A ⋅ k   m o d 2 w ) r s h ( w − r ) m=2^r\ \ h(k)=(A\cdot k\ mod 2^w)rsh(w-r) m=2r  h(k)=(Ak mod2w)rsh(wr) ( 2 w − 1 &lt; A &lt; 2 w ) (2^{w-1}&lt;A&lt;2^w) (2w1<A<2w)
    donnot pick A too close to 2 w − 1 2^{w-1} 2w1 or 2 w 2^w 2w
  1. Open addressing
    No storage is used outside of the hash table itself.
    Insertion systematically probes the table until an empty slot is found
    \quad \newline Probing strategies:
    (1)Linear probing: h ( k , i ) = ( h ′ ( k ) + i )   m o d   m h(k,i)=(h^{&#x27;}(k)+i)\ mod\ m h(k,i)=(h(k)+i) mod m suffers from primary clustering(occupied slots tend to get longer.
    (2)Quadratic probing: h ( k , i ) = ( h ′ ( k ) + c 1 i + c 2 i 2 )   m o d   m h(k,i)=(h^{&#x27;}(k)+c_1i+c_2i^2)\ mod\ m h(k,i)=(h(k)+c1i+c2i2) mod m, suffers sencondary clustering
    (3)Double hashing: h ( k , i ) = ( h 1 ( k ) + i h 2 ( k ) ) h(k,i)=(h_1(k)+ih_2(k)) h(k,i)=(h1(k)+ih2(k)) , h 2 ( k ) h_2(k) h2(k)must be co-prime to m, one way is make m a power of 2 and h 2 ( k ) h_2(k) h2(k) produce odd numbers
    \newline
    Analysis:
    Assumption of uniform hashing , we get Theorem: Given open-addressed hash table with α = n / m &lt; 1 \alpha=n/m&lt;1 α=n/m<1,the expected number of probes in an unsuccessful search is at most 1 / ( 1 − α ) 1/(1-\alpha) 1/(1α)
    Proof:
    α = n m &lt; 1 , s o   n − 1 m − 1 &lt; n m , E ( s ) = 1 + n m ( 1 + ( n − 1 m − 1 ( 1 + . . . ) ) ) \alpha=\frac{n}{m}&lt;1,so\ \frac{n-1}{m-1}&lt;\frac{n}{m}, E(s)=1+\frac{n}{m}(1+(\frac{n-1}{m-1}(1+...))) α=mn<1,so m1n1<mn,E(s)=1+mn(1+(m1n1(1+...)))
    E ( s ) ≤ 1 + α ( 1 + α ( 1 + α ( . . . ) ) ) ≤ 1 + α + α 2 + . . . = 1 1 − α E(s)\leq1+\alpha(1+\alpha(1+\alpha(...)))\leq1+\alpha+\alpha^2+...=\frac{1}{1-\alpha} E(s)1+α(1+α(1+α(...)))1+α+α2+...=1α1

Binary Search Trees

  • Properties: k e y [ l e f t ( x ) ] ≤ k e y [ x ] ≤ k e y [ r i g h t ( x ) ] key[left(x)]\leq key[x]\leq key[right(x)] key[left(x)]key[x]key[right(x)]

  • Walks:
    (1)Inorder tree walk: left,root,right output sorted (increasing) sequence
    (2)Preorder tree walk: root,left,right
    (3)Postorder tree walk: left,right,root

  • Operation:
    (1)search or insert: O ( h ) O(h) O(h) worst-case O ( n ) O(n) O(n)
    (2)successor:have right subtree: minimum node in the subtree; otherwise, lowest ancestor of x whose left child is also one ancestor of x.
    (3)Delete:

    • has no children : remove x
    • has only one child: splice out x
    • has two children swap x with successor perform case 1 or 2 to delete
  • Sorting with BST

    • Worst case: O ( n 2 ) O(n^2) O(n2) ; best case: O ( n l g n ) O(nlgn) O(nlgn) ; average case: O ( n l g n ) O(nlgn) O(nlgn)
    • Comparison to quicksort: Inserted nodes are similar to partition pivot, but in a different order, BST does not partition immediately after picking the inserted node.
      quicksort is better since it sorts in place and does not need to build data structure.
  • Randomly Built BST

    • (1)Theorem: The average height of a randomly-built binary search tree of n distinct keys is O l g ( n ) Olg(n) Olg(n)
    • (2)Proof:
    • Outline:
    1. Prove Jensen’s inequality f ( E [ X ] ) ≤ E [ f ( X ) ]   w h e r e   f   i s   c o n v e x f(E[X])\leq E[f(X)]\ where\ f\ is\ convex f(E[X])E[f(X)] where f is convex
    2. Analyze the exponential height of RBST
    3. Put together
    • Convex Funciton: α , β ≥ 0 &amp; α + β = 1 ⇒ f ( α x + β y ) ≤ α f ( x ) + β f ( y ) \alpha,\beta \geq0\&amp;\alpha+\beta=1\Rightarrow f(\alpha x+\beta y)\leq \alpha f(x)+\beta f(y) α,β0&α+β=1f(αx+βy)αf(x)+βf(y)
    • Prove Jensen’s inequality:
      For n=1, we have α 1 = 1 , h e n s e   f ( α 1 x 1 ) ≤ α 1 f ( x 1 ) \alpha_1=1, hense\ f(\alpha_1x_1)\leq \alpha_1f(x_1) α1=1,hense f(α1x1)α1f(x1)
      Suppose for it is true for n, then prove it is also true for n+1
      f ( ∑ k = 1 n + 1 α k x k ) = f ( α n + 1 x n + 1 + ( 1 − α n + 1 ) ∑ k = 1 n α k 1 − α n + 1 x k ) ≤ α n + 1 f ( x n + 1 ) + ( 1 − α n + 1 ) f ( ∑ k = 1 n α k 1 − α n + 1 x k ) ) ≤ α n + 1 f ( x n + 1 ) + ( 1 − α n + 1 ) ( ∑ k = 1 n α k 1 − α n + 1 f ( x k ) ) = ∑ k = 1 n + 1 α k f ( x k ) f(\sum^{n+1}_{k=1}\alpha_kx_k)=f(\alpha_{n+1}x_{n+1}+(1-\alpha_{n+1})\sum^{n}_{k=1}\frac{\alpha_k}{1-\alpha_{n+1}}x_k)\\ \leq \alpha_{n+1}f(x_{n+1})+(1-\alpha_{n+1})f(\sum^{n}_{k=1}\frac{\alpha_k}{1-\alpha_{n+1}}x_k))\\ \leq \alpha_{n+1}f(x_{n+1})+(1-\alpha_{n+1})(\sum^n_{k=1}\dfrac{\alpha_k}{1-\alpha_{n+1}}f(x_k))=\sum_{k=1}^{n+1}\alpha_kf(x_k) f(k=1n+1αkxk)=f(αn+1xn+1+(1αn+1)k=1n1αn+1αkxk)αn+1f(xn+1)+(1αn+1)f(k=1n1αn+1αkxk))αn+1f(xn+1)+(1αn+1)(k=1n1αn+1αkf(xk))=k=1n+1αkf(xk)
      Let f f f be a convex function, and let X X X be a random variable, α k = P { X = x k } \alpha_k=P\{X=x_k\} αk=P{X=xk}, then we get f ( E [ x ] ) ≤ E [ f ( x ) ] f(E[x])\leq E[f(x)] f(E[x])E[f(x)]
    • Analysis of BST Height:
      Let X n X_n Xn be the random variable denoting the height of RBST on n nodes, and let Y n = 2 X n Y_n=2^{X_n} Yn=2Xn be its exponential height.
      If the root of tree has a rank k k k, then X n = 1 + m a x { X k − 1 , X n − k } X_n=1+max\{X_{k-1},X_{n-k}\} Xn=1+max{Xk1,Xnk},since each side of the subtrees are built randomly. Hence, Y n = 2 ⋅ m a x { Y k − 1 , Y n − k } Y_n=2\cdot max\{Y_{k-1},Y_{n-k}\} Yn=2max{Yk1,Ynk}
      Define indicator random variable Z n k = { 1   i f   t h e   r o o t   h a s   r a n k   k , 0 o t h e r w i s e . Z_{nk}=\begin{cases} 1\quad \ &amp; if\ the\ root\ has\ rank\ k,\\ 0 &amp; otherwise. \end{cases} Znk={1 0if the root has rank k,otherwise.
      Thus, P r { Z n k = 1 } = E [ Z n k ] = 1 n Pr\{Z_{nk}=1\}=E[Z_{nk}]=\dfrac{1}{n} Pr{Znk=1}=E[Znk]=n1, and
      Y n = ∑ k = 1 n Z n k ( 2 ⋅ m a x { Y k − 1 , Y n − k } ) E [ Y n ] = E [ ∑ k = 1 n Z n k ( 2 ⋅ m a x { Y k − 1 , Y n − k } ) ] = ∑ k = 1 n E [ Z n k ( 2 ⋅ m a x { Y k − 1 , Y n − k } ) ] Y_n=\sum_{k=1}^{n}Z_{nk}(2\cdot max\{Y_{k-1},Y_{n-k}\})\\ E[Y_n]=E[\sum_{k=1}^{n}Z_{nk}(2\cdot max\{Y_{k-1},Y_{n-k}\})]=\sum_{k=1}^{n}E[Z_{nk}(2\cdot max\{Y_{k-1},Y_{n-k}\})] Yn=k=1nZnk(2max{Yk1,Ynk})E[Yn]=E[k=1nZnk(2max{Yk1,Ynk})]=k=1nE[Znk(2max{Yk1,Ynk})]
      Due to the independence of the rank of the root from the rank of the subtrees
      E [ Y n ] = 2 ∑ k = 1 n E [ Z n k ] E [ ( m a x { Y k − 1 , Y n − k } ) ] ≤ 2 n ∑ k = 1 n E [ Y k − 1 + Y n − k ] = 4 n ∑ k = 0 n − 1 E [ Y k ] E[Y_n]=2\sum_{k=1}^{n}E[Z_{nk}]E[(max\{Y_{k-1},Y_{n-k}\})]\leq \frac{2}{n}\sum_{k=1}^{n}E[Y_{k-1}+Y_{n-k}]=\frac{4}{n}\sum_{k=0}^{n-1}E[Y_k] E[Yn]=2k=1nE[Znk]E[(max{Yk1,Ynk})]n2k=1nE[Yk1+Ynk]=n4k=0n1E[Yk]
      Assume for k<n, it is true that E [ Y k ] ≤ c k 3 E[Y_k]\leq ck^3 E[Yk]ck3, for n,
      E [ Y n ] = 4 n ∑ k = 0 n − 1 E [ Y k ] ≤ 4 n ∑ k = 0 n − 1 c k 3 ≤ 4 c n ∫ 0 n x 3 d x = c n 3 E[Y_n]=\frac{4}{n}\sum_{k=0}^{n-1}E[Y_k]\leq \frac{4}{n}\sum_{k=0}^{n-1}ck^3\leq \frac{4c}{n}\int_0^nx^3dx=cn^3 E[Yn]=n4k=0n1E[Yk]n4k=0n1ck3n4c0nx3dx=cn3
    • Putting it all together
      f ( x ) = 2 x    2 E [ X n ] ≤ E [ 2 X n ] = E [ Y n ] ≤ c n 3 ⇒ E [ X n ] ≤ 3 l g n + O ( 1 ) f(x)=2^{x}\quad \ \ 2^{E[X_n]}\leq E[2^{X_n}]=E[Y_n]\leq cn^3\Rightarrow E[X_n]\leq 3lgn+O(1) f(x)=2x  2E[Xn]E[2Xn]=E[Yn]cn3E[Xn]3lgn+O(1)

Red-Black Trees

  • Red-Black Tree Properties:
    • Every node is either red or black
    • Every leaf(NULL pointer) is black
    • If a node is red, both children are black
    • Every path from node to descendent leaf contains the same nuber of black nodes
    • The root is always black
  • Black-height
    • a height-h node has black-height ≥ h / 2 \geq h/2 h/2 (according to thrid rule)
    • Theorem: A red-black tree with n internal nodes has height h ≤ 2   l g ( n + 1 ) h\leq 2\ lg(n+1) h2 lg(n+1)
      Proof:
      Claims:A subtree rooted at a node x contains at least 2 b h ( x ) − 1 2^{bh(x)}-1 2bh(x)1 internal nodes
      it is true for bh(x)=0
      ( 2 b h ( x ) − 1 − 1 ) + ( 2 b h ( x ) − 1 − 1 ) + 1 = 2 b h ( x ) − 1 (2^{bh(x)-1}-1)+(2^{bh(x)-1}-1)+1=2^{bh(x)}-1 (2bh(x)11)+(2bh(x)11)+1=2bh(x)1
      Thus n ≥ 2 b h ( r o o t ) − 1 ⇒ n ≥ 2 h / 2 − 1 ⇒ h ≤ 2   l g ( n + 1 ) n\geq 2^{bh(root)}-1\Rightarrow n\geq 2^{h/2}-1\Rightarrow h\leq 2\ lg(n+1) n2bh(root)1n2h/21h2 lg(n+1)
    • Corollary:
      Minimum(),Maximum(),Successor(),Predecessor(),Search() take O ( l g n ) O(lgn) O(lgn) time
  • Insert Operation
    if parent is the left child of its parent: (3 cases) if x’s uncle is red, then recolor is ok. Otherwise, if x is the right child of its parent, left rotate it to the case 3. For case 3, right rotate x’s parent and then recolor is ok.
  • Expansion
    • Longest simple path from a node x in a red-black tree to a descendant leaf has length at most twice that of the shortest simple path. (Proof)
    • Largest possible number of internal nodes in a bh(x)=k , 2 2 k − 1 2^{2k}-1 22k1, smallest 2 k − 1 2^k-1 2k1
    • AVL tree:
      (1)Prove that n nodes has height O(lgn)
      Least number F 0 = 0 , F 1 = 1 , F 2 = 2 , F n = F n − 1 + F n − 2 + 1 F_0=0,F_1=1,F_2=2,F_n=F_ {n-1}+F_{n-2}+1 F0=0,F1=1,F2=2,Fn=Fn1+Fn2+1
      Fi grows exponetialy so the height of the tree is lgn
      (2)Insert operation:rotation

B-tree

  • Definition
    • Every node x has following fields: n[x] where keys are stored in undescreasing order, Leaf[x]=True|False
    • Each internal x also contains n[x]+1 pointers. Leaf nodes have no children, so their c i c_i ci field are undefine
    • k 1 ≤ k e y 1 [ x ] ≤ k 2 ≤ k e y 2 [ x ] ≤ ⋯ ≤ k e y n [ x ] ≤ k n [ x ] + 1 k_1\leq key_1[x]\leq k_2\leq key_2[x]\leq \cdots \leq key_{n[x]}\leq k_{n[x]+1} k1key1[x]k2key2[x]keyn[x]kn[x]+1
    • All leaves have the same depth, which is the tree’s height h
    • There are lower and upper bounds on the number of keys a node can contain: each node other than root must have t-1 keys. If the tree is nonempty, the root must have at least one key. Every node can contain at most 2t-1 keys, therefore, an internal node can have at most 2t children.
  • B-Tree Height h ≤ l o g t ( ( n + 1 ) / 2 ) h\leq log_t((n+1)/2) hlogt((n+1)/2)
  • Basic Operations
    • Searching a B-tree: CPU time: O ( t l o g t   n ) O(tlog_t\ n) O(tlogt n), disk operation: θ ( l o g t   n ) \theta(log_t\ n) θ(logt n)
    • Splitting a node: CPU times: θ ( t ) \theta (t) θ(t), disk operation: O ( 1 ) O(1) O(1)
    • Inserting a key: need to split when the node is full
    • Deleting a key: need to merge before go down; if it is leaf, remove directly; otherwise find predecessor or successor to replace k, recursively delete, when both of them are less then t elements, then merge them.

Augmenting Data Structures

  • Dynamic Order Statistics
    • Os-Tree: find the ith element of a dynamic set in O(lg n) time
    • Os-Tree augment red-black trees:
      size[x]=size[left]+size[right]+1, core idea: rank[x]=size[left]+1
  • Interval Trees
    record the max interval of the subtree
    Correctness: if it go left, the overlap is in left tree, otherwise ,there will be no overlap in the right tree.

Skip List

  • Introduction:
    Simple random dynamic search structure.
    Maintains a dynamic set of n elements in O ( l g n ) O(lg n ) O(lgn) time per operation in expectation and with high probability

  • Search Cost
    ∣ L 1 ∣ + ∣ L 2 ∣ ∣ L 1 ∣ ⇒ 2 n |L_1|+\dfrac{|L_2|}{|L_1|}\Rightarrow 2\sqrt{n} L1+L1L22n
    k sorted lists ⇒ k ⋅ n k \Rightarrow k\cdot \sqrt[k]{n} kkn
    lg n sorted lists ⇒ l g   n ⋅ n l g n = 2 l g   n \Rightarrow lg\ n\cdot \sqrt[lg n]{n} = 2lg\ n lg nlgnn =2lg n

  • Insert Operation:
    insert x to the bottom list, promote x to next level up and flip again

  • Delete Operation:
    remove x from all lists containing it

  • Theorem: With high probability, every search in an n-element skip list costs O(lg n)
    -With high probability: Event E occurs w.h.p. if, for any α ≥ 1 \alpha \geq1 α1, there is an appropriate choise of constants for which E occurs with probability at least 1 − O ( 1 / n α ) 1-O(1/n^{\alpha}) 1O(1/nα). Formally: Parameterized event E α E_\alpha Eα occurs w.h.p if, for any α ≥ 1 \alpha \geq1 α1, there is an appropriate choise of constants for which E α E_\alpha Eα occurs with probability at least 1 − c α / n α ) 1-c_\alpha/n^{\alpha}) 1cα/nα)
    -Proof:
    (1)w.h.p. skip list has O(lg n) levels
    P r { m o r e   t h a n   c   l g   n   l e v e l s } ≤ n ⋅ P r { Pr\{more\ than\ c\ lg\ n\ levels\}\leq n\cdot Pr\{ Pr{more than c lg n levels}nPr{x promoted at least c lg n tiems } = n ⋅ ( 1 2 c   l g   n ) = n ⋅ ( 1 n c ) = 1 n c − 1 \}=n\cdot (\dfrac{1}{2^{c\ lg\ n}})=n\cdot (\dfrac{1}{n^c})=\dfrac{1}{n^{c-1}} }=n(2c lg n1)=n(nc1)=nc11
    Error probability for having at most c lg n levels are less than 1 n c − 1 \dfrac{1}{n^{c-1}} nc11, which is polynomially small. We can make α \alpha α arbitraily large by choosing the constant c in the O(lg n) bound accordingly.
    (2)w.h.p every search in skip list cost O(lg n)
    Analyze search backwards——leaf to root, If node wasnot promoted higher(got tails here) then we go left, otherwise we go up.
    Number of ‘up’ moves < #levels ≤ \leq clg n w.h.p.(Lemma(1))
    #coin flips until c lg n heads = Θ ( l g   n ) =\Theta(lg\ n) =Θ(lg n) w.h.p.
    Obviously at least flip c lg n coins to get heads, so #= Ω ( l g   n ) \Omega(lg\ n) Ω(lg n)
    Suppose we make 10c lg n flips
    P r { a t   m o s t c   l g   n   h e a d s } ≤ C ( 10 c   l g   n , c   l g   n ) ⋅ ( 1 2 ) 9 c   l g   n ≤ ( e 10 c   l g   n c   l g   n ) c   l g   n ⋅ ( 1 2 ) 9 c   l g   n = 2 l g ( 10 e ) ⋅ c   l g   n 2 − 9 c   l g   n = 2 [ l g ( 10 e ) − 9 ] c   l g   n = 1 / n α   f o r α = [ 9 − l g ( 10 e ) ] c Pr\{at\ most c\ lg\ n\ heads\}\leq C({10c\ lg\ n},{c\ lg\ n})\cdot (\dfrac{1}{2})^{9c\ lg\ n}\leq (e\dfrac{10c\ lg\ n}{c\ lg\ n})^{c\ lg\ n}\cdot (\dfrac{1}{2})^{9c\ lg\ n}\\ =2^{lg(10e)\cdot c\ lg\ n}2^{-9c\ lg\ n}=2^{[lg(10e)-9]c\ lg\ n}=1/n^\alpha\ for\alpha=[9-lg(10e)]c Pr{at mostc lg n heads}C(10c lg n,c lg n)(21)9c lg n(ec lg n10c lg n)c lg n(21)9c lg n=2lg(10e)c lg n29c lg n=2[lg(10e)9]c lg n=1/nα forα=[9lg(10e)]c
    if we flip more coins, the posibilities are even less, so the upper bound is c lg n. Proof.

  • Expansion
    Randomly built binary tree
    Assign random priority

Dynamic Programming

  • Basic idea:
    Subproblem are dependent, if we treat them independently, we will calculate redundantly.
  • Procedure:
    • Divide into subproblem
    • Solve subproblem and store the solution in a list, and access it when the solution is reused
    • Bottom-up
  • Elements:
    • Optimal structure: optimal solution to the problem is contained within its optimal solutions to subproblems
    • Overlapping subproblems
  • Cases
    • Matrix-chain Multiplication
      A[1:n]=optimal order for complute A 1 A 2 . . . A n A_1A_2...A_n A1A2...An
      A[1:n]=minimum{1 ≤ k &lt; n \leq k&lt;n k<n:A[1,k]+A[k+1,n]+ p 0 p k p n p_0p_kp_n p0pkpn }
      for A[i:j] = minimum{i ≤ k &lt; j \leq k&lt;j k<j:A[1,k]+A[k+1,n]+ p i − 1 p k p j p_{i-1}p_kp_j pi1pkpj }
      -Longest Common Sequence
      c[i,j] means LCS of X i , a n d   Y j X_i,and\ Y_j Xi,and Yj
      c [ i , j ] = { 0     i f   i = 0   o r   j = 0   c [ i − 1 , j − 1 ] + 1 i f   i , j &gt; 0   a n d   x i = y j m a x { c [ i − 1 , j ] , c [ i , j − 1 ] } i f   i , j &gt; 0   a n d   x i ≠ y j c[i,j]=\begin{cases}0\quad \ \ \ &amp;if\ i=0\ or\ j=0\ \\ c[i-1,j-1]+1&amp;if\ i,j&gt;0\ and\ x_i=y_j\\ max\{c[i-1,j],c[i,j-1]\}&amp;if\ i,j&gt;0\ and\ x_i\neq y_j\end{cases} c[i,j]=0   c[i1,j1]+1max{c[i1,j],c[i,j1]}if i=0 or j=0 if i,j>0 and xi=yjif i,j>0 and xi̸=yj
    • Triangle Decomposition
      t[i,j]: v i − 1 , v i , … , v j v_{i-1},v_i,\dots,v_j vi1,vi,,vj's minimum price.
      t [ i ] [ j ] = { 0     i = j m i n { t [ i ] [ k ] + t [ k + 1 ] [ j ] + w ( v i − 1 v k v j ) } t[i][j]=\begin{cases}0\quad \ \ \ &amp;i=j\\ min\{t[i][k]+t[k+1][j]+w(v_{i-1}v_kv_j)\}\end{cases} t[i][j]={0   min{t[i][k]+t[k+1][j]+w(vi1vkvj)}i=j
    • edit distance between X and Y
      d [ i , j ] = { 0     i f   i = 0   a n d   j = 0   m i n { d [ i − 1 , j − 1 ] + 1 , d [ i − 1 , j ] + d i f f ( i , j ) , d [ i , j − 1 ] + 1 } i f   i , j &gt; 0   a n d   x i ≠ y j d[i,j]=\begin{cases}0\quad \ \ \ &amp;if\ i=0\ and\ j=0\ \\ min\{d[i-1,j-1]+1,d[i-1,j]+diff(i,j),d[i,j-1]+1\}&amp;if\ i,j&gt;0\ and\ x_i\neq y_j\end{cases} d[i,j]={0   min{d[i1,j1]+1,d[i1,j]+diff(i,j),d[i,j1]+1}if i=0 and j=0 if i,j>0 and xi̸=yj
    • knapsack problem:
      c [ i , w ] = { 0     i f   i = 0   o r   w = 0   c [ i − 1 , w ] i f   w i &gt; w m a x { v i , c [ i − 1 , w − w i ] , c [ i − 1 ] [ w ] } i f   i , j &gt; 0   a n d   x i ≠ y j c[i,w]=\begin{cases}0\quad \ \ \ &amp;if\ i=0\ or\ w=0\ \\ c[i-1,w]&amp;if\ w_i&gt;w\\ max\{v_i,c[i-1,w-w_i],c[i-1][w]\}&amp;if\ i,j&gt;0\ and\ x_i\neq y_j\end{cases} c[i,w]=0   c[i1,w]max{vi,c[i1,wwi],c[i1][w]}if i=0 or w=0 if wi>wif i,j>0 and xi̸=yj

Greedy Algorithms

Simpler and more efficient than Dynamic Programming

  • Activity-selection problem
    greedy select earliest finish activity, Proof, cut and paste: ∣ S i j − a k ∪ a m ∣ = ∣ S i j ∣ |S_{ij}-a_k\cup a_m|=|S_{ij}| Sijakam=Sij
    O(n)
  • knapsack prob
    0-1 knapsack does not has the greedy choice, but fractional problem has.
  • Huffman codes
    Proof
  • unit-time task scheduling
  • Elelents of greedy strategy:
    1.make a choose and are left with one subproblem to solve
    2.Prove that there’s always an optimal solution that makes the greedy choice always safe.
    3.show that greedy choice and optimal solution to subproblem is the optimal solution to the problem

Linear Programming

  • Procedure:
    simplex=>find tightest constraint=>switch until all the coefficient of unknown variable in Z are negative.

Amortized Analysis

  • Analysis of dynamic tables
    Worst case for n insertions is Θ ( n ) \Theta(n) Θ(n)
  • Amortized analysis
    garantees the average performance of each operation in the worst case
    • aggregate method
    • accounting method
      charge c i ^ \hat{c_i} ci^ and the actual cost is c i c_i ci, ensure that
      ∑ i = 1 n c i ≤ ∑ i = 1 n c i ^ \sum^{n}_{i=1}c_i\leq \sum^{n}_{i=1}\hat{c_i} i=1ncii=1nci^
      Thus, the total amortized costs provide an upper bound on the total true cost
    • potential method
      c i ^ = c i + Φ ( D i ) − Φ ( D i − 1 ) \hat{c_i}=c_i+\Phi(D_i)-\Phi(D_{i-1}) ci^=ci+Φ(Di)Φ(Di1)
      ∑ i = 1 n c i ^ = ∑ i = 1 n ( c i + Φ ( D i ) − Φ ( D i − 1 ) ) = ∑ i = 1 n c i + Φ ( D n ) − Φ ( D 0 ) ≥ ∑ i = 1 n c i \sum^{n}_{i=1}\hat{c_i}=\sum^{n}_{i=1}(c_i+\Phi(D_i)-\Phi(D_{i-1}))=\sum^{n}_{i=1}{c_i}+\Phi(D_n)-\Phi(D_{0})\geq \sum^{n}_{i=1}{c_i} i=1nci^=i=1n(ci+Φ(Di)Φ(Di1))=i=1nci+Φ(Dn)Φ(D0)i=1nci
  • Expansion
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值