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