Top Banner

of 25

Stanford Cheatsheet 2012

Jun 04, 2018

Download

Documents

vasodebatman
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 8/13/2019 Stanford Cheatsheet 2012

    1/25

    Sparse max-flow (C++)1.

    Min-cost max-flow (C++)2.

    Push-relabel max-flow (C++)3.

    Min-cost matching (C++)4.

    Max bipartite matching (C++)5.

    Global min cut (C++)6.

    Convex hull (C++)7.

    Miscellaneous geometry (C++)8.

    Java geometry (Java)9.

    3D geometry (Java)10.

    Slow Delaunay triangulation (C++)11.

    Number theoretic algorithms (modular, Chinese remainder, linear Diophantine) (C++)12.

    Systems of linear equations, matrix inverse, determinant (C++)13.Reduced row echelon form, matrix rank (C++)14.

    Fast Fourier transform (C++)15.

    Simplex algorithm (C++)16.

    Fast Dijkstra's algorithm (C++)17.

    Strongly connected components (C)18.

    Suffix arrays (C++)19.

    Binary Indexed Tree20.

    Union-Find Set (C/C++)21.

    KD-tree (C++)22.

    Longest increasing subsequence (C++)23.

    Dates (C++)24.

    Regular expressions (Java)25.

    Prime numbers (C++)26.

    Knuth-Morris-Pratt (C++)27.

    // Adjacency list implementation of Dinic's blocking flow algorithm.// This is very fast in practice, and only loses to push-relabel flow.

    //// Running time:// O(|V|^2 |E|)//// INPUT:// - graph, constructed using AddEdge()// - source// - sink//// OUTPUT:// - maximum flow value// - To obtain the actual flow values, look at all edges with// capacity > 0 (zero capacity edges are residual edges).

    #include

    #include#include#include

    using namespace std;

    const intINF = 2000000000;

    structEdge {intfrom, to, cap, flow, index;

    Edge(intfrom, intto, intcap, intflow, intindex) : from(from), to(to), cap(cap), flow(flow), index(index) {}};

    structDinic {intN;

    vector G; vector dad; vector Q;

    Dinic(intN) : N(N), G(N), dad(N), Q(N) {}

    voidAddEdge(intfrom, intto, intcap) { G[from].push_back(Edge(from, to, cap, 0, G[to].size()));

    if(from == to) G[from].back().index++; G[to].push_back(Edge(to, from, 0, 0, G[from].size() - 1)); }

    long longBlockingFlow(ints, intt) { fill(dad.begin(), dad.end(), (Edge *) NULL); dad[s] = &G[0][0] - 1;

    inthead = 0, tail = 0; Q[tail++] = s;

    while(head < tail) {intx = Q[head++];for(inti = 0; i < G[x].size(); i++) {Edge &e = G[x][i];if(!dad[e.to] && e.cap - e.flow > 0) { dad[e.to] = &G[x][i]; Q[tail++] = e.to;}

    } }

    if(!dad[t]) return0;

    long longtotflow = 0;

    for(inti = 0; i < G[t].size(); i++) { Edge *start = &G[G[t][i].to][G[t][i].index];

    Stanford

    University

    1

  • 8/13/2019 Stanford Cheatsheet 2012

    2/25

  • 8/13/2019 Stanford Cheatsheet 2012

    3/25

  • 8/13/2019 Stanford Cheatsheet 2012

    4/25

  • 8/13/2019 Stanford Cheatsheet 2012

    5/25

  • 8/13/2019 Stanford Cheatsheet 2012

    6/25

  • 8/13/2019 Stanford Cheatsheet 2012

    7/25

  • 8/13/2019 Stanford Cheatsheet 2012

    8/25

  • 8/13/2019 Stanford Cheatsheet 2012

    9/25

  • 8/13/2019 Stanford Cheatsheet 2012

    10/25

  • 8/13/2019 Stanford Cheatsheet 2012

    11/25

  • 8/13/2019 Stanford Cheatsheet 2012

    12/25

    // expected: -0.233333 0.166667 0.133333 0.0666667// 0.166667 0.166667 0.333333 -0.333333// 0.233333 0.833333 -0.133333 -0.0666667// 0.05 -0.75 -0.1 0.2

    cout

  • 8/13/2019 Stanford Cheatsheet 2012

    13/25

  • 8/13/2019 Stanford Cheatsheet 2012

    14/25

  • 8/13/2019 Stanford Cheatsheet 2012

    15/25

  • 8/13/2019 Stanford Cheatsheet 2012

    16/25

  • 8/13/2019 Stanford Cheatsheet 2012

    17/25

  • 8/13/2019 Stanford Cheatsheet 2012

    18/25

  • 8/13/2019 Stanford Cheatsheet 2012

    19/25

  • 8/13/2019 Stanford Cheatsheet 2012

    20/25

  • 8/13/2019 Stanford Cheatsheet 2012

    21/25

  • 8/13/2019 Stanford Cheatsheet 2012

    22/25

  • 8/13/2019 Stanford Cheatsheet 2012

    23/25

  • 8/13/2019 Stanford Cheatsheet 2012

    24/25

    Stan

  • 8/13/2019 Stanford Cheatsheet 2012

    25/25

    Theoretical Computer Science Cheat Sheet

    Series Eschers KnotExpansions:

    1

    (1 x)n+1 ln 1

    1 x =i=0

    (Hn+i Hn)

    n + i

    i

    xi,

    1

    x

    n=

    i=0

    i

    n

    xi,

    xn =i=0

    n

    i

    xi, (ex 1)n =

    i=0

    i

    n

    n!xi

    i! ,

    ln

    1

    1 xn

    =

    i=0

    i

    n

    n!xi

    i! , x cot x =

    i=0

    (4)iB2ix2i(2i)!

    ,

    tan x =i=1

    (1)i1 22i(22i 1)B2ix2i1

    (2i)! , (x) =

    i=1

    1

    ix,

    1

    (x) =

    i=1(i)

    ix ,

    (x 1)(x)

    =

    i=1(i)

    ix ,

    Stieltjes Integration(x) =p

    11 px ,

    2(x) =i=1

    d(i)

    xi whered(n) =

    d|n1,

    (x)(x 1) =i=1

    S(i)

    xi whereS(n) =

    d|n d,

    (2n) =2 2n1|B2n|

    (2n)! 2n, n N,

    x

    sin x =

    i=0

    (1)i1 (4i 2)B2ix2i

    (2i)! ,

    1

    1 4x2x

    n

    =

    i=0

    n(2i + n 1)!i!(n + i)!

    xi,

    ex sin x =i=1

    2i/2 sin i4i!

    xi,

    1 1 x

    x =

    i=0

    (4i)!

    16i

    2(2i)!(2i + 1)!xi,

    arcsin x

    x

    2=

    i=0

    4ii!2

    (i + 1)(2i + 1)!x2i.

    IfG is continuous in the interval [a, b] andF is nondecreasing then ba

    G(x) dF(x)

    exists. Ifa b c then ca

    G(x) dF(x) =

    ba

    G(x) dF(x) +

    cb

    G(x) dF(x).

    If the integrals involved exist ba

    G(x) + H(x)

    dF(x) =

    ba

    G(x) dF(x) +

    ba

    H(x) dF(x),

    b

    a

    G(x) d

    F(x) + H(x)

    =

    b

    a

    G(x) dF(x) +

    b

    a

    G(x) dH(x),

    b

    a

    c G(x) dF(x) = b

    a

    G(x) d

    c F(x) =c ba

    G(x) dF(x),

    ba

    G(x) dF(x) = G(b)F(b) G(a)F(a) ba

    F(x) dG(x).

    If the integrals involved exist, andFpossesses a derivativeFat everypoint in [a, b] then b

    a

    G(x) dF(x) =

    ba

    G(x)F(x) dx.

    Cramers Rule0 0 4 7 1 8 7 6 2 9 9 3 8 5 3 4 6 1 5 2

    8 6 1 1 5 7 2 8 7 0 3 9 9 4 4 5 0 2 6 3

    9 5 8 0 2 2 6 7 3 8 7 1 4 9 5 6 1 3 0 4

    5 9 9 6 8 1 3 3 0 7 4 8 7 2 6 0 2 4 1 5

    7 3 6 9 9 0 8 2 4 4 1 7 5 8 0 1 3 5 2 6

    6 8 7 4 0 9 9 1 8 3 5 5 2 7 1 2 4 6 3 0

    3 7 0 8 7 5 1 9 9 2 8 4 6 6 2 3 5 0 4 1

    1 4 2 5 3 6 4 0 5 1 6 2 0 3 7 7 8 8 9 9

    2 1 3 2 4 3 5 4 6 5 0 6 1 0 8 9 9 7 7 8

    4 2 5 3 6 4 0 5 1 6 2 0 3 1 9 8 7 9 8 7

    Fibonacci Numbers

    If we have equations:a1,1x1+ a1,2x2+ + a1,nxn = b1a2,1x1+ a2,2x2+

    + a2,nxn = b2

    ......

    ...

    an,1x1+ an,2x2+ + an,nxn= bnLetA= (ai,j) andB be the column matrix (bi). Thenthere is a unique solution iff det A= 0. Let Ai b eAwith columni replaced by B . Then

    xi=det Ai

    det A.

    1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, . . .

    Definitions:

    Fi = Fi1+Fi2, F0 = F1= 1,Fi= (1)i1Fi,

    Fi = 1

    5

    i i

    ,

    Cassinis identity: for i >0:

    Fi+1Fi1 F2i = (1)i.Additive rule:

    Fn+k= FkFn+1+ Fk1Fn,

    F2n= FnFn+1+ Fn1Fn.

    Calculation by matrices:Fn2 Fn1Fn1 Fn

    =

    0 11 1

    n.

    The Fibonacci number system:Every integer n has a uniquerepresentation

    n= Fk1+ Fk2+ + Fkm,where ki ki+1 + 2 for all i ,1 i < m and km 2.

    Improvement makes strait roads, but the crookedroads without Improvement, are roads of Genius. William Blake (The Marriage of Heaven and Hell)

    nford

    University

    25