Design and Analysis of Algorithms_Fundamentals of the Analysis of Algorithm Efficiency
I collect and make up this pseudocode from the book:
<<Introduction to the Design and Analysis of Algorithms_Second Edition>> _ Anany Levitin
Note that throughout the paper, we assume that inputs to algorithms fall within their specified ranges and hence require no verfication. When implementing algorithms as programs to be used in actual applications, you should provide such verfications.
About pseudocode: For the sake of simplicity, we omit declarations of variables and use indentation to show the scope of such statements as for, if and while. As you saw later, we use an arrow <- for the assignment operation and two slashes // for comments.
Algorithm SequentialSearch(A[..n-], k)
// Searches for a given value in a given array by sequential search
// Input: An array A[0..n-1] and a search key K
// Output: The index of the first element of A that matches K or -1 if there are no matching elements
i <-
while i < n and A[i] ≠ K do
i <- i+
if i < n return i
else return - Algorithm MaxElement(A[..n-])
// Determines the value of the largest element in a given array
// Input: An array A[0..n-1] of real numbers
// Output: The value of the largest element in A
maxval <- A[]
for i <- to n- do
if A[i] > maxval
maxval <- A[i]
return maxval Algorithm UniqueElements(A[..n-])
// Determines whether all the elements in a given array are distinct
// Input: An array A[0..n-1]
// Output: Returns "true" if all the elements in A are distinct and "false" otherwise
for i <- to n- do
for j <- i+ to n- do
if A[i] = A[j]
return false
return true Given two n-by-n matrices A and B, we hava a definition-based algorithm for computer their product C = AB. By definition , C is an n-by-n matrix whose elements are computed as the scalar(dot) products of the rows of matrix A and the columns of matrix B: where C[i, j] = A[i, 0]B[0, j] + ... + A[i, k]B[k, j] + A[i, n-1]B[n-1, j] for every pair of indices 0 ≤ i, j ≤ n-1.
Algorithm MatrixMultiplication(A[0..n-1, 0..n-1], B[0..n-1, 0..n-1])
// Multiplies two n-by-n matrices by the definition-based algorithm
// Input: Two n-by-n matrices A and B
// Output: Matrix C = AB
for i <- to n- do
for j <- to n- do
C[i, j] <- 0.0
for k <- to n- do
C[i, j] <- C[i, j] + A[i, k]*B[k, j]
return C
Improve the implementation of the matrix multiplication algorithm by reduce the number of addtions made by the algorithm. What effect will this change hava on the algorithm's efficiency?
Replace the body of j loop by follwing fragment:
C[i,j] <- A[i,]*B[,j]
for k <- to n- do
C[i,j] <- C[i,j] + A[i,k]*B[k,j]
This will decrease the number of additions from n^3 to n^3-n^2, but the number of multiplications will still be n^3. The algorithm's efficiency class will remain cubic.
The following algorithm finds the number of binary digits in the binary representation of a position decimal integer. The exact formula for the number of times the comparision n > 1 will be executed is actually ⌊log2n⌋ + 1
Algorithm Binary(n)
// Input: A position decimal integer n
// Output: The number of binary digits in n's binary representation
count <-
while n > do
count <- count +
n <- ⌊n/⌋
Then we investigate a recursive version of the algorithm:
Algorithm BinRec(n)
// Input: A positive decimal integer n
// Output: The number of binary digits in n's binary representation
if n = return
else return BinRec(⌊n/⌋) + 1
Algorithm Mystery(n)
// Input: A nonnegative integer n
S <-
for i <- to n do
S <- S + i*i
return S Algorithm Secret(A[..n-])
// Input: An array A[0..n-1] of n real numbers
minval <- A[]; maxval <- A[]
for i <- to n- do
if A[i] < minval
minval <- A[i]
if A[i] > maxval
maxval <- A[i]
return maxval - minval The following algorithm return "true" if its input matrix is symmetric and "false" if it is not.
Algorithm Enigma(A[..n-, ..n-])
// Input: A matrix A[0..n-1, 0..n-1] of real numbers
for i <- to n- do
for j <- i+ to n- do
if A[i,j] ≠ A[j,i]
return false
return true Algorithm F(n)
// Computers n! recursively
// Input: A nonnegative integer n
// Output: The value of n!
if n = return
else return F(n-)*n Consider the following recursive algorithm for computing the sum of the first n cubes:
Algorithm S(n)
// Input: A positive integer n
// Output: The sum of the first n cubes
if n = return
else return S(n-) + n*n*n
Here is a pseudocode for the nonrecursive option:
Algorithm NonrecS(n)
// Computes the sum of the first n cubes nonrecursively
// Input: A positive integer n
// Output: The sum of the first n cubes.
S <-
for i <- to n do
S <- S + i*i*i
return S
The number of multiplications made both of this two algorithm will be 2(n-1), but the nonrecursive version doesn't carry the time and space overhead associated with the recursion's stack. Algorithm Power(n)
// Computes 2^n recursively by the formula 2^n = 2^(n-1) + 2^(n-1)
// Input: A nonnegative integer n
// Output: Returns 2^n
if n = return
else return Power(n-) + Power(n-) The following algorithm computes the value of the smallest element in a given array.
Algorithm Min1(A[..n-])
// Input: An array A[0..n-1] of real numbers
if n = return A[]
else temp <- Min1(A[..n-])
if temp ≤ A[n-] return temp
else return A[n-]
Consider another algorithm for solving the problem, which recursively divides an array into two halves: call Min2(A[0..n-1])
Algorithm Min2(A[l..r])
if l = r return A[l]
else
temp1 <- Min2(A[l..⌊(l+r)/⌋])
temp2 <- Min2(A[⌊(l+r)/⌋+..r])
if temp1 ≤ temp2
return temp1
else
return temp2
We can prove that both of them for the number of key comparisons is n-1.
A simple standard scan through the array in question requires the same number of key comparisons while avoiding the overhead associated with recursive calls. It is clear, however, that any algorithm for this problem must be in Ω(n). Algorithm F(n)
// Computes the nth Fibonacci number recursively by using its definition
// Input: A nonnegative integer n
// Output: The nth Fibonacci number
if n ≤ return n
else return F(n-) + F(n-) Algorithm Fib(n)
// Computes the nth Fibonacci number iteratively by using its definition
// Input: A nonnegative integer n
// Output: The nth Fibonacci number
F[] <- ; F[] <-
for i <- to n do
F[i] <- F[i-] + F[i-]
return F[n]
Algorithm Fib2(n)
// Computes the n-th Fibonacci number using just two variables
// Input: A nonnegative integer n
// Output: The n-th Fibonacci number
u <- ; v <-
for i <- to n do
v <- v + u
u <- v - u
if n = return
else return v The following well-known sorting algorithm with a counter inserted to count the number of key comparisons.
Algorithm SortAnalysis(A[..n-])
// Input: An array A[0..n-1] of n orderable elements
// Output: The total number of key comparisons made
count <-
for i <- to n- do
v <- A[i]
j <- i-
while j ≥ and A[j] > v do
count <- count+
A[j+] <- A[j]
j <- j-
if j ≥ count <- count+
A[j+] <- v
return count new words:
sequential: 顺序的 formula: 公式 symmetric: 对称的
cubic: 立方的 investigate: 调查;研究 halves: 两等份 (End_xpjiang).
Design and Analysis of Algorithms_Fundamentals of the Analysis of Algorithm Efficiency的更多相关文章
- 【转载】Analysis Service Tabular Model #002 Analysis services 的结构:一种产品 两个模型
Analysis Service 2012 Architecture – One Product, Two Models 在之前SQL Server 2008 R2 版本中的分析服务实际上只有一个版本 ...
- 【Static Program Analysis - Chapter 3】Type Analysis
类型分析,个人理解就是(通过静态分析技术)分析出代码中,哪些地方只能是某种或某几种数据类型,这是一种约束. 例如,给定一个程序: 其中,我们可以很直接地得到一些约束: 最后,经过简化可以得到: 对 ...
- malware analysis、Sandbox Principles、Design && Implementation
catalog . 引言 . sandbox introduction . Sandboxie . seccomp(short for secure computing mode): API级沙箱 . ...
- PTPX中的time_based analysis
根据VCD文件的type,PTPX支持instantaneous peak power analysis和cycle_accurate peak power analysis. Time-Based ...
- Top 40 Static Code Analysis Tools
https://www.softwaretestinghelp.com/tools/top-40-static-code-analysis-tools/ In this article, I have ...
- Analysis Services OLAP 概述2
在DW/BI系统中,关系型数据库是存储和管理数据的最佳场所.但是关系数据库本身的智能化程度不够.关系型数据库缺乏如下功能: 丰富的元数据,帮助用户浏览数据和创建查询. 强大的分析计算和函数,在对上下文 ...
- Andrew Ng机器学习公开课笔记–Principal Components Analysis (PCA)
网易公开课,第14, 15课 notes,10 之前谈到的factor analysis,用EM算法找到潜在的因子变量,以达到降维的目的 这里介绍的是另外一种降维的方法,Principal Compo ...
- BA Practice Lead Handbook 1 - Why Is Business Analysis Taking The World By Storm?
The articles in this series are focused on individual Business Analysts and their managers. https:// ...
- Computational Methods in Bayesian Analysis
Computational Methods in Bayesian Analysis Computational Methods in Bayesian Analysis [Markov chain ...
随机推荐
- linux 下如何 makefile
本文目的: 尝试着把makefile讲解清楚.非原创,仅仅是学习笔记和备忘录之用. makefile 的目的和好处: 一个工程中的源文件不计数,其按类型.功能.模块分别放在若干个目录中,makefil ...
- jvm中的年轻代 老年代 持久代 gc
虚拟机中的共划分为三个代:年轻代(Young Generation).老年代(Old Generation)和持久代(Permanent Generation).其中持久代主要存放的是Java类的类信 ...
- 图文:通过sql server 连接mysql
1.在SQL SERVER服务器上安装MYSQL ODBC驱动; 驱动下载地址:http://dev.mysql.com/downloads/connector/odbc/ 2.安装好后,在管理工具- ...
- IntelliJ IDEA 14 注册码
IntelliJ IDEA 14 下载地址: IntelliJ IDEA 14 下载 分享几个license: (1) key:IDEA value:61156-YRN2M-5MNCN-NZ8D2-7 ...
- (iOS)Base64加密和DES加密、以及JAVA和iOS中DES加密统一性问题
我们在项目中为了安全方面的考虑,通常情况下会选择一种加密方式对需要安全性的文本进行加密,而Base64加密和DES64加密是常用的加密算法.我记得我在前一个项目中使用的就是这两种加密算法的结合:Bas ...
- 2016 Multi-University Training Contest 6
5/12 2016 Multi-University Training Contest 6 官方题解 打表找规律/推公式 A A Boring Question(BH) 题意: ,意思就是在[0,n] ...
- 关于过拟合、局部最小值、以及Poor Generalization的思考
Poor Generalization 这可能是实际中遇到的最多问题. 比如FC网络为什么效果比CNN差那么多啊,是不是陷入局部最小值啊?是不是过拟合啊?是不是欠拟合啊? 在操场跑步的时候,又从SVM ...
- android studio每次启动都要在fetching Android sdk compoment information停好久的解决方案
1)进入刚安装的Android Studio目录下的bin目录.找到idea.properties文件,用文本编辑器打开.2)在idea.properties文件末尾添加一行: disable.and ...
- ZeroMQ接口函数之 :zmq_msg_more - 指出是不是还有更多的消息部分可以接收
ZeroMQ 官方地址 :http://api.zeromq.org/4-2:zmq_msg_more zmq_msg_more(3) ØMQ Manual - ØMQ/3.2.5 Name zmq_ ...
- CentOS7 编译安装 Mariadb (实测 笔记 Centos 7.0 + Mariadb 10.0.15)
环境: 系统硬件:vmware vsphere (CPU:2*4核,内存2G,双网卡) 系统版本:CentOS-7.0-1406-x86_64-DVD.iso 安装步骤: 1.准备 1.1 显示系统版 ...