aaex3 group2

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

Upload: shiang-yun-yang

Post on 12-Jul-2015

223 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Aaex3 group2

Advanced Algorithms

Exercise 3

Group 2

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

2014/1/3 1Q1 Q8

Page 2: Aaex3 group2

Q1

• Given a 2-dimensional n × n matrix of 0/1

integers, design a linear time algorithm to find

a subrectangle with the largest size such that

all the elements in the sub-rectangle are equal

to 1.

2014/1/3 2Q1 Q8

Page 3: Aaex3 group2

Q1

Keyword

• Largest Empty Rectangle

• Maximum rectangle area

2014/1/3 3Q1 Q8

Page 4: Aaex3 group2

Q1 – efficient algorithm(1)

• 一個最大矩形必然存在壁(邊界上)

• 窮舉一點面對這個壁

• 利用 DP 性質維護對壁距離

以及到壁能拓展的最大寬

• Picture from http://www.csie.ntnu.edu.tw/~u91029/LargestEmptyRectangle.html#3

2014/1/3 4Q1 Q8

Page 5: Aaex3 group2

Q1 – efficient algorithm(1)

• 依序考慮每一行

2014/1/3 5Q1 Q8

正解呢 ?

Page 6: Aaex3 group2

Q1 – efficient algorithm(1)

2014/1/3 6Q1 Q8

• l[i][j] 表示 (x, y) 向左伸展的最大寬度

• r[i][j] 表示 (x, y) 向右伸展的最大寬度

• h[i][j] 表示 (x, y) 向下伸展的最大高度

• wl[i][j] 表示 (x, y) 最大高度下的最左寬度

• wr[i][j] 表示 (x, y) 最大高度下的最右寬度

Page 7: Aaex3 group2

Q1 – efficient algorithm(1)

2014/1/3 7Q1 Q8

• l[i][j] express that the size of (x, y) expand

maximum left columns

• r[i][j] express that the size of (x, y) expand

maximum right columns

• h[i][j] express that the height of (x, y) expand

maximum rows (backword)

• wl[i][j] the maximum left width when it has

max height

• wr[i][j] the maximum right width when it has

max height

Page 8: Aaex3 group2

Q1 – efficient algorithm(1)

2014/1/3 8Q1 Q8

otherwise

empty. is A[i][j] ifjiljil

 

  

,0

,1]1][[]][[

otherwise

empty. is A[i][j] ifjirjir

 

  

,0

,1]1][[]][[

otherwise

empty. is A[i][j] ifjihjih

 

  

,0

,1]][1[]][[

otherwise

empty. is A[i][j] ifjiljiwljiwl

 

   

,0

]),][[,1]][1[min(]][[

otherwise

empty. is A[i][j] ifjirjiwrjiwr

 

   

,0

]),][[,1]][1[min(]][[

otherwise

empty. is A[i][j] ifjiwrjiwljihjiarea

 

  

,0

)1]][[]][[])(][[(]][[

Maximum empty area = max(area[i][j]), (0 ≦ i, j < n )

Page 9: Aaex3 group2

Q1 – efficient algorithm(1)

2014/1/3 9Q1 Q8

• Final result = max(area[i][j]), (0 < i, j ≦ n)

otherwise

empty. is A[i][j] ifjiwrjiwljihjiarea

 

  

,0

)1]][[]][[])(][[(]][[

Page 10: Aaex3 group2

Q1 – efficient algorithm(1)

2014/1/3 Q1 Q8 10

int wl[205] = {}, wr[205] = {};

int r[205] = {}, l[205] = {}, h[205] = {};

int sum;

int ret = 0;

for(i = 0; i < n; i++) {

for(j = 0, sum = 0; j < m; j++) {

if(g[i][j] == 0) sum = 0;

sum = wl[j] = sum + g[i][j];

}

for(j = m-1, sum = 0; j >= 0; j--) {

if(g[i][j] == 0) sum = 0;

sum = wr[j] = sum + g[i][j];

}

for(j = 0; j < m; j++) {

if(g[i][j] == 0) h[j] = 0;

else h[j]++;

}

for(j = 0; j < m; j++) {

if(l[j] == 0) l[j] = wl[j];

else l[j] = min(l[j], wl[j]);

if(r[j] == 0) r[j] = wr[j];

else r[j] = min(r[j], wr[j]);

}

for(j = 0; j < m; j++)

ret = max(ret, (l[j]+r[j]-1)*h[j]);

}

Page 11: Aaex3 group2

Q1 – efficient algorithm(2)

• 轉換問題,記錄連續單側最大值

• Picture from http://www.drdobbs.com/database/the-maximal-rectangle-problem/184410529

2014/1/3 11Q1 Q8

Page 12: Aaex3 group2

Q1 – efficient algorithm(2)

• New Problem : Largest Rectangle in a Histogram

• Histogram : (@*#&U(@*U(!*

• Picture from http://poj.org/problem?id=2559

2014/1/3 12Q1 Q8

Page 13: Aaex3 group2

Q1 – efficient algorithm(2)

• 單調堆 ? 單調棧 ? Monotone stack ?

尚且還沒有這個詞

• 維護一個由底至頂元素由小至大 的 stack

2014/1/3 13Q1 Q8

Page 14: Aaex3 group2

Q1 – efficient algorithm(2)

2014/1/3 Q1 Q8 14

int solve(int n, int h[]) {

int ret = 0;

int i, height;

stack< pair<int, int> > stk;// <height, position>

pair<int, int> e;

stk.push(make_pair(-1, 0));

h[n] = 0;// visual height.

for(i = 0; i <= n; i++) {

height = h[i];

e = make_pair(height, i);

while(height < stk.top().first) {

e = stk.top(), stk.pop();

ret = max(ret, (i - e.second)*e.first);

}

if(height > stk.top().first)

stk.push(make_pair(height, e.second));

}

return ret;

}

Page 15: Aaex3 group2

Q1 – more …

• Online JudgePOJ 2559 - Largest Rectangle in a HistogramZJ b123 最大矩形 (Area)

• More extended problem …maximum rectangle with all same element.

O(NM)maximum rectangle which

|maximum element – minimum element| < L

O(NML)

2014/1/3 Q1 Q8 15

Page 16: Aaex3 group2

Q2

• Improve the space utilization in the algorithm

of solving the 0/1 knapsack (sum-of-subsets)

problem discussed in the class.

• 優化 0/1背包問題的記憶體空間

2014/1/3 16Q1 Q8

Page 17: Aaex3 group2

Q2

• 額外維護一個新增數值的 queue

• 當 queue.top() 小於當前嘗試的值時,將前

繼.next 指向它,並且 pop()

2014/1/3 17Q1 Q8

Page 18: Aaex3 group2

Q2 – efficient algorithm

2014/1/3 Q1 Q8 18

Queue<int> Q;

Node *head = new Node(0, NULL);

for(i = 0; i < n; i++) {

for(p = q = head; p != NULL; p = (q = p)->next) {

while(Q.front() < p->val)

q = q->next = new Node(Q.front, q->next);

Q.pop();

while(Q.front() == p->val)

Q.pop();

val = p->val + s[i];

Q.push(val);

}

}

Page 19: Aaex3 group2

Q3 – a

• Solve each of the following variations of the 0/1 knapsack problem (sum-of-subsets):Pack items of given sizes in a given-sized knapsack fully, but there is an unlimited supply (無限量供應) of each item.

• 固定大小,無限量供應

2014/1/3 19Q1 Q8

Page 20: Aaex3 group2

Q3 – efficient algorithm(a)

• Time Complexity : O(nK)

• Space Complexity : O(n)

• f[i][j] = f[i][j] | f[i-1][j-s[i]]

• 可以利用 Q2 的答案進行優化

2014/1/3 Q1 Q8 20

f[] : f[0] = 1

For(i = 0; i < n; i++)

For(j = s[i]; j <= K; j++)

f[j] |= f[j-s[i]];

Page 21: Aaex3 group2

Q3 – b

• The assumptions are the same as in v1 (n items, unlimited supply, fixed-sized knapsack), but now each item has an associated value. Design an algorithm to find how to pack the knapsack fully, such that the items in it have the maximal value among all possible ways to pack the knapsack.

• 計算背包容量的方法數

2014/1/3 21Q1 Q8

Page 22: Aaex3 group2

Q3 – efficient algorithm(b)

• Time Complexity : O(nK)

• Space Complexity : O(n)

• f[i][j] = f[i][j] + f[i-1][j-s[i]]

• 可以利用 Q2 的答案進行優化2014/1/3 Q1 Q8 22

f[] : f[0] = 1

For(i = 0; i < n; i++)

For(j = s[i]; j <= K; j++)

f[j] += f[j-s[i]];

Page 23: Aaex3 group2

Q3 – c

• The assumptions are the same as in v2 (n items with sizes and values, unlimited supply, fixed-sized knapsack, and the goal of maximizing the value), but now we are not restricted to filling the knapsack exactly to capacity. We are interested only in maximizing the total value, subject to the constraint that there is enough room for the chosen items in the knapsack.

2014/1/3 23Q1 Q8

Page 24: Aaex3 group2

Q3 – efficient algorithm(c)

• Time Complexity : O(nK)

• Space Complexity : O(n)

• f[i][j] = max(f[i-1][j], f[i][j], f[i-1][j-s[i]]+v[i])

• 可以利用 Q2 的答案進行優化2014/1/3 Q1 Q8 24

f[] : f[0] = 0

For(i = 0; i < n; i++)

For(j = s[i]; j <= K; j++)

f[j] = max(f[j], f[j-s[i]]+v[i]);

Page 26: Aaex3 group2

Q3 – tricky

• 分給 n < 6 個人等值

UVa 1163 - The Right Tip

with another core algorithm.

2014/1/3 Q1 Q8 26

Page 27: Aaex3 group2

Q4

• Design an algoritm to solve Problem 10482 “The Candayman Can” in the web site:

• http://acm.uva.es/problemset/

• 將物品分成三堆,最大堆與最小堆差最小

為何 ?

2014/1/3 27Q1 Q8

Page 28: Aaex3 group2

Q4

• 紀錄兩堆資訊,剩餘一堆不用計算個數

• dp[i][j][k] :

討論前 i 個物品,其中一堆為 j 另一堆為 k

• dp[i][j][k] = dp[i-1][j-s[i]][k] | dp[i-1][j][k-

s[i]] | dp[i-1][j][k]

2014/1/3 28Q1 Q8

Page 29: Aaex3 group2

Q5

• Design an algoritm to solve Problem 10261 “Ferry Loading” in the web site:

• http://acm.uva.es/problemset/

• 依序放入 n 台車於渡口左側或右側,如果

不能放時,則後面的所有車都不能放。

渡口長 L,每台車長度 x。

2014/1/3 29Q1 Q8

Page 30: Aaex3 group2

Q5

• 維護其中一側即可,與 Q4 相同。

• dp[i][j] : 表示前 i 台車,左側長 j。

sum = sigma(x[i])

• dp[i][j] = dp[i-1][j-x[i]] |

(sum-j <= L && dp[i-1][j])

2014/1/3 30Q1 Q8

Page 31: Aaex3 group2

Q6

• Solve the even partition problem: Given a list of n positive integers, partition the list into two sublists, each of size floor(n/2) or ceil(n/2), such that the difference between the sums of the integers in the two sublists is minimized.

• 0/1 背包問題

2014/1/3 31Q1 Q8

Page 32: Aaex3 group2

Q7

• Given a set of n sticks of various lengths,

design an algorithm to determine if it is

possible to join them end-to-end to form a

square.

• Uva 10364 - Square

2014/1/3 32Q1 Q8

Page 33: Aaex3 group2

Q7

• Uva 10364 - Square

由於狀態過大,只能使用 brute force + cut

• 否則使用 dp[i][1st][2nd][3rd]

Let 1st <= 2nd <= 3rd

2014/1/3 33Q1 Q8

Page 34: Aaex3 group2

Q8

• Suppose you have one machine and a set of n jobs a1, a2, … ,

an to process on that machine. Each job aj has a processing

time tj, a profit pj, and a deadline dj. The machine can process

only one job at a time, and job aj must run uninterruptedly for

tj consecutive time units. If job aj is completed by its deadline

dj, you receive a profit pj, but if it is completed after its

deadline, you receive a profit of 0. Give an algorithm to find

the schedule that obtains the maximum amount of profit. For

each of the following special case, is it possible to find a more

efficient algorithm (comparing to the general case.) ?

• a) Each job has the same processing time.

• b) Each job has the same profit.

• c) Each job has the same deadline.2014/1/3 34Q1 Q8

Page 35: Aaex3 group2

Q8

• a) P 工作時間相同最大利益一定先做,Greedy + disjoint set O(nlogn)

從最大利益開始挑,盡可能將它靠近到 deadline。對於 processing time > 1,則將 deadline in

[processing time, processing time*2] 之間的設定為

deadline = processing time

• b) P 工作利益相同烏龜塔,前一份作業討論過。

• c) NPC 截止日期相同最大價值背包問題。

• 當三者都不同時,根據截止日期排序dp[i][j] 表示討論前 i 個工作,截止日期為 j 的最大獲益。

2014/1/3 35Q1 Q8

Page 36: Aaex3 group2

Q9

• Given a weighted directed acyclic graph (DAG;

a directed graph is acyclic if it contains no

directed cycles), and a pair of vertices u and v,

design an algorithm to find a longest and a

shortest path from u to v. Here, the length of a

path is defined as the sum of weights of edges

in the path.

2014/1/3 36Q1 Q8

Page 37: Aaex3 group2

Q9

• 類似拓樸排序的 DP

• dp[v] = max(dp[u] + wuv)

• O(V+E)

2014/1/3 37Q1 Q8

Page 38: Aaex3 group2

Q10

• Given a weighted directed graph G=(V, E),

design an algorithm to detect if G contains a

negative cycle. Moreover, if G contains no

negative cycles, design an algorithm to find a

cycle in G of minimum weight.

2014/1/3 38Q1 Q8

Page 39: Aaex3 group2

Q10

• Floyd - Warshell algorithm

• Let g[i][i] = infinity large

Floyd - Warshell algorithm O(V3)

find minimum weight cycle

• Detect negative cycle by Bellman-ford

algorithm or SPFA(Shortest Path Faster

Algorithm with SLF and LLL strategy)

2014/1/3 39Q1 Q8

Page 40: Aaex3 group2

Q10 - more

• Minimum Mean Cycle Problem

2014/1/3 40Q1 Q8