Design and Analysis of Algorithms_Fundamentals of the Analysis of Algorithm Efficiency_Pseudocode

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[0..n-1], 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 <- 0
    while i < n and A[i] ≠ K do
        i <- i+1
    if i < n return i
    else return -1

Algorithm MaxElement(A[0..n-1])
    // 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[0]
    for i <- 1 to n-1 do
        if A[i] > maxval
            maxval <- A[i]
    return maxval

Algorithm UniqueElements(A[0..n-1])
    // 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 <- 0 to n-2 do
        for j <- i+1 to n-1 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 <- 0 to n-1 do
        for j <- 0 to n-1 do
            C[i, j] <- 0.0
            for k <- 0 to n-1 do
                C[i, j] <- C[i, j] + A[i, k]*B[k, j]
    return CImprove 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,0]*B[0,j]
    for k <- 1 to n-1 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 <- 1
    while n > 1 do
        count <- count + 1
        n <- ⌊n/2⌋ 
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 = 1 return 1
    else return BinRec(⌊n/2⌋) + 1
Algorithm Mystery(n)
    // Input: A nonnegative integer n
    S <- 0
    for i <- 1 to n do
        S <- S + i*i
    return S

Algorithm Secret(A[0..n-1])
    // Input: An array A[0..n-1] of n real numbers
    minval <- A[0]; maxval <- A[0]
    for i <- 1 to n-1 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[0..n-1, 0..n-1])
    // Input: A matrix A[0..n-1, 0..n-1] of real numbers
    for i <- 0 to n-2 do
        for j <- i+1 to n-1 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 = 0 return 1
    else return F(n-1)*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 = 1 return 1
    else return S(n-1) + 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 <- 1
    for i <- 2 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 = 0 return 1
    else return Power(n-1) + Power(n-1)

The following algorithm computes the value of the smallest element in a given array.
Algorithm Min1(A[0..n-1])
    // Input: An array A[0..n-1] of real numbers
    if n = 1 return A[0]
    else temp <- Min1(A[0..n-2])
        if temp ≤ A[n-1] return temp
        else return A[n-1]
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)/2⌋])         temp2 <- Min2(A[⌊(l+r)/2⌋+1..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 ≤ 1 return n
    else return F(n-1) + F(n-2)

Algorithm Fib(n)
    // Computes the nth Fibonacci number iteratively by using its definition
    // Input: A nonnegative integer n
    // Output: The nth Fibonacci number
    F[0] <- 0; F[1] <- 1
    for i <- 2 to n do
        F[i] <- F[i-1] + F[i-2]
    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 <- 0; v <- 1
    for i <- 2 to n do
        v <- v + u
        u <- v - u
    if n = 0 return 0
    else return v

The following well-known sorting algorithm with a counter inserted to count the number of key comparisons.
Algorithm SortAnalysis(A[0..n-1])
    // Input: An array A[0..n-1] of n orderable elements
    // Output: The total number of key comparisons made
    count <- 0
    for i <- 1 to n-1 do
        v <- A[i]
        j <- i-1
        while j ≥ 0 and A[j] > v do
            count <- count+1
            A[j+1] <- A[j]
            j <- j-1
        if j ≥ 0 count <- count+1
        A[j+1] <- v
    return count

new words:
sequential: 顺序的 formula: 公式 symmetric: 对称的
cubic: 立方的 investigate: 调查;研究 halves: 两等份

(End_xpjiang).
时间: 2024-10-15 16:50:35

Design and Analysis of Algorithms_Fundamentals of the Analysis of Algorithm Efficiency_Pseudocode的相关文章

Analysis of Algorithms--preface

Analysis of Algorithms: First part of the course is focused on analysis. Second part of the course is focused on design. The analysis of algorithm is the theoretical study.(算法分析是理论研究) The theoretical study of computer-program performance and resource

时序分析Timing Analysis

以下内容来自Quartus II Handbook Version 13.1 Volume 3: Verification /Section II. Timing Analysis 6. Timing Analysis Overview Comprehensive static timing analysis involves analysis of register-to-register, I/O, and asynchronous reset paths. 综合的静态时序分析对象包括:寄存

Top 40 Static Code Analysis Tools

https://www.softwaretestinghelp.com/tools/top-40-static-code-analysis-tools/ In this article, I have summarised some of the top static code analysis tools. Can we ever imagine sitting back and manually reading each line of codes to find flaws? To eas

Data analysis system

A data analysis system, particularly, a system capable of efficiently analyzing big data is provided. The data analysis system includes an analyst server, at least one data storage unit, a client terminal independent of the analyst server, and a cach

x264源代码简单分析:宏块分析(Analysis)部分-帧间宏块(Inter)

本文记录x264的 x264_slice_write()函数中调用的x264_macroblock_analyse()的源代码.x264_macroblock_analyse()对应着x264中的分析模块.分析模块主要完成了下面2个方面的功能: (1)对于帧内宏块,分析帧内预测模式(2)对于帧间宏块,进行运动估计,分析帧间预测模式 上一篇文章记录了帧内宏块预测模式的分析,本文继续记录帧间宏块预测模式的分析. 函数调用关系图 宏块分析(Analysis)部分的源代码在整个x264中的位置如下图所示

【技术文档】使用ADOMD.NET建立与Analysis Services的连接

C#操作Analysis Services,AMO是Analysis Services的管理类的完整集合,可在托管环境中,在Microsoft.AnalysisServices命名空间下以编程方式使用. 这些类包含在 AnalysisServices.dll文件中,该文件通常位于SQL Server安装目录下的\100\SDK\Assemblies\文件夹中.使用AMO可以创建.修改和删除对象,如多维数据集.维度.挖掘结构以及Analysis Services数据库. 但需要注意的是:无法通过

Computational Methods in Bayesian Analysis

Computational Methods in Bayesian Analysis Computational Methods in Bayesian Analysis [Markov chain Monte Carlo][Gibbs Sampling][The Metropolis-Hastings Algorithm][Random-walk Metropolis-Hastings][Adaptive Metropolis] About the author This notebook w

多维建模 在 Analysis Services 项目中定义数据源视图(一)

我本机安装的mssql2012,在下图中我们看到SQL Server Data Tools(SSDT),制作SSRS报表.SSIS项目.及SSAS项目都可以在DataTools应用程序中操作.       新建项目后,已安装模板中有一个商业智能=>Analysis Services, 选择第一个Analysis Services多维和数据挖掘项目后,点击确定. 下面是一个新建的Analysis Services项目示例, 现在我们来创建数据源视图, 1.定义连接可以点击新建按钮 2.使用服务账户

Analysis Services OLAP 概述2

在DW/BI系统中,关系型数据库是存储和管理数据的最佳场所.但是关系数据库本身的智能化程度不够.关系型数据库缺乏如下功能: 丰富的元数据,帮助用户浏览数据和创建查询. 强大的分析计算和函数,在对上下文敏感的查询语句中定义. 各种即席查询中的杰出,一致的查询性能.   在Microsoft平台上,首选的体系结构是使用SSAS作为主要的展示数据库,在关系数据仓库上定义Analsis Services数据库时,就在创建这个丰富的元数据层,同时,还可以创建一个物理存储层,以包含聚合和索引,获得杰出的查询