320 likes | 496 Views
Military Technical Academy Data Structures and Algorithms. Lecturer: Dr. Nguyen Nam Hong Tel: 04 8781 437 Mob: 091 2312 816 Email: nguyennamhong2003@yahoo.com.au Lecture 1. Introduction. Lecture 1. Introduction. Contents: 1.1. Algorithms 1.2. Data and Data Structures
E N D
Military Technical Academy Data Structures and Algorithms Lecturer: Dr. Nguyen Nam Hong Tel: 04 8781 437 Mob: 091 2312 816 Email: nguyennamhong2003@yahoo.com.au Lecture 1. Introduction Dr. Nguyen Nam Hong, Le Quy Don Technical University
Lecture 1. Introduction Contents: 1.1. Algorithms 1.2. Data and Data Structures 1.3. Some Simple Rules 1.4. PseudoCode and Flowchart Reference: Elliz Horowitz - Fundamentals of data structures, Chapter 1: Introduction Dr. Nguyen Nam Hong, Le Quy Don Technical University
1.1. Algorithms 1.1.1. What is an Algorithm? 1.1.2. Example: Sorting an array. 1.1.3. Definition of Algorithm. Dr. Nguyen Nam Hong, Le Quy Don Technical University
1.1.1. What Is An Algorithm? (1/2) • Algorithms are the ideas behind • computer programs. • An algorithm is the thing which stays the • same whether • the program is in Pascal running on a • Cray in New York • or it is in BASIC running on a • Macintosh in Kathmandu! Dr. Nguyen Nam Hong, Le Quy Don Technical University
1.1.1. What Is An Algorithm? (2/2) • To be interesting, an algorithm has to • solve a general, specified problem. • An algorithmic problem is specified by • describing the set of instances it must • work on • what desired properties the output • must have. Dr. Nguyen Nam Hong, Le Quy Don Technical University
1.1.2. Example: Sorting • Input: A sequence of N numbers • a[1], a[2], … , a[n]. • Output: The permutation (reordering) of the input sequence such as • a[1] <= a[2] <= … <= a[n] . • We seek algorithms which are correct and efficient. Dr. Nguyen Nam Hong, Le Quy Don Technical University
1.1.3. Four areas of algorithm study 1. Machines for executing algorithms. 2. Languages for describing algorithms. 3. Foundations of algorithms. 4. Analysis of algorithms. Dr. Nguyen Nam Hong, Le Quy Don Technical University
1.1.4. Definition of Algorithms (1/2) An algorithm is a finite set of instructions which, if followed, accomplish a particular task. In addition every algorithm must satisfy the following criteria: 1. Input: there are zero or more quantities which are externally supplied; 2. Output: at least one quantity is produced; 3. Definiteness: each instruction must be clear and unambiguous; Dr. Nguyen Nam Hong, Le Quy Don Technical University
1.1.4. Definition of Algorithms (2/2) 4. Finiteness: if we trace out the instructions of an algorithm, then for all cases the algorithm will terminate after a finite number of steps; 5. Effectiveness: every instruction must be sufficiently basic that it can in principle be carried out by a person using only pencil and paper. It is not enough that each operation be definite as in 3, but it must also be feasible. Dr. Nguyen Nam Hong, Le Quy Don Technical University
1.2. Data structures 1.2.1. Computer science - the study of data. 1.2.2. Data types. 1.2.3. Data Object 1.2.4. Data structures. 1.2.5. Data Implementation. Dr. Nguyen Nam Hong, Le Quy Don Technical University
1.2.1. Computer science - the study of data 1. Machines that hold data; 2. Languages for describing data manipulation; 3. Foundations which describe what kinds of refined data can be produced from raw data; 4. Structures for representing data. Dr. Nguyen Nam Hong, Le Quy Don Technical University
1.2.2. Data type • A data type is a term which refers to the kinds of data that variables may "hold" in a programming language. • In FORTRAN the data types are INTEGER, REAL, LOGICAL, COMPLEX, and DOUBLE PRECISION. • In PL/Ithere is the data type CHARACTER. Dr. Nguyen Nam Hong, Le Quy Don Technical University
1.2.3. Data object (1/2) • Data object is a term referring to a set of elements, say D. • For example the data object Bool refers to D = {0, 1}. • For example the data object Int refers to D = {0, 1, 2, ...}. Dr. Nguyen Nam Hong, Le Quy Don Technical University
1.2.3. Data object (2/2) • The data object alphabetic character strings of length less than thirty one implies D = {",'A','B', ...,'Z','AA', ...}. • Thus, D may be finite or infinite • If D is very large we may need to devise special ways of representing its elements in our computer. Dr. Nguyen Nam Hong, Le Quy Don Technical University
1.2.4. Data structure • A data structure is • a set of domains D (a designated domain), • a set of functions F and • a set of axioms X. • The triple (D, F, X) denotes the data structure d and it will usually be abbreviated by writing d. Dr. Nguyen Nam Hong, Le Quy Don Technical University
1.2.5. Data Implementation (1/2) • An implementationof a data structure d is a mapping from d to a set of other data structures e (already exist). • This mapping specifies how every object of d is to be represented by the objects of e. Dr. Nguyen Nam Hong, Le Quy Don Technical University
1.2.5. Data Implementation (2/2) • Secondly, it requires that every function of d must be written using the functions of the implementing data structures e. • Thus we say that integers are represented by bit strings, boolean is represented by zero and one, an array is represented by a set of consecutive words in memory. Dr. Nguyen Nam Hong, Le Quy Don Technical University
1.3. Some Simple Rules (1/2) 1. Every procedure should carefully specify its input and output variables. 2. The meaning of variables should be defined. 3. The flow of the program should generally be forward except for normal looping or unavoidable instances. Dr. Nguyen Nam Hong, Le Quy Don Technical University
1.3. Some Simple Rules (2/2) 4. Indentation rules should be established and followed so that computational units of program text can more easily be identified. 5. Documentation should be short, but meaningful. Avoid sentences like ''i is increased by one." 6. Use subroutines where appropriate. Dr. Nguyen Nam Hong, Le Quy Don Technical University
1.4. PseudoCode and Flowchart(1/2) 1.4.1. Character and symbol set. 1.4.2. Key Words. 1.4.3. Predefined Items. 1.4.4. Statements. 1.4.5. Branch statements. Dr. Nguyen Nam Hong, Le Quy Don Technical University
1.4. PseudoCode and Flowchart (2/2) 1.4.6. Loop statements. 1.4.7. Program and subprograms. 1.4.8. A sub example. 1.4.9. Flowchart Example Dr. Nguyen Nam Hong, Le Quy Don Technical University
1.4.1. Character and Symbol Set • 26 Latin characters • 10 digits • Mathematical operators: + , - , * , /, ^ • Relational operators: < , = , > , <= , >= , <> • Logic value: True, False • Logic Operators: and, or, not, xor • Other symbols: […], (…), {…} • Identifier: sequence of characters, digits, or underscores, starting with a character. Dr. Nguyen Nam Hong, Le Quy Don Technical University
1.4.2. Key Words AND ARRAY BEGIN CASE CONST DIV DO ELSE END EXIT FILE FOR FUNC GOTO IF IN LABEL MOD NEXT NIL NOT OF OR PACK PROG RECO REPEAT SET SUB THEN TO TYPE UNTIL VAR WHILE WITH Dr. Nguyen Nam Hong, Le Quy Don Technical University
1.4.3. Predefined Items Types: Bool Char Int Real Text Funcs: Abs() Arctg() Chr() Cos() Eof() Eoln() Exp() Ln() Odd() Subs: Dispose Get New Pack Page Put Read/Readln Reset Rewrite Unpack Write/Writeln Ord() Pred() Round() Sin() Sqr() Sqrt() Succ Trunc Consts: False MaxInt True Dr. Nguyen Nam Hong, Le Quy Don Technical University
1.4.4. Statements • Assignment statement varName = expression • Grouped Statement Begin StatList End • StatList = Stat_1: Stat_2 : … : Stat_n • Read(VarList): Readln(VarList) • Write(VarList): Writeln(VarList) • VarList = Var_1, Var_2, …, Var_n • Exit • Goto LabelName Dr. Nguyen Nam Hong, Le Quy Don Technical University
1.4.5. Branch Statements • if cond thenS1 • if cond then S1 else S2 • Select Case C1: S1 Case C2: S2 … Case Else: SEnd End Select Dr. Nguyen Nam Hong, Le Quy Don Technical University
Branch Flowcharts Dr. Nguyen Nam Hong, Le Quy Don Technical University
1.4.6. Loop Statements 1. For varName = start To finish [Step increment] StatList End For 2. While cond StatList End While 3. Repeat StatList Until cond Dr. Nguyen Nam Hong, Le Quy Don Technical University
Loop Flowcharts Dr. Nguyen Nam Hong, Le Quy Don Technical University
1.4.7. Program and Subprograms 1. Prog ProgName Declarations StatList End Prog 2. Func FuncName[(ParList)]: RetType StatList End Func 3. Sub SubName[(ParList)] StatList End Sub Dr. Nguyen Nam Hong, Le Quy Don Technical University
1.4.8. Sub Example in PseudoCode SubSORT(A,n) 1 Fori = 1Ton 2 j = i 3 Fork = j + 1To n 4 IfA[k] < A[j]Thenj = k 5 End For 6 t = A[i] 7 A[i] = A[j] 8 A[j] = t 9 End For End Sub Dr. Nguyen Nam Hong, Le Quy Don Technical University
1.4.9. Flowchart Example Dr. Nguyen Nam Hong, Le Quy Don Technical University