Complete algorithm design and analysis overnight

Chapter One Algorithm Overview

algorithm

定义: Algorithm is an effective strategy for problem solving. It is a set of finite rules for solving a specific problem
特性:

  • Determinism: each operation of the algorithm must have a precise definition, and there must be no ambiguity
  • Feasibility: The operations to be implemented in the algorithm are all basic operations. In principle, each operation can be completed in a limited time with paper and pen.
  • Input: Each algorithm has 0 or more inputs. These inputs are the quantities given before the algorithm starts and are taken from a specific set of objects
  • Output: An algorithm produces one or more outputs, these outputs are quantities that have a certain relationship with the input
  • Finiteness: an algorithm always terminates after performing a finite step operation

Understand the difference between algorithm and calculation process : 算法Yes 可以终止的计算过程, the calculation process that cannot be terminated: operating system

Design algorithm → express algorithm → confirm algorithm → analyze algorithm → test program

Algorithm complexity analysis

The amount of computer resources required to run the algorithm

Time complexity : the amount of time resources required
Space complexity : the amount of space resources required

基本操作:For a certain elementary operation in the algorithm, if its highest execution frequency and all other elementary highest execution frequencies are within a constant factor, then this elementary operation is said to be a basic operation.
The teacher emphasized it many times in class, and it is estimated that Short answerInsert picture description here

Chapter 2 Recursion and Divide and Conquer Strategy

Recursion

**The definition of recursion: **If an object partially contains itself, or fixes itself by itself, the object is said to be recursive; if a process calls itself directly or indirectly, the process is said to be recursive
Two ways of process recursion :

  • Direct recursion: call yourself
  • Introduction Recursion: A calls B, B calls A

Recursive model : It consists of a recursive boundary and a recursive body. The former determines when the recursion ends, and the latter determines the characteristics of the recursive algorithm of the recursive relationship when recursive solving
Insert picture description here
:

  • The recursive process generally passes 函数或子过程来实现, that is to say we have to define the sub-function or sub-process first
  • 问题规模逐渐缩小, Turning the problem into a sub-problem of the same kind of reduced scale. Then recursively solve
  • 相邻两次重复之间有紧密的联系, The previous output is used as the next input
  • 是否收敛,即终止条件. When the size of the question is small enough, the answer should be given instead of following recursion

Summary of the execution process of the recursive algorithm:

  • The execution process of the recursive algorithm keeps calling itself until it reaches the recursive exit and then ends the self calling process
  • After reaching the recursive exit, the recursive algorithm begins to 最后调用最先返回return in the order
  • When returning to the outermost call statement, the recursive algorithm execution process ends

Divide and conquer strategy

Basic idea : Divide into several sub-problems, solve them separately, and then merge the solutions

Applicable conditions :

  • The original problem can be easily solved after it is reduced to a certain extent
  • The original problem can be decomposed into several smaller-scale identical problems, that is, the problem has an optimal substructure property
  • The solutions of the sub-problems can be merged into the solution of the original problem
  • The decomposed sub-problems are independent of each other

Algorithm time complexity table

algorithm time complexity
Binary search O(log2n)
Merge sort O (n log n )
Quick sort O(n log2n)

The process of merge sort is explained with a picture
Insert picture description here

Quick sorting process
Refer to blog quick sorting (process diagram)

Chapter 3 Dynamic Planning

Dynamic programming is a way to solve a certain type of problem. It is a way to investigate the problem, rather than
the characteristics of an algorithm dynamic decision problem : The state of the system and the important factors for making decisions at all times

Insert picture description here
Use multi-stage solution, refer to ppt for the solution process

The basic elements of dynamic programming algorithm:
1. Optimal substructure :

  • The optimal solution of the problem contains the optimal solution of its sub-problems, this property is called the optimal substructure property
  • When analyzing the nature of the optimal substructure of the problem: the method used is universal: first assume that the solution of the subproblem derived from the optimal solution of the problem is not the optimal solution, and then try to explain that the divisor problem can be constructed under this assumption The optimal solution is a better solution, which leads to contradictions
  • Using the properties of the optimal sub-structure of the problem, the optimal solution of the whole problem is gradually constructed from the optimal solution of the sub-problem in a bottom-up manner. The optimal substructure is the premise that the problem can be solved by the dynamic programming algorithm
    2. Overlapping subproblems :
  • When a recursive algorithm solves a problem, the sub-problems generated each time are not always new problems. Some sub-problems are repeatedly calculated multiple times. This property is called the overlap value of the subproblem
  • The dynamic programming algorithm solves each sub-problem only once, and then saves its solution in a table. When the problem needs to be solved again, it simply takes a constant time to check the result

0/1 knapsack problem : the longest common
Insert picture description here
subsequence problem : the
注意:subscript of the longest common subsequence only needs to increase, not necessarily continuous
Insert picture description here

Algorithm type solved problem
Divide and conquer Fast power algorithm, checkerboard coverage algorithm, merge sort algorithm, fast sort algorithm, linear time selection, round robin schedule
Dynamic programming The longest common subsequence, 0-1 knapsack problem,
how are you Optimal loading, Huffman coding, single source shortest path, minimum spanning tree, multi-machine scheduling problem
Backtracking n queen problem, 0-1 knapsack problem, m coloring problem of graphs, traveling salesperson problem
Branch and bound Loading problem, 0-1 backpack problem, common salesperson problem

Guess you like

Origin blog.csdn.net/weixin_43716048/article/details/112291531