Pseudocode, Abstract Data Type, ADT Implementation, Algorithm Efficiency.

Презентация:



Advertisements
Похожие презентации
1/27 Chapter 9: Template Functions And Template Classes.
Advertisements

Unit II Constructor Cont… Destructor Default constructor.
© 2009 Avaya Inc. All rights reserved.1 Chapter Two, Voic Pro Components Module Two – Actions, Variables & Conditions.
Data Types in C. A Data Type A data type is –A set of values AND –A set of operations on those values A data type is used to –Identify the type of a variable.
UNIT 2. Introduction to Computer Programming. COM E 211: Basic Computer Programming UNIT 2. Introduction to Computer Programming Algorithm & Flowcharting.
In mathematics, the notion of permutation is used with several slightly different meanings, all related to the act of permuting (rearranging) objects.
© Luxoft Training 2013 Annotations. © Luxoft Training 2013 Java reflection / RTTI // given the name of a class, get a "Class" object that // has all info.
Loader Design Options Linkage Editors Dynamic Linking Bootstrap Loaders.
S4-1 PAT328, Section 4, September 2004 Copyright 2004 MSC.Software Corporation SECTION 4 FIELD IMPORT AND EXPORT.
Inner Classes. 2 Simple Uses of Inner Classes Inner classes are classes defined within other classes The class that includes the inner class is called.
Sequences Sequences are patterns. Each pattern or number in a sequence is called a term. The number at the start is called the first term. The term-to-term.
Linear Block Codes Mahdi Barhoush Mohammad Hanaysheh.
Conditional Statements. Program control statements modify the order of statement execution. Statements in a C program normally executes from top to bottom,
Multiples Michael Marchenko. Definition In mathematics, a multiple is the product of any quantity and an integer. in other words, for the quantities a.
© 2002 IBM Corporation Confidential | Date | Other Information, if necessary © Wind River Systems, released under EPL 1.0. All logos are TM of their respective.
BREADTH FIRST TRAVERSAL Lesson Plan -3. Evocation.
Yogesh Mehla Now concept of logic building is not so complex and not so simple. We will not work on how to make logic program in.
The problem of String Matching Given a string S, the problem of string matching deals with finding whether a pattern p occurs in S and if p does occur.
© 2005 Cisco Systems, Inc. All rights reserved. BGP v Route Selection Using Policy Controls Applying Route-Maps as BGP Filters.
HPC Pipelining Parallelism is achieved by starting to execute one instruction before the previous one is finished. The simplest kind overlaps the execution.
Транксрипт:

Pseudocode, Abstract Data Type, ADT Implementation, Algorithm Efficiency

What is Pseudocode? Used to define algorithms An English-like representation of the algorithm logic It is part English, part structured code

Example 1 Algorithm sample(pageNumber) This algorithm reads a file and prints a report. PrepageNumber passed by reference Post Report Printed pageNumber contains number of pages in report Return Number of lines printed 1 loop (not end of file) 1 read file 2 if (full page) 1 Increment page number 2 Write page heading 3 end if 4 write report line 5 increment line count 2 end loop 3 return line count end sample

Parts of a Pseudocode Algorithm Header Names the algorithm, lists its parameters, and describes any preconditions and postconditions Purpose, Conditions and Return Algorithm search(list, argument location) Search array for specific item and prints out index location. Prelist contains data array to be searched argument contains data to be located in list Postlocation contains matching index -or- undetermined if not found Return true if found, false if not found

Parts of a Pseudocode Statement Numbers Variables Intelligent data names Not necessary to define the variables used in an algorithm Rules Do not use single-character names Do not use generic names in application programs Abbreviations are not excluded as intelligent data names

Parts of a Pseudocode Statement Constructs Sequence Do not alter the execution path within an algorithm Selection Evaluates a condition and executes zero or more alternatives Loop Iterates a block of code Algorithm Analysis 1 if (condition) 1 action1 2 else 1 action2 3 end if

Example 2 Analysis There are two points worth mentioning in the algorithm. First, there are no parameters. Second, the variables have not been declared. A variables type and purpose should be easily determined by its name and usage Algorithm deviation Prenothing Post average and numbers w/ their deviation printed 1 loop (not end of file) 1 Read number into array 2 Add number to total 3 Increment count 2 End loop 3 Set average to total / count 4 Print average 5 Loop (not end of array) 1 set devFromAve to array element – average 2 print array element and devFromAve 6 End loop end deviation

The Abstract Data Type Spaghetti code Modular Programming Structured Programming (Edsger Dijkstra and Niklaus Wirth)

Atomic and Composite Data Atomic Data Data that consist of a single piece of information Example: integer number 4562 Composite Data Data that can be broken into subfields that have meaning Example: telephone number

Data Type Consists of two parts A set of values A set of operations on values TypeValuesOperations Integer…-2, -1, 0, 1, 2, …*, +, -, %, /, ++, --, … Floating point…, 0.0, …*, +, -, /, … Character\0,…,A, B,…,a, b,…, ~, …

Data Structure An aggregation of atomic and composite data into a set with defined relationships In this definition, structure means a set of rules that holds the data together A combination of elements in which each is either a data type or another data structure A set of associations or relationships involving the combined elements ArrayRecord Homogeneous sequence of data or data types known as elements Heterogeneous combination of data into a single structure with an identified key Position association among the elementsNo association

Abstract Data Type Abstraction We know what a data type can do How it is done is hidden Matrix Linear Tree Graph

Abstract Data Type Definition A data declaration packaged together with the operations that are meaningful for the data type. In other words, we encapsulate the data and the operations on the data, then we hide them from the user Abstract Data Type Declaration of Data Declaration of Operations Encapsulation of Data and Operations

ADT Implementations Array Implementations The sequentiality of a list is maintained by the order structure of elements in the array (indexes) Linked List Implementations An ordered collection of data in which each element contains the location of the next element or elements.

ADT Implementations list datalink datalinkdatalink list datalink datalink datalink LINEAR LIST NON-LINEAR LIST EMPTY LIST

ADT Iplementations Node The elements in a linked list Structure that has two parts: the data and one or more links Self-referential structures data

ADT Implementations numbernameidgrdPts nameaddressphone Node with one data field Node with three data fields Structure in a node

ADT Implementations Pointers to Linked Lists A linked list must always have a head pointer Depending on how the list is used, there could be several other pointers as well

Generic Code typedef struct node { void *dataPtr; struct node* link; } NODE; dataPtrlink void To next node

Example A createNode main Dynamic Memory 7 dataPtrlink newData nodeData node itemPtrnodePtr

Creating a Node Header File typedef struct node { void* dataPtr; struct node* link; } NODE; NODE* createNode (void* itemPtr) { NODE* nodePtr; nodePtr = malloc(sizeof(NODE)); nodePtr->dataPtr = itemPtr; nodePtr->link = NULL; return nodePtr; }

Demonstrate Node Creation #include #include ProgCN.h int main(void) { int * newData; int * nodeData; NODE* node; newData = malloc(sizeof(int)); *newData = 7; node = createNode(newData); nodeData = (int*)node->dataPtr; printf(Data from node: %d\n, *nodeData); return 0; }

Activity 2 (Structure for two linked nodes) createNode main Dynamic Memory 7 dataPtrlink node nodePtr dataPtrlink 75

Demonstrate Node Creation #include #include ProgCN.h int main(void) { int * newData; int * nodeData; NODE* node; newData = malloc(sizeof(int)); *newData = 7; node = createNode(newData); newData = malloc(sizeof(int)); *newData = 75; node->link = createNode(newData); nodeData = (int*)node->dataPtr; printf(Data from node 1: %d\n, *nodeData); nodeData = (int*)node->link->dataPtr; printf(Data from node 2: %d\n, *nodeData); return 0; }

Algorithm Efficiency Algorithmics term used by Brassard and Bratley to define the systematic study of the fundamental techniques used to design and analyze efficient algorithms. General Format of an algorithms efficiency f(n) = efficiency

Linear Loops The number of iterations is directly proportional to the loop factor Plotting these would give us a straight line. For this reason, they are called linear loops. for (i=0; i<1000; i++) application code f(n) = n for (i=0; i<1000; i+=2) application code f(n) = n/2

Logarithmic Loops The controlling variable is multiplied or divided in each iteration. for (i=1; i<=1000; i*=2) application code for (i=1000; i>=1; i/=2) application code MultiplyDivide IterationValue of IIterationValue of i (exit) (exit)

Logarithmic Loops The number of iterations is a function of the multiplier or divisor Generalizing this analysis Multiply 2 iterations < 1000 Divide 1000/2 iterations >= 1 f(n) = logn

Nested Loops To find the total number of iterations in nested loops, we find the product of the number of iterations in the inner loop and the number of iterations in the outer loop Three nested loops Linear Logarithmic Quadratic Dependent Quadratic

Linear Logarithmic Total number of iterations = 10log10 for (i=0; i<10; i++) for (j=0; j<10; j*=2) application code f(n) = nlogn

Quadratic Loops Total number of iterations = 10 * 10 for (i=0; i<10; i++) for (j=0; j<10; j++) application code f(n) = n 2

Dependent Quadratic The number of iterations in the body if the inner loops is calculated as Total number of iterations = n * inner loop iterations for (i=0; i<10; i++) for (j=0; j<i; j++) application code … = 55 (n+1) 2 f(n) = n * ((n+1)/2)

Big-O Notation With the speed of computers today, we are not concerned with an exact measurement of an algorithms efficiency as much as we are with its general order of magnitude. Although developed as a part of pure mathematics, it is now frequently also used in computational complexity theory to describe how the size of the input data affects an algorithms usage of computational resources (usually running time or memory). It is also used in many other fields to provide similar estimates. We dont need to determine the complete measures of efficiency, only the factor that determines the magnitude. This factor is the big-O. O(n)

Big-O Notation For example, the time (or the number of steps) it takes to complete a problem of size n might be found to be T(n) = 4n² 2n + 2. As n grows large, the n² term will come to dominate, so that all other terms can be neglected for instance when n = 500, the term 4n² is 1000 times as large as the 2n term. Ignoring the latter would have negligible effect on the expression's value for most purposes.

Derivation of the Big-O Steps 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 lowest to highest as shown below logn n nlogn n2n2 n3n3 … nknk 2n2n n!

Standard Measures of Efficiency EfficiencyBig-OIterationsEstimated Time LogarithmicO(log n)14Microseconds LinearO(n)10000Seconds Linear Logarithmic O(n(logn))140000Seconds QuadraticO(n 2 ) Minutes PolynomialO(n k )10000 k Hours ExponentialO(c n ) Intractable FactorialO(n!)10000!Intractable

Exercise Calculate the run-time efficiency of the following program segment If the algorithm doIt() has an efficiency factor of 5n, calculate the run- time efficiency of the following program segment If the efficiency of the algorithm doIt() can be expressed as O(n 2 ), calculate the efficiency of the following program segment. for (i=1; i<=n; i++) doIt(…) for (i=1; i<=n; i++) printf(%d, i); for (i=1; i<=n; i*=2) doIt(…)

Exercise Given that the efficiency of an algorithm is n 3, if a step in this algorithm takes 1 nanosecond(10 -9 seconds), how long does it take the algorithm to process an input of size 1000? An algorithm processes a given input of size n. If n is 4096, the run time is 512 milliseconds. If n is 16,384, the run time is 2048 milliseconds. What is the big-O notation? An algorithm processes a given input of size n. If n is 4096, the run time is 512 milliseconds. If n is 16,384, the run time is 8192 milliseconds. What is the big-O notation?