算法分析大纲

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

算法的定义

An algorithm is a sequence of unambiguous instructions for solving a computational problem, obtaining a required output for any legitimate input in a finite amount of time.

算法是一系列解决问题的清晰指令,也就是说,对于符合一定规范的输入,能够在有限时间内获得所要求的输出。

算法的特点

Finiteness Definiteness Input Output Effectiveness

算法的设计步骤

(1)Understanding the problem. Read the problem description carefully and ask question if you

have any doubts about the problems.

(2)Deciding on. deciding on appropriate data structures.

(3)Designing an algorithm.An algorithm design technique is a general approach to solving

problems algorithmically that is applicable to a variety of problems from different areas of computing

(4)Proving correctness.The notions of correctness of approximation algorithm is less

straightforward than it is for exact algorithm.

(5)Analyzing an algorithm. Time efficiency indicates how fast the algorithm runs, space

efficiency indicates how much extra memory the algorithm needs.

(6)Coding the algorithm.Most algorithm are destined to be ultimately implemented as

computer programs. Programming an algorithm presents both a peril and an opportunity.

算法效率Time efficiency Space efficiency

增长次数比较1 logn n nlogn n²n³2的n次方n!

蛮力法定义

A straightforward approach, usually based directly on the problem’s statement and definitions of the concepts involved.

蛮力法是一种简单直接地解决问题的方法,通常直接基于问题的描述和所涉及的概念定义。蛮力法优缺点

Strengths :1 wide applicability 2 simplicity 3 yields reasonable algorithms for some important problems(e.g., matrix multiplication, sorting, searching, string matching)

Weaknesses:1 rarely yields efficient algorithms 2 some brute-force algorithms are unacceptably slow not as constructive as some other design techniques

选择排序和冒泡排序两个算法的比较

Selection Sort Bubble Sort

1Stability: Selection Sort is instable ;Bubble Sort is stable

2Efficiency:Selection Sort n²;Bubble Sort n²

3Comparion number: Selection Sort :n(n-1)/2 Bubble Sort: n(n-1)/2

4input siz e:n

解决背包问题过程和分析

Analysis: The exhaustive-search approach to this problem leads to generating all the subsets of the set n items given ,computing the total weight of each subset to identify feasible subsets(the ones which the total weight not exceeding the Knapsack’s capacity),and finding a subset of the largest value among them. Since the number of subset of an n-element set is 2的n次方,the exhaustive search leads to 2的n次方algorithm.

分治法定义

Divide and conquer is a general algorithm design technique that solves a problem’s instance by dividing it into several smaller instances (ideally,of equal size),solving each of them recursively,then combining their solution to the original instance of the problem .

分治法是一种一般性的算法设计技术,它将问题的实例划分为若干个较小的实例,最好拥有相同的规模,对这些较小的实例递归求解,然后合并这些解,以得到原始的解。

三个步骤

Divide the problem into two or more smaller subproblems. 将问题划分成两个或更多小问题Conquer the subproblems by solving them recursively.对小问题求解

Combine the solutions to the subproblems into the solutions to the original problem合并小问题的解,就得到原问题的

合并排序和快速排序算法比较

Merge Sort Quick Sort

1Stability: Merge Sort: stable Quick Sort: instable

2Time effeicency : Merge Sort nlogn ; Quick Sort best-case O(nlog2n) worst-base O(n ^2)

3 Exceptional space :Merge Sort O(n)

合并排序是一种分治排序算法,它把一个输入数组一分为二,并对它们递归排序,然后把这两个拍好序的子数组合并为原数组的一个有序排列。在任何情况下,这个算法的时间效率都是nlogn,而且它的键值比较次数非常接近理论上的最小值,它的最主要缺点是需要相当大的额外空间。

快速排序是一种分治排序算法,它根据元素值和某些事先确定的元素的比较结果,来对元素进行分区。快速排序十分有名,不仅是因为对于随机排列的数组,它是较为出众的nlogn效率算法,而且因为它的最差效率是平方级的。

折半查找递归和非递归伪码

non-recursive algorithm非递归

ALGORITHM BinarySearch(A[0..n-1], K)

l ← 0; r←n-1

相关文档
最新文档