随笔分类 - [41]Algorithm算法
-
寻找两个已序数组中的第k大元素
摘要:寻找两个已序数组中的第k大元素1、问题描述 给定两个数组与,其大小分别为、,假定它们都是已按照增序排序的数组,我们用尽可能快的方法去求两个数组合并后第大的元素,其中,。例如,对于数组,。我们记第大的数为,则时,。这是因为排序之后的数组,第4大的数是4。我们针对这一个问题进行探讨。2、算法一 第一眼看到这个题的时候,我们能够很快地想出来最基本的一种解法:对数组和进行合并,然后求出其第大的数,即找到答案。合并的过程,我们可以参考归并排序的合并子数组的过程,时间复杂度为。下面给出算法:View Codeint findKthMaxNumOfArrays(int *a,int m,int *b,i.. 阅读全文
-
逆序对:从插入排序到归并排序
摘要:逆序对:从插入排序到归并排序设A[1..n]是一个包含N个非负整数的数组。如果在iA[j],则(i,j)就称为A中的一个逆序对(inversion)。a)列出数组[2,3,8,6,1]的5个逆序。b)如果数组的元素取自集合{1,2,...,n},那么,怎样的数组含有最多的逆序对?它包含多少个逆序对?c)插入排序的运行时间与输入数组中逆序对的数量之间有怎样的关系?说明你的理由。d)给出一个算法,它能用O(nlogn)的最坏情况运行时间,确定n个元素的任何排列中逆序对的数目(提示:修改归并排序) ——《算法导论》,思考题2-4 逆序对的应用很多,比如各类OJ中的... 阅读全文
-
谱聚类(Spectral Clustering)详解
摘要:谱聚类(Spectral Clustering)详解谱聚类(Spectral Clustering, SC)是一种基于图论的聚类方法——将带权无向图划分为两个或两个以上的最优子图,使子图内部尽量相似,而子图间距离尽量距离较远,以达到常见的聚类的目的。其中的最优是指最优目标函数不同,可以是割边最小分割——如图1的Smallest cut(如后文的Min cut), 也可以是分割规模差不多且割边最小的分割——如图1的Best cut(如后文的Normalized cut)。图1 谱聚类无向图划分——Smallest cut和Best cut 这样,谱聚类能够识别任意形状的样本空间且收敛于全局最优解 阅读全文
-
[珠玑之椟]估算的应用与Little定律
摘要:[珠玑之椟]估算的应用与Little定律估算的数据主要依赖于所能获得的数据和常识,有时还包括实践而不仅仅是理论。它常常作为一个大问题中的子问题,恰当地估算可以省去精确计算的时间和开销。在计算机领域,所谓常识的内容很宽泛,比如硬盘的传输速度、CPU每秒能执行多少指令、各种数据结构的大小甚至每分钟录入的单词数。有些数据是能够从各种资料中查得的,但仅仅靠记忆总难免遗漏;如果有经过学习而建立起的系统的知识结构,那便能很方便地把这些常识组织起来,除此以外,还可以靠平时经验的积累和一些面试题上的启发了。这里将进行一个收集,随时更新。 Little定律深入了估算所依赖的法则的细节:总花费等于各个部分的花费. 阅读全文
-
珠玑之椟
摘要:[珠玑之椟]位向量/位图的定义和应用 位向量/位图是一个很有用的数据结构,在充分利用小空间存储大量数据方面非常具有优势,Linux内核中很多地方都是用了位图。同时,它不但基础,而且用到了很多编程语言的知识,以及对细节的把握,常常作为面试题出现。这里将要介绍它的实现、操作、应用。 与位图(bitmap)比,我更倾向于用位向量(bit vector),前者比较容易与图形学里的名词混淆,其实提到位图,多指的是“是使用像素阵列来表示的图像”(维基百科),为了避免这一点,下文中使用位向量。先来看看产生位向量的需求: 一般地,对于多个对象和一个性质,这些对象可能满足(true)也可能不满足(fals... 阅读全文
-
简单的理解deflate算法
摘要:简单的理解deflate算法最近做压缩算法. 用到了deflate压缩算法, 找了很多资料, 这篇文章算是讲的比较易懂的,这篇文章不长,但却浅显易懂, 基本上涵盖了我想要知道的所有要点. 翻译出来, 留存. 可能对正在学习或者准备学习deflate算法的童鞋有所帮助.先说一下deflate算法吧. deflate是zip压缩文件的默认算法. 其实deflate现在不光用在zip文件中, 在7z, xz等其他的压缩文件中都用. 实际上deflate只是一种压缩数据流的算法. 任何需要流式压缩的地方都可以用. deflate 还有微型改进版本deflate64. “微型改进型” 这个... 阅读全文
-
指针参数传递、引用的陷阱
摘要:二叉树算法引发的思考:指针参数传递、引用的陷阱最近想熟悉一下基本的数据结构和算法,于是写了一个二叉树程序,功能很简单,只有二叉树的建立和遍历。在这个过程中,却发现了一些平时没有注意到的细节问题,写出来作为总结和大家分享。待讨论和遇到的细节问题如下:(1)常见的定式思维:指针作为参数传递,是否就不需要再赋值,因为指针指向的值会同步更改,但是,你有考虑过,如果指针变量本身的值被更改了呢?怎么办?(2)比较不常用的用法,针对指针变量的引用,你用过吗?前提:(1)笔者考虑到自己一直混淆使用C和C++,于是,打算这个简单的算法程序采用纯C来写,因此使用的是C编译器。(2)关于C和C++的一些区别,笔者打 阅读全文
-
算法实践——数独的基本解法
摘要:算法实践——数独的基本解法数独(Sudoku)是一种运用纸、笔进行演算的逻辑游戏。玩家需要根据9×9盘面上的已知数字,推理出所有剩余空格的数字,并满足每一行、每一列、每一个粗线宫内的数字均含1-9,不重复。 每一道合格的数独谜题都有且仅有唯一答案,推理方法也以此为基础,任何无解或多解的题目都是不合格的。如下图所示,就是一个数独的题目关于数独的详细介绍,参看“百度百科——数独”数独的基本解法就是利用规则的摒弃法一些定义每一行称为数独的行,每一列称为数独的列,每一个小九宫格称为数独的宫。数独的基本规则就是每一行、每一列、每一宫中,1-9这9个数字都只出现一次。用(行,列)表示上图的单元格 阅读全文
-
一道数学运算题
摘要:一道数学运算题事由是同事在工作中遇到的一个小问题,然后发到群里大家研究出来原因到底在哪里,问题是这样的:int? a = 2;int? b = 1;var res1= ((a ?? 0) + (b ?? 0)); var res2 = (a ?? 0 + b ?? 0); 求res1,res2的值?我相信,大部分程序员都会有这样的答案,都会说3,但是知道肯定不会都是3这么简单的,于是我把它封装成一个类里的两个方法。public class Class1 { public void A() { int? a = 2; ... 阅读全文
-
BST&AVL&红黑树简单介绍
摘要:(BST&AVL&红黑树简单介绍)前言: 节主要是给出BST,AVL和红黑树的C++代码,方便自己以后的查阅,其代码依旧是data structures and algorithm analysis in c++ (second edition)一书的作者所给,关于这3中二叉树在前面的博文算法设计和数据结构学习_4(《数据结构和问题求解》part4笔记)中已经有所介绍。这里不会去详细介绍它们的实现和规则,一是因为这方面的介绍性资料超非常多,另外这3种树的难点都在插入和删除部分,其规则本身并不多,但是要用文字和图形解释其实还蛮耗时的。所以,我们在看教程时,主要是要抓住这几种树的思 阅读全文
-
求数组子序列的最大和
摘要:求数组子序列的最大和一、问题描述输入一个整形数组,数组里可以有正数或负数。数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和。求所有子数组的和的最大值。要求时间复杂度为O(n)。 例如输入的数组为1, -2, 3, 10, -4, 7, 2, -5,和最大的子数组为3, 10, -4, 7, 2,因此输出为该子数组的和18。第一次遇到这道题是参加x迅的笔试。题目中给出了两种解法,让填空。二、简单解拿到这道题,如果不考虑性能和复杂度,最简单的方法就是穷举。穷举出所有的子数组,并求出他们的和,返回最大值。不过,复杂度为O(n3),不符合题目的要求(复杂度On)int max_sum(i 阅读全文
-
普林斯顿大学算法公开课(1)----介绍
摘要:课程概况这个课程是什么?(1)中级研究课程。(2)编程解决问题。(3)算法:解决问题的思路方法。(4)数据结构:存储信息的方法。课程分为两个部分为什么学习算法?算法的影响力是宽广和深远的。影响的领域不完全列表如下。(1)网络。包括搜索,包路由,分布式共享文件。(2)生物。包括基因工程,蛋白质折叠。(3)计算机。电路草图,文件系统,编译器。(4)计算机图形图像。电影,电子游戏,虚拟现实。(5)安全。手机,电子商务,投票计算机。(6)多媒体。mp3,jpg,divx,hdtv,人脸识别。(7)社会网络。推荐系统,新闻feeding,广告学。(8)物理学。n体模拟,粒子碰撞模拟。针对个人,公司,学习 阅读全文
-
非等值折半查找
摘要:非等值折半查找折半查找也就是二叉查找,其查找时间复杂度为O(logn),比顺序查找的效率高得多,唯一的要求就是待查表已经有序。1、等值折半查找比较简单,算法如下:def binarySearch(data,value): low = 0 high = len(data) - 1 while low <= high: middle = (high-low) / 2 + low#这个处理可以防止整数相加溢出 if data[middle] == value: return middle #找到,返回下标 if da... 阅读全文
-
高斯过程简单理解
摘要:机器学习&数据挖掘笔记_10(高斯过程简单理解) 前言: GP(高斯过程)是一种自然界中普遍存在且重要的随机过程,也叫正态随机过程,在ML等领域应用比较广泛。本次实验目的是简单理解下GP,特别是要体验到GP的一个sample不再是一个普通的点,而是一个函数。实验部分完成了常见的GP的一维和二维sample的显示,常见的GP有线性GP,布朗运动,指数GP,Ornstein-Uhlenbeck过程,对称和周期性的GP。参考资料全为视频http://www.youtube.com/playlist?list=PLD0Z06AA0D2E8ZZBA中相关的部分。 实验基础: 首先来看看GP的定义 阅读全文
-
求数组的子数组之和的最大值
摘要:一个有N个整数元素的一维数组(A[0],A[1],...,A[n-2],A[n-1]),这个数组当然有很多子数组,那么子数组之和的最大值是什么呢?【解法一】:我们先明确题意。 1、题目说的子数组是连续的; 2、题目只需要求和,并不需要返回子数组的具体位置; 3、数组的元素是整数,所以数组可能包含有正整数、零、负整数;举几个例子: 数组:[1, -2, 3, 5, -3, 2]应返回:8 数组:[0, -2, 3, 5, -1, 2]应返回:9 数组:[-9, -2, -3, -5, -3]应返回:-2,这也是最大子数组的和。 这个几个典型的输入能帮助我们测试算法的逻辑。在写具体算... 阅读全文
-
泛型KMP算法
摘要:泛型KMP算法当我们需要从一个字符串(主串)中寻找一个模式串(子串)时,使用KMP算法可以极大地提升效率。KMP是一个高效的字符串匹配算法,它巧妙的消除了在匹配的过程中指针回溯的问题,关于KMP算法的更多介绍,可以参考这里。原始的KMP算法适用的对象是字符串的匹配搜索,其实针对任意类型的串(实际上就是一个数组)的子串搜索,都可以使用KMP算法。比如,我们可能需要在byte[]中查找一个特定的字节数组,这同样可以使用KMP算法来提升匹配性能。为此,我实现了泛型的KMP算法,使之可以应用于任意类型的串匹配。下面是该算法的完整实现。 /// <summary> /// 泛型KMP算法。 阅读全文
-
不用中间变量交换两个整型变量的值
摘要:不用中间变量交换两个整型变量的值在我们平常的变量值交换方法中,我们一般都是采用中间变量的方法来实现变量值在两个变量之间互换。但是对于整型变量,我们可以用其他更多的方法来实现变量值互换,比如整型变量a,b在不利用中间变量的情况下,我们可以用:a=a+b; b=a-b; a=a-b;来实现变量值互换。在这里我们介绍一种更加快速的无中间变量的交换方法,即利用异或操作来实现变量值互换。主要代码如下:a = a^b;b = a^b;a = a^b;由于异或运算满足结合律和交换律,所以我们可以很容易得出上述等式是成立的。而且用于在计算机中位运算操作明显要比加减操作更快,所以上面这种方法更快。以下就贴出完整 阅读全文
-
强大的二分查找法(Binary Search)
摘要:今天帮助David 调程序,他的程序是做Windows下目标程序分析,其中有个数组存放目标程序heap中所有数据结构,David的程序遍历目标程序全局数据段所有内容,如果某个值落在heap中某个数据结构地址范围内,就把它找出来,并建立从全局数据段到该数据结构的一个路径。David给我演示时, 说运行Toy program 没问题,但运行一个比较大的真实程序时,就会出现性能问题,果然,我们等了二十多分钟,还是没有算完。于是我检查了他的代码,告诉他问题可能出在检查指针是否指向有效数据结构时,他是用的线性查找方法,如果试着使用二分查找法,可能会快很多,因为算法复杂度从O(N)降到了O(logN)。没 阅读全文
-
渐近符号、递归及解法
摘要:渐近符号、递归及解法这节课,大概讲了一些符号的用法,毕竟偏数学化,没有涉及算法的知识。我也参考了下别人的笔记,本节课内容不是太多,主要是符号表示和递归的复杂度求解方式,下面分2个部分讲解。 一,渐进符号 (1)O符号,f(n) = O(g(n)),表示f(n)的复杂度最多与g(n)一个数量级,即小于等于。 (2)Ω符号,f(n) = Ω(g(n)),f(n)的复杂度最少与g(n)一个数量级,即大于等于。 (3)o符号,f(n) = o(g(n)),表示f(n)的复杂度要比g(n)的数量级小,即小于。 (4)ω符号,f(n) = ω(g(n)),表示f(n)的复杂度要比g(n)的数量级... 阅读全文
-
算法介绍
摘要:麻省理工算法导论学习笔记(1)----算法介绍 我决定啃几个大块头,今天开始跟着公开课学习,一是巩固自己对算法理解,二是可以分享学习心得。 普林斯顿大学的算法课好像也讲完了,只有讲义,好像还没视频,下面我先学麻省理工的算法导论课。 我是一个懒孩子,希望大家监督我。没有监督,俺继续不下去,呃哈哈。谢谢。 为什么学习算法? (1)什么比性能更重要?正确性,简洁,可维护性,成本开销,功能性,模块化,安全,可扩展性,用户体验等。 (2)如果算法和性能不重要,为啥关注呢?为什么学习?性能与体验关联,实时性,性能是其他的基础,性能就像货币,你用java来代替c,损失三倍性能,为的是其他方面的优越... 阅读全文