aaex6 group2(中英夾雜)

38
Advanced Algorithms Exercise 6 Group 2 陳右緯、楊翔雲、蔡宗衛、BINAYA KAR、林淑慧 2014/1/3 1 Q1 Q8

Upload: shiang-yun-yang

Post on 12-Jul-2015

274 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Aaex6 group2(中英夾雜)

Advanced Algorithms

Exercise 6

Group 2

陳右緯、楊翔雲、蔡宗衛、BINAYA KAR、林淑慧

2014/1/3 1Q1 Q8

Page 2: Aaex6 group2(中英夾雜)

Q1

• Show that LCD≦LIS and LCS≦LIS. Does this result imply that there exist O(n log n) algorithms for LCD and LCS? (Here n represents the input length.)

2014/1/3 2Q1 Q8

Page 3: Aaex6 group2(中英夾雜)

Q1

• // Longest Chain of Dominations(LCD)

• Yes

• Because of right-side is more difficult than left-side.

• If we can transfer problem to the right-side, then using right-side's algorithm solve it.

• The left-side algorithm upper bound smaller than O(n log n)

2014/1/3 3Q1 Q8

Page 4: Aaex6 group2(中英夾雜)

Q2

• Given a set of points S in the plane, a subset of S is called an anti-chain if the points of the subset can be arranged in an order such that the x coordinates of the points in the order are increasing and their y coordinates are decreasing. Design an algorithm to find an anti-chain with maximal size.

2014/1/3 4Q1 Q8

Page 5: Aaex6 group2(中英夾雜)

Q2

• ley y_new_value = infinite value - y_value for all points. same as LCD problem.

• 將平面座標軸上下翻轉,也就是相當於將

所有原本的 y 座標用一個很大的數字去減。

而這一個轉換操作在 O(n) 時間內完成,之

後可以使用 LCD 去解。

2014/1/3 5Q1 Q8

Page 6: Aaex6 group2(中英夾雜)

Q2 – efficient algorithm

2014/1/3 6Q1 Q8

ANTI-CHAIN(Point p[], int n) {max_y_value = findMax(p, n) - O(n)for i = 0 to n-1

p[i].y = max_y_value – p[i].y - O(n)return LCD(p, n) - O(n log n)

}

• 由於轉換只需要 O(n) 不會到 n log n,

ANTI-CHAIN ≦ LCD

Page 7: Aaex6 group2(中英夾雜)

Q3

• Show that any comparison-based algorithm needs (n log n) time to solve the following two problems.

• (1) The Euclidean minimum spanning tree problem: given n points in the 2D plane, construct a tree of minimum total length whose vertices are the given points and the edge length of an edge is the Euclidean distancebetween the two end points of the edge.

• (2) LCD.

2014/1/3 71 Q8

Page 8: Aaex6 group2(中英夾雜)

Q3

• 題目看不懂。

• 不過簡單地猜測,當所有點共線時。歐幾里得平

面最小生成樹和 LCD 問題,都需要找到下一個

比它小的最鄰近點,因此相當於在一條數線上找

排序問題。

• 如果排序問題 THETA (n log n),則這幾個問

題的 LOWER_BOUND ≧ THETA(n log n)

2014/1/3 81 Q8

Page 9: Aaex6 group2(中英夾雜)

Q4

• Let x1, x2, … , xn be n distinct points. Give an efficient algorithm to calculate the coefficients of a degree n polynomial p(x) such that p(xi) = 0 for every 1 <= i <= n.

• 找一個 n 次多項式通過所有點 x 軸上的座標。

2014/1/3 91 Q8

Page 10: Aaex6 group2(中英夾雜)

Q4

• Let x1, x2, … , xn be n distinct points. Give an efficient algorithm to calculate the coefficients of a degree n polynomial p(x) such that p(xi) = 0 for every 1 <= i <= n.

• 找一個 n 次多項式通過所有點 x 軸上的座標。

2014/1/3 101 Q8

Page 11: Aaex6 group2(中英夾雜)

Q4

• FFT merge two polynomial - O(n log n)經由 FFT 轉換,兩個多項式相乘可以在

O(n log n) 內完成。

• 目標計算 p(x) = (x-x1)* ... * (xn)

• Divide and Conquer algorithm

• p(x) = q(x) * r(x)

• q(x) 通過前 n/2 個點。

r(x) 通過後 n/2 個點。

2014/1/3 111 Q8

Page 12: Aaex6 group2(中英夾雜)

Q4

• 合併 q(x)*r(x) in O(n log n)

• 得到遞迴式

T(n) = 2T(n/2) + O(n log n)

• By extended master theorem T(n) = O(n * log n * log n)

2014/1/3 121 Q8

Page 13: Aaex6 group2(中英夾雜)

Q5

• Let p(x) be a polynomial of degree n, and let x1, x2, … , xn be n distinct numbers. Give an efficient algorithm to calculate each yi = p(xi) for every 1 <= i <= n. (Hint: use the fact that the division problem of polynomials can be solved in O(n log n) time and the result of the previous problem.)

• 計算 n 次多項式,代入 n 個值的個別結果

暴力法 O(n2)

2014/1/3 131 Q8

Page 14: Aaex6 group2(中英夾雜)

Q5

• Assumep(x) = h(x) * q1(x) + r1(x) h(x) = (x-x1)*(x-x2)*...(x-x(n/2))

• h(x) - O(n log n log n) by Q4 discuss

• We get q1(x) and r(x) in O(n log n)// problem description give.

• We will calculate p(xi) = r1(xi)n/2+1 <= i <= n

2014/1/3 141 Q8

Page 15: Aaex6 group2(中英夾雜)

Q5

• Same for h(x) = (x-x(n/2+1))*(x-xn/2+2)*...(x-x(n))

• We will calculate p(xi) = r2(xi)1 <= i <= n/2

• r1(x) & r2(x) degree < h(x) degree = n/2

• T(n) = 2T(n/2) + O(n log n log n)

• By extended master theorem.T(n) = O(n log n log n log n)

2014/1/3 151 Q8

Page 16: Aaex6 group2(中英夾雜)

Q6

• Given n pairs of points (xi, yi) with x1, x2, … , xn being distinct. Design an efficient time algorithm to find the coefficients of the unique polynomial of degree less than n that passes through these n points.

2014/1/3 161 Q8

Page 17: Aaex6 group2(中英夾雜)

Q6

• p(x) = a1 + a2*(x-x1) + a3*(x-x1)*(x-x2) + ... + an*(x-x1)*(x-x2)*..*(x-xn-1)

• -----

• not solved.

2014/1/3 171 Q8

Page 18: Aaex6 group2(中英夾雜)

Q7

• Given two images A and B, use image B to cover image A. Where would we put B on A, so that the overlapping part of A and B has the most likelihood? The difference between A and B is defined as the square sum of the differences of corresponding elements in the overlapped parts of A and B.

2014/1/3 181 Q8

Page 19: Aaex6 group2(中英夾雜)

Q7

• http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1637

/**

* Given two sequences {a1, a2, a3.. an} and {b1, b2, b3... bn},

* their repeat convolution means:

* r1 = a1*b1 + a2*b2 + a3*b3 + ... + an*bn

* r2 = a1*bn + a2*b1 + a3*b2 + ... + an*bn-1

* r3 = a1*bn-1 + a2*bn + a3*b1 + ... + an*bn-2

* ...

* rn = a1*b2 + a2*b3 + a3*b4 + ... + an-1*bn + an*b1

* Notice n >= 2 and n must be power of 2.

*/

I don’t understand.

2014/1/3 191 Q8

Page 20: Aaex6 group2(中英夾雜)

Q7

• http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1637

/**

* Given two sequences {a1, a2, a3.. an} and {b1, b2, b3... bn},

* their repeat convolution means:

* r1 = a1*b1 + a2*b2 + a3*b3 + ... + an*bn

* r2 = a1*bn + a2*b1 + a3*b2 + ... + an*bn-1

* r3 = a1*bn-1 + a2*bn + a3*b1 + ... + an*bn-2

* ...

* rn = a1*b2 + a2*b3 + a3*b4 + ... + an-1*bn + an*b1

* Notice n >= 2 and n must be power of 2.

*/

I don’t understand.

2014/1/3 201 Q8

Page 21: Aaex6 group2(中英夾雜)

Q8

• Given a set S of k strings, we want to find every string in S that is a substring of some other string in S. Assume that the total length of all the strings in S is n, give an O(n) time algorithm to solve this problem.

• 給 k 個字串,其長度總和為 n。

詢問有哪些字串是另一個字串的子字串 (substring),

要在線性時間內完成。

2014/1/3 211 Q8

Page 22: Aaex6 group2(中英夾雜)

Q8

• => _____$_____#______ ....

• => build suffix tree - O(n)

• => trival tree - O(n) solve that each node record subtree have two different strings.

• => query each string by trival tree - O(n)

• 首先將所有字串以額外不存在的 n-1 個字元串接,意即

$, # 不會出現在原先的字符集中。

• 接著,使用後綴樹在線性時間內建造 O(n)

• 接著對於這 k 個字串,分別作詢問。在最後停留的點地

方,查看是否其後還有其它字串的後綴編號存在,如果存

在即這個字串在其它字串中出現過。

2014/1/3 221 Q8

Page 23: Aaex6 group2(中英夾雜)

Q8

• 後綴的編號存取方式為

記錄兩個不同的後綴編號,

其做法不影響後綴樹的線性建造。

• Tree[n].suffixIndex.insert(subtree[sb].suffixIndex);

• Tree[n].suffixIndex.getUnique(2);

2014/1/3 231 Q8

Page 24: Aaex6 group2(中英夾雜)

Q8

• 後綴的編號存取方式為

記錄兩個不同的後綴編號,

其做法不影響後綴樹的線性建造。

• Tree[n].suffixIndex.insert(subtree[sb].suffixIndex);

• Tree[n].suffixIndex.getUnique(2);

2014/1/3 241 Q8

Page 25: Aaex6 group2(中英夾雜)

Q9

• Give a linear-time algorithm that takes in a string and finds the longest maximal pair of equal substrings in which the two copies do not overlap. That is, if the two copies begin at positions p1 < p2 and are of length n', then p1+ n' < p2.

• 在線性時間內查找兩個不重疊的最長子字串。

• s=ababa

• 如果不允許重複,最長不重疊的子字串為 ab

• 反之,為 aba

2014/1/3 251 Q8

Page 26: Aaex6 group2(中英夾雜)

Q9

• build suffix tree - O(n)

• trival tree - O(n) solve that each node record subtree hava minimum and maximum suffix index.

• maximum - minimum suffix index >= now_search_string_length.

• 首先,一樣在線性時間內建造後綴樹。

• 接著,對於每個節點記錄其子樹的最大和最小後綴編號。

• 走訪後綴樹 O(n),走的時候記錄離根的距離。

• 找尋符合 maximum - minimum suffix index >= now_search_string_length. 的最大長度即可。

2014/1/3 261 Q8

Page 27: Aaex6 group2(中英夾雜)

Q9

• Give a linear-time algorithm that takes in a string and finds the longest maximal pair of equal substrings in which the two copies do not overlap. That is, if the two copies begin at positions p1 < p2 and are of length n', then p1+ n' < p2.

• 在線性時間內查找兩個不重疊的最長子字串。

• s=ababa

• 如果不允許重複,最長不重疊的子字串為 ab

• 反之,為 aba

2014/1/3 271 Q8

Page 28: Aaex6 group2(中英夾雜)

Q10

• Show how to count the number of distinct substrings of a string T in time O(n), where the length of T is n. Also show how to enumerate one copy of each distinct substring in time proportional to the length of all those strings.

• 給一個字串 T,找到有多少不同的子字串。並且計算這些

不同的子字串的長度和。

• T 具有 n2 個子字串,不同的可能很少。

• ex. T = aaaaaa

• 只有 7 種子字串,含有空字串。

2014/1/3 281 Q8

Page 29: Aaex6 group2(中英夾雜)

Q10

• build suffix tree - O(n)

• calculate sum the distance of all character between root on suffix tree node. - O(n)

• 首先,一樣將字串 T 建造一個後綴樹。

• 接著走訪整棵後綴樹,並且記錄每一個字符到樹根的距離。

一個 node 可能涵蓋一個連續子字串,可以在 O(1) 時

間內用數學計算。

• 因此可以在 O(n) 時間內完成。

• 後綴樹的節點個數 V = O(n)

2014/1/3 291 Q8

Page 30: Aaex6 group2(中英夾雜)

Q11

• Solve the minimum coloring problem for interval graphs (or see Exercise 16.1-4 inchapter 16 of the book [CLRS].)

• 區間圖的最小染色問題

• 區間圖為每一個節點代表一個區間,假如有這兩個區間重

疊一部分,則這兩個節點之間存在一條邊。

2014/1/3 301 Q8

Page 31: Aaex6 group2(中英夾雜)

Q11

• Solve the minimum coloring problem for interval graphs (or see Exercise 16.1-4 inchapter 16 of the book [CLRS].)

• 區間圖的最小染色問題

• 區間圖為每一個節點代表一個區間,假如有這兩個區間重

疊一部分,則這兩個節點之間存在一條邊。

2014/1/3 311 Q8

Page 32: Aaex6 group2(中英夾雜)

Q11

• keyword : 區間圖、著色問題

• Greedy

• 使用 BFS 盡可能填入不衝突的最小顏色編號。

• 對於每次使用 BFS 得到的節點,查找鄰近已經塗的顏色,

少出一個當前使用的顏色集中不與鄰近點衝突的顏色。如

果找不到則新增一個顏色。

2014/1/3 321 Q8

Page 33: Aaex6 group2(中英夾雜)

Q11

• keyword : 區間圖、著色問題

• Greedy

• 使用 BFS 盡可能填入不衝突的最小顏色編號。

• 對於每次使用 BFS 得到的節點,查找鄰近已經塗的顏色,

少出一個當前使用的顏色集中不與鄰近點衝突的顏色。如

果找不到則新增一個顏色。

2014/1/3 331 Q8

Page 34: Aaex6 group2(中英夾雜)

Q12

• Design an efficient algorithm for determining (G) and k(G) on a bipartite graph G.

• 在二分圖中查找最大獨立集 和 最小團覆蓋數。

2014/1/3 341 Q8

Page 35: Aaex6 group2(中英夾雜)

Q12

• 最大獨立集 = 頂點總數 - 最大匹配數

• 最小團覆蓋數 = 二分圖取補圖的最大獨立集 = 頂點總

數 - 最大匹配數

// 二分圖取補為 X - Y 有連變沒連,反之亦然。但 X, Y 集合內部之間仍沒有邊。

• 二分圖最大匹配用匈牙利演算法 O(V3)

2014/1/3 351 Q8

Page 36: Aaex6 group2(中英夾雜)

Q12

• 最大獨立集 = 頂點總數 - 最大匹配數

• 最小團覆蓋數 = 二分圖取補圖的最大獨立集 = 頂點總

數 - 最大匹配數

// 二分圖取補為 X - Y 有連變沒連,反之亦然。但 X, Y 集合內部之間仍沒有邊。

• 二分圖最大匹配用匈牙利演算法 O(V3)

2014/1/3 361 Q8

Page 37: Aaex6 group2(中英夾雜)

Q13

• Given an nn non-negative matrix W = [wi,j], we want to find an assignment for 2nvariables: u1, u2, … , un and v1, v2, … , vnsuch that wi,j <= ui + vj for all i, j, and the sum 1i nui + 1i nvi is minimized.

• 給 n*n 非 0 矩陣,找 2n 個數字總和湊出每個加總會

大於每一個格。找這 2n 個數字總合要最小。

• For i = 1 to nFor j = 1 to n

Satisfy w[i][j] <= u[i] + v[j].2014/1/3 371 Q8

Page 38: Aaex6 group2(中英夾雜)

Q13

• 共有 n2 條不等式,求目標函數最小。

• 線性規劃 Linear Programming.

2014/1/3 381 Q8