hon9g / algorithms

TIL: to keep practice on algorithms
6 stars 1 forks source link

[MIT6.006] Introduction to Algorithms #4

Open hon9g opened 5 years ago

hon9g commented 5 years ago

TO-DO

Unit 1: Introduction

Unit 2: Sorting and Trees

; Event Simulation

Unit 4: Numerics

; RSA encrypton

Unit 5: Graphs

; Rubik's cube

Unit 6: Shortest Paths

; Caltech to MIT

Unit 7: Dynamic Programming

; Image comperision

Unit 8: Advanced Topics

Read Textbook

INDEX 1. **Foundation** - [x] The Role of Algorithms in Computing - [ ] Getting Started - [x] Growth of Functions - [ ] Divide and Conquer - [ ] Probabilistic Analysis and Randomized Algorithms 2. **Sorting and Order Statistics** - [ ] HeapSort - [ ] QuickSort - [ ] Sorting in Linear Time - [ ] Medians and Order Statistics 3. **Data Structures** - [ ] Elementary Data Structures - [ ] Hash Tables - [ ] Binary Search Trees - [ ] Red-Black Trees - [ ] Augmenting Data Structures 4. **Advanced Design and Analysis Techniques** - [ ] Dynamic Programming - [ ] Greedy Algorithms - [ ] Amortized Analysis 5. **Advanced Data Structure** - [ ] B-Trees - [ ] Fibonacci Heaps - [ ] van Emde Boas Trees - [ ] Data Structures for Disjoint Sets 6. **Graph Algorithms** - [ ] Elementary Graph Algorithms - [ ] Minimum Spanning Trees - [ ] Single-Source Shortest Paths - [ ] All-Pairs Shortest Paths - [ ] Maximum Flow 7. **Selected Topics** - [ ] Multithreaded Algorithms - [ ] Matrix Operations - [ ] Linear Programming - [ ] Polynomials and the FFT - [ ] Number-Theoretic Algorithms - [ ] String Mathing - [ ] Computational Geometry - [ ] NP-Completeness - [ ] Approximation Algorithms 8. **Mathmatical Background** - [ ] Summations - [ ] Sets, Etc. - [ ] Counting and Probability - [ ] Matrices

Resource

hon9g commented 5 years ago

Course Overview

- Efficient procedure for solving large scale problems.
- Scalability
- Classic data structure & Classical algorithms
- Real implementations in Python

Algorithmic Thinking

Peak Finding Problem

one dimensional version;

1 2 n/2 n-1 n
a b c d e f g h i

a straight forward algorithm is

If the element is on the middel, it should look at n/2 elements. anyway, the worst-case complexity is Θ(n). asymptotic analysis: Θ(n).

if you use binary search

asymptotic analysis:

two dimensional version;

1 2 ... m-1 m
1 c
2 b a d
... e
n-1
n
.. .. .. ..
14 13 12 ..
15 9 11 17
16 17 19 20

It looks like an efficient algorithm but doesn't work. a less efficient and correct algorithm is better than the incorrect one

Models of Computation

Program and Algorithm

program algorithm
programming language pseudocode / structured english
computer model of computation

Random Access Machine (RAM)

The RAM model contains instructions commonly found in real computers:

image word: w bits

Pointer Machine

Python model

more about go

Document Distance Problem

image

algorithm:

1. split doc into words
2. compute word frequencies
3. dot product

in python:

for word in doc: # O(|doc|)
  count[word] += 1
hon9g commented 5 years ago

Sorting

If you want to find an item from the unsorted array, it would take O(n). If you want to find an item from the sorted array, it would take O(log n).

Insertion Sort

Concept: insert the value at the right position. image

5 2 4 6 1 3
2 5 4 6 1 3
2 4 5 6 1 3
2 4 5 6 1 3
1 2 4 5 6 3
1 2 3 4 5 6

Space Complexity: inplace Time Complexity: O(n^2)

Θ(n) steps (key positions).
Each step is Θ(n) swaps & comparison.

Implementation;

for j = 2 to A.length
  key = A[j] 
  # insert A[j] into sorted sequence A[1:j-1]/
  i = j - 1
  while i > 0 and A[i] > key
    A[i+1] = A[i]
    i += -1
  A[i+1] = key

Merge Sort

Concept:

Time Complexity: O(n*log(n))

Implementation;

with recurrence.

Heap Sort

Concept: Convert A[1:n] into a max-heap

Time Complexity: O(n*log(n))

Implementation;

hon9g commented 5 years ago

Graph

Graph G = (V,E)

Applications:

example;

diameter:

Graph Representation

Adjacency list

implementation;

in more object-oriented fashion (you cound't do this with multiple graph)

V.neigbors = Adj[v]

Graph Search

; explore a graph.

Breadth-First Search

Implementation:

BFS(V, Adj, s):
    level = { s: 0 }
    parent = { s: None }
    i = 1
    frontier = [s]
    while frontier:
        next = []
        for u in fronteir:
            for v in Adj[u]:
                if v not in level:
                    level[v] = i
                    parent[v] = u
                    next.append(v)
            frontier = next
            i += 1

Analysis: O(E)

Shortest Path

Depth-First Search

Implementation: It could be simply implemented with recursion.

parent = {s: None}
DFS_Visit(V, Adj, s):
    for v in Adj[s]:
        if v not in parent:
            parent[v]=s
            DFS-Visit(V, Adj, v)

we dont really start. so try all the possible vertex.

parent = {}
for s in V:
    if s not in parent:
        parent[s]= None
        DFS_Visit(V, Adj, s)

Analysis: Θ(V+E) (linear time)

- visit each vertex once
- DFS_Visit(.., .., v) called once per vertex v
- pay |Adj[v]| => O(sum of all |Adj[v]|) == O(E)

Edge classification

Cycle Detection

Topological Sort

hon9g commented 5 years ago

Single-Source Shortest Paths Problem

hon9g commented 5 years ago

Dictionaries & Python

Motivation

How do we solve the dictionary problem?

simple approach: Direct-access table

solution: PREHASH keys to integers

solution: HASH

How do we deal with collisions?

solution: CHAINING

today's solution.

Simple Uniform Hashing

An assumption (cheating): Each key is equally likely to be hashed to any slot of table, independent of where other keys are hashed.

"Good" Hash functions

hon9g commented 5 years ago

Heap

Priority Queue (ADT)

16 14 10 8 7 9 3 2 4 1

Heap Operations

Implementation;

  1. build_max_heap from unordered array
  2. Find max element A[i]
  3. Swap elements A[n] with A[i], now max element is at the end of array.
  4. Discard node n from heap. decrementing heap size.
  5. Now root may not maxheap property, but children are max heaps. so, we can correct it with one max_heapify (go back to number 2)

Time Complexity: O(n*log(n))

hon9g commented 5 years ago

Binary Search Trees