80 likes | 172 Views
What to Analyze. Analyze the algorithm rather than the program Running time worst-case average case. example. Maximum subsequence sum problem Given A 1 ,A 2 ,…,A n find the maximum value of Ex : -2 , 11 , -4 , 13 , -5 , -2 有四種 Algorithm.
E N D
What to Analyze • Analyze the algorithm rather than the program • Running time worst-case average case
example Maximum subsequence sum problem Given A1,A2,…,An find the maximum value of Ex:-2 , 11 , -4 , 13 , -5 , -2 有四種Algorithm
A1: -2 11 -4 13 -5 -2 假設n個Ai
A2: -2 11 -4 13 -5 -2 i -2 9 5 18 13 11 11 7 20 15 13 -4 9 4 2 13 8 6 -5 -7 -2 ∴O(N2)
A3: divide and conquer -2 11 -4 13 -5 -2 11 13 7+13=20
A4: -2 11 -4 13 -5 -2 Sum-2放棄 max 0 11 7 20 15 13 Note:最佳解開頭不為負 step1:由左至右,sum=0 累加若為負,則捨去 若大於max,則設定 O(N)
On-line algorithm:only require constant space and runs in linear time.
T(1,8)=_____ T(5,8)=_____ T(1,4)=_____ T(7,8)=_____ T(5,6)=_____ T(3,4)=_____ T(1,2)=_____ • 1. The Maximum Subsequence Sum Problem • Given integers a1=4, a2=-5, a3=-3, a4=3, a5=7, a6=-1, a7=2, a8=6, find the maximum subsequence sum • by divide and conquer • Let T(i, j) denote the maximum subsequence sum from ai to aj. Fill the following: • (b)By on-line algorithm