第4章 分治策略.ppt

  1. 1、本文档共87页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
第4章 分治策略

1 void closest(POINT X[], A_POINT Y[], int low, int high, POINT a, POINT b, float d) 2 { 3 int i, j, k, m; 4 POINT al, bl, ar, br; 5 float dl, dr; 6 if ((high-low)==1) // n=2,calculate directly 7 (a = X[low], b = X[high], d = dist(X[low],X[high])); 8 else if ((high-low)==2) { // n=3,calculate directly 9 dl = dist(X[low],X[low+1]); 10 dr = dist(X[low],X[low+2]); 11 d = dist(X[low+1],X[low+2]); 12 if ((dl=dr) (dl=d)) 13 (a = X[low], b = X[low+1], d = dl); 14 else if (dr=d) 15 (a = X[low], b = X[low+2], d = dr); 16 else 17 (a = X[low+1], b = X[low+2]); 18 } 2D Closest-Pair Algorithm 19 else { // n3 Divede and Conquer 20 A_POINT *SL = new A_POINT[(high-low)/2+1]; A_POINT *SR = new A_POINT[(high-low)/2]; // divide X into two subarray by m 22 m = (high + low) / 2; 23 j = k = 0; 24 for (i=0; i high-low+1; i++) 25 if (Y[i].index = m) // collect the elements of auxiliary array of left subarray of X 26 SL[j++] = Y[i]; 27 else // collect the elements of auxiliary array of right subarray of X SR[k++] = Y[i]; // calculate the closest-pair of left subarray of X closest(X, SL, low, m, al, bl, dl); // calculate the closest-pair of right subarray of X 30 closest(X, SR, m+1, high, ar, br, dr); 2D Closest-Pair Algorithm 31 if (dldr) // combination 32 (a = al, b = bl, d = dl); 33 else 34 (a = ar, b = br, d = dr); 35 POINT *Z = new POINT[high-low+1]; 36 k =0; // collect the points within the two strips of width d of L from Y 37 for (

文档评论(0)

dajuhyy + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档