3. Algorithms

3.1 Introduction to Algorithm

In many domains there are key general problems that ask for output with specific properties when given valid input.
To precisely state the problem, using the appropriate structures to specify the input and the desired output. Solve the general problem by specifying the steps of a procedure that takes a valid input and produces the desired output. This procedure is called an algorithm.

3.1.1 Basic Concepts

**Algorithms**: An algorithm is a finite set of precise instructions for performing a computation or for solving a problem.
**Pseudocode**: An intermediate step between an English language description of the steps and a coding of these steps using a programming language.
⚠️ Algorithms can be specified in different ways. Their steps can be described in English or in pseudocode. Pseudocodes are clearer to read than natural languages and easier to understand than programming languages. Moreover, pseudocodes are independent from any specific programming languages, so it is more transplantable.
**Input**: An algorithm has input values from a specified set.
**Output**: From the input values, the algorithm produces the output values from a specified set. The output values are the solution.
And the followings are properties we expect algorithms to have:

  • **Definiteness**: The steps of an algorithm must be defined precisely.
  • **Correctness**: An algorithm should produce the correct output values for each set of input values.
  • **Finiteness**: An algorithm should produce the output after a finite number of steps for any input.
  • **Effectiveness**: It must be possible to perform each step of the algorithm in a finite amount of time.
  • **Generality**: The algorithm should work for all problems of the desired form.

    3.1.2 Example Algorithm Problems

    **Algorithmic Paradigms**: A general approach based on a particular concept that can be used to construct algorithms for solving a variety of problems.

  • Greedy algorithm

  • Brute-force algorithm
  • Divide-and-conquer algorithms
  • Dynamic programming
  • Backtracking
  • Probabilistic algorithms

📋 Searching Problems
Locate an element x in a list of finite distinct elements 离散数学及其应用 第3章 算法 - 图1, or determine that it is not in the list.

  • Linear Search Algorithm in Pseudocode

%ZT_XMN%IGGGA7Y@ULS7S4T.png

  • Binary Search Algorithm in Pseudocode

AX_0VCE%3W950`Z9K]GUDVU.png
The binary search algorithm iteratively restricts the relevant search interval until it closes in on the position of the element to be located. It is more efficient than linear search, but it only applies to searching increasing sequences.
📋 Sorting Problems
To sort the elements of a list is to put them in increasing order (numerical order, alphabetic, and so on). It is an important problem with many different algorithms for various situations.

  • Bubble Sort Algorithm

image.png

  • Insertion Sort Algorithm

ED2]UET1Y~5G(3YR%1}`_1T.png
📋 Optimization Problems
Minimize or maximize some parameter over all possible inputs.

  • Greedy Algorithms

Make the “best” choice at each step.
Making the “best choice” at each step does not necessarily produce an optimal solution to the overall problem, but in many instances, it does. It requires proof that the local best choices can lead to overall optimal solutions in the specific case before using the greedy algorithm.

3.2 The Growth of Functions

In computer science, we want to understand how quickly an algorithm can solve a problem as the size of the input grows, so that we can compare the efficiency of two different algorithms for solving the same problem and determine whether it is practical to use a particular algorithm as the input grows.

3.2.1 Big-O Notations

**Big-O Notations** Let 离散数学及其应用 第3章 算法 - 图6 and 离散数学及其应用 第3章 算法 - 图7 be functions. We say that 离散数学及其应用 第3章 算法 - 图8, read as “离散数学及其应用 第3章 算法 - 图9 is big-oh of 离散数学及其应用 第3章 算法 - 图10“ if there exist constants 离散数学及其应用 第3章 算法 - 图11 and 离散数学及其应用 第3章 算法 - 图12 such that 离散数学及其应用 第3章 算法 - 图13 whenever 离散数学及其应用 第3章 算法 - 图14.
The constants 离散数学及其应用 第3章 算法 - 图15 and 离散数学及其应用 第3章 算法 - 图16 are called **witnesses** to the relationship 离散数学及其应用 第3章 算法 - 图17 is 离散数学及其应用 第3章 算法 - 图18. Only one pair of witnesses is needed to show 离散数学及其应用 第3章 算法 - 图19, and there are infinite pairs of witnesses if there is a pair.
If 离散数学及其应用 第3章 算法 - 图20 and 离散数学及其应用 第3章 算法 - 图21, we say that the two functions are of the **same order**.
For many applications, the goal is to select the function 离散数学及其应用 第3章 算法 - 图22 in 离散数学及其应用 第3章 算法 - 图23 as small as possible (up to multiplication by a constant, of course).
📋 If 离散数学及其应用 第3章 算法 - 图24 and 离散数学及其应用 第3章 算法 - 图25 is larger than 离散数学及其应用 第3章 算法 - 图26 when 离散数学及其应用 第3章 算法 - 图27 is sufficiently large, then 离散数学及其应用 第3章 算法 - 图28.

3.2.2 Big-O Estimates for some Important Functions

image.png
Other functions are usually represented as big-O of these functions.
🌰 e.g.
Use big-O notation to estimate the sum of the first 离散数学及其应用 第3章 算法 - 图30 positive integers.
Solution:
离散数学及其应用 第3章 算法 - 图31
So 离散数学及其应用 第3章 算法 - 图32 is 离散数学及其应用 第3章 算法 - 图33 taking 离散数学及其应用 第3章 算法 - 图34.
📋 Big-O Estimates for Polynomials
Let 离散数学及其应用 第3章 算法 - 图35, where 离散数学及其应用 第3章 算法 - 图36 are real numbers, then 离散数学及其应用 第3章 算法 - 图37. The leading term of a polynomial 离散数学及其应用 第3章 算法 - 图38 dominates its growth.
Proof:
H~P6ZG8N24)A(OQBDISQ8(1.png
📋 Useful Big-O Estimates Involving Logarithms, Powers, and Exponents

  • If 离散数学及其应用 第3章 算法 - 图40, then 离散数学及其应用 第3章 算法 - 图41, but 离散数学及其应用 第3章 算法 - 图42 is not 离散数学及其应用 第3章 算法 - 图43.
  • If 离散数学及其应用 第3章 算法 - 图44 and 离散数学及其应用 第3章 算法 - 图45 and 离散数学及其应用 第3章 算法 - 图46 are positive, then 离散数学及其应用 第3章 算法 - 图47, but 离散数学及其应用 第3章 算法 - 图48 is not 离散数学及其应用 第3章 算法 - 图49.
    • Every positive power of the logarithm of 离散数学及其应用 第3章 算法 - 图50 to the base 离散数学及其应用 第3章 算法 - 图51, where 离散数学及其应用 第3章 算法 - 图52, is big-O of every positive power of 离散数学及其应用 第3章 算法 - 图53, but the reverse relationship never holds.
  • If 离散数学及其应用 第3章 算法 - 图54 and 离散数学及其应用 第3章 算法 - 图55 is positive, then 离散数学及其应用 第3章 算法 - 图56 is 离散数学及其应用 第3章 算法 - 图57, but 离散数学及其应用 第3章 算法 - 图58 is not 离散数学及其应用 第3章 算法 - 图59.
    • Every power of 离散数学及其应用 第3章 算法 - 图60 is big-O of every exponential function of 离散数学及其应用 第3章 算法 - 图61 with a base that is greater than one, but the reverse relationship never holds.
  • If 离散数学及其应用 第3章 算法 - 图62, then 离散数学及其应用 第3章 算法 - 图63, but 离散数学及其应用 第3章 算法 - 图64 is not 离散数学及其应用 第3章 算法 - 图65.
    • if we have two exponential functions with different bases greater than one, one of these functions is big-O of the other if and only if its base is smaller or equal.

📋The Growth of Combinations of Functions

  • If 离散数学及其应用 第3章 算法 - 图66 is 离散数学及其应用 第3章 算法 - 图67 and 离散数学及其应用 第3章 算法 - 图68 is 离散数学及其应用 第3章 算法 - 图69, then 离散数学及其应用 第3章 算法 - 图70 is 离散数学及其应用 第3章 算法 - 图71.
  • If 离散数学及其应用 第3章 算法 - 图72 and 离散数学及其应用 第3章 算法 - 图73 are both 离散数学及其应用 第3章 算法 - 图74, then 离散数学及其应用 第3章 算法 - 图75 is 离散数学及其应用 第3章 算法 - 图76.

Proof:
image.png

3.2.3 Other Notations

**Big-Omega Notation**: Let 离散数学及其应用 第3章 算法 - 图78 and 离散数学及其应用 第3章 算法 - 图79 be functions. We say that 离散数学及其应用 第3章 算法 - 图80, read as “离散数学及其应用 第3章 算法 - 图81 is big-omega of 离散数学及其应用 第3章 算法 - 图82“, if there are constants 离散数学及其应用 第3章 算法 - 图83 and 离散数学及其应用 第3章 算法 - 图84 such that 离散数学及其应用 第3章 算法 - 图85 whenever 离散数学及其应用 第3章 算法 - 图86.
离散数学及其应用 第3章 算法 - 图87. This follows from the definitions.
⚠️ Big-O gives an upper bound on the growth of a function, while Big-Omega gives a lower bound. Big-O tells us that a function grows no faster than another, while Big Omega tells us that a function grows at least as fast as another.
**Big-Theta Notation**: Let 离散数学及其应用 第3章 算法 - 图88 and 离散数学及其应用 第3章 算法 - 图89 be functions. We say that 离散数学及其应用 第3章 算法 - 图90, read as “离散数学及其应用 第3章 算法 - 图91 is big-theta of 离散数学及其应用 第3章 算法 - 图92“, if and only if there exists constants 离散数学及其应用 第3章 算法 - 图93, 离散数学及其应用 第3章 算法 - 图94 and 离散数学及其应用 第3章 算法 - 图95 such that 离散数学及其应用 第3章 算法 - 图96 whenever 离散数学及其应用 第3章 算法 - 图97. That is, 离散数学及其应用 第3章 算法 - 图98.
⚠️ Sometimes writers are careless and write as if big-O notation has the same meaning as big-Theta.
📋 Big-Theta Estimates for Polynomials
Let 离散数学及其应用 第3章 算法 - 图99, where 离散数学及其应用 第3章 算法 - 图100 with 离散数学及其应用 第3章 算法 - 图101. Then 离散数学及其应用 第3章 算法 - 图102 is of order 离散数学及其应用 第3章 算法 - 图103 or 离散数学及其应用 第3章 算法 - 图104.
image.png
📋(不考,是课外积累的) Stirling’s Approximation
离散数学及其应用 第3章 算法 - 图106, or more precisely 离散数学及其应用 第3章 算法 - 图107.
Proof:
6BJ@V)BH2RXL)53]RN49ORG.png

3.3 How to Measure Complexity of Algorithms

An algorithm provides a satisfactory solution when the output is correct and efficient. The efficiency is implied by time complexity and space complexity. In this course, we focus on time complexity, and space complexity of algorithms is studied in later courses.
We measure time complexity in terms of the number of operations an algorithm uses, such as comparisons and arithmetic operations (addition, multiplication, etc.). Big-O and big-Theta notations are applied.
We will focus on the **worst-case time complexity** of an algorithm. This provides an upper bound on the number of operations an algorithm uses to solve a problem with an input of a particular size.
⚠️ It is usually much more difficult to determine the **average case time complexity** of an algorithm, because we often have difficulty defining what is the average case.
🌰 e.g. Worst-Case Complexity of Linear Search
Determine the time complexity of the linear search algorithm.

  1. procedure linear search(x:integer, a1, a2, …,an: distinct integers)
  2. i := 1
  3. while (i n and x ai)
  4. i := i + 1
  5. if i n then location := i
  6. else location := 0
  7. return location
  8. {location is the subscript of the term that equals x, or is 0 if x is not found}

Solution:
Count the number of comparisons.
At each step two comparisons are made; 离散数学及其应用 第3章 算法 - 图109 and 离散数学及其应用 第3章 算法 - 图110 .
To end the loop, one comparison 离散数学及其应用 第3章 算法 - 图111 is made.
After the loop, one more 离散数学及其应用 第3章 算法 - 图112 comparison is made.
If 离散数学及其应用 第3章 算法 - 图113, 离散数学及其应用 第3章 算法 - 图114 comparisons are used. If 离散数学及其应用 第3章 算法 - 图115 is not on the list, 离散数学及其应用 第3章 算法 - 图116 comparisons are made and then an additional comparison is used to exit the loop. So, in the worst case, 离散数学及其应用 第3章 算法 - 图117 comparisons are made.
Hence, the complexity is 离散数学及其应用 第3章 算法 - 图118.