Algorithm - HsuJv/Note GitHub Wiki

Introduction to Algorithms

  • This is the lecture note to the SMA 5503 (Analysis and Design of Algorithms)
  • Course Description
    • This course teaches techniques for the design and analysis of efficient algorithms, emphasizing methods useful in practice. Topics covered include: sorting; search trees, heaps, and hashing; divide-and-conquer; dynamic programming; amortized analysis; graph algorithms; shortest paths; network flow; computational geometry; number-theoretic algorithms; polynomial and matrix calculations; caching; and parallel computing.
  • Textbooks: Introduction to Algorithms 3rd Edition
    • THOMAS H. CORMEN
    • CHARLES E. LEISERSON
    • RONALD L. RICEST
    • CLIFFORD STEIN

Introduction

Analysis of Algorithms

  • The analysis of algorithm is the theoretical study of computer program performance and resource usage.
  • What's more important than performance?
    • Correctness, Security, User-friendliness, etc.
    • Performance is like money which is the bedrock that to implement all other counts.

Insertion Sort

  • Problem Description

    • Input: Sequence $[a_1,\ a_2,\ \ldots,\ a_n]$ of numbers
    • Output: Permutation $[a^{'}_1,\ a^{'}_2,\ \ldots,\ a^{'}_n]$ such that $a^{'}_1 \leq a^{'}_2 \leq \ldots \leq a^{'}_n$
  • Pseudocode:

// INSERTION-SORT(A)
for j := 2 to n do
    key := a[j]
    i := j - 1
    while i > 0 and A[i] > key do
        A[i+1] := A[i]
        i := i - 1
    A[i+1] := key
  • Running Time
    • Depends on the input itself (e.g. already sorted, reverse sorted)
    • Depends on the input size (6 elem. vs 6 * 10^9 elem.)
      • Parameterize the input size
    • Want the upper bounds
      • guarantee for the user
  • Kinds of analysis
    • Worst case (usually)
      • T(n) = max time on any input of size n
    • Average case (sometimes)
      • T(n) = expected time over all inputs of size n
      • Need assumption of the statistical distribution of inputs
      • Usually uniform distribution
    • Best case (bogus)
      • Can cheat with slow algorithm
  • Asymptotic Analysis
    • Ignore machine-dependent constants
    • Look at the growth of the running time
    • Notation:
      • Θ-notation: drop low order terms and ignore leading constants
      • As $n \rightarrow \infty$, $Θ (n^2)$ algorithm always beats $Θ (n^3)$ alg.
  • Insertion Sort Analysis
    • Worst case: reverse sorted:
      • $T(n) = \sum^{n}_{j=2}Θ(j)=Θ(n^2)$ (Arithmetic series)
    • For small n, it is moderately fast
    • Not at all for large n

Merge Sort

  • Merge sort $[a_1, \ a_2, \ \ldots, \ a_n]$
    1. If n = 1, done
      • Θ(1)
    2. Recursively sort $[a_1,\ \ldots,\ a_{[\frac{n}{2}]}]$ $[ a_{[\frac{n}{2}]+1},\ \ldots,\ a_n]$
      • $2 T(\frac{n}{2})$
    3. Merge 2 sorted lists
      • Θ(n)
  • Key subroutine: Merge
    • Time: Θ(n) on n total elems (linear time)
  • Recurrence
    • $T(n)=\begin{cases}Θ(1)&,n=1\\ 2T(\frac{n}{2})+Θ(n)&,n \geq 2\end{cases}$

Correctness of Algorithms

Horner's rule

Asymptotic Notation

  • Big O notation O : $ f(n) = O(g(n)) $
    • Defination: there are consts $ c > 0 $ and $ n_0 > 0 $, such then $ 0 \leq f(n) \leq c \cdot g(n) $ for all $ n \geq n_0 $
    • More intuitive notion: assume that f(n) is non-negative, and is to be bounded above by g(n)
    • Example: $ 2n^2 = O(n^3) $
    • Note: it is asymmetric

Recurrences

Substitution, Master Method

Divide-and-Conquer: Strassen, Fibonacci, Polynomial Multiplication

Recurrences, Sloppiness

Quicksort, Randomized Algorithms

Heapsort, Dynamic Sets, Priority Queues

Linear-time Sorting: Lower Bounds, Counting Sort, Radix Sort

Order Statistics, Median

Applications of Median

Bucketsort

Hashing, Hash Functions

Universal Hashing, Perfect Hashing

Quiz 1 Review

Quiz 1, In-class

Binary Search Trees, Tree Walks

Relation of BSTs to Quicksort

Analysis of Random BST

Red-black Trees, Rotations, Insertions, Deletions

2-3 Trees, B-trees

Augmenting Data Structures, Dynamic Order Statistics, Interval Trees

Skip Lists

Range Trees

Amortized Algorithms, Table Doubling, Potential Method

Competitive Analysis: Self-organizing Lists

Competitive Analysis: Ski Rental, Randomized Competitive Algorithm

Dynamic Programming, Longest Common Subsequence

Greedy Algorithms, Minimum Spanning Trees

Shortest Paths I: Properties, Dijkstra's Algorithm, Breadth-first Search

Shortest Paths II: Bellman-Ford, Linear Programming, Difference Constraints

Graph Searching: Depth-first Search, Topological Sort, DAG Shortest Paths

Shortest Paths III: All-pairs Shortest Paths, Matrix Multiplication, Floyd-Warshall, Johnson

Quiz 2 Review

Ethics, Problem Solving (Mandatory Attendance)

Quiz 2, In-class

Advanced Topics

Advanced Topics (cont. )

Advanced Topics

Advanced Topics (cont. )

Advanced Topics (cont. )

Discussion of Follow-on Classes

Final Exam

⚠️ **GitHub.com Fallback** ⚠️