随笔分类 - [41]Algorithm算法
-
遗传算法
摘要:遗传算法1、遗传算法是非常好的非线性搜索算法,是一种全局寻优的近似方法2、遗传算法的步骤:1)将问题的解写成编码的形式,随机产生第一代种群。2)选择:选择一些染色体来产生下一代。根据适应度F(i)以比例选择算法从种群中选出2个个体。3)对选出的2个个体按交叉概率执行交叉操作,再按变异概率执行变异操作4)重插入:将处理后的2个个体加入新的种群。3、工具箱这里运用的工具箱是GATBX遗传算法工具箱1)创建随机的初始种群Chrom=crtbp(Nind, Lind)Nind种群个体数目,Lind是个体编码的长度;Chrom是Nind*Lind的矩阵2)选择适应度值Fitnv=rank(Objv)根据 阅读全文
-
从优化到再优化,最长公共子串
摘要:从优化到再优化,最长公共子串最长公共子串(Longest Common Substring)是一个非常经典的面试题目,在实际的程序中也有很高的实用价值,所以把该问题的解法总结在本文重。不过不单单只是写出该问题的基本解决代码而已,关键还是享受把学习算法一步步的优化,让时间和空间复杂度一步步的减少的惊喜。概览最长公共子串问题的基本表述为:给定两个字符串,求出它们之间最长的相同子字符串的长度。最直接的解法自然是找出两个字符串的所有子字符串进行比较看他们是否相同,然后取得相同最长的那个。对于一个长度为n的字符串,它有n(n+1)/2个非空子串。所以假如两个字符串的长度同为n,通过比较各个子串其算法复杂 阅读全文
-
简单动态规划——三逆数的O(N^2)解法!
摘要:【算法】简单动态规划——三逆数的O(N^2)解法!问题描述:三逆数定义:给一个数的序列A[0,1,....N-1]),当iA[j]>A[k]时,称作ai,aj,ak为一个三逆数。现在给定一个长度为N的数组,求此数组序列中存在三逆数的总个数。本人暂时只想到O(N^2)时间复杂度的解法。不知道还没有没更好更快的解法。(谁有更好的解法,欢迎分享~)O(N^3)解法:这个最直观了,直接三层循环进行统计,即可求和三逆数总和。代码太简单了,就略过了~O(N^2)解法:1.进行预处理,先用R[1..N]数组记录,R[i]表示在第i个元素后面比第i个元素小的个数之和,此步为基本的动态规划,时间复杂度为O 阅读全文
-
图像偏色检测算法
摘要:图像偏色检测算法本文的算法并不是我自己提出的,而是一些论文中提出的,详见 : 1、基于图像分析的偏色检测及颜色校正方法徐晓昭 , 蔡轶珩 , 刘长江 , 贾克斌 , 沈兰荪 2、皮肤检测技术的研究及改进 为方便大家理解,这里还是从这些论文里摘取些具体的过程予以描述。 RGB颜色空间是最简单的一种颜色空间,但是RGB颜色空间最大的局限性在于当用欧氏距离来刻画两种颜色之间的差异时,所计算出的两种颜色之间的距无法正确表征人们实际所感知到的这两种颜色之间的真实差异。采用CIE Lab颜色空间,此空间所计算出来的颜色之间的距离与实际感知上的差别基本一致。其直方图可以客观的反映图像色偏程度,在CI... 阅读全文
-
Instant Radiosity实现
摘要:Instant Radiosity实现本来说等把课程作业做完再来弄这个,但是还是没有忍住,先做了,主要原因还是这个算法很容易实现。这个算法在1997年由Keller首次提出。虽然名字叫Instant Radiosity,但是它和Radiosity还是有很大区别的,主要表现为Radiosity是确定性的算法,而Instant Radiosity是一个随机算法。再说该算法的思想,就是从光源像场景透射出很多粒子,与场景中物体发生碰撞后,就在碰撞点处创建出虚拟点光源(VirtualPointLight),利用这些VPL来近似模拟间接光照,思想十分简单。下图形象地展示了整个算法过程:对于图中的这个室内场 阅读全文
-
区间数据计算
摘要:区间数据计算最近一年多来,一直比较忙,最近一段时间终于空闲了,把以前没写的都补上.....这边随笔主要是计算一系列数据的间隔数据。从一堆数据中查询出每个区间的起始数据,结束数据以及数据个数,同时可以设置相应精度(小数位数)。区间数据数据结构1、区间数据主要包括当前区间的起始数据,结束数据以及数据个数。结构如下:?publicstructIntervalData{privateTKey _startValue;privateTKey _endValue;privateTValue _count;publicIntervalData(TKey startValue, TKey endValue, 阅读全文
-
红黑树数据结构剖析
摘要:红黑树数据结构剖析红黑树是计算机科学内比较常用的一种数据结构,它使得对数据的搜索,插入和删除操作都能保持在O(lgn)的时间复杂度。然而,相比于一般的数据结构,红黑树的实现的难度有所增加。网络上关于红黑树的实现资料汗牛充栋,但是乏于系统介绍红黑树实现的资料。本文通过一个自己实现的红黑树数据结构以及必要的搜索,插入和删除操作算法,为大家更系统地剖析红黑树数据结构的实现。对于大部分数据结构,一般都会使用抽象数据类型的方式实现,C++提供的模板机制可以做到数据结构与具体数据类型无关,就像STL实现的那样。不过本文并非去实现STL中的红黑树,更重要的是透过红黑树的实现学习相关的算法和思想。当然,我们还 阅读全文
-
算法--中位数计算
摘要:算法--中位数计算中位数(Median)1、定义:一组数据按从小到大(或从大到小)的顺序依次排列,处在中间位置的一个数(或最中间两个数据的平均数,注意:和众数不同,中位数不一定在这组数据中)。 注:当个数为基数时,取最中间位置的数;当个数为偶数时,取最中间两个数的平均数。2、从小到大排序,可以先用冒泡排序,然后取中位数,那么先看下冒泡排序算法,代码如下:?publicstaticvoidBubbleSort(thisIList array){if(array == null|| array.Count == 0){return;}intcount = array.Count;for(inti 阅读全文
-
一个简单的统计图像主颜色的算法(C#源代码)
摘要:一个简单的统计图像主颜色的算法(C#源代码)前段日子有朋友咨询了下分析图像主颜色的算法,我对这一块也没有什么深入的研究,参考了一些小代码,然后自己写了一个很简单的小工具,现共享给大家。 界面截图如下: 算法的原理很简单,就是统计出图像中各种颜色的分布情况,然后取前N个颜色作为主成分。 当然,实际上如果直接对图像的各通道256个色阶进行统计,得到的结果可能是没有意义的,所以一般都需要先把256个色阶线性的隐射到更少的色阶范围。 主要的代码如下: static unsafe class Statistics { //'*******************... 阅读全文
-
[LeetCode 116 117] - 填充每一个节点的指向右边邻居的指针I & II (Populating Next Right Pointers in Each Node I & II)
摘要:[LeetCode 116 117] - 填充每一个节点的指向右边邻居的指针I & II (Populating Next Right Pointers in Each Node I & II)问题给出如下结构的二叉树:struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next;}填充每一个next指针使其指向自己的右边邻居节点。如果没有右边的邻居节点,next指针须设成NULL。在开始时,所有的next指针被初始化成NULL。注意:你只能使用常数级别的额外空间你可以假设该树 阅读全文
-
ACM Skiing问题
摘要:ACM Skiing问题描述 Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激。可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你。Michael想知道载一个区域中最长底滑坡。区域由一个二维数组给出。数组的每个数字代表点的高度。下面是一个例子1 2 3 4 516 17 18 19 615 24 25 20 714 23 22 21 813 12 11 10 9一个人可以从某个点滑向上下左右相邻四个点之一,当且仅当高度减小。在上面的例子中,一条可滑行的滑坡为24-17-16-1。当然25-24-23-...-3-2-1更长。事实上... 阅读全文
-
[珠玑之椟]字符串和序列:左移、哈希、最长重复子序列的后缀数组解法、最大连续子序列
摘要:[珠玑之椟]字符串和序列:左移、哈希、最长重复子序列的后缀数组解法、最大连续子序列字符串和数组在存储上是类似的,把它们归为同一主题之下。本文主要介绍三大类问题和它们衍生的问题,以及相应算法。 本文主要介绍和讨论的问题和介绍的算法(点击跳转):字符串循环移位(左旋转)问题算法1:“杂技”代码算法2:块交换算法3:求逆(推荐)以字符串散列为例的哈希表最长重复子序列问题的后缀数组解法最大连续子序列基本问题直接解法O(n2):优化解法与累加和数组分治法解法扫描解法附加讨论相关问题查找总和最接近某个数的连续子序列数组分段赋值问题给定长度的最大连续子序列矩阵求最大和子矩阵字符串循环移位(左旋转)问题问题. 阅读全文
-
最长回文子串(Longest Palindromic Substring)
摘要:最长回文子串(Longest Palindromic Substring)一个「对称」的序列,就可称为回文序列,譬如:aba,abba 等。详细介绍参看:http://zh.wikipedia.org/wiki/%E5%9B%9E%E6%96%87%E6%95%B0最长回文子串问题是要求在给出的一个序列中,找到最长的回文字串。譬如:一个序列 cabccba,它的最长回文子串是 abccba。暴力暴力穷举可以解决问题。三个循环穷举所有可能的序列。?123fori in range(0,len(str))forj in range(i,len(str)is_palindromic_number(i 阅读全文
-
二分思想与分治法、排序思想
摘要:二分思想与分治法、排序思想二分查找是《编程珠玑》作者很喜爱的一个话题,之前我曾经专门写了一篇博文:如何写出正确的二分查找?——利用循环不变式理解二分查找及其变体的正确性以及构造方式,在这里将换几个角度,继续探讨二分查找的相关内容,以及与它联系紧密的分治法和排序思想。 目录二分思想和分治法在O(n)时间内从数组x[0...n-1]中找出第k个最小的元素给定一个最多包含40亿个随机排列的32位整数的顺序文件,找出一个不在文件中的32位整数扩展:给定包含43亿个32位整数,找出至少出现两次的整数排序思想给定一个英语字典,找出所有变位词集合给定一个n元实数集合、一个实数t和一个整数k,确定是否存... 阅读全文
-
A*算法&博弈树α-β剪枝
摘要:A*算法&博弈树α-β剪枝A*算法/博弈树 前阵子考试学了A*算法、博弈树和回溯,自己真是愚蠢至极,根本没就搞明白这些,所以对于这些算法问道的话就不能说清楚,也记不住,所以才有了这篇笔记。在这里感谢面试我的那位工程师~~ A*算法一些重要的概念 启发式信息:用于帮助减少搜索量的与问题有关的信息或知识。 启发式搜索:使用启发信息指导的搜索过程叫做启发式搜索。 估价函数:定义在状态空间上的实值函数。 open表:未扩展的节点 close表:已扩展或正在扩展的节点用f(n)表示节点n的估价函数: 1. f(n)表示从起点到目标,经由节点n最小费用路径上费用的估计。(最短路径 = 目前最... 阅读全文
-
折半查找
摘要:折半查找折半查找(二分查找)对处理元素要求有序顺序存储结构基本思想就是每次查找中间元素和待查找元素比较如果相同,返回该位置不同,且中间元素大于待查元素,则从右半边查找不同,且中间元素小于待查元素,则从左半边查找注意:如果前边标记大于小于后边标记了,那就说明没找到两种思路递归#include int erfen_d(int *a, int begin, int end, int value){ if (end >= begin) { int mid = (begin + end) / 2; if (a[mid] == value) r... 阅读全文
-
【算法剖析】求字符串中无重复字符的最长字串
摘要:【算法剖析】求字符串中无重复字符的最长字串1、问题描述 这个问题来自leetcode中的Longest Substring Without Repeating Characters,诚如标题所述,我们需要寻找的是在一个字符串中,没有重复字符的最长字串。我们假定字符串中的字符只由a~z这26个字符构成。例如,对于字符串"abcabcbb",它的无重复字符最长字串是"abc",长度为3;对于字符串"bbbb",它的无重复最长字串是b,长度为1。2、算法一 我们能够立即想到的,最原始的算法就是,从字符串的每一个位置开始构造字串,并逐渐增大字 阅读全文
-
杨辉三角形II(Pascal's Triangle II)
摘要:杨辉三角形II(Pascal's Triangle II)问题给出一个索引k,返回杨辉三角形的第k行。例如,给出k = 3,返回[1, 3, 3, 1]注意:你可以优化你的算法使之只使用O(k)的额外空间吗?初始思路首先来复习复习杨辉三角形的性质(来自wiki):杨辉三角以正整数构成,数字左右对称,每行由1开始逐渐变大,然后变小,回到1。第行的数字个数为个。第行的第个数字为组合数。第行数字和为。除每行最左侧与最右侧的数字以外,每个数字等于它的左上方与右上方两个数字之和(也就是说,第行第个数字等于第行的第个数字与第个数字的和)。这是因为有组合恒等式:。可用此性质写出整个杨辉三角形。看到第 阅读全文
-
三角形(Triangle)
摘要:三角形(Triangle)问题给出一个三角形,找出从顶部至底部的最小路径和。每一步你只能移动到下一行的邻接数字。例如,给出如下三角形:[ [2], [3,4], [6,5,7], [4,1,8,3]]从顶部至底部的最小路径和为11(即2+3+5+1=11)。注意:加分项-如果你能只使用O(n)的额外空间,n为三角形中的总行数。初始思路最直接的思路就是把路径都走一遍。即从顶点出发,分别往左中右移动(如果可能的话);然后对走到的位置继续进行同样移动,直到走到最后一行。这样就可以得到一个递归的方案,而递归的结束条件就是前面所说的走到最后一行。伪代码如下:[最短路径长度] 查找路径(当前节点坐标,.. 阅读全文
-
随机数函数取样与概率
摘要:随机数函数取样与概率本节主要受到《编程珠玑》第12章随机取样问题的启发,但不仅仅限于随机取样问题,进一步地,研究讨论了一些在笔试面试中常见的和随机函数以及概率相关的问题。 阅读本文所需的知识: 1.对C语言中或其他语言中等价的rand()、srand()有所了解。本文不讨论种子的设定和伪随机数的问题; 2.中学或以上水平的概率基本概念。 目录利用随机数函数生成随机数利用随机数函数产生随机事件取样问题:从n个元素中选取m个从概率角度出发从集合插入出发从“打乱顺序”出发从一般到特殊取样问题:从未知总数的元素中选择一个概率问题选编利用随机数函数生成随机数问题1(《编程珠玑》习题12.... 阅读全文