Basic Concepts

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

description

Basic Concepts. 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) - PowerPoint PPT Presentation

Transcript of Basic Concepts

Page 1: Basic Concepts

Pseudocode, Abstract Data Type, ADT Implementation, Algorithm Efficiency

Page 2: Basic Concepts

What is Pseudocode?Used to define algorithms

An English-like representation of the algorithm logic

It is part English, part structured code

Page 3: Basic Concepts

Example 1Algorithm sample(pageNumber)This algorithm reads a file and prints a report. Pre pageNumber passed by reference Post Report Printed

pageNumber contains number of pages in report Return Number of lines printed1 loop (not end of file) 1 read file

2 if (full page)1 Increment page number2 Write page heading

3 end if 4 write report line 5 increment line count

2 end loop3 return line countend sample

Page 4: Basic Concepts

Parts of a PseudocodeAlgorithm 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.

Pre list contains data array to be searchedargument contains data to be located in list

Post location contains matching index-or- undetermined if not found

Return true if found, false if not found

Page 5: Basic Concepts

Parts of a PseudocodeStatement NumbersVariables

Intelligent data namesNot necessary to define the variables used in

an algorithmRules

Do not use single-character names Do not use generic names in application programs Abbreviations are not excluded as intelligent data

names

Page 6: Basic Concepts

Parts of a PseudocodeStatement 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 action12 else 1 action23 end if

Page 7: Basic Concepts

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 Pre nothing

Post average and numbers w/ their deviation printed1 loop (not end of file)

1 Read number into array2 Add number to total

3 Increment count2 End loop

3 Set average to total / count4 Print average

5 Loop (not end of array) 1 set devFromAve to array element – average

2 print array element and devFromAve6 End loopend deviation

Page 8: Basic Concepts

The Abstract Data TypeSpaghetti codeModular ProgrammingStructured Programming (Edsger Dijkstra

and Niklaus Wirth)

Page 9: Basic Concepts

Atomic and Composite Data Atomic Data

Data that consist of a single piece of information

Example: integer number 4562Composite Data

Data that can be broken into subfields that have meaning

Example: telephone number

Page 10: Basic Concepts

Data TypeConsists of two parts

A set of valuesA set of operations on values

Type Values Operations

Integer …-2, -1, 0, 1, 2, … *, +, -, %, /, ++, --, …

Floating point …, 0.0, … *, +, -, /, …

Character \0,…,’A’, ‘B’,…,’a’, ‘b’,…, ~ <, >, …

Page 11: Basic Concepts

Data StructureAn 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

Array Record

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 elements No association

Page 12: Basic Concepts

Abstract Data TypeAbstraction

We know what a data type can doHow it is done is hidden

Matrix Linear

Tree Graph

Page 13: Basic Concepts

Abstract Data TypeDefinition

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

Page 14: Basic Concepts

ADT ImplementationsArray Implementations

The sequentiality of a list is maintained by the order structure of elements in the array (indexes)

Linked List ImplementationsAn ordered collection of data in which each

element contains the location of the next element or elements.

Page 15: Basic Concepts

ADT Implementations

list

list data link data link data link

list data linklink

data linklink data linklink

LINEAR LIST

NON-LINEAR LIST

EMPTY LIST

Page 16: Basic Concepts

ADT IplementationsNode

The elements in a linked listStructure that has two parts: the data and one

or more linksSelf-referential structures

data

data

Page 17: Basic Concepts

ADT Implementations

number

name id grdPts

name address phone

Node with one data field

Node with three data fields

Structure in a node

Page 18: Basic Concepts

ADT ImplementationsPointers to Linked Lists

A linked list must always have a head pointerDepending on how the list is used, there could

be several other pointers as well

Page 19: Basic Concepts

Generic Code

typedef struct node{

void *dataPtr; struct node* link;} NODE;

dataPtr link

void

To next node

Page 20: Basic Concepts

Example A

createNode

main

Dynamic Memory

7

dataPtr link

newData

nodeData

node

itemPtr nodePtr

Page 21: Basic Concepts

Creating a Node Header Filetypedef 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;

}

Page 22: Basic Concepts

Demonstrate Node Creation#include <stdio.h>#include <conio.h>#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;

}

Page 23: Basic Concepts

Activity 2 (Structure for two linked nodes)

createNode

main Dynamic Memory

7

dataPtr link

node

nodePtrdataPtr link

75

Page 24: Basic Concepts

Demonstrate Node Creation#include <stdio.h>#include <conio.h>#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;

}

Page 25: Basic Concepts

Algorithm EfficiencyAlgorithmics

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 algorithm’s efficiencyf(n) = efficiency

Page 26: Basic Concepts

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

Page 27: Basic Concepts

Logarithmic LoopsThe 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

Multiply Divide

Iteration Value of I Iteration Value of i

1

2

3

4

5

6

7

8

9

10

(exit)

1

2

4

8

16

32

64

128

256

512

1024

1

2

3

4

5

6

7

8

9

10

(exit)

1000

500

250

125

62

31

15

7

3

1

0

Page 28: Basic Concepts

Logarithmic LoopsThe number of iterations is a function of the

multiplier or divisor

Generalizing this analysis

Multiply 2iterations < 1000

Divide 1000/2iterations >= 1

f(n) = logn

Page 29: Basic Concepts

Nested LoopsTo 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 loopsLinear LogarithmicQuadraticDependent Quadratic

Page 30: Basic Concepts

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

Page 31: Basic Concepts

Quadratic Loops

Total number of iterations = 10 * 10

for (i=0; i<10; i++)

for (j=0; j<10; j++)

application code

f(n) = n2

Page 32: Basic Concepts

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

1 + 2 +3 + … + 9 + 10 = 55

(n+1)

2

f(n) = n * ((n+1)/2)

Page 33: Basic Concepts

Big-O NotationWith the speed of computer’s today, we are not concerned

with an exact measurement of an algorithm’s 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 algorithm’s usage of computational resources (usually running time or memory). It is also used in many other fields to provide similar estimates.

We don’t need to determine the complete measures of efficiency, only the factor that determines the magnitude. This factor is the big-O.

O(n)

Page 34: Basic Concepts

Big-O NotationFor 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.

Page 35: Basic Concepts

Derivation of the Big-OSteps

In each term, set the coefficient of the term to 1Keep the largest term in the function and discard the

others. Terms are ranked from lowest to highest as shown below logn

n

nlogn

n2

n3

nk

2n

n!

Page 36: Basic Concepts

Standard Measures of EfficiencyEfficiency Big-O Iterations Estimated

TimeLogarithmic O(log n) 14 Microseconds

Linear O(n) 10000 Seconds

Linear Logarithmic

O(n(logn)) 140000 Seconds

Quadratic O(n2) 100002 Minutes

Polynomial O(nk) 10000k Hours

Exponential O(cn) 210000 Intractable

Factorial O(n!) 10000! Intractable

Page 37: Basic Concepts

ExerciseCalculate 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(n2), 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(…)

Page 38: Basic Concepts

ExerciseGiven that the efficiency of an algorithm is n3, 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?