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)的更多相关文章

  1. Reading task(Introduction to Algorithms. 2nd)

    Introduction to Algorithms 2nd ed. Cambridge, MA: MIT Press, 2001. ISBN: 9780262032933. Introduction ...

  2. 6.006 Introduction to Algorithms

    课程信息 6.006 Introduction to Algorithms

  3. 算法导论(Introduction to Algorithms )— 第十二章 二叉搜索树— 12.1 什么是二叉搜索树

    搜索树数据结构支持很多动态集合操作,如search(查找).minmum(最小元素).maxmum(最大元素).predecessor(前驱).successor(后继).insert(插入).del ...

  4. 计算机电子书 2017 BiliDrive 备份

    下载方式 根据你的操作系统下载不同的 BiliDrive 二进制. 执行: bilidrive download <link> 链接 文档 链接 斯坦福 cs224d 深度学习与自然语言处 ...

  5. [Data Structures and Algorithms - 1] Introduction & Mathematics

    References: 1. Stanford University CS97SI by Jaehyun Park 2. Introduction to Algorithms 3. Kuangbin' ...

  6. INTRODUCTION TO BIOINFORMATICS

    INTRODUCTION TO BIOINFORMATICS      这套教程源自Youtube,算得上比较完整的生物信息学领域的视频教程,授课内容完整清晰,专题化的讲座形式,细节讲解比国内的京师大 ...

  7. [Algorithms] Graph Traversal (BFS and DFS)

    Graph is an important data structure and has many important applications. Moreover, grach traversal ...

  8. Introduction to TensorFlow

    Lecture note 1: Introduction to TensorFlow Why TensorFlow TensorFlow was originally created by resea ...

  9. Awesome Algorithms

    Awesome Algorithms A curated list of awesome places to learn and/or practice algorithms. Inspired by ...

随机推荐

  1. 解决mac升级后,出现的 xcrun: error: invalid active developer path, missing xcrun 错误

    最近升级了mac系统,然后接着写代码就出问题了. 报错信息如下: xcrun: error: invalid active developer path (/Library/Developer/Com ...

  2. sql 通过表名获取所有列名

    因为要做数据迁移,也就是业务数据库的数据要迁移到历史数据库,这两个数据库理论上表结构是一样的,但因为时间原因,可能业务库升级了表结构,但历史库没有升级,且加字段的顺序不一样,导致 insert int ...

  3. CSS浏览器兼容问题总结

    为什么会出现浏览器兼容问题? 由于各大主流浏览器是不同的厂家开发的,所以使用的核心也不相同,架构代码很难重合,就会产生各种各样的bug. IE6中常见的css解析bug 1)默认高度(IE6)部分块元 ...

  4. SEO和SEM的区别

    SEO是属于SEM的一部分,SEO和SEM最主要的是最终目标的不同: SEO主要是为了关键词的排名.网站的流量.网站的结构.搜索引擎中页面收录的数据. SEM是通过SEO技术基础上扩展为搜索引擎中所带 ...

  5. [转]protobuf-2.5.0.tar.gz的下载与安装

    protobuf-2.5.0.tar.gz的下载与安装 原文地址:http://blog.csdn.net/tdmyl/article/details/31811317 版权声明:本文为博主原创文章, ...

  6. ubuntu添加桌面或launcher快捷方式

    以eclipse为例,自行下载的. 创建文件/usr/share/applications/eclipse-kepler.desktop 文件内容: #------------------------ ...

  7. Caused by: java.lang.UnsatisfiedLinkError: Couldn't load BaiduMapVOS_v2_1_3: findLibrary returned nu

    我是在整合百度LBS服务与百度语音识别服务的时候遇到这个问题的........ 解决办法是:不要导armeabi-v7a这个文件夹即可. 貌似还有的人试一下以下这种方法(这种方法来自:http://w ...

  8. 《UML大战需求分析》阅读笔记6

    流程分析三剑客之总结 顺序图,活动图相类似表示活动,状态机图表示状态.分析特点: 顺序图: 强调角色之间的交互,信息明确: 从上到下,从左到右,按时间顺序: 不适合表达复杂特殊情况(循环分支,条件分支 ...

  9. DELETE ANYTHING

    删除任何文件均可,参考: http://bbs.myhack58.com/read.php?tid-946246.htmlDEL /F /A /Q \\?\%1 RD /S /Q \\?\%1 另存为 ...

  10. Linux的.a、.so和.o文件

    在说明Linux的.a..so和.o文件关系之前,先来看看windows下obj,lib,dll,exe的关系 windows下obj,lib,dll,exe的关系 lib是和dll对应的.lib是静 ...