23

MELJUN CORTES Jedi slides data st-chapter01-basic concepts and notations

Embed Size (px)

DESCRIPTION

MELJUN CORTES Jedi slides data st-chapter01-basic concepts and notations

Citation preview

Page 1: MELJUN CORTES Jedi slides data st-chapter01-basic concepts and notations

Data Structures – Basic Concepts and Notations 1

1 Basic Concepts and Notations

Page 2: MELJUN CORTES Jedi slides data st-chapter01-basic concepts and notations

Data Structures – Basic Concepts and Notations 2

ObjectivesAt the end of the lesson, the student should be able to:● Explain the process of problem solving● Define data type, abstract data type and data structure● Identify the properties of an algorithm● Differentiate the two addressing methods - computed

addressing and link addressing● Use the basic mathematical functions to analyze

algorithms● Measure complexity of algorithms by expressing the

efficiency in terms of time complexity and big-O notation

Page 3: MELJUN CORTES Jedi slides data st-chapter01-basic concepts and notations

Data Structures – Basic Concepts and Notations 3

Problem Solving Process ● Programming – a problem-solving process which could be

viewed in terms of the following domains:– Problem domain

● input or the raw data to process● output or the processed data

– Machine domain ● storage medium - consists of serially arranged bits that are addressable

as a unit● processing unit - allow us to perform basic operations

– Solution domain - links the problem and machine domains

Page 4: MELJUN CORTES Jedi slides data st-chapter01-basic concepts and notations

Data Structures – Basic Concepts and Notations 4

Problem Solving ProcessProblem Domain

Page 5: MELJUN CORTES Jedi slides data st-chapter01-basic concepts and notations

Data Structures – Basic Concepts and Notations 5

Problem Solving Process Machine Domain

Page 6: MELJUN CORTES Jedi slides data st-chapter01-basic concepts and notations

Data Structures – Basic Concepts and Notations 6

Problem Solving ProcessSolution Domain

Page 7: MELJUN CORTES Jedi slides data st-chapter01-basic concepts and notations

Data Structures – Basic Concepts and Notations 7

Problem Solving Process

● Two related tasks at the solution domain– Structuring of higher level data representations– Synthesis of algorithms

● Data structures and algorithms are the building blocks of computer programs

Page 8: MELJUN CORTES Jedi slides data st-chapter01-basic concepts and notations

Data Structures – Basic Concepts and Notations 8

Data Type, Abstract Data Type and Data Structure

● Data type - kind of data that variables can assume in a programming language and for which operations are automatically provided

● Abstract Data Type (ADT) - mathematical model with defined operations. In Java, an ADT can be expressed with an interface

Page 9: MELJUN CORTES Jedi slides data st-chapter01-basic concepts and notations

Data Structures – Basic Concepts and Notations 9

Data Type, Abstract Data Type and Data Structure

public interface Stack{ public int size(); /* returns the size of the stack */ public boolean isEmpty(); /* checks if empty */ public Object top() throws StackException; public Object pop() throws StackException; public void push(Object item) throws StackException;

}

● Data structure – the implementation of ADT in terms of the data types or other data structures.In Java, a data structure can be expressed with a class

Page 10: MELJUN CORTES Jedi slides data st-chapter01-basic concepts and notations

Data Structures – Basic Concepts and Notations 10

Algorithm● Finite set of instructions which, if followed, will accomplish a

task– Finiteness - an algorithm must terminate after a finite number of

steps– Definiteness - ensured if every step of an algorithm is precisely

defined– Input - domain of the algorithm which could be zero or more

quantities– Output - set of one or more resulting quantities; also called the range

of the algorithm– Effectiveness - ensured if all the operations in the algorithm are

sufficiently basic that they can, in principle, be done exactly and in finite time by a person using paper and pen

Page 11: MELJUN CORTES Jedi slides data st-chapter01-basic concepts and notations

Data Structures – Basic Concepts and Notations 11

Addressing Methods● Computed Addressing Method - used to access the

elements of a structure in pre-allocated spaceint x[10][20];a = x[4][3];

● Link Addressing Method – used to manipulate dynamic structures where the size and shape are not known beforehand or changes at runtime class Node{ Object info; Node link; Node() { } Node (Object o, Node l){ info = o; link = l; } }

Page 12: MELJUN CORTES Jedi slides data st-chapter01-basic concepts and notations

Data Structures – Basic Concepts and Notations 12

Addressing Methods– Memory pool or avail list - source of the nodes from which linked

structures are built class AvailList { Node head; AvailList(){ head = null; } AvailList(Node n){ head = n; } }

Page 13: MELJUN CORTES Jedi slides data st-chapter01-basic concepts and notations

Data Structures – Basic Concepts and Notations 13

Addressing Methods– Two basic procedures that manipulate the avail list are getNode and

retNode, which requests for a node and returns a node respectively. Node getNode(){ Node a; if ( head == null) { return null; /* avail list is empty */ } else { a = head.link; /* assign node to return to a */ head = head.link.link; return a; } } void retNode(Node n){ n.link = head.link; /* adds the new node at the start of the avail list */ head.link = n; }

Page 14: MELJUN CORTES Jedi slides data st-chapter01-basic concepts and notations

Data Structures – Basic Concepts and Notations 14

Mathematical Functions● Floor of x ( x ) - greatest integer less than or equal to x, x

is any real number

● Ceiling of x ( x ) - smallest integer greater than or equal to x, where x is any real number

● Modulo - given any two real numbers x and y,– x mod y = x if y = 0

= x - y * x / y if y <> 0

Page 15: MELJUN CORTES Jedi slides data st-chapter01-basic concepts and notations

Data Structures – Basic Concepts and Notations 15

Mathematical Functions ● Identities

– x = x if and only if x is an integer– x = x if and only if x is not an integer– - x = − x

– x + y <= x + y

– x = x + x mod 1– z ( x mod y ) = zx mod zy

Page 16: MELJUN CORTES Jedi slides data st-chapter01-basic concepts and notations

Data Structures – Basic Concepts and Notations 16

Complexity of Algorithms● Algorithm Efficiency

– Space utilization - amount of memory required to store the data– Time efficiency - amount of time required to process the data

● Execution time - amount of time spent in executing instructions of a given algorithm. Notation: T(n). Several factors that affect the execution time include:

– input size– Instruction type – machine speed– quality of source code of the algorithm implementation – quality of the machine code generated from the source code by the

compiler

Page 17: MELJUN CORTES Jedi slides data st-chapter01-basic concepts and notations

Data Structures – Basic Concepts and Notations 17

Complexity of Algorithms● The Big-Oh Notation (or simply O-Notation)

– T(n) grows at a rate proportional to n and thus T(n) is said to have “order of magnitude n” denoted by the O-notation: T(n) = O(n)

– Formal definition: g(n) = O(f(n)) if there exists two constants c and n

0 such that

| g(n) | <= c * | f(n) | for all n >= n0.

– Operations on the O-Notation:● Rule for Sums

Suppose that T1(n) = O( f(n) ) and T

2(n) = O( g(n) ).

Then, t(n) = T1(n) + T

2(n) = O( max( f(n), g(n) ) ).

● Rule for Products Suppose that T1(n) = O( f(n) ) and T2(n) = O( g(n) ). Then, T(n) = T1(n) * T2(n) = O( f(n) * g(n) ).

Page 18: MELJUN CORTES Jedi slides data st-chapter01-basic concepts and notations

Data Structures – Basic Concepts and Notations 18

Complexity of AlgorithmsRule for Sums

Suppose that T1(n) = O( f(n) ) and T

2(n) = O( g(n) ).

Then, t(n) = T1(n) + T

2(n) = O( max( f(n), g(n) ) ).

Proof: By definition of the O-notation, T1(n) <= c1 f(n) for n >= n1 and T2(n) <= c2 g(n) for n >= n2.

Let n0 = max(n1, n2). ThenT1(n) + T2(n) <= c1 f(n) + c2 g(n) n >= n0.

<= (c1 + c2) max(f(n),g(n)) n >= n0.<= c max ( f(n), g(n) ) n >= n0.

Thus,

T(n) = T1(n) + T2(n) = O( max( f(n), g(n) ) ).

e.g. a. T(n) = 3n3 + 5n2 = O( n3 ) b. T(n) = 2n + n4 + n log n = O( 2n )

Page 19: MELJUN CORTES Jedi slides data st-chapter01-basic concepts and notations

Data Structures – Basic Concepts and Notations 19

Complexity of AlgorithmsExamples

Consider the algorithm below :for (i=1; i <= n, i++)

for (j=1; j <= n, j++)// steps which take O(1) time

Since the steps in the inner loop will take n + n-1 + n-2 + ... + 2 + 1 times, then the running time is

n( n+1 ) / 2 = n2 / 2 + n / 2

= O( n2 )

Page 20: MELJUN CORTES Jedi slides data st-chapter01-basic concepts and notations

Data Structures – Basic Concepts and Notations 20

Complexity of AlgorithmsExamples

LINEAR SEARCH ALGORITHM

1 found = false;2 loc = 1;3 while ((loc <= n) && (!found)){4 if (item == a[loc]found = true;5 else loc = loc + 1;6 }

STATEMENT # of times executed1 12 13 n + 14 n5 n

T( n ) = 3n + 3 so that T( n ) = O( n )

Since g( n ) <= c f( n ) for n >= n 0, then

3n + 3 <= c n

3n + 3 <= c = 3 + 3 / n <= c

---------

n

Thus c = 4 and n0 = 3.

Page 21: MELJUN CORTES Jedi slides data st-chapter01-basic concepts and notations

Data Structures – Basic Concepts and Notations 21

Complexity of Algorithms● The Big-Oh Notation

Big-Oh Description AlgorithmO(1) Constant

Logarithmic Binary SearchO(n) Linear Sequential Search

Heapsort

Quadratic Insertion Sort Cubic Floyd’s Algorithm

Exponential

O(log2n)

O(n log2n)

O(n2)

O(n3)O( 2n )

F(n) Running Time 19.93 microseconds

n 1.00 seconds19.93 seconds11.57 days317.10 centuriesEternity

log2n

n log2n

n2 n3

2n

Page 22: MELJUN CORTES Jedi slides data st-chapter01-basic concepts and notations

Data Structures – Basic Concepts and Notations 22

Complexity of AlgorithmsGeneral rules on determining the running time of an algorithm● FOR loops

– At most the running time of the statement inside the for loop times the number of iterations.

● NESTED FOR loops – Analysis is done from the inner loop going outward. The total

running time of a statement inside a group of for loops is the running time of the statement multiplied by the product of the sizes of all the for loops.

● CONSECUTIVE STATEMENTS – The statement with the maximum running time.

● IF/ELSE – Never more than the running time of the test plus the larger of the

running times of the conditional block of statements.

Page 23: MELJUN CORTES Jedi slides data st-chapter01-basic concepts and notations

Data Structures – Basic Concepts and Notations 23

Summary● Programming as a problem solving process could be viewed in

terms of 3 domains – problem, machine and solution.● Data structures provide a way for data representation. It is an

implementation of ADT.● An algorithm is a finite set of instructions which, if followed, will

accomplish a task. It has five important properties: finiteness, definiteness, input, output and effectiveness.

● Addressing methods define how the data items are accessed. Two general types are computed and link addressing.

● Algorithm efficiency is measured in two criteria: space utilization and time efficiency. The O-notation gives an approximate measure of the computing time of an algorithm for large number of input