Data Structures( 数据结构 ) Course 1:Pseudocode, ADT, Algorithm Efficiency

30
Data Structures( Data Structures( 数数数数 数数数数 ) ) Course 1:Pseudocode, ADT, Algorithm Course 1:Pseudocode, ADT, Algorithm Efficiency Efficiency

description

Data Structures( 数据结构 ) Course 1:Pseudocode, ADT, Algorithm Efficiency. Vocabulary. Pseudocode: 伪代码。 Algorithm: 算法 , 解决一个问题的一系列基本步骤。 Atomic Data: 不可拆分的数据,或原子数据。 Composite data: 复合数据。 Modular programming: 模块化程序设计。 Object Oriented programming: 面向对象程序设计。 - PowerPoint PPT Presentation

Transcript of Data Structures( 数据结构 ) Course 1:Pseudocode, ADT, Algorithm Efficiency

Page 1: Data Structures( 数据结构 ) Course 1:Pseudocode, ADT, Algorithm Efficiency

Data Structures(Data Structures( 数据结数据结构构 ))

Course 1:Pseudocode, ADT, Algorithm Course 1:Pseudocode, ADT, Algorithm EfficiencyEfficiency

Page 2: Data Structures( 数据结构 ) Course 1:Pseudocode, ADT, Algorithm Efficiency

2西南财经大学天府学院

VocabularyVocabulary

Pseudocode: 伪代码。Algorithm: 算法 , 解决一个问题的一系列基本步骤。Atomic Data: 不可拆分的数据,或原子数据。Composite data: 复合数据。Modular programming: 模块化程序设计。Object Oriented programming: 面向对象程序设计。Encapsulation: 封装,是面向对象程序设计的重要思想之一。Abstract Data Type: 抽象数据类型。Algorithm Efficiency: 算法效率。

Page 3: Data Structures( 数据结构 ) Course 1:Pseudocode, ADT, Algorithm Efficiency

3西南财经大学天府学院

PseudocodeAbstract data typeAlgorithm efficiency analysis

Highlights in this chapterHighlights in this chapter

Page 4: Data Structures( 数据结构 ) Course 1:Pseudocode, ADT, Algorithm Efficiency

4西南财经大学天府学院

About PseudocodeAbout Pseudocode

AlgorithmA list of instructions which are carried out in a fixed order to find answer to a question.

Tools to define algorithmsThere are several tools can be used to define algorithms, for example pseudocode, flow chart , and others.

Pseudocode Pseudocode is one of the most common tools for defining algorithms. It is part English, part structured code.

Page 5: Data Structures( 数据结构 ) Course 1:Pseudocode, ADT, Algorithm Efficiency

5西南财经大学天府学院

About PseudocodeAbout Pseudocode

English PartProvide a relax syntaxCode PartConsists of an extended version of basic algorithmic constructs(Sequence, Selection, Iteration 循环 )

We use pseudocode for both data structures and code.

Page 6: Data Structures( 数据结构 ) Course 1:Pseudocode, ADT, Algorithm Efficiency

6西南财经大学天府学院

About PseudocodeAbout Pseudocode

Basic format for data type Count<Integer>

Name

Type

The Structure of the Node

data <DataType>Link <Pointer to node>

End Node

Page 7: Data Structures( 数据结构 ) Course 1:Pseudocode, ADT, Algorithm Efficiency

7西南财经大学天府学院

Algorithm HeaderAlgorithm Header

Each algorithm begin with a header

Algorithm Header consists of :Name: 算法名称Parameters( 参数) :pass by reference or pass by

valueList: a short statement about what algorithm

doesPre: Pre Condition 调用算法前提条件Post: Post Condition 算法执行完后的状况描述

Page 8: Data Structures( 数据结构 ) Course 1:Pseudocode, ADT, Algorithm Efficiency

8西南财经大学天府学院

Purpose, Conditions and ReturnPurpose, Conditions and Return

Purpose: 它不必描述算法的所有处理过程,而只是概要处理过程。ConditionsPre: sometime there are no precondition(pre Nothing)

Several parameters, Pre should shown for each

Post :identifies any action taken and the status of any output parametersReturn: values returned, it will identified by a return condition. Otherwise, no return condition need

Page 9: Data Structures( 数据结构 ) Course 1:Pseudocode, ADT, Algorithm Efficiency

9西南财经大学天府学院

Purpose, Conditions and ReturnPurpose, Conditions and Return

Example:Algorithm search( Val list <array>,

Val argument <integer>,Ref Location <index>)

Search array for specific item and return index location

PrePre List Contain data array to be searchedargument contains data to be located in list

PostPost Location contains index of element matching argument or undetermined if not find

ReturnReturn <Boolean> true if found, false if not found

Page 10: Data Structures( 数据结构 ) Course 1:Pseudocode, ADT, Algorithm Efficiency

10西南财经大学天府学院

Algorithm HeaderAlgorithm Header

ImportanceThe programmer using the algorithm often see only the header informationThe Header information must complete enough to communicate to the programmer everything he or she must know to use the algorithm

Page 11: Data Structures( 数据结构 ) Course 1:Pseudocode, ADT, Algorithm Efficiency

11西南财经大学天府学院

Variables(Variables( 变量)变量)

在一个算法中没有必要定义没一个变量,特别是变量名具有一定的含义。为了使变量容易理解,我们往往使用智能变量,也既是变量名称能描述变量的意思。

Rules♠ Do not use single character names(i,j)♠ Do not use generic names(max,count,sum)♠ 可能在智能变量中采用缩写 (StuCnt,NumofStu,Nostu)

Page 12: Data Structures( 数据结构 ) Course 1:Pseudocode, ADT, Algorithm Efficiency

12西南财经大学天府学院

Statement ConstructsStatement Constructs

Sequence statementsSelection statementsLoop statements

A sequence is a series of statements that do not alter the execution path within an algorithm.Because an algorithm has only one entry and one exit, a statement that call another algorithm can be regarded as a sequence statement.

According to the evaluation, different execution path will be taken. Pseudocode statement is:1 if (condition) 1 action1(statements group1)2 else 1 action2(statements group2)3 end if

Iterate a block of statements, until the condition turns to false. Pseudocode is:1 loop (condition) 1 action (statements)2 end loop

Page 13: Data Structures( 数据结构 ) Course 1:Pseudocode, ADT, Algorithm Efficiency

13西南财经大学天府学院

Statement numbers:These numbers are used to identify the individual statement.For instance statement form feed can be expressed as statement 3.2.1

Algorithm Header

Algorithm name: sampleParameter(s): pageNumberPurpose descriptionPre-conditions Post-conditionsReturn

Sample Sample AlgorithmAlgorithm

Intelligent Data Name:The name of variable or data can tell the meaning of the data. This is very important for a computer programmer

Algorithm sample (ref pageNumber <integer>)

This algorithm reads a file and prints a report

Pre pageNumber must be initialized

Post Report printed. pageNumber contains

number of pages in report.

Return Number of lines printed.1 open file2 lines=03 loop (not end of file) 1 read file 2 if (full page) 1 form feed 2 pageNumber= pageNumber+1 3 write page heading 3 end if 4 write report line 5 lines=lines+14 end loop5 close file6 return linesend sample

Page 14: Data Structures( 数据结构 ) Course 1:Pseudocode, ADT, Algorithm Efficiency

14西南财经大学天府学院

The Abstract Data TypeThe Abstract Data Type

Development of programming techniquesData typeAtomic DataComposite DataData StructureAbstract Data Type

♠ A data type consists two parts, a set of data and the operations that can be performed on the data.♠ 16bits integer can be taken as an example of a data type. The data set consists of numbers between the range of {-32768~32767}, and the operations are {+,-,*,/…}

♠ A data type consists two parts, a set of data and the operations that can be performed on the data.♠ 16bits integer can be taken as an example of a data type. The data set consists of numbers between the range of {-32768~32767}, and the operations are {+,-,*,/…}

Composite data can be broken into sub-fields that have definite meaning.

•Atomic Data are single and non-decomposable entity. •Atomic data typeAtomic data type is defined as a set of atomic data and a set of operation on the data.•An atomic data type has identical properties that distinguish it from another atomic data type.

•Atomic Data are single and non-decomposable entity. •Atomic data typeAtomic data type is defined as a set of atomic data and a set of operation on the data.•An atomic data type has identical properties that distinguish it from another atomic data type.

★Non-structured, linear programs.In linear program, the logic flow wound through the program like spaghetti on a plate.

★Modular programming.Programs were organized in functions or subroutines. In each function, statements were still organized in linear fashion.

★Structure programming.Formulated in the 1970s and still use today.

★Object oriented programming. Programs are taken as a series of object. Each of them encapsulates its properties, actions and operations. The objects interact each other, exchange information among them, and thus the task will be finished.

Data Structure can be defined as:

1.A combination of elements each of which is either a data type or another data structure.

2.A set of associations or relationships(structure) involving the combined elements

Data Structure can be defined as:

1.A combination of elements each of which is either a data type or another data structure.

2.A set of associations or relationships(structure) involving the combined elements

An abstract data type is a data declaration packaged together with the operations that are meaningful for the data type. That means:

1. Declaration of data2. Declaration of operations3. Encapsulation of data and operations

An abstract data type is a data declaration packaged together with the operations that are meaningful for the data type. That means:

1. Declaration of data2. Declaration of operations3. Encapsulation of data and operations

Page 15: Data Structures( 数据结构 ) Course 1:Pseudocode, ADT, Algorithm Efficiency

15西南财经大学天府学院

Four Logic structuresFour Logic structures

Page 16: Data Structures( 数据结构 ) Course 1:Pseudocode, ADT, Algorithm Efficiency

16西南财经大学天府学院

A Model for an Abstract Data A Model for an Abstract Data TypeType

Data Structure

Public

Function APublic

Function B

PrivateFunctionsDATA

DATAExternal Interface

InternalCall

Internaldata flow

Page 17: Data Structures( 数据结构 ) Course 1:Pseudocode, ADT, Algorithm Efficiency

17西南财经大学天府学院

A Model for an Abstract Data A Model for an Abstract Data TypeType(Continue)(Continue)

From the model we can get:

The physical representation of the data type has been encapsulated within the ADT (the irregular outlined area).The operations of the data type have been also encapsulated within the ADT.ADT can interact and exchange information with outer environment through and only through public functions.

Page 18: Data Structures( 数据结构 ) Course 1:Pseudocode, ADT, Algorithm Efficiency

18西南财经大学天府学院

The Concept of ADT Data The Concept of ADT Data StructureStructure

Dynamic Memory

Application program

Insert Traverse

Search Delete

Create Destroy

ADT class code

ADT class object

Page 19: Data Structures( 数据结构 ) Course 1:Pseudocode, ADT, Algorithm Efficiency

19西南财经大学天府学院

Algorithm EfficiencyAlgorithm Efficiency

Choose right algorithm to solve a problem.The efficiency of linear function depends on the number of instructions it contains.The study of algorithm efficiency focuses on loops. We introduce f(n) to express the efficiency of an algorithm, n represents the number of elements to be processed.

f(n)=efficiency

Page 20: Data Structures( 数据结构 ) Course 1:Pseudocode, ADT, Algorithm Efficiency

20西南财经大学天府学院

Typical Loops and their Typical Loops and their efficiencyefficiency

★Linear Loops★Logarithmic

Loops★Nested Loops

▪ Linear logarithmic

▪ Quadratic▪ Dependent

Quadratic

Ch1Ex4.c

Ch1Ex1.c

Ch1Ex2.c

Ch1Ex3.c

Page 21: Data Structures( 数据结构 ) Course 1:Pseudocode, ADT, Algorithm Efficiency

21西南财经大学天府学院

Big-O NotationBig-O Notation

Big-O and its purpose.The way to derive Big-O expression

In each term, set the coefficient of the term to 1.Keep the largest term in the function and discard the others.Terms are ranked from the lowest to highest as:

常 数, log2n, n, nlog2n, n2, n3 ,…, nk , 2n , n!

Page 22: Data Structures( 数据结构 ) Course 1:Pseudocode, ADT, Algorithm Efficiency

22西南财经大学天府学院

Set the coefficient of each term to 1

Example of Big-O derivationExample of Big-O derivation

5n4+10nlog2n+100log2n

n4+nlog2n+log2n Keep the largest term in the function and discard the others.

O(n4)

Page 23: Data Structures( 数据结构 ) Course 1:Pseudocode, ADT, Algorithm Efficiency

23西南财经大学天府学院

Big-O analysis ExampleBig-O analysis Example(Add two matrices)(Add two matrices)

Problem analysis

264

-123

716+

265

4-30

124=

4129

3-13

8310

4 + 6 10

2 1+ 3

•The calculation begins from the first column on the first row, followed by the second item on the first row, then the third one, until to the last item on the first row.•After completes the work on the first row; the same work will begin on the second row; and followed by the work on the successive rows until the work on all rows has been finished.•We can use nested loop algorithm to solve this problem, in the outer loop, complete the work on the rows. In the inner loop complete the work on each item on one row.

•The calculation begins from the first column on the first row, followed by the second item on the first row, then the third one, until to the last item on the first row.•After completes the work on the first row; the same work will begin on the second row; and followed by the work on the successive rows until the work on all rows has been finished.•We can use nested loop algorithm to solve this problem, in the outer loop, complete the work on the rows. In the inner loop complete the work on each item on one row.

Page 24: Data Structures( 数据结构 ) Course 1:Pseudocode, ADT, Algorithm Efficiency

24西南财经大学天府学院

AlgorithAlgorithm form for add add two two matricematricess

Algorithm addMatrix(val matrix1 <matrix>, val matrix2 <matrix>, val size <integer>, ref matrix3 <matrix>)Pre matrix1 and matrix2 have data Size is the number of columns and rows in matrixPost matrices added—result in matrix31 r=02 loop (r<size)

1 c=02 loop (c<size) 1 matrix3[r,c]=matrix1[r,c]+matrix2[r,c]

2 c=c+13 end loop

4 r=r+13 end loop4 return

The outer loop in this algorithm will repeat for size times and within each outer loop the inner loop will also repeat for size times. Therefore the number of add of two items will be size×size or size2. So the efficiency of this algorithm is O(sizeO(size22)) or O(nO(n22))

The outer loop in this algorithm will repeat for size times and within each outer loop the inner loop will also repeat for size times. Therefore the number of add of two items will be size×size or size2. So the efficiency of this algorithm is O(sizeO(size22)) or O(nO(n22))

CH1Ex5.c

Page 25: Data Structures( 数据结构 ) Course 1:Pseudocode, ADT, Algorithm Efficiency

25西南财经大学天府学院

Summary of this chapterSummary of this chapter

One of the most common tools used to define algorithms is pseudocode.Pseudocode is an English-like representation of the code required for an algorithm. It is part English, part structured code.Atomic data are data that are single, non-decomposable entities.An atomic data type is a set of atomic data with identical properties.Atomic data types are defined by a set of values and a set of operations that act on the values.A data structure is an aggregation of atomic data and composite data types into a set with defined relationship.An ADT is a data declaration packaged together with the operations that are meaningful for the data type.Algorithm efficiency is generally defined as a function of the number of elements being processed and the type of loop being used.

Page 26: Data Structures( 数据结构 ) Course 1:Pseudocode, ADT, Algorithm Efficiency

26西南财经大学天府学院

Summary of this chapter Summary of this chapter (Continue)(Continue)

The efficiency of a logarithmic loop is f(n)=log2n

The efficiency of a linear loop is f(n)=n.The efficiency of a linear logarithmic loop is f(n)=n(log2n).

The efficiency of a dependent quadratic loop is f(n)=n(n+1)/2.The efficiency of a quadratic loop is f(n)= n2.The efficiency of a cubic loop is f(n)= n3.The simplification of efficiency is known as big-O notation.The seven standard measures of efficiencies are O(log2n), O(n), O(nlog2n), O(n2), O(nk), O(Cn), O(n!).

Page 27: Data Structures( 数据结构 ) Course 1:Pseudocode, ADT, Algorithm Efficiency

27西南财经大学天府学院

ExercisesExercisesCalculate the deviation( 偏差) from a mean(average)requirement:

Read number into array Calculate their average and print it Print the data in array and its deviation

Complete this algorithm with pseudocode algorithm deviationPre NothingPost Average and numbers with their deviation printed1 i=02 loop(not end of file)…

Page 28: Data Structures( 数据结构 ) Course 1:Pseudocode, ADT, Algorithm Efficiency

28西南财经大学天府学院

ExercisesExercisesDetermine the Big-O notation for the following5n5/2+n2/5

6log2(n)+9n

Calculate the run-time efficiency of the following program segment

1 i=12 loop (i<=n)

1 print (i) 2 i=i+1

3 end loop

Page 29: Data Structures( 数据结构 ) Course 1:Pseudocode, ADT, Algorithm Efficiency

29西南财经大学天府学院

ExercisesExercisesCalculate the run-time efficiency of the following program segment1 i=12 loop (i<=n)

1 j=12 loop(j<=n) 1 k=1 2 loop (k<=n) 1 print(i,j,k)

2 k=k+1 3 end loop 4 j=j+13 end loop 4 i=i+1

3 end loop

Page 30: Data Structures( 数据结构 ) Course 1:Pseudocode, ADT, Algorithm Efficiency

30西南财经大学天府学院

homeworkhomework

Recorder the following efficiencies from smallest to largest2n,n!,N5,10000,nlog2(n)

Determine the Big-O notation for the following3n4+nlog2(n)

5n2+n3/2

Calculate the run-time efficiency of the following program segment1 i=12 loop(i<=n)

1 print(i)2 i=i+1

3 end loop