zoukankan      html  css  js  c++  java
  • Steps to develop an iterative algorithm

    The code structure

    begin routine
        <pre-cond>
        pre-loop code % Establish loop invariant
        loop
            <loop-invariant>
            exit when <exit-cond>
            loop code % Make progress while maintaining the loop invariant
        end loop
        post-loop code % Clean up loose ends
        <post-cond>
    end routine

     1) Specifications:

    Preconditions: assertions about the input instances

    Postconditions: assertions about the output

    <pre-cond> & Codealg => <post-cond>

    Pre- and postconditons are the contract between the implementer and the user of the coded algorithm

    2) Loop invariant

    Coming up with the loop invariant is the hardest part of designing an algorithm. However, from it the rest of the algorithm often follows easily.

    A loop invariant is an assertion that is placed at the top of a loop and that must hold true every time the computation returns to the top of the loop.

    • Eatablish the loop invariant
      •   <pre-cond> & codepre-loop => <loop-invariant>
    • Maintain the loop invariant
      •   <loop-invariant'> & not <exit-cond> & codeloop => <loop-invariant''> 
    • Main steps
      •   codeloop must be defined so that it can work for any state of the strucutre for which the loop invariant is true. Worry about one step at a time, and make sure you make progress everytime the algorithm goes around the loop.

    3) Ending

      <loop-invariant> & <exit-cond> & codepost-loop => <post-cond>

     Mathematical induction

    Different types of Iterative Algorithms

    The key point of any iterative algorithm is design a measure of progress and a loop invariant.

    1) More of the output

    Measure of progress: The amount of the output constructed

    Loop invariant: The output constructed so far is correct

    Selction sort

    2) More of the input

    Measure of progress: The amount of the input considered

    Loop invariant: Pretending that this prefix of the input is the entire input, we have a complete solution

    Insertion sort

    3) Narrowing the search space

    Measure of progress: the size of the space in which you have narrowed the search

    Loop invariant: If the thing being searched for is anywhere, then it is in this narrowed subspace

    Binary search

    4) Work done

    Measure of progress: some other creative frunction of the work done so far

    Bubble sort

  • 相关阅读:
    Chrome开发者工具详解(1)Elements、Console、Sources面板
    在Mac下运行ASP.NET Core应用程序
    网络性能优化实战
    Chrome开发者工具详解(5)Application、Security、Audits面板
    Chrome开发者工具详解(3)Timeline面板
    Chrome开发者工具详解(2)Network面板
    Chrome开发者工具详解(4)Profiles面板
    1.1专题介绍「深入浅出ASP.NET Core系列」
    1.2环境安装「深入浅出ASP.NET Core系列」
    1.3创建项目「深入浅出ASP.NET Core系列」
  • 原文地址:https://www.cnblogs.com/littledot/p/3764510.html
Copyright © 2011-2022 走看看