算法设计与实现 Algorithm Design and Implementation

时间:2021-09-15
本文章向大家介绍算法设计与实现 Algorithm Design and Implementation,主要包括算法设计与实现 Algorithm Design and Implementation使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

Overview

In this course, we mainly learned algorithm analysis and some classic algorithm ideas:

  • Algorithm analysis: time complexity and space complexity
  • Ideas: Brute Force, Divide and Conquer, Dynamic Programming

一、Algorithm Analysis

For an algorithm, we need to use some indicators to measure its performance, such as time complexity and space complexity.

Time complexity refers to the time required to execute the algorithm, and space complexity refers to the memory space required by the algorithm.

1) Time Complexity

We usually use the progressive symbol \(O\) to represent time complexity.

Symbol \(O\) indicates the upper limit of the function.

In order to estimate time complexity, we need to find out the basic operation of the algorithm and estimate how many times it has been executed.

For example, the program:

for(int i = 0; i < n; i++){
    for(int j = 0; j < n; j++){
        num++;
    }
}

The basic operation is num++. And how many times it has been exexuted?

\[n + n + n+...+n = n^2 \]

Therefore, the time complexity of the algorithm is \(O(n^2)\)

2) Space Complexity

It represents the size of the storage space temporarily occupied by the algorithm during operation.

If you used an single-dimensional array, then the space compexity is \(O(n)\).

If you don't need auxillary space, then the space compexity if \(O(1)\).

二、Classic Algorithm Ideas

1) Brute Force

The brute force method is a direct problem-solving algorithm.

Usually the brute force method traverses all the solutions of the problem and finds the one that meets the requirements.

For example, Selection Sort.

The brute force method is obviously not the best algorithm, but it is always an effective method.

2) Divide and Conquer

The steps of the divide-and-conquer method is :

  • Decompose the problem into smaller sub-problems
  • Solvie these sub-problems
  • Merge the solutions of the sub-problems

Classic algorithm using divide and conquer strategy is Merge Sort.

3) Dynamic Programming

Sometimes we can't get the solution of the main problems by simply combining solutions of sub-problems.

Then we can use the dynamic programming method.

The overall idea of the dynamic programming method is: save the answers to the sub-questions that have been solved, and get the answers directly when needed, thereby avoiding a lot of double calculations.

For example:

A Fibonacci sequence can be recursively defined as:

\[F(n)= \begin{cases} 1& \text{n = 0}\\ 1& \text{n = 1}\\ F(n-1) + F(n-2)& \text{n > 1} \end{cases} \]

The programm is:

int fibonacci(int n){
    if(n == 0 || n == 1){
        return 1;
    }
    return fibonacci(n-1)+fibonacci(n-2)
}

But, when \(n = 5\), its calculation process is:

We can see that \(F(3)\) and \(F(2)\) are repeatedly calculated many times, which wastes a lot of time.

To this end, we can define a dp table with length of \(n\) to store the result of each calculation.

\[dp[n]= \begin{cases} 1& \text{n = 0}\\ 1& \text{n = 1}\\ dp[n-1] + dp[n-2]& \text{n > 1} \end{cases} \]

It is worth mentioning that the dynamic programming method requires that the problem has an optimal substructure property, that is, the optimal solution of the main problem contains the optimal solution of the sub-problem.

原文地址:https://www.cnblogs.com/danielwong2021/p/15271268.html