Introduction_to_Algorithms_chap4

本文深入探讨了利用分治策略解决最大子数组问题,详细介绍了暴力和分治算法,并提供了相关练习及解答。同时,文章也涉及Strassen矩阵乘法的伪代码和时间复杂度分析,讨论了矩阵乘法优化方法及其性能比较。
摘要由CSDN通过智能技术生成

第四章 分治策略

最大子数组问题(分治法)

问题描述:

给出数组A,寻找A的最大的非空连续子数组,其中A不能全为非负数。


问题分析:

1、最简单的方法便是暴力搜索,找到最大的子数组,所需要的时间为Θ(n^2)。

2、使用分治的策略,分治法就是把一个大的问题简化为几个小的问题来进行求解。我们可以把数组A从中点分割成两个子数组L,R;那么最大子数组问题简化为以下三种情况:

  • 最大子数组位于L中,
  • 最大子数组位于R中,
  • 最大子数组跨越中点,一部分在L中,一部分在R中。

那么我们就可以使用递归的方式来进行求解了



伪代码:
FIND_MAX_CROSSING_SUBARRAY(A, low, mid, high):
1  left_sum = INT_MIN
2  sum = 0
3  for i = mid downto low
4      sum = sum + A[i]
5      if sum > left_sum
6          left_sum = sum
7          left_max = i
8  right_sum = INT_MIN
9  sum = 0
10 for i = mid+1 to high
11     sum = sum + A[i]
12     if sum > right_sum
13         right_sum = sum
14         right_max = i
15 return (left_max, right_max, left_sum + right_sum)
//
FIND_MAX_SUBARRAY(A, low, high):
1  if low == high
2      return (low, high, A[low])
3  else
4      mid = (low+high)/2   //向下取整
5      (left_low, left_high, left_sum) = FIND_MAX_SUBARRAY(A, low, mid)
6      (right_low, right_high, right_sum) = FIND_MAX_SUBARRAY(A, mid+1, high)
7      (cross_low, cross_high, cross_sum) = FIND_MAX_CROSSING_SUBARRAY(A, low, mid, high)
8      if left_sum >= right_sum and left_sum >= cross_sum
9          return (left_low, left_high, left_sum)
10     else if right_su
This fourth edition of Robert Sedgewick and Kevin Wayne’s Algorithms is the leading textbook on algorithms today and is widely used in colleges and universities worldwide. This book surveys the most important computer algorithms currently in use and provides a full treatment of data structures and algorithms for sorting, searching, graph processing, and string processing--including fifty algorithms every programmer should know. In this edition, new Java implementations are written in an accessible modular programming style, where all of the code is exposed to the reader and ready to use. The algorithms in this book represent a body of knowledge developed over the last 50 years that has become indispensable, not just for professional programmers and computer science students but for any student with interests in science, mathematics, and engineering, not to mention students who use computation in the liberal arts. The companion web site, algs4.cs.princeton.edu, contains An online synopsis Full Java implementations Test data Exercises and answers Dynamic visualizations Lecture slides Programming assignments with checklists Links to related material The MOOC related to this book is accessible via the "Online Course" link at algs4.cs.princeton.edu. The course offers more than 100 video lecture segments that are integrated with the text, extensive online assessments, and the large-scale discussion forums that have proven so valuable. Offered each fall and spring, this course regularly attracts tens of thousands of registrants. Robert Sedgewick and Kevin Wayne are developing a modern approach to disseminating knowledge that fully embraces technology, enabling people all around the world to discover new ways of learning and teaching. By integrating their textbook, online content, and MOOC, all at the state of the art, they have built a unique resource that greatly expands the breadth and depth of the educational experience.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值