Data Structure Lecture Note (Week 2, Lecture 6)

Homework 1 is very important to understand. In the final exam, many questions are just of such forms.

Amortized analysis practices: Binary Counter

Let

A be anarray of size K, with 0, 1 in eah slot, A is a counter and the count is the binary stored in A. (Assume right most is the lowest end).

e.g. A = [0,0,0,0,01] then the counter = 1. If A=[0,1,0,1,0] then the counter is 2^3 + 2^1 = 10

The counter = ∑ 0 ≤ i ≤ K − 1 2 i A [ i ] \sum_{0\le i\le K-1} 2^i A[i] 0iK12iA[i]

A.tick() is an op taht tick the counter up 1. N % 2^K . If binary exceeds, A will continue ticking the available bits

Naive analysis yield O ( K N ) O(KN) O(KN)

But carefully observation says, A[0] with N operations, tick every time, A[1] with N operations, will tick N/2 times, A[2] with N operations will tick N/4 times …

So, in total,

# f l i p s = ∑ 0 ≤ i ≤ K − 1 N / 2 i = O ( N ) \# flips = \sum_{0\le i \le K-1} N/2^i = O(N) #flips=0iK1N/2i=O(N)

Potential function method

Let D j D_j Dj be the state of the data structure at j’th operation

Let P(D_j) be a function (势能函数) from the state of the data structure to some real value

Let x j x_j xj be the actual cost of j’th operation

Let d j = c j + P ( D j ) − P ( D j − 1 ) d_j = c_j + P(D_j)-P(D_{j-1}) dj=cj+P(Dj)P(Dj1) then
∑ j d j = ∑ j ( c j + P ( D j ) − P ( D j − 1 ) ) = ( ∑ j c j ) + P ( D N ) − P ( D 0 ) \sum_j d_j = \sum_j (c_j + P(D_j) - P(D_{j-1})) = (\sum_j c_j) + P(D_N) - P(D_0) jdj=j(cj+P(Dj)P(Dj1))=(jcj)+P(DN)P(D0)
The idea is d i d_i di is easier to calculate than c j c_j cj, and P ( D N ) , P ( D 0 ) P(D_N), P(D_0) P(DN),P(D0) are easy to find.

Dynamic Programming

Closest pair of points in 2D

problem: given N points with coordinate (x_i, y_i) Find the closet 2 points. (naive algorithm is O(n^2))

How about a divide and conquer approach?

There is a way to O(N(logN)^2). Which could be improved to O(N log N)

Finding the k-th largest number in A

Naive algorithm is O(N logN)

There is a smart algorithm gives O ( N ) O(N) O(N) page 220 MIT book

suppose we are going to find median for N - 25 numbers

First break them into groups of 5 numbers, in general, N/5 groups. Suppose each is sorted

It’s easy to figure out the median for each group

Pick the median out. get N/5 numbers. Then find the median of N/5 numbers. Take it as a pivot, divide the entire array by the pivot (just like in quick sort). Say left A, right B

Claim: ∣ A ∣ o r ∣ B ∣ < 0.7 N |A| or |B| < 0.7N AorB<0.7N

Suppose F(N) is running time of finding the k-th number in an array of size N

we have

F(N) = F(N/5) + O(N) + F(7/10 N + 6)

Types of algorithms

  • Try to finish a task
    • e.g. sorting, simulating a stack, evaluate the repression, find the median ,etc.
  • Yes/no problem, decision problem
    • Whether there is a solution to sth.
  • Optimization Problems
    • Solved by optimization algorithms

Example: knapsack problem: I have a bag of X volume, and N items, each item has a weight and value, given by array W and V (all numbers are int)

(Input size: N, log X.----> O(N) + log X)

Optimization problem, try to optimiza a value/goal with constaints

yes/no decision version: whether there is a way to pack the backpack to at least value Y


Claim: if you can solve the yes/no problem with a polynomial time algorithm A, you can solve the optimization problem with polynomial time.

pf: do a binary search, it takes log(sum(value)) * T(A)

Methodology to deal with optimization problems

Greedy algo. : easy to implement, fast running; usually not optimal solutions [example: sort the items by value/weight, from large to small. pack from the bset item until it can’t fit in (O(Nlog N))]

Searching algo. : pro: can find the optimal solution; con: usually slow. [example: ]


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值