博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
loop invariant for Iterative algorithm {make progress: i-1 --> i?}
阅读量:4963 次
发布时间:2019-06-12

本文共 3388 字,大约阅读时间需要 11 分钟。

Repetition is an opportunity.

Reconsidering Simple Examples

A good martial arts student will attentively repeat each fundamental technique many times.

In contrast, many college students tune out when a concept (eg. depth first search) is taught more than once.

The better students listen carefully in order to refine and develop their understanding.

Designing an algorithm

define problem

define loop invariants

define measure of progress

define step

define exit condition

maintain loop inv

make progress

initial conditions

ending

//insert sort void sort(int a[]){
for(int i =1;i
0&&a[j-1]>b) a[j]=a[j-1]; j--; } a[j]=b; }

explaining the insertion sort:

If you can't explain what you're doing and why you're doing it to any intelligent layman, that really means that you don't understand it yourself.

By Albeit Einstein.

we maintain a subset of elements sorted within a list. 
The remaining elements are off to the side somewhere. Initially, think of the first element in the array as a sorted list of length one. One at a time, we take one of the elements that is off to the side and we insert it into the sorted list where it belongs. This gives a sorted list that is one element longer than it was before. When the last element has been inserted, the array is completely sorted.
state of computation determined by values of all variables.

loop invariant

  • some subsets of the elements are sorted.
  • the remaining elements are off to the side.

making the progress while keeping the loop invariant.

running time:

inserting an element into a list of size i takes O(i)

1+2+3+...+n = O(n2)

hope you are beginning to appreciate loop invariants for describing algorithms.

another example

explaining the selection sort

We maintain that the k smallest of elements are sorted in a list. The larger elements are in a set on the side. Initially, with k=0, all the elements are in this set. Progress is made by finding the smallest element in the remaining set of larger elements and adding this selected element at the end of the sorted list of elements. This increases k by one. Stop with k=n. At this point, all the elements have been selected and the list is sorted.

Design algorithm

don't start coding, you must design a working algorithm first.

try solving the problem on small input examples

what basic steps might you follow to make some kind of progress towards the answer?

describe or draw a picture of what the data structure might look like after a number of these steps?

Leap into the middle of the algorithm.

What would you like your data structure to look like when you are half done?

The loop invariant should state what work has been completed towards solving the problem and what works still needs to be done.

the loop invariant should flow smoothly from the beginning to the end of the algorithm

at the beginning, it should follow easily from the preconditions.

it should progress in small natural steps

once the exit condition has benn met, the postconditions should easily follow.

pretend that a genie has granted your wish.

You are now in the middle of your computation and your dream loop invariant is true.

Loop invariants are pictures of current state. No Actions, No algorithms.

Typical types of loop invariant

  • more of input
  • more of output
  • narrowed the search scope
  • case analysis
  • work done

转载于:https://www.cnblogs.com/greginfo/archive/2012/02/21/2361812.html

你可能感兴趣的文章
perl读取excel
查看>>
$("this") $(this) 区别
查看>>
python代码格式规范
查看>>
IOS获取系统相簿里的照片
查看>>
OS开发UI篇—无限轮播(功能完善)
查看>>
[模板]数学整合
查看>>
不受控制的 position:fixed
查看>>
safari的坑
查看>>
awk根据指定的字符串分割字符串
查看>>
ubuntu下apt-get的配置文件是哪个
查看>>
[九省联考2018]一双木棋chess
查看>>
6.循环
查看>>
tp3.2 自带的文件上传及生成缩略图功能
查看>>
Angular 入门学习
查看>>
[单选题]条件语句的时候不应该使用哪一种控制结构
查看>>
1049 I Think I Need a Houseboat ACM题答案 java版
查看>>
socket tcp
查看>>
散列表(一).散列表基本内容介绍
查看>>
11-2犀牛读书笔记
查看>>
Openjudge 1.12-04
查看>>