note of introduction of Algorithms(Lecture 3 - Part1)
Lecture 3(part 1)
Divide and conquer
1. the general paradim of algrithm as bellow:
1. divide the problem into subproblems;
2. conqure each subproblems recrusively;
3. combine solution
2. Some typical problem (part 1)
the matrix mutiplication(strassen's algorithm) and the vlsi layout problem will be in the note leceture part 2.
- binary search
/*-
* MIT introduction of algrithm
* Lecture 3: binary search
* Fredric : 2013-11-18
*/
#include<stdio.h>
#include<stdlib.h> typedef unsigned int uint; #define MAX 11
uint g_array[MAX] = {,,,,,,,,,,};
uint target = ; //target number
int binarysearch(uint start, uint end); void main(void)
{
int n = ;
printf("start to find the num:%d..\t\n", target);
if(- != (n = binarysearch(, MAX-))){
printf("the target %d has been found in num:%d", g_array[n],n);
}
system("pause");
return;
} /*-
* binary search recursive
*/
int binarysearch(uint start, uint end){
uint n = (start + end)/;
uint tmp = g_array[n]; if(target == tmp){
return n;
}else{
if(tmp > target){
return binarysearch(start, n);
}else{
return binarysearch(n+,end);
}
}
return -;
}
- powering a number
/*-
* MIT introduction of algrithm
* Lecture 3: powering a number
* Fredric : 2013-11-17
*/
#include<stdio.h>
#include<stdlib.h> typedef unsigned int uint; //calculate the result of n^m, like n = 2, m = 3, result = 8
uint n = ;
uint m = ;// m > 1 double power_number(uint n, uint m); /*
* main function
*/
void main(void)
{
double result = 0.0;
result = power_number(n,m);
printf("the result of %d^%d is %lf /t/n", n,m,result);
system("pause");
return;
} /*-
* powering a number
* result =
* n^(m/2) * n^(m/2) if m is even
* n^((m-1)/2) * n^((m-1)/2)*n if m is odd
*/
double power_number(uint n, uint m){
if( == m){
return ;
} if( == m%){
return power_number(n,m/)*power_number(n,m/);
}else{
return power_number(n,(m-)/)*power_number(n,(m-)/)*n;
}
}
- Fibonacci number(using matrix mutiplication)
/*-
* MIT introduction of algrithm
* Lecture 3: Fibonnaci,using the matrix method
* Fredric : 2013-11-17
*/
#include<stdio.h>
#include<stdlib.h> typedef unsigned int uint; /*-
* Input:
* pa00/01/10/11 according to the element of the Array Aij
* n: the number of the fibonacci
*/
void fibonacci_number(uint *pa00, uint *pa01, uint *pa10,uint *pa11, uint n); void main(void)
{
uint a00 = ;
uint a01 = ;
uint a10 = ;
uint a11 = ; uint num = ;//num > 0
fibonacci_number(&a00,&a01,&a10,&a11, num);
printf("The num %d fibonacci number is:%d\t\n", num, a10); system("pause");
return;
} /*-
* calculate the fibonacci number
* f(n) =
* 0 if n = 0;
* 1 if n = 1;
* f(n-1) + f(n-1) if n > 1
* the divide and conquer algrithm is:
* fn+1 fn 1 1
*( ) = ( )^n
* fn fn-1 1 0
*/
void fibonacci_number(uint *pa00, uint *pa01, uint *pa10,uint *pa11, uint n){
uint tmp00 = *pa00;
uint tmp01 = *pa01;
uint tmp10 = *pa10;
uint tmp11 = *pa11; if( == n){
return;
}else{
//Matrix mutiplication
*pa00 = tmp00 * tmp00 + tmp01 * tmp10;
*pa01 = tmp00 * tmp01 + tmp01 * tmp11;
*pa10 = tmp10 * tmp00 + tmp11 * tmp10;
*pa11 = tmp10 * tmp01 + tmp11 * tmp11;
if( == n%){
fibonacci_number(pa00,pa01,pa10,pa11,n/);
}else{ fibonacci_number(pa00,pa01,pa10,pa11,(n-)/);
uint tmp00 = *pa00;
uint tmp01 = *pa01;
uint tmp10 = *pa10;
uint tmp11 = *pa11; *pa00 = tmp00 + tmp01;
*pa01 = tmp00;
*pa10 = tmp10 + tmp11;
*pa11 = tmp10;
}
}
}
note of introduction of Algorithms(Lecture 3 - Part1)的更多相关文章
- Reading task(Introduction to Algorithms. 2nd)
Introduction to Algorithms 2nd ed. Cambridge, MA: MIT Press, 2001. ISBN: 9780262032933. Introduction ...
- 6.006 Introduction to Algorithms
课程信息 6.006 Introduction to Algorithms
- 算法导论(Introduction to Algorithms )— 第十二章 二叉搜索树— 12.1 什么是二叉搜索树
搜索树数据结构支持很多动态集合操作,如search(查找).minmum(最小元素).maxmum(最大元素).predecessor(前驱).successor(后继).insert(插入).del ...
- 计算机电子书 2017 BiliDrive 备份
下载方式 根据你的操作系统下载不同的 BiliDrive 二进制. 执行: bilidrive download <link> 链接 文档 链接 斯坦福 cs224d 深度学习与自然语言处 ...
- [Data Structures and Algorithms - 1] Introduction & Mathematics
References: 1. Stanford University CS97SI by Jaehyun Park 2. Introduction to Algorithms 3. Kuangbin' ...
- INTRODUCTION TO BIOINFORMATICS
INTRODUCTION TO BIOINFORMATICS 这套教程源自Youtube,算得上比较完整的生物信息学领域的视频教程,授课内容完整清晰,专题化的讲座形式,细节讲解比国内的京师大 ...
- [Algorithms] Graph Traversal (BFS and DFS)
Graph is an important data structure and has many important applications. Moreover, grach traversal ...
- Introduction to TensorFlow
Lecture note 1: Introduction to TensorFlow Why TensorFlow TensorFlow was originally created by resea ...
- Awesome Algorithms
Awesome Algorithms A curated list of awesome places to learn and/or practice algorithms. Inspired by ...
随机推荐
- bash fifo管道使用测试例子
碰到一个场景: 一个脚本内起了多个后台线程,往一个日志文件写日志,结果因为线程之间争抢写锁,导致脚本执行效率很低,为了解决这个问题,希望减少写锁的争抢,尝试使用fifo解决该问题,以下是实验用例子. ...
- 总结JS 常用函数
希望本文总结的内容能给各位看官带来焕然一新的感觉.另外,如果你们有什么值得推荐的js技巧,欢迎在评论中补充,我可以收纳在本文中. PS:此文档会持续新增内容. Ajax请求 jquery ajax函数 ...
- @helper函数使用方法
这个函数方法,我也是通过别人博客看到的,感觉不错和大家一起学习分享一下. 1.自定义函数方法,只在同一个view视图文件里调用 Controller public ActionResult Index ...
- CodeSmith Generator 7.0.2激活步骤
地址是:http://www.cnblogs.com/dunitian/p/4096917.html
- Ado.net中简单的DBHelper类(增删改查)
private static string connString = "server=.;database=hotel;uid=aa;pwd=123";//最好从配置文件中取出 p ...
- 【OpenGL】如何绘制Shadow
背景 Shadow即阴影,它是光线被不透明物体遮挡而产生的黑暗区域,与光源的方向相反. 在Blender中编辑过程中没有Shadow,只有在经过渲染后才能显示.目前有一个基于Blender的项目,要求 ...
- 虚拟机上安装Linux操作系统
很久之前就知道虚拟机这个东西,也都在虚拟机上安装过Windows的操作系统和Linux的操作系统,但是一直都没有去做笔记. 最近还是比较有时间,就移除了前两天刚刚安装的Linux系统,重新安装一次,做 ...
- 《uml大战需求分析》阅读笔记05
<uml大战需求分析>阅读笔记05 这次我主要阅读了这本书的第九十章,通过看这章的知识了解了不少的知识开发某系统的重要前提是:这个系统有谁在用?这些人通过这个系统能做什么事? 一般搞清楚这 ...
- php安装libiconv-1.14.tar.gz遇到的问题
遇到的Error code In file included from progname.c:26:0: ./stdio.h:1010:1: error: ‘gets‘ undeclared here ...
- Python基于pandas的数据处理(二)
14 抽样 df.sample(10, replace = True) df.sample(3) df.sample(frac = 0.5) # 按比例抽样 df.sample(frac = 10, ...