diff --git a/Writerside/topics/Data-Structures-and-Algorithms-1.md b/Writerside/topics/Data-Structures-and-Algorithms-1.md index 31ff7fd..bc6f339 100644 --- a/Writerside/topics/Data-Structures-and-Algorithms-1.md +++ b/Writerside/topics/Data-Structures-and-Algorithms-1.md @@ -918,7 +918,7 @@ class ArrayDeque: -

Defect:

+

Defect

  • Union too expensive (N array accesses).

    @@ -950,7 +950,7 @@ class ArrayDeque: -

    Defect:

    +

    Defect

  • Trees can get tall.

    @@ -1211,7 +1211,7 @@ class UnionFind: ### 4.1 Stacks -

    Defintions:

    +

    Defintions

  • @@ -1349,7 +1349,7 @@ print(len(stack) == 0) Stack push -

    Properties:

    +

    Properties

  • @@ -2235,7 +2235,7 @@ public class Bag<Item> implements Iterable<Item> { -

    Properties:

    +

    Properties

  • @@ -2341,7 +2341,7 @@ def selection_sort(arr): -

    Definitions:

    +

    Definitions

  • @@ -2365,7 +2365,7 @@ arrays, insertion sort runs in linear time.

    equals the number of inversions (number of compares = exchanges + (N – 1)).

    -

    Running Time Analysis:

    +

    Running Time Analysis

  • @@ -2467,7 +2467,7 @@ def insertion_sort(arr): -

    Increment Sequence:

    +

    Increment Sequence

  • @@ -2644,7 +2644,7 @@ array[i], array[r] = array[r], array[i] -### 5.5 Convex Hull +### 5.5 Convex Hull {id="convex-hull"}

    Convex Hull: A convex hull of a set of N points is the smallest perimeter fence @@ -2652,7 +2652,7 @@ enclosing the points.

    Convex Hull -

    Equivalent definitions:

    +

    Equivalent definitions

  • @@ -2667,7 +2667,7 @@ enclosing the points.

  • -

    Geometric properties:

    +

    Geometric properties

  • @@ -2681,7 +2681,7 @@ enclosing the points.

  • -Geometric Properties +Geometric Properties @@ -2693,8 +2693,7 @@ enclosing the points.

    Consider points in order; discard unless it create a ccw - (counterclockwise) turn. -

    + (counterclockwise) turn.

    @@ -2722,7 +2721,7 @@ turn?

    -

    Proof:

    +

    Proof

    \begin{equation} @@ -2730,10 +2729,9 @@ turn?

    \end{equation}
    -Determinant and 
-Positions +Determinant and Positions -

    Applications:

    +

    Applications

  • @@ -2755,7 +2753,20 @@ Positions"/>
  • -

    Point2D:

    +

    Cost of Convex Hull: N \log N + N

    + +

    Proof: Convex hull reduces to sorting

    + + +
  • +

    N \log N cost of sorting

    +
  • +
  • +

    N cost of reduction

    +
  • +
    + +

    Point2D

    @@ -2967,7 +2978,7 @@ class Point2D: -

    Graham Scan:

    +

    Graham Scan

    @@ -3228,7 +3239,7 @@ is in-place if it uses most N \lg N compares and 6N \lg N array accesses to sort any array of size N.

    -

    Proof:

    +

    Proof

    The number of compares C(N) and array accesses A(N) to mergesort an array of size @@ -3525,7 +3536,7 @@ def sort(a): ### 6.3 Computational Complexity -

    Definitions:

    +

    Definitions

  • @@ -3585,7 +3596,7 @@ def sort(a): sorting algorithm must use at least \lg(N!) \sim N \lg N compares in the worst case.

    -

    Proof:

    +

    Proof

  • @@ -3619,7 +3630,7 @@ compares in the worst case.

    ="OrangeRed">stable sort preserves the relative order of items with equal keys.

    -

    Methods:

    +

    Methods

  • @@ -3633,7 +3644,7 @@ items with equal keys.

  • -

    Conclusion:

    +

    Conclusion

  • @@ -3693,7 +3704,7 @@ items with equal keys.

    -

    Runtime Analysis:

    +

    Runtime Analysis

  • @@ -3715,7 +3726,7 @@ compares C_{N} to quicksort an array of N distinct keys is \sim 2N \ln N (and the number of exchanges is \sim \frac{1}{3} N \ln N ).

    -

    Proof:

    +

    Proof

    C_{N} satisfies the recurrence C_0 = C_1 = 0 and for N \leq 2:

    @@ -3804,7 +3815,7 @@ for partitioning probability.

  • -

    Random shuffle:

    +

    Random shuffle

  • Probabilistic guarantee against worst case.

    @@ -3816,7 +3827,7 @@ for partitioning probability.

  • -

    Caveat emptor:

    +

    Caveat emptor

    Many textbook implementations go quadratic if array:

    @@ -3999,7 +4010,7 @@ def quicksort(arr, comparator=lambda x, y: x < y):

    Property: Quick-select takes linear time on average.

    -

    Proof:

    +

    Proof

  • @@ -4260,7 +4271,7 @@ def sort_all(arr: list): -

    Conclusion:

    +

    Conclusion

    @@ -4336,7 +4347,7 @@ def sort_all(arr: list): ### 8.1 API and Elementary Implementations -

    Applications:

    +

    Applications

  • @@ -5404,7 +5415,7 @@ class IndexedPriorityQueue: -

    Properties:

    +

    Properties

  • @@ -7801,7 +7812,7 @@ following three operations in a certain order:

  • -

    Three types of traversal:

    +

    Three types of traversal

  • diff --git a/Writerside/topics/Data-Structures-and-Algorithms-2.md b/Writerside/topics/Data-Structures-and-Algorithms-2.md index 37868b5..47c6454 100644 --- a/Writerside/topics/Data-Structures-and-Algorithms-2.md +++ b/Writerside/topics/Data-Structures-and-Algorithms-2.md @@ -96,10 +96,10 @@

    Allow 1 or 2 keys per node.

  • -

    2-node: one key, two children.

    +

    2-node: one key, two children.

  • -

    3-node: two keys, three children.

    +

    3-node: two keys, three children.

  • @@ -149,7 +149,7 @@

    Maintain symmetric order and perfect balance: Every path from root to null link has same length.

    -

    Proof:

    +

    Proof

  • Worst case: @@ -2134,7 +2134,7 @@ rectangles.

    ### 12.1 Hash Tables -

    Definitions:

    +

    Definitions

  • @@ -2206,7 +2206,7 @@ public final class StringTest { Separate Chaining -

    Properties:

    +

    Properties

  • @@ -3355,7 +3355,7 @@ of length n ? => n

    ### 14.1 Introduction to Graphs -

    Terminology:

    +

    Terminology

  • @@ -3381,7 +3381,7 @@ there is a path between them.

    ### 14.2 Graph API -

    Representation Types:

    +

    Representation Types

  • @@ -3556,7 +3556,7 @@ class UndirectedGraph:

    Goal: Systematically search through a graph.

    -

    Typical applications:

    +

    Typical applications

  • @@ -3577,7 +3577,7 @@ through a graph.

    -

    Properties:

    +

    Properties

  • @@ -3800,7 +3800,7 @@ class DepthFirstSearch: -

    Property:

    +

    Property

    BFS computes shortest paths (fewest number of edges) from s to all other vertices in a graph in time proportional to E + V @@ -4627,7 +4627,7 @@ same method as for undirected graphs!

  • -

    Application:

    +

    Application

  • @@ -5427,7 +5427,7 @@ color="OrangeRed">spanning tree is a subgraph T ### 16.2 Greedy Algorithm -

    Definitions:

    +

    Definitions

  • @@ -5460,7 +5460,7 @@ color="OrangeRed">spanning tree is a subgraph T
  • Given any cut, the crossing edge of min weight is in MST.

    -

    Proof:

    +

    Proof

    Suppose min-weight crossing edge e is not in the MST.

    @@ -5488,7 +5488,7 @@ color="OrangeRed">spanning tree is a subgraph T
  • The greedy algorithm computes the MST.

    -

    Proof:

    +

    Proof

  • Any edge colored black is in the MST (via cut property).

    @@ -5818,7 +5818,7 @@ class EdgeWeightedGraph: -

    Correctness Proof:

    +

    Correctness Proof

    Kruskal's Algorithm is a special case of the greedy MST algorithm.

    @@ -6076,7 +6076,7 @@ class UnionFind: -

    Correctness Proof:

    +

    Correctness Proof

    Prim's Algorithm is a special case of the greedy MST algorithm.

    @@ -6308,7 +6308,7 @@ class PrimMSTLazy: #### 16.5.2 Eager Implementation -

    Property:

    +

    Property

    Running time depends on PQ implementation: V insert, V delete-min, E decrease-key.

    @@ -6351,7 +6351,7 @@ class PrimMSTLazy:

    *: amortized

    -

    Bottom Line:

    +

    Bottom Line

  • @@ -6566,7 +6566,7 @@ and do it in \sim cN \log N

    #### 16.6.2 Single Link Clustering -

    Definitions:

    +

    Definitions

  • @@ -6610,7 +6610,7 @@ components).

    Run Prim's algorithm and delete k–1 max weight edges.

    -

    Applications:

    +

    Applications

  • diff --git a/Writerside/topics/Data-Structures-and-Algorithms-3.md b/Writerside/topics/Data-Structures-and-Algorithms-3.md index 2a9fa67..cd0a1af 100644 --- a/Writerside/topics/Data-Structures-and-Algorithms-3.md +++ b/Writerside/topics/Data-Structures-and-Algorithms-3.md @@ -5,7 +5,7 @@ -## 17 Shortest Paths +## 17 Shortest Paths {id="shortest-paths"} ### 17.1 Shortest Paths APIs @@ -325,29 +325,44 @@ class EdgeWeightedDigraph: -### 17.2 Shortest Path Properties - - - -

    distTo[v] is length of shortest known path from s to - v.

    -
    - -

    distTo[w] is length of shortest known path from s to - w.

    -
    - -

    edgeTo[w] is last edge on shortest known path from s to - w.

    -
    - -

    If e = v->w gives shorter path to w - through v, update both distTo[w] - and edgeTo[w].

    -
    +### 17.2 Shortest Path Properties {id="shortest-path-properties"} + +

    Cost of undirected shortest paths: +E \log V + E

    + +

    Proof: Undirected shortest paths +(with nonnegative weights) reduces to directed shortest path.

    + + +
  • +

    E \log V cost of shortest paths in digraph

    +
  • +
  • +

    E cost of reduction

    +
  • +
    + + + +

    distTo[v] is length of shortest known path from s to + v.

    +
    + +

    distTo[w] is length of shortest known path from s to + w.

    +
    + +

    edgeTo[w] is last edge on shortest known path from s to + w.

    +
    + +

    If e = v->w gives shorter path to w + through v, update both distTo[w] + and edgeTo[w].

    +
    Edge Relaxation @@ -359,21 +374,21 @@ Shortest-paths optimality conditions

    Then distTo[] are the shortest path distances from s iff:

    - +
  • -

    distTo[s] = 0.

    +

    distTo[s] = 0.

  • -

    For each vertex v, distTo[v] is the length of some path from -s to v.

    +

    For each vertex v, distTo[v] is the length of some path from + s to v.

  • -

    For each edge e = v→w, -distTo[w] ≤ distTo[v] + e.weight().

    +

    For each edge e = v -> w, distTo[w] ≤ distTo[v] + + e.weight().

  • -

    Proof:

    +

    Proof

  • @@ -694,7 +709,7 @@ class Dijkstra: -

    Property:

    +

    Property

    Running time depends on PQ implementation: V insert, V delete-min, E decrease-key.

    @@ -739,7 +754,7 @@ class Dijkstra:

    *: amortized

    -

    Bottom Line:

    +

    Bottom Line

  • @@ -1511,7 +1526,7 @@ class BellmanFordSP: -

    Find A Negative Cycle:

    +

    Find A Negative Cycle

    If there is a negative cycle, Bellman-Ford gets stuck in loop, updating distTo[] and edgeTo[] entries of vertices in the cycle.

    @@ -1557,7 +1572,7 @@ cycle (and can trace back edgeTo[v] entries to find it).

    ### 18.1 Introduction -

    Definitions:

    +

    Definitions

    st-cut: A st-cut (cut) is a @@ -1578,7 +1593,7 @@ capacities of the edges from A to B.

    Minimum cut problem: Find a cut of minimum capacity.

    -

    Definitions:

    +

    Definitions

    st-flow: An st-flow (flow) is @@ -1630,7 +1645,7 @@ no edge points to s or from t.

    ### 18.3 Maxflow-Mincut Theorem -

    Definition:

    +

    Definition

    Net Flow: The net flow across a cut (A, B @@ -1660,7 +1675,7 @@ equals the value of f.

    be any flow and let (A, B) be any cut. Then, the value of the flow ≤ the capacity of the cut.

    -

    Proof:

    +

    Proof

    Value of flow f = net flow across cut (A, B) ≤ capacity of cut (A, B).

    @@ -1687,7 +1702,7 @@ conditions are equivalent for any flow f.

  • -

    1 -> 2:

    +

    1 -> 2

  • @@ -1763,12 +1778,12 @@ augmenting path with respect to f.

    U.

    -

    Properties:

    +

    Properties

  • The flow is integer-valued throughout Ford-Fulkerson.

    -

    Proof:

    +

    Proof

  • Bottleneck capacity is an integer.

    @@ -1830,7 +1845,7 @@ integer capacities between 1 and U

    #### 18.5.1 Flow Edge -

    Implementation:

    +

    Implementation

    Use residual capcity:

    @@ -2832,7 +2847,7 @@ class FordFulkerson: ### 18.6 Maxflow Applications -

    Applications:

    +

    Applications

  • @@ -2907,7 +2922,7 @@ perfect matching.

    Bipartite Matching

    When no perfect matching, mincut -explains why:

    +explains why

    Consider mincut (A, B):

    @@ -2950,7 +2965,7 @@ Push-relabel method with gap relabeling: E^{\frac {3}{2}}

    String: Sequence of characters.

    -

    The char data type:

    +

    The char data type

  • C char data type: Typically an @@ -3070,7 +3085,7 @@ index.

    Key-Indexed Counting -

    Properties:

    +

    Properties

  • Key-indexed counting uses \sim 11 N + 4 R array @@ -3206,7 +3221,7 @@ counting).

    Correctness Proof: LSD sorts fixed-length strings in ascending order.

    -

    Proof:

    +

    Proof

    After pass i, strings are sorted by last i characters.

    @@ -3355,7 +3370,7 @@ C strings =>Have extra char '\0' at end => no extra work needed.
  • -

    Improvements:

    +

    Improvements

    Cutoff to insertion sort for small subarrays.

    @@ -3365,7 +3380,7 @@ C strings =>Have extra char '\0' at end => no extra work needed. d^{th} character.
    -

    Performance:

    +

    Performance

    Number of characters examined.

    @@ -3390,7 +3405,7 @@ Strings

  • -

    Disadvantage of quicksort:

    +

    Disadvantage of quicksort

  • Linearithmic number of string compares (not linear).
  • Has to rescan many characters in keys with long prefix matches @@ -3597,7 +3612,7 @@ def sort(arr, low, high, d, aux, CUTOFF):

    Do 3-way partitioning on the d^{th} character.

    -

    Properties:

    +

    Properties

  • Less overhead than R-way partitioning in MSD string sort.
  • @@ -4133,7 +4148,7 @@ class RWayTrie: ### 20.2 Ternary Search Tries {id="tst"} -

    Ternary Search Trees:

    +

    Ternary Search Trees

  • @@ -4637,7 +4652,7 @@ class TernarySearchTree: ### 21.2 Brute-Force Substring Search {id="brute-force"} -

    Disadvantages:

    +

    Disadvantages

  • @@ -4944,7 +4959,7 @@ class KMP:

    nfa[i] or next[i] array represent the checkpoint for the longest prefix probable of pat[0...i] that is also the suffix of txt[0...i]

    -

    Example:

    +

    Example

    pattern: ABABAC next[5] = 3

    When text is ABABA_ and the char on _ is not C, check if the char on _ is equal to pattern[3] = B.

    @@ -5605,7 +5620,7 @@ class RabinKarp:

    Pattern Searching: Find one of a specified set of strings in text.

    -

    Applications:

    +

    Applications

  • @@ -5709,7 +5724,7 @@ notation to specify a set of strings.

  • -

    Shortcuts:

    +

    Shortcuts

    @@ -5752,7 +5767,7 @@ notation to specify a set of strings.

    -

    Examples:

    +

    Examples

    @@ -5793,7 +5808,7 @@ notation to specify a set of strings.

    -

    Caveat:

    +

    Caveat

  • @@ -5812,7 +5827,7 @@ notation to specify a set of strings.

    ### 22.2 REs and NFAs -

    Kleene's theorem:

    +

    Kleene's theorem

  • @@ -5862,7 +5877,7 @@ notation to specify a set of strings.

    -

    Construction:

    +

    Construction

  • @@ -5882,7 +5897,7 @@ notation to specify a set of strings.

    -

    Left parenthesis:

    +

    Left parenthesis

  • Add ε-transition to next state.

    @@ -5893,7 +5908,7 @@ notation to specify a set of strings.

    -

    Alphabet symbol:

    +

    Alphabet symbol

  • Add match transition to next state.

    @@ -5905,7 +5920,7 @@ notation to specify a set of strings.

    -

    Or symbol:

    +

    Or symbol

  • Push index of state corresponding to | onto stack.

    @@ -5913,7 +5928,7 @@ notation to specify a set of strings.

    -

    Right parenthesis:

    +

    Right parenthesis

  • Add ε-transition to next state.

    @@ -6461,7 +6476,35 @@ reduces to sorting

    Cost of solving this problem: N \log N + N

    -### 24.2 +### 24.2 Designing Algorithms + +

    Examples

    + + +
  • +

    3-collinear reduces to sorting.

    +
  • +
  • +

    Finding the median reduces to sorting.

    +
  • +
  • +

    Element distinctness reduces to sorting.

    +
  • +
  • +

    CPM reduces to topological sort.

    +
  • +
  • +

    Arbitrage reduces to shortest paths.

    +
  • +
  • +

    Burrows-Wheeler transform reduces to suffix sort.

    +
  • +
    + +

    For more examples on algorithm designing using reductions, please visit +convex hull or shortest path.

    ## 30 Catalan Number