算法Lecture 3.pdfVIP

  • 11
  • 0
  • 约1.18万字
  • 约 32页
  • 2017-06-06 发布于河南
  • 举报
算法Lecture 3

Algorithms Design and Analysis Lecture 3 Beihang University 2008 Divide and Conquer (Continue) Example 4. Finding Minimum (最小) and Maximum (最大) 1 Backgound (背景) Find the lightest (最轻) and heaviest (最重) of n elements using a balance (天平) that allows you to compare the weight of 2 elements. Minimize (使最小) the number of comparisons. 2 Max element Find element with max weight (重量) from w[0, n-1] maxElement=0 for (int i = 1; i n; i++) if (w[maxElement] w[i]) maxElement = i; Number of comparisons ( 比较次数) is n−1. 3 Obvious method • Find the max of n elements making n−1 comparisons. • Find the min of the remaining n−1 elements making n−2 comparisons. • Total number of comparisons is 2n−3. 4 Divide and Conquer How to combine? • Find the min and max of {3,5,6,2,4,9,3,1}. • Large instance. • A = {3,5,6,2} and B = {4,9,3,1}. • min(A) = 2, min(B) = 1. • max(A) = 6, max(B) = 9. • min{min(A),min(B)} = 1. • max{max(A), max(B)} = 9. 5 Dividing Into Smaller Problems {8,2,6,3,9,1,7,5,4,2,8} {8,2,6,3,9} {1,7,5,4,2,8} {8,2} {6,3,9} {1,7,5} {4,2,8} {6} {3,9} {1} {7,5} {4} {2,8} 6 Solve Small Problems and Combine {1,9} {2,9}

您可能关注的文档

文档评论(0)

1亿VIP精品文档

相关文档