zoukankan      html  css  js  c++  java
  • 算法设计与分析——Introduction

    1、Computational Problems and Algorithms

    • Definition: A computational problem is a specification of the desired input-output relationship.

    • Definition: An instance of a problem is all the inputs needed to compute a solution to the problem.

    • Definition: An algorithm is a well defined computational procedure that transforms inputs into outputs, achieving the desired input-output relationship.

    • Definition: A correct algorithm halts with the correct output for every input instance. We can then say that the algorithm solves the problem.

    2、Example of Problems and Instances

    image.png

    3、Example of Algorithm : Insertion Sort

    In-Place means 就地

    image.png

    4、Insertion Sort: an Incremental Approach

    image.png

    5、Analyzing Algorithms

    image.png

    依赖于计算模型(顺序or并行,一般取顺序,即单处理器)

    image.png

    6、Three Cases of Analysis

    image.png

    7、Three Analyses of Insertion Sorting

    • Best:

    image.png

    • Worst:

    image.png

    • Average:

    image.png

    8、Asymptotic Time Complexity Analysis

    • We would like to compare efficiencies of different algorithms for the same problem, instead of different programs or implementations. This removes dependency on machines and programming skill.
    • It becomes meaningless to measure absolute time since we do not have a particular machine in mind. Instead, we measure the number of steps. We call this the time complexity or running time and denote it by T(n).
    • We would like to estimate how T(n) varies with the input size n.

    9、Big-Oh

    If A is a much better algorithm than B, then it is not necessary to calculate TA(n) and TB(n) exactly. As n increases, since TB(n) will grow much more rapidly, TA(n) will always be less than TB(n) for large enough n.

    Thus, it suffices to measure the growth rate of time complexity to get a rough comparison.

    f(n) = O(g(n)):

    There exists constant c > 0 and n0 such that f(n) ≤ c · g(n) for n ≥ n0.

    When estimating the growth rate of T(n) using big-Oh:

    • Ignore the low order terms.
    • Ignore the constant coefficient of the most significant term.
    • The remaining term is the estimate

    For example,

    image.png

    10、Big Omega and Big Theta

    f(n) = Ω(g(n)) (big-Omega):

    There exists constant c > 0 and n0 such that f(n) ≥ c · g(n) for n ≥ n0.

    f(n) = Θ(g(n)) (big-Theta):

    f(n) = O(g(n)) and f(n) = Ω(g(n)).

    11、Some thoughts on Algorithm Design

    • Algorithm Design, as taught in this class, is mainly about designing algorithms that have small big-Oh running times.
    • “All other things being equal”, O(n log n) algorithms will run more quickly than O(n 2) ones and O(n) algorithms will beat O(n log n) ones.
    • Being able to do good algorithm design lets you identify the hard parts of your problem and deal with them effectively.
    • Too often, programmers try to solve problems using brute force techniques and end up with slow complicated code! A few hours of abstract thought devoted to algorithm design could have speeded up the solution substantially and simplified it.

    Computer A(faster, 1010 instructions/second ), insertion sort, T1(n) = c1 O(n 2)

    Computer B(slower, 107 instructions/second), merge sort, T2(n) = c2 O(n log n)

    suppose n = 107 , c1 = 2, c2 = 50

    image.png

    We should consider algorithms, like computer hardware, as a technology. Total system performance depends on choosing efficient algorithms as much as on choosing fast hardware.

  • 相关阅读:
    林大妈的JavaScript进阶知识(二):JS异步行为
    前端技术基础(一):浏览器相关
    林大妈的JavaScript进阶知识(一):对象与内存
    林大妈的JavaScript进阶知识(四):HTML5 History API
    林大妈的CSS知识清单(二)可见格式化模型(内含margin塌陷与浮动闭合的解决方案)
    林大妈的CSS知识清单(一)添加样式
    林大妈的JavaScript基础知识(三):JavaScript编程(4)数组
    林大妈的JavaScript基础知识(三):JavaScript编程(3)原型
    林大妈的JavaScript基础知识(三):JavaScript编程(2)函数
    基于vue-cli快速构建
  • 原文地址:https://www.cnblogs.com/wangzheming35/p/15556871.html
Copyright © 2011-2022 走看看