算法导论(c++实现)chapter4
算法导论的第四章——分治策略,主要的算法为寻找最大子数组问题,还有矩阵乘法的Strassen算法,还有一些课后的练习题,主要讲了求解递归式的三种方法:代入法(感觉像是猜测,然后用数学归纳法验证),递归树法(比较的直观明了),主方法(给出了递归式的大部分通解)。现在把该chapter的所有涉及的算法都贴上来。
#include<iostream>
using namespace std;//this is the program that in order to find the max crossing subarray.
//in order to return the subarray's low bound and the high bound,
//i have to create a struct to contain all this information
//this is the constrction of the struct
template<class T>
struct SUB_Array {int low;int high;T sum;
};//this is the realization of FIND-MAX-CROSSING-SUBARRAY
template<class T>
SUB_Array<T> F_M_C_S(T* A, int low, int mid, int high)
{T left_sum = -99999;T sum = 0;int max_left;for (int i = mid; i >= low; i--){sum += A[i];if (sum > left_sum){left_sum = sum;max_left = i;}}T right_sum = -9999999;int max_right;sum = 0;for (int j = mid + 1; j <= high; j++){sum += A[j];if (sum > right_sum){right_sum = sum;max_right = j;}}SUB_Array<T> x;x.low = max_left;x.high = max_right;x.sum = left_sum + right_sum;return x;
}//this is the FIND-MAXIMUM-SUBBARRAY
template<class T>
SUB_Array<T> F_M_S(T* A, int low, int high)
{SUB_Array<T> x,y,z;if(high==low){x.low = low;x.high = high;x.sum = A[low];return x;}else{int mid = (low + high) / 2;x = F_M_S(A, low, mid);y = F_M_S(A, mid + 1, high);z = F_M_C_S(A, low, mid, high);if (x.sum >= y.sum&&x.sum >= z.sum)return x;else if (y.sum >= x.sum&&y.sum >= z.sum)return y;elsereturn z;}
}//solution to 4.1-2 . so i will use the violent way to solve the max subarray;template<class T>SUB_Array<T> Viol_Maxarray(T* A, int low, int high){SUB_Array<T> x;T Max_sum = A[low];int Max_low, Max_high;for (int i = low; i <= high; i++){T sum = 0;for (int j = i; j <= high; j++){sum += A[j];if (sum > Max_sum){Max_sum = sum;Max_low = i;Max_high = j;}}}x.sum = Max_sum;x.low = Max_low;x.high = Max_high;return x;}//solution to 4.1-4//in my opinion,i think the biggest subarray is that the sum must more than zero,and if the sum \// is less than zero,it just returns the zero ,which means not have.// and it is easy to change the program ,just use the upper program,and make the left_sum=0 ,right_sum=0;//solution to 4.1-5template<class T>SUB_Array<T> Nrecurr_SUBARR(T* A, int low, int high){SUB_Array<T> x;int t_high;T Max_sum = A[low];int t_low=low;for (int i = low; i <= high; i++){T sum = 0;for (int j = i + 1; j >= t_low; j--){sum += A[j];if (sum > Max_sum){Max_sum = sum;t_low = j;t_high = i + 1;}}}x.low = t_low;x.high = t_high;x.sum = Max_sum;return x;}//this is the SQUARE-MATRIX-MULTIPLYconst int n = 4;template<class T>void SQMA_MUL(T a[n][n], T b[n][n], T c[n][n]){for(int i=0;i<n;i++)for (int j = 0; j < n; j++){c[i][j] = 0;for (int k = 0; k < n; k++)c[i][j] += a[i][k] * b[k][j];}}//this is the Strassen meathod//we just assure the input number is N*N,so i just split into n/2 * n/2.//and i didn't have much more simple coded way to realize it.//so i just use the ordinary way .//there are several steps to make ahead,so it will be sort of simple.const int Size = 4;template<class T>void Strassen(T a[Size][Size], T b[Size][Size], T c[Size][Size]){const int newsize = Size/2;T S1[newsize][newsize] = { 0 }, S2[newsize][newsize] = { 0 }, S3[newsize][newsize] = { 0 };T S4[newsize][newsize] = { 0 }, S5[newsize][newsize] = { 0 }, S6[newsize][newsize] = { 0 }, S7[newsize][newsize] = { 0 }, S8[newsize][newsize] = { 0 };T S9[newsize][newsize] = { 0 }, S10[newsize][newsize] = { 0 };for (int i = 0; i < newsize; i++)for (int j = 0; j < newsize; j++){S1[i][j] = b[i][j+newsize] - b[newsize + i][newsize + j];S2[i][j] = a[i][j] + a[i][newsize + j];S3[i][j] = a[newsize + i][j] + a[newsize + i][newsize + j];S4[i][j] = b[newsize + i][j] - b[i][j];S5[i][j] = a[i][j] + a[newsize + i][newsize + j];S6[i][j] = b[i][j] + b[newsize + i][newsize + j];S7[i][j] = a[i][newsize + j] - a[newsize + i][newsize + j];S8[i][j] = b[newsize + i][j] + b[newsize + i][newsize + j];S9[i][j] = a[i][j] - a[newsize + i][j];S10[i][j] = b[i][j] + b[i][newsize + j];}T P1[newsize][newsize] = { 0 }, P2[newsize][newsize] = { 0 }, P3[newsize][newsize] = { 0 }, P4[newsize][newsize] = { 0 };T P5[newsize][newsize] = { 0 }, P6[newsize][newsize] = { 0 }, P7[newsize][newsize] = { 0 };for (int i = 0; i < newsize; i++)for (int j = 0; j < newsize; j++){for (int k = 0; k < newsize; k++){P1[i][j] += a[i][k] * S1[k][j];P2[i][j] += S2[i][k] * b[newsize + k][newsize + j];P3[i][j] += S3[i][k] * b[k][j];P4[i][j] += a[newsize + i][newsize + k] * S4[k][j];P5[i][j] += S5[i][k] * S6[k][j];P6[i][j] += S7[i][k] * S8[k][j];P7[i][j] += S9[i][k] * S10[k][j];}}for (int i = 0; i < newsize; i++)for (int j = 0; j < newsize; j++){c[i][j] = 0;c[i][j] = P5[i][j] + P4[i][j] - P2[i][j] + P6[i][j];c[i][newsize + j] = 0;c[i][newsize + j] = P1[i][j] + P2[i][j];c[i + newsize][newsize + j] = 0;c[i + newsize][j + newsize] = P5[i][j] + P1[i][j] - P3[i][j] - P7[i][j];c[newsize + i][j] = 0;c[newsize + i][j] = P4[i][j] + P3[i][j];}}int main(){//int A[16] = { 13,-3,-25,20,-3,-16,-23,18,20,-7,12,-5,-22,15,-4,7 };//SUB_Array<int> x;//x = F_M_S(A, 0, 15);//x = Viol_Maxarray(A, 0, 15);//x = Nrecurr_SUBARR(A, 0, 15);//cout << x.low <<" "<< x.high <<" "<< x.sum << endl;int a[4][4] = { 1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2 };int b[4][4] = { 1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2 };int c[4][4] = { 0 };int d[4][4];int e[2][2] = { 1,3,7,5 };int f[2][2] = { 6,8,4,2 };// SQMA_MUL(e, f, c);SQMA_MUL(a, b, d);Strassen<int>(a, b, c);for (int i = 0; i < n; i++){for (int j = 0; j < n; j++)cout << c[i][j] << " ";cout << endl;}cout << endl;for (int i = 0; i < n; i++){ for (int j = 0; j < n; j++)cout << d[i][j] << " ";cout << endl;}return 0;
}
总的算法就那么多,但是有些东西可以继续完善。有些函数的接口有待斟酌,还有就是Strassen算法,我感觉我的实现还是O(n^3),递归实现我暂时没有想出来。先上传了,等有思路在补上!欢迎大家提供思路,一起探讨~
算法导论(c++实现)chapter4相关推荐
- 算法导论Java实现-构建MaxHeap
package lhz.algorithm.chapter.six; /** * "构建堆",<算法导论>6.3章节 Building a heap * 利用之前实现的 ...
- 算法导论读书笔记-第十九章-斐波那契堆
算法导论第19章--斐波那契堆 可合并(最小)堆(mergeable min-heap) : 支持以下5种操作的一种数据结构, 其中每一个元素都有一个关键字: MAKE-HEAP(): 创建和返回一个 ...
- 《算法导论》读书笔记--第三章 函数的增长
好长时间了,继续算法导论. 当输入规模足够大时,并不计算精确的运行时间,倍增常量和低阶项被舍去.我们要研究的是算法的渐近效率,即在输入规模无限量时,在极限中,算法的运行时间如何随着输入规模的变大而增加 ...
- 《算法导论》中parallel for 的时间复杂度
最近在看<算法导论>,看到多线程算法这章中,有一个parallel for循环的例子,如下: parallel for i = 1 to n parallel for j = ...
- 算法导论中求解时间复杂度的三种方法
这一章讲的是递归式(recurrence),递归式是一组等式或不等式,它所描述的函数是用在更小的输入下该函数的值来定义的. 本章讲了三种方法来解递归式,分别是代换法,递归树方法,主方法. 1.代换法( ...
- 算法导论Java实现-随机化数组的两种方式(5.3章节)
package lhz.algorithm.chapter.five; /** * 随机数组两种实现,<算法导论>第五章第三节 * 本文地址:http://mushiqianmeng.bl ...
- 《算法导论》读书笔记(七)
<算法导论>读书笔记之第16章 贪心算法-活动选择问题 前言:贪心算法也是用来解决最优化问题,将一个问题分成子问题,在现在子问题最优解的时,选择当前看起来是最优的解,期望通过所做的局部最优 ...
- 算法导论chapter6 堆排序的代码
按照<算法导论>上的伪代码实现了,刚开始没注意index的问题,导致错误,看来对于伪代码实现C还是要注意下啊!! #include<iostream> #include < ...
- Python语言程序设计之urllib.request抓取页面,网易公开课之《麻省理工学院公开课:算法导论》
Python语言用urllib.request模块抓取页面非常简单,再将抓取的页面内容用re模块解析,找出自己想要的东西.下面就就此方法来抓取网易公开课之<麻省理工学院公开课:算法导论>, ...
最新文章
- swift golang java,解决两数之和 (Javascript, Java, C#, Swift, Kotlin, Python,C++, Golang)
- 友盟分享快速集成--学习记录
- LeetCode 1601. 最多可达成的换楼请求数目(回溯+剪枝)
- 【LeetCode】 sort list 单清单归并
- zynq开发系列6:创建AXI IP实现PS对PL的数据配置(步骤一)
- android view
- 电脑连接的手机真机,利用Chrome调试WebView
- web前端期末大作业:美食文化网页设计与实现——美食餐厅三级(HTML+CSS+JavaScript)
- heeds matlab,Ricardo IGNITE下载-整车性能仿真分析软件Ricardo IGNITE下载v2018.1 最新版-西西软件下载...
- 湖南张家界夫妻自助旅游攻略
- 自主招生计算机网测考什么,自主招生是什么意思 2019自主招生考试考什么
- DB2的VALUE函数
- mysql fatch array_辩别WEB服务程序,,了解常见的几种脚本和数据库之间的搭配组合及特点...
- 【虹科技术分享】如何测试 DNS 服务器:DNS 性能和响应时间测试
- python的彪悍特性--自省
- 公众号第三方平台开发 教程一 创建公众号第三方平台
- 第十七天(续第十六天BPDU相关知识以及STP的配置)
- Microsoft Visual Studio 2013 产品密匙
- python在两行中分别输入一个字符串s和整数n,定义一个函数将字符串s循环向右移动n位
- win10——戴尔笔记本电脑插上耳机没有声音电脑外放
热门文章
- 芒果互娱与淘宝内容生态达成合作,进一步升级台网融合模式
- Vue中,views和components的区别
- 向kaggle中添加包
- 开源数据库HAWQ,架构调研
- 病毒假冒工行电子银行升级盗取帐号密码(转)
- Python 获取指定模块基址
- 每日一道面试题:Vue 组件间通信有哪几种方式?
- 当web.config文件放置在共享目录下(UNC),启动IIS会提示有错误信息500.19,伴随有错误代码0x80070003和错误代码0x80070005的解决办法...
- JDBC学习第二站之ResultSet与ResultSetMeta
- Ubuntu 下 Python 版本管理