Open hon9g opened 5 years ago
- Efficient procedure for solving large scale problems.
- Scalability
- Classic data structure & Classical algorithms
- Real implementations in Python
b
is a peak if and only if b>=a and b>=c
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:
a
is a 2D-peak if a>=b and a>=d and a>=c and a>=e
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 |
Gradient Desent Algorithm
- you should determind where to start.
- move to higer value nearby
- such as `12 -> 13 -> 14 -> 15 -> 16 -> 17 -> 19 -> 20 (the peak! end)`
- It takes `Θ(nm)` complexity, it eqeul to `Θ(n^2)` if m = n.
Bineary search version
- Pick middle column j=m/2.
- Find a 1D-peak at (i, j)
- Use (i, j) as a start to find a 1D-peak on row i
It looks like an efficient algorithm but doesn't work. a less efficient and correct algorithm is better than the incorrect one
( i, j )>=( i, j-1 ) and ( i, j )>=( i-1, j )
. Then ( i, j ) is a peak. Done
program | algorithm |
---|---|
programming language | pseudocode / structured english |
computer | model of computation |
The RAM model contains instructions commonly found in real computers:
word: w bits
list()
= array:
L[i] = L[j] + 5
-> O(1) time
L.append(x)
-> table doubling (Lq) O(1) time
L = L1 + L2
-> O(L1+L2) time
L.sort()
-> O(L log(L)) time
x = x.next
O(1) time
dict()
:
D[key] = val
-> O(1) time
with high probablitykey in D
-> O(1) time
long
O(|x|+|y|) time
O(|x|+|y|)^log_3) time
heapq
more about
algorithm:
1. split doc into words
2. compute word frequencies
3. dot product
in python:
for word in doc: # O(|doc|)
count[word] += 1
re
is usally takes exponential time, so don't use it!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)
.
Concept: insert the value at the right position.
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.
A[0:key-1]
, where already sorted, in Θ(log n)
time.Θ(n log n)
compares.Θ(n)
.Θ(n^2)
swaps.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
Concept:
Time Complexity: O(n*log(n))
Implementation;
with recurrence.
Concept: Convert A[1:n]
into a max-heap
Time Complexity: O(n*log(n))
Implementation;
build_max_heap(A)
for i=n/2 down to 1
do `max_heapify(A, i)`
Graph G = (V,E)
V = list of vertices.
E = list of edges.
e = {v, w}
e = (v, w)
Applications:
example;
vertices = 8! * 3^8 = 264,539,520
diameter:
implementation;
u in V
, Adj[u] stores us neighbors.
Adj[b] = {a, c}
Adj[a] = {c}
Adj[c] = {b}
Space Complexity: Θ(V+E)
in more object-oriented fashion (you cound't do this with multiple graph)
V.neigbors = Adj[v]
; explore a graph.
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)
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)
dictionary: Abstract Data Type (ADT), maintain set of item, each with a key.
insert(item)
: add item to setdelete(item)
: remove item from setsearch(key)
: item with key if it existsO(log n)
via AVL tree. -> our goal is O(1)
Python dict
D[key]
: searchD[key] = val
: insertdel D[key]
: deletex = y
<=> hash(x) = hash(y)
u
of all keys (say, integers) down to reasonable size m
for table.k_i, k_j ∈ K
collide if h(k_i) =h(k_j)
today's solution.
An assumption (cheating): Each key is equally likely to be hashed to any slot of table, independent of where other keys are hashed.
S
of elements, each of elements associated with a key.Insert(S, x)
: insert element x
in to set S
max(S)
: return element of S
with the largest keyextract_max(S)
: return element of S
with the largest key and remove it from S
Increase_key(S, x, k)
: increase the value of x
's key to new value k
16 | 14 | 10 | 8 | 7 | 9 | 3 | 2 | 4 | 1 |
---|
i=1
i = child_idx/2
i = 2*parent_idx
i = 2parent_idx +1
invariance is property of a max heap
build_max_heap
: produce a max heap from an
max_heapify(A, i)
: correct a single violation of the heap property in a subtree's root
left(i)
and right(i)
are max heaps.Time Complexity of max_heapify
: O(log n)
log(N)
max_heapify
is that there are single violation and trees rooted at left(i)
and right(i)
are max heapsO(N)
Concept: Convert A[1:n]
into a max-heap
build_max_heap(A)
for i=n/2 down to 1
do `max_heapify(A, i)`
Implementation;
build_max_heap
from unordered arrayn
from heap. decrementing heap size.max_heapify
(go back to number 2)Time Complexity: O(n*log(n))
max_heapify
taken O(1)
for nodes that are one level above leavesO(l)
time for nodes that are l
length above the leaves1
, n/8 with level 2
, ... , 1 node at log n
level
TO-DO
Unit 1: Introduction
Unit 2: Sorting and Trees
; Event Simulation
Unit 3: Hashing
; Gene Comparision
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 - [ ] MatricesResource